func (e *Engine) processActionRequest(req *action.Request) bool { // [GAME-SPECIFIC] Handle action requests according to game rules switch req.Action.Id { case game.ThrowBallAction: log.Println("Accepting ball", req) initiator := e.entity.Get(req.Action.Initiator) ball := entity.New() ball.Type = game.BallEntity ball.Sprite = game.BallSprite ball.Position = initiator.Position ball.Speed.Norm = float64(e.config.DefaultBallSpeed) ball.Speed.Angle = float64(req.Action.Params[0].(float32)) ball.Attributes[game.TicksLeftAttr] = e.config.DefaultBallLifespan ball.Attributes[game.SenderAttr] = initiator.Id r := entity.NewCreateRequest(req.GetTick(), ball) if err := e.entity.AcceptRequest(r); err != nil { // This shouldn't fail panic(err) } session := e.auth.GetSessionByEntity(req.Action.Initiator) // TODO: error handling e.clients[session.Id].Write(func(w io.Writer) error { return builder.SendEntityIdChange(w, req.Action.Params[1].(message.EntityId), ball.Id) }) } return true }
func TestEntityIdChange(t *testing.T) { oldId := message.EntityId(24) newId := message.EntityId(87) testMessage(t, message.Types["entity_id_change"], func(w io.Writer) error { return builder.SendEntityIdChange(w, oldId, newId) }, func(conn *message.Conn, t *testing.T) { roldId, rnewId := handler.ReadEntityIdChange(conn) if roldId != oldId { t.Fatal("Sent old id", oldId, "but received", roldId) } if rnewId != newId { t.Fatal("Sent new id", newId, "but received", rnewId) } }) }