Reinstate BearingFromString function
This commit is contained in:
parent
db8ed0302d
commit
57f668ae54
2 changed files with 21 additions and 5 deletions
|
@ -11,7 +11,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mdiluz/rove/cmd/rove/internal"
|
"github.com/mdiluz/rove/cmd/rove/internal"
|
||||||
"github.com/mdiluz/rove/pkg/maths"
|
|
||||||
"github.com/mdiluz/rove/pkg/version"
|
"github.com/mdiluz/rove/pkg/version"
|
||||||
"github.com/mdiluz/rove/proto/roveapi"
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
@ -124,6 +123,21 @@ func checkAccount(a Account) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BearingFromString converts a string to a bearing
|
||||||
|
func BearingFromString(s string) roveapi.Bearing {
|
||||||
|
switch s {
|
||||||
|
case "N":
|
||||||
|
return roveapi.Bearing_North
|
||||||
|
case "E":
|
||||||
|
return roveapi.Bearing_East
|
||||||
|
case "S":
|
||||||
|
return roveapi.Bearing_South
|
||||||
|
case "W":
|
||||||
|
return roveapi.Bearing_West
|
||||||
|
}
|
||||||
|
return roveapi.Bearing_BearingUnknown
|
||||||
|
}
|
||||||
|
|
||||||
// InnerMain wraps the main function so we can test it
|
// InnerMain wraps the main function so we can test it
|
||||||
func InnerMain(command string, args ...string) error {
|
func InnerMain(command string, args ...string) error {
|
||||||
|
|
||||||
|
@ -215,13 +229,15 @@ func InnerMain(command string, args ...string) error {
|
||||||
i++
|
i++
|
||||||
if len(args) == i {
|
if len(args) == i {
|
||||||
return fmt.Errorf("move command must be passed bearing")
|
return fmt.Errorf("move command must be passed bearing")
|
||||||
} else if _, err := maths.BearingFromString(args[i]); err != nil {
|
}
|
||||||
return err
|
var b roveapi.Bearing
|
||||||
|
if b = BearingFromString(args[i]); b == roveapi.Bearing_BearingUnknown {
|
||||||
|
return fmt.Errorf("unrecognised bearing: %s", args[i])
|
||||||
}
|
}
|
||||||
commands = append(commands,
|
commands = append(commands,
|
||||||
&roveapi.Command{
|
&roveapi.Command{
|
||||||
Command: roveapi.CommandType_move,
|
Command: roveapi.CommandType_move,
|
||||||
Data: &roveapi.Command_Bearing{Bearing: args[i]},
|
Data: &roveapi.Command_Bearing{Bearing: b},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
case "broadcast":
|
case "broadcast":
|
||||||
|
|
|
@ -427,7 +427,7 @@ func (w *World) Enqueue(rover string, commands ...Command) error {
|
||||||
switch c.Command {
|
switch c.Command {
|
||||||
case roveapi.CommandType_move:
|
case roveapi.CommandType_move:
|
||||||
if c.Bearing == roveapi.Bearing_BearingUnknown {
|
if c.Bearing == roveapi.Bearing_BearingUnknown {
|
||||||
return fmt.Errorf("bearing must be cardinal")
|
return fmt.Errorf("bearing must be valid")
|
||||||
}
|
}
|
||||||
case roveapi.CommandType_broadcast:
|
case roveapi.CommandType_broadcast:
|
||||||
if len(c.Message) > 3 {
|
if len(c.Message) > 3 {
|
||||||
|
|
Loading…
Add table
Reference in a new issue