From 14977de5bcfea81c497771452643a7c066e90c35 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Thu, 4 Jun 2020 22:14:55 +0100 Subject: [PATCH] Rename /view to /radar and fill in more of the response --- pkg/game/world.go | 24 +++++++++++++++++++++--- pkg/game/world_test.go | 2 +- pkg/server/api.go | 20 ++++++++++++-------- pkg/server/routes.go | 27 +++++++++++++++------------ pkg/server/routes_test.go | 14 +++++++------- 5 files changed, 56 insertions(+), 31 deletions(-) diff --git a/pkg/game/world.go b/pkg/game/world.go index c4f560c..813f07d 100644 --- a/pkg/game/world.go +++ b/pkg/game/world.go @@ -18,8 +18,8 @@ type RoverAttributes struct { // Speed represents the Speed that the rover will move per second Speed float64 `json:"speed"` - // Sight represents the distance the unit can see - Sight float64 `json:"sight"` + // Range represents the distance the unit's radar can see + Range float64 `json:"range"` } // Rover describes a single rover in the world @@ -53,7 +53,7 @@ func (w *World) SpawnRover() uuid.UUID { // TODO: Stop these being random numbers Attributes: RoverAttributes{ Speed: 1.0, - Sight: 20.0, + Range: 20.0, }, } @@ -125,6 +125,24 @@ func (w *World) MoveRover(id uuid.UUID, bearing float64, duration float64) (Vect } } +// RadarDescription describes what a rover can see +type RadarDescription struct { + // Rovers is the set of rovers that this radar can see + Rovers []Vector `json:"rovers"` +} + +// RadarFromRover can be used to query what a rover can currently see +func (w World) RadarFromRover(id uuid.UUID) (RadarDescription, error) { + if _, ok := w.Rovers[id]; ok { + + // TODO: Gather nearby rovers within the range + + return RadarDescription{}, nil + } else { + return RadarDescription{}, fmt.Errorf("no rover matching id") + } +} + // Execute will run the commands given func (w *World) Execute(commands ...Command) error { for _, c := range commands { diff --git a/pkg/game/world_test.go b/pkg/game/world_test.go index 154bed7..55fee3c 100644 --- a/pkg/game/world_test.go +++ b/pkg/game/world_test.go @@ -33,7 +33,7 @@ func TestWorld_RoverAttributes(t *testing.T) { attribs, err := world.RoverAttributes(a) assert.NoError(t, err, "Failed to get rover attribs") - assert.NotZero(t, attribs.Sight, "Rover should not be spawned blind") + assert.NotZero(t, attribs.Range, "Rover should not be spawned blind") assert.NotZero(t, attribs.Speed, "Rover should not be spawned unable to move") } diff --git a/pkg/server/api.go b/pkg/server/api.go index dc3f639..6ceb479 100644 --- a/pkg/server/api.go +++ b/pkg/server/api.go @@ -1,5 +1,7 @@ package server +import "github.com/mdiluz/rove/pkg/game" + // ============================== // API: /status method: GET // Queries the status of the server @@ -44,8 +46,7 @@ type SpawnResponse struct { Error string `json:"error"` // The location of the spawned entity - X float64 `json:"x"` - Y float64 `json:"y"` + Position game.Vector `json:"position"` } // ============================== @@ -81,16 +82,19 @@ type Command struct { } // ================ -// API: /view POST -// Queries the current view for the user +// API: /radar POST +// Queries the current radar for the user -// ViewData describes the input data to request an accounts current view -type ViewData struct { +// RadarData describes the input data to request an accounts current radar +type RadarData struct { Id string `json:"id"` } -// ViewResponse describes the response to a /view call -type ViewResponse struct { +// RadarResponse describes the response to a /radar call +type RadarResponse struct { Success bool `json:"success"` Error string `json:"error"` + + // The set of positions for nearby rovers + Rovers []game.Vector `json:"rovers"` } diff --git a/pkg/server/routes.go b/pkg/server/routes.go index 495ec87..d572b19 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -44,9 +44,9 @@ var Routes = []Route{ handler: HandleCommands, }, { - path: "/view", + path: "/radar", method: http.MethodPost, - handler: HandleView, + handler: HandleRadar, }, } @@ -135,8 +135,7 @@ func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) error { response.Error = err.Error() } else { response.Success = true - response.X = pos.X - response.Y = pos.Y + response.Position = pos } } @@ -201,10 +200,10 @@ func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) error { return nil } -// HandleView handles the view request -func HandleView(s *Server, b io.ReadCloser, w io.Writer) error { +// HandleRadar handles the radar request +func HandleRadar(s *Server, b io.ReadCloser, w io.Writer) error { // Set up the response - var response = ViewResponse{ + var response = RadarResponse{ Success: false, } @@ -220,12 +219,16 @@ func HandleView(s *Server, b io.ReadCloser, w io.Writer) error { } else if id, err := uuid.Parse(data.Id); err != nil { response.Error = fmt.Sprintf("Provided account ID was invalid: %s", err) - } else { - // log the data sent - fmt.Printf("\tcommands data: %v\n", data) + } else if inst, err := s.accountant.GetRover(id); err != nil { + response.Error = fmt.Sprintf("Provided account has no rover: %s", err) - // TODO: Query the view for this account - fmt.Println(id) + } else if radar, err := s.world.RadarFromRover(inst); err != nil { + response.Error = fmt.Sprintf("Error getting radar from rover: %s", err) + + } else { + // Fill in the response + response.Rovers = radar.Rovers + response.Success = true } // Log the response diff --git a/pkg/server/routes_test.go b/pkg/server/routes_test.go index a70eeb4..b2b63f2 100644 --- a/pkg/server/routes_test.go +++ b/pkg/server/routes_test.go @@ -119,7 +119,7 @@ func TestHandleCommands(t *testing.T) { assert.Equal(t, pos, pos2, "Rover should have moved by bearing") } -func TestHandleView(t *testing.T) { +func TestHandleRadar(t *testing.T) { s := NewServer() a, err := s.accountant.RegisterAccount("test") assert.NoError(t, err, "Error registering account") @@ -127,24 +127,24 @@ func TestHandleView(t *testing.T) { // Spawn the rover rover for the account _, _, err = s.SpawnRoverForAccount(a.Id) - data := ViewData{ + data := RadarData{ Id: a.Id.String(), } b, err := json.Marshal(data) assert.NoError(t, err, "Error marshalling data") - request, _ := http.NewRequest(http.MethodPost, "/view", bytes.NewReader(b)) + request, _ := http.NewRequest(http.MethodPost, "/radar", bytes.NewReader(b)) response := httptest.NewRecorder() - s.wrapHandler(http.MethodPost, HandleView)(response, request) + s.wrapHandler(http.MethodPost, HandleRadar)(response, request) - var status ViewResponse + var status RadarResponse json.NewDecoder(response.Body).Decode(&status) if status.Success != true { - t.Errorf("got false for /view") + t.Errorf("got false for /radar") } - // TODO: Verify the view information + // TODO: Verify the radar information }