Require player data to exist

This commit is contained in:
Layla 2020-08-23 00:19:30 -04:00
parent dca2ce0599
commit 4fd93b6c9e
No known key found for this signature in database
GPG Key ID: A494D9357BA1BE31
3 changed files with 115 additions and 1 deletions

View File

@ -72,7 +72,19 @@ func (m *Match) MatchJoinAttempt(ctx context.Context, logger runtime.Logger, db
if _, ok := mState.presences[presence.GetUserId()]; ok {
return mState, false, "User already logged in."
} 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!"
}
}
}

View File

@ -1,15 +1,24 @@
package entities
import (
"context"
"encoding/json"
"fmt"
"github.com/heroiclabs/nakama-common/runtime"
"github.com/josephbmanley/family/server/plugin/gameworld"
"strconv"
)
type PlayerSaveData struct {
Faction int
Name string
}
// PlayerEntity is the go struct representing the player's location
type PlayerEntity struct {
Presence runtime.Presence
Faction gameworld.Faction
Name string
X float64
Y float64
}
@ -20,6 +29,89 @@ type PlayerPosResponse struct {
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
func (p *PlayerEntity) ParsePositionRequest(data []byte) (PlayerPosResponse, error) {
var response PlayerPosResponse

View File

@ -0,0 +1,10 @@
package gameworld
type Faction int
const (
FactionBurningWall = 1
FactionLunaki = 2
FactionRegium = 3
FactionElectus = 4
)