Large refactor to properly implement radar
/radar now returns a set of non-empty tile blips
This commit is contained in:
parent
fc54775df9
commit
43648926ca
11 changed files with 182 additions and 50 deletions
|
@ -197,7 +197,7 @@ func HandleRadar(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
|
|||
|
||||
} else {
|
||||
fmt.Printf("Responded with radar\taccount:%s\tradar:%+v\n", id, radar)
|
||||
response.Rovers = radar.Rovers
|
||||
response.Blips = radar
|
||||
response.Success = true
|
||||
}
|
||||
|
||||
|
|
|
@ -140,7 +140,22 @@ func TestHandleRadar(t *testing.T) {
|
|||
assert.NoError(t, err, "Error registering account")
|
||||
|
||||
// Spawn the rover rover for the account
|
||||
_, _, err = s.SpawnRoverForAccount(a.Id)
|
||||
_, id, err := s.SpawnRoverForAccount(a.Id)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Warp this rover to 0
|
||||
assert.NoError(t, s.world.WarpRover(id, game.Vector{}))
|
||||
|
||||
// Spawn another rover
|
||||
id, err = s.world.SpawnRover()
|
||||
assert.NoError(t, err)
|
||||
// Warp this rover to just above the other one
|
||||
roverPos := game.Vector{X: 0, Y: 1}
|
||||
assert.NoError(t, s.world.WarpRover(id, roverPos))
|
||||
|
||||
// Set a tile to wall below this rover
|
||||
wallPos := game.Vector{X: 0, Y: -1}
|
||||
assert.NoError(t, s.world.Atlas.SetTile(wallPos, game.TileWall))
|
||||
|
||||
request, _ := http.NewRequest(http.MethodGet, path.Join("/", a.Id.String(), "/radar"), nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
@ -155,7 +170,18 @@ func TestHandleRadar(t *testing.T) {
|
|||
t.Errorf("got false for /radar: %s", status.Error)
|
||||
}
|
||||
|
||||
// TODO: Verify the radar information
|
||||
foundWall := false
|
||||
foundRover := false
|
||||
for _, b := range status.Blips {
|
||||
if b.Position == wallPos && b.Tile == game.TileWall {
|
||||
foundWall = true
|
||||
} else if b.Position == roverPos && b.Tile == game.TileRover {
|
||||
foundRover = true
|
||||
}
|
||||
}
|
||||
assert.True(t, foundWall)
|
||||
assert.True(t, foundRover)
|
||||
|
||||
}
|
||||
|
||||
func TestHandleRover(t *testing.T) {
|
||||
|
@ -164,8 +190,9 @@ func TestHandleRover(t *testing.T) {
|
|||
a, err := s.accountant.RegisterAccount("test")
|
||||
assert.NoError(t, err, "Error registering account")
|
||||
|
||||
// Spawn the rover rover for the account
|
||||
_, _, err = s.SpawnRoverForAccount(a.Id)
|
||||
// Spawn one rover for the account
|
||||
attribs, _, err := s.SpawnRoverForAccount(a.Id)
|
||||
assert.NoError(t, err)
|
||||
|
||||
request, _ := http.NewRequest(http.MethodGet, path.Join("/", a.Id.String(), "/rover"), nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
@ -178,7 +205,7 @@ func TestHandleRover(t *testing.T) {
|
|||
|
||||
if status.Success != true {
|
||||
t.Errorf("got false for /rover: %s", status.Error)
|
||||
} else if attribs != status.Attributes {
|
||||
t.Errorf("Missmatched attributes: %+v, !=%+v", attribs, status.Attributes)
|
||||
}
|
||||
|
||||
// TODO: Verify the rover information
|
||||
}
|
||||
|
|
|
@ -269,7 +269,7 @@ func (s *Server) wrapHandler(method string, handler Handler) func(w http.Respons
|
|||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
} else if err := json.NewEncoder(w).Encode(val); err != nil {
|
||||
fmt.Printf("Failed to encode return to json: %s", err)
|
||||
fmt.Printf("Failed to encode reply to json: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
} else {
|
||||
|
@ -280,9 +280,11 @@ func (s *Server) wrapHandler(method string, handler Handler) func(w http.Respons
|
|||
|
||||
// SpawnRoverForAccount spawns the rover rover for an account
|
||||
func (s *Server) SpawnRoverForAccount(accountid uuid.UUID) (game.RoverAttributes, uuid.UUID, error) {
|
||||
inst := s.world.SpawnRover()
|
||||
if attribs, err := s.world.RoverAttributes(inst); err != nil {
|
||||
return game.RoverAttributes{}, uuid.UUID{}, fmt.Errorf("No attributes found for created rover")
|
||||
if inst, err := s.world.SpawnRover(); err != nil {
|
||||
return game.RoverAttributes{}, uuid.UUID{}, err
|
||||
|
||||
} else if attribs, err := s.world.RoverAttributes(inst); err != nil {
|
||||
return game.RoverAttributes{}, uuid.UUID{}, fmt.Errorf("No attributes found for created rover: %s", err)
|
||||
|
||||
} else {
|
||||
if err := s.accountant.AssignRover(accountid, inst); err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue