Send player state
This commit is contained in:
parent
b493e10794
commit
257fb07e94
@ -20,6 +20,8 @@ const (
|
||||
OpCodeTileUpdate = 1
|
||||
// OpCodeUpdatePosition is used for player position updates
|
||||
OpCodeUpdatePosition = 2
|
||||
// OpCodePlayerState is for player object updates
|
||||
OpCodePlayerState = 3
|
||||
)
|
||||
|
||||
// Match is the object registered
|
||||
@ -102,18 +104,24 @@ func (m *Match) MatchJoin(ctx context.Context, logger runtime.Logger, db *sql.DB
|
||||
// Add presence to map
|
||||
mState.presences[precense.GetUserId()] = precense
|
||||
|
||||
player := entities.PlayerEntity{
|
||||
X: 16,
|
||||
Y: 16,
|
||||
Presence: precense,
|
||||
player, loadPlayerErr := entities.LoadPlayer(ctx, nk, precense)
|
||||
if loadPlayerErr != nil {
|
||||
logger.Error(loadPlayerErr.Error())
|
||||
}
|
||||
|
||||
player.X = 16
|
||||
player.Y = 16
|
||||
|
||||
if jsonObj, err := player.GetPosJSON(); err != nil {
|
||||
logger.Error(err.Error())
|
||||
} else {
|
||||
if sendErr := dispatcher.BroadcastMessage(OpCodeUpdatePosition, jsonObj, []runtime.Presence{precense}, player.Presence, true); sendErr != nil {
|
||||
logger.Error(sendErr.Error())
|
||||
}
|
||||
stateJSON, _ := player.GetStateJSON()
|
||||
if sendErr := dispatcher.BroadcastMessage(OpCodePlayerState, stateJSON, mState.GetPrecenseList(), player.Presence, true); sendErr != nil {
|
||||
logger.Error(sendErr.Error())
|
||||
}
|
||||
}
|
||||
|
||||
mState.players[precense.GetUserId()] = player
|
||||
@ -175,7 +183,6 @@ func (m *Match) MatchLoop(ctx context.Context, logger runtime.Logger, db *sql.DB
|
||||
player.UpdateBasedOnResponse(response)
|
||||
if jsonObject, err := player.GetPosJSON(); err == nil {
|
||||
dispatcher.BroadcastMessage(OpCodeUpdatePosition, jsonObject, mState.GetPrecenseList(), player.Presence, false)
|
||||
logger.Info("Yes")
|
||||
} else {
|
||||
logger.Error(fmt.Sprintf("Failed to get player json: %s", err.Error))
|
||||
}
|
||||
|
@ -85,6 +85,17 @@ func LoadPlayer(ctx context.Context, nk runtime.NakamaModule, presence runtime.P
|
||||
return player, nil
|
||||
}
|
||||
|
||||
// GetPosJSON returns the player's position as a JSON object
|
||||
func (p *PlayerEntity) GetPosJSON() ([]byte, error) {
|
||||
playerMap := map[string]string{
|
||||
"player": p.Presence.GetUserId(),
|
||||
"x": fmt.Sprintf("%f", p.X),
|
||||
"y": fmt.Sprintf("%f", p.Y),
|
||||
}
|
||||
jsonData, err := json.Marshal(playerMap)
|
||||
return jsonData, err
|
||||
}
|
||||
|
||||
// Save passes the precensce id to SaveUserID
|
||||
func (p *PlayerEntity) Save(ctx context.Context, nk runtime.NakamaModule) error {
|
||||
|
||||
@ -141,12 +152,12 @@ func (p *PlayerEntity) UpdateBasedOnResponse(response PlayerPosResponse) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPosJSON returns the player's position as a JSON object
|
||||
func (p *PlayerEntity) GetPosJSON() ([]byte, error) {
|
||||
// GetStateJSON builds a json object for player state
|
||||
func (p *PlayerEntity) GetStateJSON() ([]byte, error) {
|
||||
playerMap := map[string]string{
|
||||
"player": p.Presence.GetUserId(),
|
||||
"x": fmt.Sprintf("%f", p.X),
|
||||
"y": fmt.Sprintf("%f", p.Y),
|
||||
"player": p.Presence.GetUserId(),
|
||||
"name": p.Name,
|
||||
"faction": strconv.Itoa(int(p.Faction)),
|
||||
}
|
||||
jsonData, err := json.Marshal(playerMap)
|
||||
return jsonData, err
|
||||
|
Reference in New Issue
Block a user