Add new RPC method

This commit is contained in:
Layla 2020-08-23 03:06:28 -04:00
parent 4fd93b6c9e
commit 859c1d7238
No known key found for this signature in database
GPG Key ID: A494D9357BA1BE31
4 changed files with 50 additions and 5 deletions

View File

@ -73,7 +73,7 @@ func (m *Match) MatchJoinAttempt(ctx context.Context, logger runtime.Logger, db
return mState, false, "User already logged in."
} else {
dataExist, err := entities.PlayerDataExists(ctx, nk, presence)
dataExist, err := entities.PlayerDataExists(ctx, nk, presence.GetUserId())
if err != nil {
logger.Error(err.Error())
return mState, false, err.Error()

View File

@ -30,13 +30,13 @@ type PlayerPosResponse struct {
}
// PlayerDataExists checks if precense has saved data
func PlayerDataExists(ctx context.Context, nk runtime.NakamaModule, presence runtime.Presence) (bool, error) {
func PlayerDataExists(ctx context.Context, nk runtime.NakamaModule, userID string) (bool, error) {
Reads := []*runtime.StorageRead{
&runtime.StorageRead{
Collection: "playerdata",
Key: "data",
UserID: presence.GetUserId(),
UserID: userID,
},
}
records, err := nk.StorageRead(ctx, Reads)
@ -85,9 +85,15 @@ func LoadPlayer(ctx context.Context, nk runtime.NakamaModule, presence runtime.P
return player, nil
}
// Save saves player data to nakama
// Save passes the precensce id to SaveUserID
func (p *PlayerEntity) Save(ctx context.Context, nk runtime.NakamaModule) error {
return p.SaveUserID(ctx, nk, p.Presence.GetUserId())
}
// SaveUserID saves player data to nakama
func (p *PlayerEntity) SaveUserID(ctx context.Context, nk runtime.NakamaModule, userID string) error {
saveData := PlayerSaveData{
Name: p.Name,
Faction: int(p.Faction),
@ -103,7 +109,7 @@ func (p *PlayerEntity) Save(ctx context.Context, nk runtime.NakamaModule) error
Collection: "playerdata",
Key: "data",
Value: string(saveJSON),
UserID: p.Presence.GetUserId(),
UserID: userID,
},
}

View File

@ -20,5 +20,9 @@ func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runti
logger.Error("Unable to register: %v", err)
return err
}
if err := initializer.RegisterRpc("create_character", rpc.CreateCharacter); err != nil {
logger.Error("Unable to register: %v", err)
return err
}
return nil
}

View File

@ -3,7 +3,11 @@ package rpc
import (
"context"
"database/sql"
"encoding/json"
"errors"
"github.com/heroiclabs/nakama-common/runtime"
"github.com/josephbmanley/family/server/plugin/entities"
"github.com/josephbmanley/family/server/plugin/gameworld"
)
func getFirstWorld(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule) (string, error) {
@ -47,3 +51,34 @@ func GetWorldId(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runti
matchID, err := getFirstWorld(ctx, logger, nk)
return matchID, err
}
func CreateCharacter(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
userID, ok := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
if !ok {
dataExist, err := entities.PlayerDataExists(ctx, nk, userID)
if err != nil {
logger.Error(err.Error())
return "", err
}
if dataExist {
return "", errors.New("user already has a character")
} else {
playerData := entities.PlayerSaveData{}
err := json.Unmarshal([]byte(payload), &playerData)
if err != nil {
return "", err
}
player := entities.PlayerEntity{
Name: playerData.Name,
Faction: gameworld.Faction(playerData.Faction),
}
saveErr := player.SaveUserID(ctx, nk, userID)
if saveErr != nil {
return "", err
}
return "", nil
}
}
return "", errors.New("Unknown error occured!")
}