From e3ce87e96447c3753ec6eb50dfe3af8e7c9849fd Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sat, 6 Jun 2020 12:45:45 +0100 Subject: [PATCH] Simplify duplicate command types --- cmd/rove-server/api_test.go | 5 +++-- pkg/game/command.go | 18 +++++++++--------- pkg/game/command_test.go | 4 ++-- pkg/game/world.go | 13 ++++++++++--- pkg/rove/api.go | 18 +----------------- pkg/server/routes.go | 5 +---- pkg/server/routes_test.go | 4 ++-- pkg/server/server.go | 17 ----------------- 8 files changed, 28 insertions(+), 56 deletions(-) diff --git a/cmd/rove-server/api_test.go b/cmd/rove-server/api_test.go index 50008a6..8799e5e 100644 --- a/cmd/rove-server/api_test.go +++ b/cmd/rove-server/api_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/google/uuid" + "github.com/mdiluz/rove/pkg/game" "github.com/mdiluz/rove/pkg/rove" "github.com/mdiluz/rove/pkg/server" "github.com/stretchr/testify/assert" @@ -95,9 +96,9 @@ func TestServer_Command(t *testing.T) { assert.True(t, r2.Success) c := rove.CommandData{ - Commands: []rove.Command{ + Commands: []game.Command{ { - Command: rove.CommandMove, + Command: game.CommandMove, Bearing: "N", Duration: 1, }, diff --git a/pkg/game/command.go b/pkg/game/command.go index d3af43b..d4487c1 100644 --- a/pkg/game/command.go +++ b/pkg/game/command.go @@ -1,14 +1,14 @@ package game -import "github.com/google/uuid" +const ( + CommandMove = "move" +) -// A command is simply a function that acts on the a given rover in the world -type Command func() error +// Command represends a single command to execute +type Command struct { + Command string `json:"command"` -// CommandMove will move the rover in question -func (w *World) CommandMove(id uuid.UUID, bearing Direction, duration int) Command { - return func() error { - _, err := w.MoveRover(id, bearing, duration) - return err - } + // Used in the move command + Bearing string `json:"bearing,omitempty"` + Duration int `json:"duration,omitempty"` } diff --git a/pkg/game/command_test.go b/pkg/game/command_test.go index c946981..0988dd9 100644 --- a/pkg/game/command_test.go +++ b/pkg/game/command_test.go @@ -23,8 +23,8 @@ func TestCommand_Move(t *testing.T) { bearing := North duration := 1 // Try the move command - moveCommand := world.CommandMove(a, bearing, duration) - assert.NoError(t, world.Execute(moveCommand), "Failed to execute move command") + moveCommand := Command{Command: CommandMove, Bearing: bearing.String(), Duration: duration} + assert.NoError(t, world.Execute(a, moveCommand), "Failed to execute move command") newpos, err := world.RoverPosition(a) assert.NoError(t, err, "Failed to set position for rover") diff --git a/pkg/game/world.go b/pkg/game/world.go index 3b050a8..367b63b 100644 --- a/pkg/game/world.go +++ b/pkg/game/world.go @@ -146,10 +146,17 @@ func (w World) RadarFromRover(id uuid.UUID) (RadarDescription, error) { } // Execute will run the commands given -func (w *World) Execute(commands ...Command) error { +func (w *World) Execute(id uuid.UUID, commands ...Command) error { for _, c := range commands { - if err := c(); err != nil { - return err + switch c.Command { + case "move": + if dir, err := DirectionFromString(c.Bearing); err != nil { + return fmt.Errorf("unknown direction: %s", c.Bearing) + } else if _, err := w.MoveRover(id, dir, c.Duration); err != nil { + return err + } + default: + return fmt.Errorf("unknown command: %s", c.Command) } } return nil diff --git a/pkg/rove/api.go b/pkg/rove/api.go index 7ad5062..c72d685 100644 --- a/pkg/rove/api.go +++ b/pkg/rove/api.go @@ -79,7 +79,7 @@ func (s Server) Command(account string, d CommandData) (r CommandResponse, err e // CommandData is a set of commands to execute in order type CommandData struct { - Commands []Command `json:"commands"` + Commands []game.Command `json:"commands"` } // CommandResponse is the response to be sent back @@ -88,22 +88,6 @@ type CommandResponse struct { Error string `json:"error,omitempty"` } -const ( - // CommandMove describes a single move command - CommandMove = "move" -) - -// Command describes a single command to execute -// it contains the type, and then any members used for each command type -type Command struct { - // Command is the main command string - Command string `json:"command"` - - // Used for CommandMove - Bearing string `json:"bearing"` // The direction to move on a compass in short (NW) or long (NorthWest) form - Duration int `json:"duration"` // The duration of the move in ticks -} - // ================ // API: /{account}/radar method: GET diff --git a/pkg/server/routes.go b/pkg/server/routes.go index 9869f95..b1e2f6a 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -146,10 +146,7 @@ func HandleCommand(s *Server, vars map[string]string, b io.ReadCloser, w io.Writ } else if inst, err := s.accountant.GetRover(id); err != nil { response.Error = fmt.Sprintf("Provided account has no rover: %s", err) - } else if cmds, err := s.ConvertCommands(data.Commands, inst); err != nil { - response.Error = fmt.Sprintf("Couldn't convert commands: %s", err) - - } else if err := s.world.Execute(cmds...); err != nil { + } else if err := s.world.Execute(inst, data.Commands...); err != nil { response.Error = fmt.Sprintf("Failed to execute commands: %s", err) } else { diff --git a/pkg/server/routes_test.go b/pkg/server/routes_test.go index cade48f..74edb4b 100644 --- a/pkg/server/routes_test.go +++ b/pkg/server/routes_test.go @@ -96,9 +96,9 @@ func TestHandleCommand(t *testing.T) { assert.NoError(t, err, "Couldn't get rover position") data := rove.CommandData{ - Commands: []rove.Command{ + Commands: []game.Command{ { - Command: rove.CommandMove, + Command: game.CommandMove, Bearing: "N", Duration: 1, }, diff --git a/pkg/server/server.go b/pkg/server/server.go index 55c6fc9..78b8b92 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -15,7 +15,6 @@ import ( "github.com/mdiluz/rove/pkg/accounts" "github.com/mdiluz/rove/pkg/game" "github.com/mdiluz/rove/pkg/persistence" - "github.com/mdiluz/rove/pkg/rove" ) const ( @@ -195,19 +194,3 @@ func (s *Server) SpawnRoverForAccount(accountid uuid.UUID) (game.Vector, uuid.UU } } } - -// ConvertCommands converts server commands to game commands -func (s *Server) ConvertCommands(commands []rove.Command, inst uuid.UUID) ([]game.Command, error) { - var cmds []game.Command - for _, c := range commands { - switch c.Command { - case rove.CommandMove: - if bearing, err := game.DirectionFromString(c.Bearing); err != nil { - return nil, err - } else { - cmds = append(cmds, s.world.CommandMove(inst, bearing, c.Duration)) - } - } - } - return cmds, nil -}