This repository has been archived on 2023-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
family-lineage/server/plugin/entities/entities.go

48 lines
1.1 KiB
Go
Raw Normal View History

2020-08-21 04:21:29 +02:00
package entities
2020-08-21 06:25:31 +02:00
import (
"encoding/json"
"fmt"
"github.com/heroiclabs/nakama-common/runtime"
"strconv"
)
2020-08-21 04:21:29 +02:00
// PlayerEntity is the go struct representing the player's location
type PlayerEntity struct {
2020-08-21 06:25:31 +02:00
Presence runtime.Presence
X float64
Y float64
}
// PlayerPosResponse struct that represents client data
type PlayerPosResponse struct {
2020-08-21 07:17:55 +02:00
X float64
Y float64
2020-08-21 06:25:31 +02:00
}
// ParsePositionRequest parses data from client
func (p *PlayerEntity) ParsePositionRequest(data []byte) (PlayerPosResponse, error) {
var response PlayerPosResponse
err := json.Unmarshal(data, &response)
return response, err
}
//UpdateBasedOnResponse updates the player object based on a response object
func (p *PlayerEntity) UpdateBasedOnResponse(response PlayerPosResponse) error {
2020-08-21 07:17:55 +02:00
p.Y = response.Y
p.X = response.X
2020-08-21 06:25:31 +02:00
return 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
2020-08-21 04:21:29 +02:00
}