Rename /view to /radar and fill in more of the response
This commit is contained in:
parent
b2b782f61d
commit
14977de5bc
5 changed files with 56 additions and 31 deletions
|
@ -18,8 +18,8 @@ type RoverAttributes struct {
|
||||||
// Speed represents the Speed that the rover will move per second
|
// Speed represents the Speed that the rover will move per second
|
||||||
Speed float64 `json:"speed"`
|
Speed float64 `json:"speed"`
|
||||||
|
|
||||||
// Sight represents the distance the unit can see
|
// Range represents the distance the unit's radar can see
|
||||||
Sight float64 `json:"sight"`
|
Range float64 `json:"range"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rover describes a single rover in the world
|
// Rover describes a single rover in the world
|
||||||
|
@ -53,7 +53,7 @@ func (w *World) SpawnRover() uuid.UUID {
|
||||||
// TODO: Stop these being random numbers
|
// TODO: Stop these being random numbers
|
||||||
Attributes: RoverAttributes{
|
Attributes: RoverAttributes{
|
||||||
Speed: 1.0,
|
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
|
// Execute will run the commands given
|
||||||
func (w *World) Execute(commands ...Command) error {
|
func (w *World) Execute(commands ...Command) error {
|
||||||
for _, c := range commands {
|
for _, c := range commands {
|
||||||
|
|
|
@ -33,7 +33,7 @@ func TestWorld_RoverAttributes(t *testing.T) {
|
||||||
|
|
||||||
attribs, err := world.RoverAttributes(a)
|
attribs, err := world.RoverAttributes(a)
|
||||||
assert.NoError(t, err, "Failed to get rover attribs")
|
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")
|
assert.NotZero(t, attribs.Speed, "Rover should not be spawned unable to move")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
|
import "github.com/mdiluz/rove/pkg/game"
|
||||||
|
|
||||||
// ==============================
|
// ==============================
|
||||||
// API: /status method: GET
|
// API: /status method: GET
|
||||||
// Queries the status of the server
|
// Queries the status of the server
|
||||||
|
@ -44,8 +46,7 @@ type SpawnResponse struct {
|
||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
|
|
||||||
// The location of the spawned entity
|
// The location of the spawned entity
|
||||||
X float64 `json:"x"`
|
Position game.Vector `json:"position"`
|
||||||
Y float64 `json:"y"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==============================
|
// ==============================
|
||||||
|
@ -81,16 +82,19 @@ type Command struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================
|
// ================
|
||||||
// API: /view POST
|
// API: /radar POST
|
||||||
// Queries the current view for the user
|
// Queries the current radar for the user
|
||||||
|
|
||||||
// ViewData describes the input data to request an accounts current view
|
// RadarData describes the input data to request an accounts current radar
|
||||||
type ViewData struct {
|
type RadarData struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ViewResponse describes the response to a /view call
|
// RadarResponse describes the response to a /radar call
|
||||||
type ViewResponse struct {
|
type RadarResponse struct {
|
||||||
Success bool `json:"success"`
|
Success bool `json:"success"`
|
||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
|
|
||||||
|
// The set of positions for nearby rovers
|
||||||
|
Rovers []game.Vector `json:"rovers"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,9 +44,9 @@ var Routes = []Route{
|
||||||
handler: HandleCommands,
|
handler: HandleCommands,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/view",
|
path: "/radar",
|
||||||
method: http.MethodPost,
|
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()
|
response.Error = err.Error()
|
||||||
} else {
|
} else {
|
||||||
response.Success = true
|
response.Success = true
|
||||||
response.X = pos.X
|
response.Position = pos
|
||||||
response.Y = pos.Y
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,10 +200,10 @@ func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleView handles the view request
|
// HandleRadar handles the radar request
|
||||||
func HandleView(s *Server, b io.ReadCloser, w io.Writer) error {
|
func HandleRadar(s *Server, b io.ReadCloser, w io.Writer) error {
|
||||||
// Set up the response
|
// Set up the response
|
||||||
var response = ViewResponse{
|
var response = RadarResponse{
|
||||||
Success: false,
|
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 {
|
} else if id, err := uuid.Parse(data.Id); err != nil {
|
||||||
response.Error = fmt.Sprintf("Provided account ID was invalid: %s", err)
|
response.Error = fmt.Sprintf("Provided account ID was invalid: %s", err)
|
||||||
|
|
||||||
} else {
|
} else if inst, err := s.accountant.GetRover(id); err != nil {
|
||||||
// log the data sent
|
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
||||||
fmt.Printf("\tcommands data: %v\n", data)
|
|
||||||
|
|
||||||
// TODO: Query the view for this account
|
} else if radar, err := s.world.RadarFromRover(inst); err != nil {
|
||||||
fmt.Println(id)
|
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
|
// Log the response
|
||||||
|
|
|
@ -119,7 +119,7 @@ func TestHandleCommands(t *testing.T) {
|
||||||
assert.Equal(t, pos, pos2, "Rover should have moved by bearing")
|
assert.Equal(t, pos, pos2, "Rover should have moved by bearing")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleView(t *testing.T) {
|
func TestHandleRadar(t *testing.T) {
|
||||||
s := NewServer()
|
s := NewServer()
|
||||||
a, err := s.accountant.RegisterAccount("test")
|
a, err := s.accountant.RegisterAccount("test")
|
||||||
assert.NoError(t, err, "Error registering account")
|
assert.NoError(t, err, "Error registering account")
|
||||||
|
@ -127,24 +127,24 @@ func TestHandleView(t *testing.T) {
|
||||||
// Spawn the rover rover for the account
|
// Spawn the rover rover for the account
|
||||||
_, _, err = s.SpawnRoverForAccount(a.Id)
|
_, _, err = s.SpawnRoverForAccount(a.Id)
|
||||||
|
|
||||||
data := ViewData{
|
data := RadarData{
|
||||||
Id: a.Id.String(),
|
Id: a.Id.String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := json.Marshal(data)
|
b, err := json.Marshal(data)
|
||||||
assert.NoError(t, err, "Error marshalling 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()
|
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)
|
json.NewDecoder(response.Body).Decode(&status)
|
||||||
|
|
||||||
if status.Success != true {
|
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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue