Read response as float
This commit is contained in:
parent
8d1b95060a
commit
d700b27487
@ -16,32 +16,21 @@ type PlayerEntity struct {
|
|||||||
|
|
||||||
// PlayerPosResponse struct that represents client data
|
// PlayerPosResponse struct that represents client data
|
||||||
type PlayerPosResponse struct {
|
type PlayerPosResponse struct {
|
||||||
X string
|
X float64
|
||||||
Y string
|
Y float64
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
err := json.Unmarshal(data, &response)
|
err := json.Unmarshal(data, &response)
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to parse: %s", string(data[:]))
|
|
||||||
}
|
|
||||||
return response, err
|
return response, err
|
||||||
}
|
}
|
||||||
|
|
||||||
//UpdateBasedOnResponse updates the player object based on a response object
|
//UpdateBasedOnResponse updates the player object based on a response object
|
||||||
func (p *PlayerEntity) UpdateBasedOnResponse(response PlayerPosResponse) error {
|
func (p *PlayerEntity) UpdateBasedOnResponse(response PlayerPosResponse) error {
|
||||||
if fx, err := strconv.ParseFloat(response.X, 64); err != nil {
|
p.Y = response.Y
|
||||||
return err
|
p.X = response.X
|
||||||
} else {
|
|
||||||
p.X = fx
|
|
||||||
if fy, err := strconv.ParseFloat(response.Y, 64); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
p.Y = fy
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user