Convert number to repeat to avoid confusion

This commit is contained in:
Marc Di Luzio 2020-07-26 18:02:06 +01:00
parent c0d4a809c9
commit 74e1cd4564
6 changed files with 25 additions and 30 deletions

View file

@ -26,14 +26,14 @@ func printUsage() {
fmt.Fprintln(os.Stderr, "Usage: rove ARG [OPT...]") fmt.Fprintln(os.Stderr, "Usage: rove ARG [OPT...]")
fmt.Fprintf(os.Stderr, "\n") fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintln(os.Stderr, "Arguments:") fmt.Fprintln(os.Stderr, "Arguments:")
fmt.Fprintln(os.Stderr, "\tversion outputs version") fmt.Fprintln(os.Stderr, "\tversion outputs version")
fmt.Fprintln(os.Stderr, "\thelp outputs this usage text") fmt.Fprintln(os.Stderr, "\thelp outputs this usage text")
fmt.Fprintln(os.Stderr, "\tconfig [HOST] outputs the local config, optionally sets host") fmt.Fprintln(os.Stderr, "\tconfig [HOST] outputs the local config, optionally sets host")
fmt.Fprintln(os.Stderr, "\tserver-status prints the server status") fmt.Fprintln(os.Stderr, "\tserver-status prints the server status")
fmt.Fprintln(os.Stderr, "\tregister NAME registers an account and spawns a rover") fmt.Fprintln(os.Stderr, "\tregister NAME registers an account and spawns a rover")
fmt.Fprintln(os.Stderr, "\tradar prints radar data in ASCII form") fmt.Fprintln(os.Stderr, "\tradar prints radar data in ASCII form")
fmt.Fprintln(os.Stderr, "\tstatus gets rover status") fmt.Fprintln(os.Stderr, "\tstatus gets rover status")
fmt.Fprintln(os.Stderr, "\tcommand [NUM] CMD [VAL...] queues commands, accepts multiple, see below") fmt.Fprintln(os.Stderr, "\tcommand [REPEAT] CMD [VAL...] queues commands, accepts multiple in sequence for command values see below")
fmt.Fprintf(os.Stderr, "\n") fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintln(os.Stderr, "Rover commands:") fmt.Fprintln(os.Stderr, "Rover commands:")
fmt.Fprintln(os.Stderr, "\ttoggle toggles the current sail mode") fmt.Fprintln(os.Stderr, "\ttoggle toggles the current sail mode")
@ -244,7 +244,7 @@ func InnerMain(command string, args ...string) error {
number = num number = num
i++ i++
if i >= len(args) { if i >= len(args) {
return fmt.Errorf("must pass command after number") return fmt.Errorf("must pass command after repeat number")
} }
} }
@ -262,7 +262,7 @@ func InnerMain(command string, args ...string) error {
&roveapi.Command{ &roveapi.Command{
Command: roveapi.CommandType_turn, Command: roveapi.CommandType_turn,
Bearing: b, Bearing: b,
Number: int32(number), Repeat: int32(number),
}, },
) )
case "broadcast": case "broadcast":
@ -276,7 +276,7 @@ func InnerMain(command string, args ...string) error {
&roveapi.Command{ &roveapi.Command{
Command: roveapi.CommandType_broadcast, Command: roveapi.CommandType_broadcast,
Data: []byte(args[i]), Data: []byte(args[i]),
Number: int32(number), Repeat: int32(number),
}, },
) )
default: default:
@ -284,7 +284,7 @@ func InnerMain(command string, args ...string) error {
commands = append(commands, commands = append(commands,
&roveapi.Command{ &roveapi.Command{
Command: roveapi.CommandType(roveapi.CommandType_value[args[i]]), Command: roveapi.CommandType(roveapi.CommandType_value[args[i]]),
Number: int32(number), Repeat: int32(number),
}, },
) )
} }

View file

@ -60,6 +60,5 @@ func Test_InnerMain(t *testing.T) {
// Give it malformed commands // Give it malformed commands
assert.Error(t, InnerMain("command", "unknown")) assert.Error(t, InnerMain("command", "unknown"))
assert.Error(t, InnerMain("command", "broadcast")) assert.Error(t, InnerMain("command", "broadcast"))
assert.Error(t, InnerMain("command", "0", "wait"))
assert.Error(t, InnerMain("command", "1")) assert.Error(t, InnerMain("command", "1"))
} }

View file

@ -229,10 +229,10 @@ func TestCommand_Wait(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, roveapi.SailPosition_SolarCharging, r.SailPosition) assert.Equal(t, roveapi.SailPosition_SolarCharging, r.SailPosition)
err = w.Enqueue(a, &roveapi.Command{Command: roveapi.CommandType_wait, Number: 5}, &roveapi.Command{Command: roveapi.CommandType_toggle}) err = w.Enqueue(a, &roveapi.Command{Command: roveapi.CommandType_wait, Repeat: 4}, &roveapi.Command{Command: roveapi.CommandType_toggle})
assert.NoError(t, err) assert.NoError(t, err)
// Tick 5 times during the wait // Tick 5 times during the wait (1 normal execute + 4)
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
w.Tick() w.Tick()

View file

@ -583,9 +583,6 @@ func (w *World) Enqueue(rover string, commands ...*roveapi.Command) error {
return fmt.Errorf("turn command given unknown bearing") return fmt.Errorf("turn command given unknown bearing")
} }
case roveapi.CommandType_wait: case roveapi.CommandType_wait:
if c.GetNumber() <= 0 {
return fmt.Errorf("wait command must be given positie number of ticks to wait")
}
case roveapi.CommandType_toggle: case roveapi.CommandType_toggle:
case roveapi.CommandType_stash: case roveapi.CommandType_stash:
case roveapi.CommandType_repair: case roveapi.CommandType_repair:
@ -717,9 +714,6 @@ func (w *World) Tick() {
func (w *World) ExecuteCommand(c *roveapi.Command, rover string) (done bool, err error) { func (w *World) ExecuteCommand(c *roveapi.Command, rover string) (done bool, err error) {
log.Printf("Executing command: %+v for %s\n", c.Command, rover) log.Printf("Executing command: %+v for %s\n", c.Command, rover)
// Decrement the number of the command
c.Number--
switch c.Command { switch c.Command {
case roveapi.CommandType_toggle: case roveapi.CommandType_toggle:
_, err = w.RoverToggle(rover) _, err = w.RoverToggle(rover)
@ -741,7 +735,9 @@ func (w *World) ExecuteCommand(c *roveapi.Command, rover string) (done bool, err
return true, fmt.Errorf("unknown command: %s", c.Command) return true, fmt.Errorf("unknown command: %s", c.Command)
} }
return c.Number <= 0, err // Decrement the repeat number
c.Repeat--
return c.Repeat < 0, err
} }
// Daytime returns if it's currently daytime // Daytime returns if it's currently daytime

View file

@ -641,8 +641,8 @@ type Command struct {
// The command type // The command type
Command CommandType `protobuf:"varint,1,opt,name=command,proto3,enum=roveapi.CommandType" json:"command,omitempty"` Command CommandType `protobuf:"varint,1,opt,name=command,proto3,enum=roveapi.CommandType" json:"command,omitempty"`
// The number of times to execute the command (assumes 1 if not present or 0) // The number of times to repeat the command after the first
Number int32 `protobuf:"varint,2,opt,name=number,proto3" json:"number,omitempty"` Repeat int32 `protobuf:"varint,2,opt,name=repeat,proto3" json:"repeat,omitempty"`
// broadcast - a simple message, must be composed of up to 3 printable ASCII // broadcast - a simple message, must be composed of up to 3 printable ASCII
// glyphs (32-126) // glyphs (32-126)
Data []byte `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"` Data []byte `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"`
@ -689,9 +689,9 @@ func (x *Command) GetCommand() CommandType {
return CommandType_none return CommandType_none
} }
func (x *Command) GetNumber() int32 { func (x *Command) GetRepeat() int32 {
if x != nil { if x != nil {
return x.Number return x.Repeat
} }
return 0 return 0
} }
@ -1427,8 +1427,8 @@ var file_roveapi_roveapi_proto_rawDesc = []byte{
0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x2e, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x2e, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64,
0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x14, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x14, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69,
0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x54, 0x79, 0x70, 0x65, 0x52, 0x07, 0x63, 0x6f, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x54, 0x79, 0x70, 0x65, 0x52, 0x07, 0x63, 0x6f,
0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x70, 0x65, 0x61, 0x74, 0x18,
0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x12, 0x0a, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x72, 0x65, 0x70, 0x65, 0x61, 0x74, 0x12, 0x12, 0x0a,
0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74,
0x61, 0x12, 0x2a, 0x0a, 0x07, 0x62, 0x65, 0x61, 0x72, 0x69, 0x6e, 0x67, 0x18, 0x04, 0x20, 0x01, 0x61, 0x12, 0x2a, 0x0a, 0x07, 0x62, 0x65, 0x61, 0x72, 0x69, 0x6e, 0x67, 0x18, 0x04, 0x20, 0x01,
0x28, 0x0e, 0x32, 0x10, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x42, 0x65, 0x61, 0x28, 0x0e, 0x32, 0x10, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x42, 0x65, 0x61,

View file

@ -126,8 +126,8 @@ message Command {
// The command type // The command type
CommandType command = 1; CommandType command = 1;
// The number of times to execute the command (assumes 1 if not present or 0) // The number of times to repeat the command after the first
int32 number = 2; int32 repeat = 2;
// broadcast - a simple message, must be composed of up to 3 printable ASCII // broadcast - a simple message, must be composed of up to 3 printable ASCII
// glyphs (32-126) // glyphs (32-126)