Simplify - remove duplicate command types in favor of a better defined Command type in proto

This commit is contained in:
Marc Di Luzio 2020-07-10 00:12:54 +01:00
parent 7d780d05bd
commit 96a137ad2f
9 changed files with 370 additions and 471 deletions

View file

@ -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
}