Add SailPosition to the rover and implement toggle command
This also converts the commands to use the proto type for simplicity
This commit is contained in:
parent
6f30b665c7
commit
f78efd1223
10 changed files with 490 additions and 267 deletions
|
@ -5,7 +5,6 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/rove"
|
||||
"github.com/mdiluz/rove/pkg/version"
|
||||
"github.com/mdiluz/rove/proto/roveapi"
|
||||
)
|
||||
|
@ -76,31 +75,13 @@ func (s *Server) Status(ctx context.Context, req *roveapi.StatusRequest) (respon
|
|||
inv = append(inv, byte(i.Type))
|
||||
}
|
||||
|
||||
i, q := s.world.RoverCommands(resp)
|
||||
in, qu := s.world.RoverCommands(resp)
|
||||
var incoming, queued []*roveapi.Command
|
||||
for _, i := range i {
|
||||
c := &roveapi.Command{
|
||||
Command: i.Command,
|
||||
}
|
||||
switch i.Command {
|
||||
case roveapi.CommandType_broadcast:
|
||||
c.Data = &roveapi.Command_Message{
|
||||
Message: i.Message,
|
||||
}
|
||||
}
|
||||
incoming = append(incoming, c)
|
||||
for i := range in {
|
||||
incoming = append(incoming, &in[i])
|
||||
}
|
||||
for _, q := range q {
|
||||
c := &roveapi.Command{
|
||||
Command: q.Command,
|
||||
}
|
||||
switch q.Command {
|
||||
case roveapi.CommandType_broadcast:
|
||||
c.Data = &roveapi.Command_Message{
|
||||
Message: q.Message,
|
||||
}
|
||||
}
|
||||
queued = append(queued, c)
|
||||
for i := range qu {
|
||||
queued = append(queued, &qu[i])
|
||||
}
|
||||
var logs []*roveapi.Log
|
||||
for _, log := range rover.Logs {
|
||||
|
@ -116,6 +97,7 @@ func (s *Server) Status(ctx context.Context, req *roveapi.StatusRequest) (respon
|
|||
X: int32(rover.Pos.X),
|
||||
Y: int32(rover.Pos.Y),
|
||||
},
|
||||
Bearing: rover.Bearing,
|
||||
Range: int32(rover.Range),
|
||||
Inventory: inv,
|
||||
Capacity: int32(rover.Capacity),
|
||||
|
@ -125,6 +107,7 @@ func (s *Server) Status(ctx context.Context, req *roveapi.StatusRequest) (respon
|
|||
MaximumCharge: int32(rover.MaximumCharge),
|
||||
IncomingCommands: incoming,
|
||||
QueuedCommands: queued,
|
||||
SailPosition: rover.SailPosition,
|
||||
Logs: logs,
|
||||
}
|
||||
}
|
||||
|
@ -179,19 +162,7 @@ func (s *Server) Command(ctx context.Context, req *roveapi.CommandRequest) (*rov
|
|||
return nil, err
|
||||
}
|
||||
|
||||
var cmds []rove.Command
|
||||
for _, c := range req.Commands {
|
||||
n := rove.Command{
|
||||
Command: c.Command,
|
||||
}
|
||||
switch c.Command {
|
||||
case roveapi.CommandType_broadcast:
|
||||
n.Message = c.GetMessage()
|
||||
}
|
||||
cmds = append(cmds, n)
|
||||
}
|
||||
|
||||
if err := s.world.Enqueue(resp, cmds...); err != nil {
|
||||
if err := s.world.Enqueue(resp, req.Commands...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
@ -127,12 +127,20 @@ func BearingFromString(s string) roveapi.Bearing {
|
|||
switch s {
|
||||
case "N":
|
||||
return roveapi.Bearing_North
|
||||
case "NE":
|
||||
return roveapi.Bearing_NorthEast
|
||||
case "E":
|
||||
return roveapi.Bearing_East
|
||||
case "SE":
|
||||
return roveapi.Bearing_SouthEast
|
||||
case "S":
|
||||
return roveapi.Bearing_South
|
||||
case "SW":
|
||||
return roveapi.Bearing_SouthWest
|
||||
case "W":
|
||||
return roveapi.Bearing_West
|
||||
case "NW":
|
||||
return roveapi.Bearing_NorthWest
|
||||
}
|
||||
return roveapi.Bearing_BearingUnknown
|
||||
}
|
||||
|
@ -224,6 +232,21 @@ func InnerMain(command string, args ...string) error {
|
|||
var commands []*roveapi.Command
|
||||
for i := 0; i < len(args); i++ {
|
||||
switch args[i] {
|
||||
case "turn":
|
||||
i++
|
||||
if len(args) == i {
|
||||
return fmt.Errorf("turn command must be passed a compass bearing")
|
||||
}
|
||||
b := BearingFromString(args[i])
|
||||
if b == roveapi.Bearing_BearingUnknown {
|
||||
return fmt.Errorf("turn command must be given a valid bearing %s", args[i])
|
||||
}
|
||||
commands = append(commands,
|
||||
&roveapi.Command{
|
||||
Command: roveapi.CommandType_broadcast,
|
||||
Data: &roveapi.Command_Broadcast{Broadcast: []byte(args[i])},
|
||||
},
|
||||
)
|
||||
case "broadcast":
|
||||
i++
|
||||
if len(args) == i {
|
||||
|
@ -234,7 +257,7 @@ func InnerMain(command string, args ...string) error {
|
|||
commands = append(commands,
|
||||
&roveapi.Command{
|
||||
Command: roveapi.CommandType_broadcast,
|
||||
Data: &roveapi.Command_Message{Message: []byte(args[i])},
|
||||
Data: &roveapi.Command_Broadcast{Broadcast: []byte(args[i])},
|
||||
},
|
||||
)
|
||||
default:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue