From 15146035177e6ba8da33bb26861beb70b905a1bb Mon Sep 17 00:00:00 2001
From: Marc Di Luzio <marc.diluzio@gmail.com>
Date: Sun, 26 Jul 2020 17:19:04 +0100
Subject: [PATCH] Allow number to be used in all commands

---
 pkg/rove/world.go           | 43 +++++++++++--------------------------
 proto/roveapi/roveapi.pb.go | 42 +++++++++++++++++++-----------------
 proto/roveapi/roveapi.proto | 12 +++++------
 3 files changed, 40 insertions(+), 57 deletions(-)

diff --git a/pkg/rove/world.go b/pkg/rove/world.go
index 07e8e5e..4d08c7a 100644
--- a/pkg/rove/world.go
+++ b/pkg/rove/world.go
@@ -717,50 +717,31 @@ func (w *World) Tick() {
 func (w *World) ExecuteCommand(c *roveapi.Command, rover string) (done bool, err error) {
 	log.Printf("Executing command: %+v for %s\n", c.Command, rover)
 
+	// Decrement the number of the command
+	c.Number--
+
 	switch c.Command {
 	case roveapi.CommandType_toggle:
-		if _, err := w.RoverToggle(rover); err != nil {
-			return true, err
-		}
+		_, err = w.RoverToggle(rover)
 	case roveapi.CommandType_stash:
-		if _, err := w.RoverStash(rover); err != nil {
-			return true, err
-		}
-
+		_, err = w.RoverStash(rover)
 	case roveapi.CommandType_repair:
-		if _, err := w.RoverRepair(rover); err != nil {
-			return true, err
-		}
-
+		_, err = w.RoverRepair(rover)
 	case roveapi.CommandType_broadcast:
-		if err := w.RoverBroadcast(rover, c.GetData()); err != nil {
-			return true, err
-		}
-
+		err = w.RoverBroadcast(rover, c.GetData())
 	case roveapi.CommandType_turn:
-		if _, err := w.RoverTurn(rover, c.GetBearing()); err != nil {
-			return true, err
-		}
-
+		_, err = w.RoverTurn(rover, c.GetBearing())
 	case roveapi.CommandType_salvage:
-		if _, err := w.RoverSalvage(rover); err != nil {
-			return true, err
-		}
-
+		_, err = w.RoverSalvage(rover)
 	case roveapi.CommandType_transfer:
-		if _, err := w.RoverTransfer(rover); err != nil {
-			return true, err
-		}
-
+		_, err = w.RoverTransfer(rover)
 	case roveapi.CommandType_wait:
-		c.Number--
-		return c.Number == 0, nil
-
+		// Nothing to do
 	default:
 		return true, fmt.Errorf("unknown command: %s", c.Command)
 	}
 
-	return
+	return c.Number <= 0, err
 }
 
 // Daytime returns if it's currently daytime
diff --git a/proto/roveapi/roveapi.pb.go b/proto/roveapi/roveapi.pb.go
index 2b4df0b..4c0178e 100644
--- a/proto/roveapi/roveapi.pb.go
+++ b/proto/roveapi/roveapi.pb.go
@@ -53,7 +53,7 @@ const (
 	CommandType_salvage CommandType = 6
 	// Transfers remote control into dormant rover
 	CommandType_transfer CommandType = 7
-	// Waits for the specified number of server ticks (requires number)
+	// Waits before performing the next command
 	CommandType_wait CommandType = 8
 )
 
@@ -193,7 +193,8 @@ const (
 	Object_RockSmall Object = 3
 	// RockLarge is a large blocking rock
 	Object_RockLarge Object = 4
-	// RoverParts is one unit of rover parts, used for repairing and fixing the rover
+	// RoverParts is one unit of rover parts, used for repairing and fixing the
+	// rover
 	Object_RoverParts Object = 5
 )
 
@@ -640,12 +641,13 @@ type Command struct {
 
 	// The command type
 	Command CommandType `protobuf:"varint,1,opt,name=command,proto3,enum=roveapi.CommandType" json:"command,omitempty"`
-	// broadcast - a simple message, must be composed of up to 3 printable ASCII glyphs (32-126)
-	Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
+	// The number of times to execute the command (assumes 1 if not present or 0)
+	Number int32 `protobuf:"varint,2,opt,name=number,proto3" json:"number,omitempty"`
+	// broadcast - a simple message, must be composed of up to 3 printable ASCII
+	// glyphs (32-126)
+	Data []byte `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"`
 	// move - the bearing for the rover to turn to
-	Bearing Bearing `protobuf:"varint,3,opt,name=bearing,proto3,enum=roveapi.Bearing" json:"bearing,omitempty"`
-	// wait - the number of server ticks to wait
-	Number int32 `protobuf:"varint,4,opt,name=number,proto3" json:"number,omitempty"`
+	Bearing Bearing `protobuf:"varint,4,opt,name=bearing,proto3,enum=roveapi.Bearing" json:"bearing,omitempty"`
 }
 
 func (x *Command) Reset() {
@@ -687,6 +689,13 @@ func (x *Command) GetCommand() CommandType {
 	return CommandType_none
 }
 
+func (x *Command) GetNumber() int32 {
+	if x != nil {
+		return x.Number
+	}
+	return 0
+}
+
 func (x *Command) GetData() []byte {
 	if x != nil {
 		return x.Data
@@ -701,13 +710,6 @@ func (x *Command) GetBearing() Bearing {
 	return Bearing_BearingUnknown
 }
 
-func (x *Command) GetNumber() int32 {
-	if x != nil {
-		return x.Number
-	}
-	return 0
-}
-
 // CommandRequest describes a set of commands to be requested for the rover
 type CommandRequest struct {
 	state         protoimpl.MessageState
@@ -1425,12 +1427,12 @@ var file_roveapi_roveapi_proto_rawDesc = []byte{
 	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,
 	0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x54, 0x79, 0x70, 0x65, 0x52, 0x07, 0x63, 0x6f,
-	0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20,
-	0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x12, 0x2a, 0x0a, 0x07, 0x62, 0x65, 0x61,
-	0x72, 0x69, 0x6e, 0x67, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x10, 0x2e, 0x72, 0x6f, 0x76,
-	0x65, 0x61, 0x70, 0x69, 0x2e, 0x42, 0x65, 0x61, 0x72, 0x69, 0x6e, 0x67, 0x52, 0x07, 0x62, 0x65,
-	0x61, 0x72, 0x69, 0x6e, 0x67, 0x12, 0x16, 0x0a, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18,
-	0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x22, 0x6a, 0x0a,
+	0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18,
+	0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x12, 0x0a,
+	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,
+	0x28, 0x0e, 0x32, 0x10, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x42, 0x65, 0x61,
+	0x72, 0x69, 0x6e, 0x67, 0x52, 0x07, 0x62, 0x65, 0x61, 0x72, 0x69, 0x6e, 0x67, 0x22, 0x6a, 0x0a,
 	0x0e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12,
 	0x2a, 0x0a, 0x07, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b,
 	0x32, 0x10, 0x2e, 0x72, 0x6f, 0x76, 0x65, 0x61, 0x70, 0x69, 0x2e, 0x41, 0x63, 0x63, 0x6f, 0x75,
diff --git a/proto/roveapi/roveapi.proto b/proto/roveapi/roveapi.proto
index 46cb858..21c992b 100644
--- a/proto/roveapi/roveapi.proto
+++ b/proto/roveapi/roveapi.proto
@@ -103,7 +103,7 @@ enum CommandType {
   salvage = 6;
   // Transfers remote control into dormant rover
   transfer = 7;
-  // Waits for the specified number of server ticks (requires number)
+  // Waits before performing the next command
   wait = 8;
 }
 
@@ -126,15 +126,15 @@ message Command {
   // The command type
   CommandType command = 1;
 
+  // The number of times to execute the command (assumes 1 if not present or 0)
+  int32 number = 2;
+
   // broadcast - a simple message, must be composed of up to 3 printable ASCII
   // glyphs (32-126)
-  bytes data = 2;
+  bytes data = 3;
 
   // move - the bearing for the rover to turn to
-  Bearing bearing = 3;
-
-  // wait - the number of server ticks to wait
-  int32 number = 4;
+  Bearing bearing = 4;
 }
 
 // CommandRequest describes a set of commands to be requested for the rover