Rename /view to /radar and fill in more of the response

This commit is contained in:
Marc Di Luzio 2020-06-04 22:14:55 +01:00
parent b2b782f61d
commit 14977de5bc
5 changed files with 56 additions and 31 deletions

View file

@ -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"`
}

View file

@ -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

View file

@ -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
}