Simplify - remove duplicate command types in favor of a better defined Command type in proto
This commit is contained in:
parent
7d780d05bd
commit
96a137ad2f
9 changed files with 370 additions and 471 deletions
|
@ -1,25 +1,10 @@
|
|||
package game
|
||||
|
||||
const (
|
||||
// CommandMove Moves the rover in the chosen bearing
|
||||
CommandMove = "move"
|
||||
|
||||
// CommandStash Will attempt to stash the object at the current location
|
||||
CommandStash = "stash"
|
||||
|
||||
// CommandRepair Will attempt to repair the rover with an inventory object
|
||||
CommandRepair = "repair"
|
||||
|
||||
// CommandRecharge Will use one tick to charge the rover
|
||||
CommandRecharge = "recharge"
|
||||
|
||||
// CommandBroadcast will broadcast a message to nearby rovers within range
|
||||
CommandBroadcast = "broadcast"
|
||||
)
|
||||
import "github.com/mdiluz/rove/pkg/rove"
|
||||
|
||||
// Command represends a single command to execute
|
||||
type Command struct {
|
||||
Command string `json:"command"`
|
||||
Command rove.CommandType `json:"command"`
|
||||
|
||||
// Used in the move command
|
||||
Bearing string `json:"bearing,omitempty"`
|
||||
|
|
|
@ -3,6 +3,7 @@ package game
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/rove"
|
||||
"github.com/mdiluz/rove/pkg/vector"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -20,7 +21,7 @@ func TestCommand_Move(t *testing.T) {
|
|||
assert.NoError(t, err, "Failed to set position for rover")
|
||||
|
||||
// Try the move command
|
||||
moveCommand := Command{Command: CommandMove, Bearing: "N"}
|
||||
moveCommand := Command{Command: rove.CommandType_move, Bearing: "N"}
|
||||
assert.NoError(t, world.Enqueue(a, moveCommand), "Failed to execute move command")
|
||||
|
||||
// Tick the world
|
||||
|
@ -46,7 +47,7 @@ func TestCommand_Recharge(t *testing.T) {
|
|||
assert.NoError(t, err, "Failed to set position for rover")
|
||||
|
||||
// Move to use up some charge
|
||||
moveCommand := Command{Command: CommandMove, Bearing: "N"}
|
||||
moveCommand := Command{Command: rove.CommandType_move, Bearing: "N"}
|
||||
assert.NoError(t, world.Enqueue(a, moveCommand), "Failed to queue move command")
|
||||
|
||||
// Tick the world
|
||||
|
@ -56,7 +57,7 @@ func TestCommand_Recharge(t *testing.T) {
|
|||
rover, _ := world.GetRover(a)
|
||||
assert.Equal(t, rover.MaximumCharge-1, rover.Charge)
|
||||
|
||||
chargeCommand := Command{Command: CommandRecharge}
|
||||
chargeCommand := Command{Command: rove.CommandType_recharge}
|
||||
assert.NoError(t, world.Enqueue(a, chargeCommand), "Failed to queue recharge command")
|
||||
|
||||
// Tick the world
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
"github.com/mdiluz/rove/pkg/atlas"
|
||||
"github.com/mdiluz/rove/pkg/bearing"
|
||||
"github.com/mdiluz/rove/pkg/objects"
|
||||
"github.com/mdiluz/rove/pkg/rove"
|
||||
"github.com/mdiluz/rove/pkg/vector"
|
||||
)
|
||||
|
||||
|
@ -429,11 +430,11 @@ func (w *World) Enqueue(rover string, commands ...Command) error {
|
|||
// First validate the commands
|
||||
for _, c := range commands {
|
||||
switch c.Command {
|
||||
case CommandMove:
|
||||
case rove.CommandType_move:
|
||||
if _, err := bearing.FromString(c.Bearing); err != nil {
|
||||
return fmt.Errorf("unknown bearing: %s", c.Bearing)
|
||||
}
|
||||
case CommandBroadcast:
|
||||
case rove.CommandType_broadcast:
|
||||
if len(c.Message) > 3 {
|
||||
return fmt.Errorf("too many characters in message (limit 3): %d", len(c.Message))
|
||||
}
|
||||
|
@ -442,9 +443,9 @@ func (w *World) Enqueue(rover string, commands ...Command) error {
|
|||
return fmt.Errorf("invalid message character: %c", b)
|
||||
}
|
||||
}
|
||||
case CommandStash:
|
||||
case CommandRepair:
|
||||
case CommandRecharge:
|
||||
case rove.CommandType_stash:
|
||||
case rove.CommandType_repair:
|
||||
case rove.CommandType_recharge:
|
||||
// Nothing to verify
|
||||
default:
|
||||
return fmt.Errorf("unknown command: %s", c.Command)
|
||||
|
@ -508,19 +509,19 @@ func (w *World) ExecuteCommand(c *Command, rover string) (err error) {
|
|||
log.Printf("Executing command: %+v for %s\n", *c, rover)
|
||||
|
||||
switch c.Command {
|
||||
case CommandMove:
|
||||
case rove.CommandType_move:
|
||||
if dir, err := bearing.FromString(c.Bearing); err != nil {
|
||||
return err
|
||||
} else if _, err := w.MoveRover(rover, dir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case CommandStash:
|
||||
case rove.CommandType_stash:
|
||||
if _, err := w.RoverStash(rover); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case CommandRepair:
|
||||
case rove.CommandType_repair:
|
||||
r, err := w.GetRover(rover)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -532,12 +533,12 @@ func (w *World) ExecuteCommand(c *Command, rover string) (err error) {
|
|||
r.AddLogEntryf("repaired self to %d", r.Integrity)
|
||||
w.Rovers[rover] = r
|
||||
}
|
||||
case CommandRecharge:
|
||||
case rove.CommandType_recharge:
|
||||
_, err := w.RoverRecharge(rover)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case CommandBroadcast:
|
||||
case rove.CommandType_broadcast:
|
||||
if err := w.RoverBroadcast(rover, c.Message); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"github.com/mdiluz/rove/pkg/atlas"
|
||||
"github.com/mdiluz/rove/pkg/bearing"
|
||||
"github.com/mdiluz/rove/pkg/objects"
|
||||
"github.com/mdiluz/rove/pkg/rove"
|
||||
"github.com/mdiluz/rove/pkg/vector"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -272,7 +273,7 @@ func TestWorld_RoverRepair(t *testing.T) {
|
|||
assert.NoError(t, err, "couldn't get rover info")
|
||||
assert.Equal(t, originalInfo.Integrity-1, newinfo.Integrity, "rover should have lost integrity")
|
||||
|
||||
err = world.ExecuteCommand(&Command{Command: CommandRepair}, a)
|
||||
err = world.ExecuteCommand(&Command{Command: rove.CommandType_repair}, a)
|
||||
assert.NoError(t, err, "Failed to repair rover")
|
||||
|
||||
newinfo, err = world.GetRover(a)
|
||||
|
@ -286,7 +287,7 @@ func TestWorld_RoverRepair(t *testing.T) {
|
|||
assert.NoError(t, err, "Failed to stash")
|
||||
assert.Equal(t, objects.SmallRock, o, "Failed to get correct object")
|
||||
|
||||
err = world.ExecuteCommand(&Command{Command: CommandRepair}, a)
|
||||
err = world.ExecuteCommand(&Command{Command: rove.CommandType_repair}, a)
|
||||
assert.NoError(t, err, "Failed to repair rover")
|
||||
|
||||
newinfo, err = world.GetRover(a)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue