More de-scope - remove duration on move command
This isn't even needed, as commands can just be queued up
This commit is contained in:
parent
383e834cef
commit
2f6465987d
10 changed files with 97 additions and 134 deletions
|
@ -11,8 +11,7 @@ type Command struct {
|
|||
Command string `json:"command"`
|
||||
|
||||
// Used in the move command
|
||||
Bearing string `json:"bearing,omitempty"`
|
||||
Duration int `json:"duration,omitempty"`
|
||||
Bearing string `json:"bearing,omitempty"`
|
||||
}
|
||||
|
||||
// CommandStream is a list of commands to execute in order
|
||||
|
|
|
@ -19,9 +19,8 @@ func TestCommand_Move(t *testing.T) {
|
|||
err = world.WarpRover(a, pos)
|
||||
assert.NoError(t, err, "Failed to set position for rover")
|
||||
|
||||
var duration = 1
|
||||
// Try the move command
|
||||
moveCommand := Command{Command: CommandMove, Bearing: "N", Duration: duration}
|
||||
moveCommand := Command{Command: CommandMove, Bearing: "N"}
|
||||
assert.NoError(t, world.Enqueue(a, moveCommand), "Failed to execute move command")
|
||||
|
||||
// Tick the world
|
||||
|
@ -30,6 +29,6 @@ func TestCommand_Move(t *testing.T) {
|
|||
|
||||
newPos, err := world.RoverPosition(a)
|
||||
assert.NoError(t, err, "Failed to set position for rover")
|
||||
pos.Add(vector.Vector{X: 0.0, Y: int(duration)})
|
||||
pos.Add(vector.Vector{X: 0.0, Y: 1})
|
||||
assert.Equal(t, pos, newPos, "Failed to correctly set position for rover")
|
||||
}
|
||||
|
|
|
@ -396,19 +396,14 @@ func (w *World) ExecuteCommandQueues() {
|
|||
if len(cmds) != 0 {
|
||||
// Extract the first command in the queue
|
||||
c := cmds[0]
|
||||
w.CommandQueue[rover] = cmds[1:]
|
||||
|
||||
// Execute the command and clear up if requested
|
||||
if done, err := w.ExecuteCommand(&c, rover); err != nil {
|
||||
w.CommandQueue[rover] = cmds[1:]
|
||||
// Execute the command
|
||||
if err := w.ExecuteCommand(&c, rover); err != nil {
|
||||
log.Println(err)
|
||||
} else if done {
|
||||
w.CommandQueue[rover] = cmds[1:]
|
||||
} else {
|
||||
w.CommandQueue[rover][0] = c
|
||||
// TODO: Report this error somehow
|
||||
}
|
||||
|
||||
// If there was an error
|
||||
|
||||
} else {
|
||||
// Clean out the empty entry
|
||||
delete(w.CommandQueue, rover)
|
||||
|
@ -420,36 +415,26 @@ func (w *World) ExecuteCommandQueues() {
|
|||
}
|
||||
|
||||
// ExecuteCommand will execute a single command
|
||||
func (w *World) ExecuteCommand(c *Command, rover uuid.UUID) (finished bool, err error) {
|
||||
func (w *World) ExecuteCommand(c *Command, rover uuid.UUID) (err error) {
|
||||
log.Printf("Executing command: %+v\n", *c)
|
||||
|
||||
switch c.Command {
|
||||
case CommandMove:
|
||||
if dir, err := bearing.FromString(c.Bearing); err != nil {
|
||||
return true, err
|
||||
return err
|
||||
|
||||
} else if _, err := w.MoveRover(rover, dir); err != nil {
|
||||
return true, err
|
||||
return err
|
||||
|
||||
} else {
|
||||
// If we've successfully moved, reduce the duration by 1
|
||||
c.Duration -= 1
|
||||
|
||||
// If we've used up the full duration, remove it, otherwise update
|
||||
if c.Duration == 0 {
|
||||
finished = true
|
||||
}
|
||||
}
|
||||
|
||||
case CommandStash:
|
||||
if _, err := w.RoverStash(rover); err != nil {
|
||||
return true, err
|
||||
} else {
|
||||
return true, nil
|
||||
return err
|
||||
}
|
||||
|
||||
default:
|
||||
return true, fmt.Errorf("unknown command: %s", c.Command)
|
||||
return fmt.Errorf("unknown command: %s", c.Command)
|
||||
}
|
||||
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue