Simplify duplicate command types
This commit is contained in:
parent
97d3583384
commit
e3ce87e964
8 changed files with 28 additions and 56 deletions
|
@ -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,
|
||||
},
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue