Require player data to exist
This commit is contained in:
		@ -72,7 +72,19 @@ func (m *Match) MatchJoinAttempt(ctx context.Context, logger runtime.Logger, db
 | 
				
			|||||||
	if _, ok := mState.presences[presence.GetUserId()]; ok {
 | 
						if _, ok := mState.presences[presence.GetUserId()]; ok {
 | 
				
			||||||
		return mState, false, "User already logged in."
 | 
							return mState, false, "User already logged in."
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		return mState, true, ""
 | 
					
 | 
				
			||||||
 | 
							dataExist, err := entities.PlayerDataExists(ctx, nk, presence)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								logger.Error(err.Error())
 | 
				
			||||||
 | 
								return mState, false, err.Error()
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if dataExist {
 | 
				
			||||||
 | 
								return mState, true, ""
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								return mState, false, "User does not have a character!"
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,15 +1,24 @@
 | 
				
			|||||||
package entities
 | 
					package entities
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"github.com/heroiclabs/nakama-common/runtime"
 | 
						"github.com/heroiclabs/nakama-common/runtime"
 | 
				
			||||||
 | 
						"github.com/josephbmanley/family/server/plugin/gameworld"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type PlayerSaveData struct {
 | 
				
			||||||
 | 
						Faction int
 | 
				
			||||||
 | 
						Name    string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// PlayerEntity is the go struct representing the player's location
 | 
					// PlayerEntity is the go struct representing the player's location
 | 
				
			||||||
type PlayerEntity struct {
 | 
					type PlayerEntity struct {
 | 
				
			||||||
	Presence runtime.Presence
 | 
						Presence runtime.Presence
 | 
				
			||||||
 | 
						Faction  gameworld.Faction
 | 
				
			||||||
 | 
						Name     string
 | 
				
			||||||
	X        float64
 | 
						X        float64
 | 
				
			||||||
	Y        float64
 | 
						Y        float64
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -20,6 +29,89 @@ type PlayerPosResponse struct {
 | 
				
			|||||||
	Y string
 | 
						Y string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// PlayerDataExists checks if precense has saved data
 | 
				
			||||||
 | 
					func PlayerDataExists(ctx context.Context, nk runtime.NakamaModule, presence runtime.Presence) (bool, error) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Reads := []*runtime.StorageRead{
 | 
				
			||||||
 | 
							&runtime.StorageRead{
 | 
				
			||||||
 | 
								Collection: "playerdata",
 | 
				
			||||||
 | 
								Key:        "data",
 | 
				
			||||||
 | 
								UserID:     presence.GetUserId(),
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						records, err := nk.StorageRead(ctx, Reads)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return false, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for _, record := range records {
 | 
				
			||||||
 | 
							if record.Key == "data" {
 | 
				
			||||||
 | 
								return true, nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return false, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// LoadPlayer creates player object
 | 
				
			||||||
 | 
					func LoadPlayer(ctx context.Context, nk runtime.NakamaModule, presence runtime.Presence) (PlayerEntity, error) {
 | 
				
			||||||
 | 
						player := PlayerEntity{Presence: presence}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Read storage
 | 
				
			||||||
 | 
						PlayerReads := []*runtime.StorageRead{
 | 
				
			||||||
 | 
							&runtime.StorageRead{
 | 
				
			||||||
 | 
								Collection: "playerdata",
 | 
				
			||||||
 | 
								Key:        "data",
 | 
				
			||||||
 | 
								UserID:     player.Presence.GetUserId(),
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						records, err := nk.StorageRead(ctx, PlayerReads)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return player, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Load storage records into object
 | 
				
			||||||
 | 
						for _, record := range records {
 | 
				
			||||||
 | 
							switch record.Key {
 | 
				
			||||||
 | 
							case "data":
 | 
				
			||||||
 | 
								responseData := PlayerSaveData{}
 | 
				
			||||||
 | 
								err := json.Unmarshal([]byte(record.Value), &responseData)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return player, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								player.Name = responseData.Name
 | 
				
			||||||
 | 
								player.Faction = gameworld.Faction(responseData.Faction)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return player, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Save saves player data to nakama
 | 
				
			||||||
 | 
					func (p *PlayerEntity) Save(ctx context.Context, nk runtime.NakamaModule) error {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						saveData := PlayerSaveData{
 | 
				
			||||||
 | 
							Name:    p.Name,
 | 
				
			||||||
 | 
							Faction: int(p.Faction),
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						saveJSON, err := json.Marshal(saveData)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						PlayerWrites := []*runtime.StorageWrite{
 | 
				
			||||||
 | 
							&runtime.StorageWrite{
 | 
				
			||||||
 | 
								Collection: "playerdata",
 | 
				
			||||||
 | 
								Key:        "data",
 | 
				
			||||||
 | 
								Value:      string(saveJSON),
 | 
				
			||||||
 | 
								UserID:     p.Presence.GetUserId(),
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err = nk.StorageWrite(ctx, PlayerWrites)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ParsePositionRequest parses data from client
 | 
					// ParsePositionRequest parses data from client
 | 
				
			||||||
func (p *PlayerEntity) ParsePositionRequest(data []byte) (PlayerPosResponse, error) {
 | 
					func (p *PlayerEntity) ParsePositionRequest(data []byte) (PlayerPosResponse, error) {
 | 
				
			||||||
	var response PlayerPosResponse
 | 
						var response PlayerPosResponse
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										10
									
								
								server/plugin/gameworld/gameworld.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								server/plugin/gameworld/gameworld.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					package gameworld
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Faction int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						FactionBurningWall = 1
 | 
				
			||||||
 | 
						FactionLunaki      = 2
 | 
				
			||||||
 | 
						FactionRegium      = 3
 | 
				
			||||||
 | 
						FactionElectus     = 4
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
		Reference in New Issue
	
	Block a user