Refactor rover attributes to add a name

This commit is contained in:
Marc Di Luzio 2020-06-06 16:52:30 +01:00
parent db38ad6091
commit 5e1f9b0d31
10 changed files with 43 additions and 72 deletions

View file

@ -126,14 +126,14 @@ func HandleSpawn(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
} else if id, err := uuid.Parse(id); err != nil {
response.Error = "Provided account ID was invalid"
} else if pos, rover, err := s.SpawnRoverForAccount(id); err != nil {
} else if attribs, rover, err := s.SpawnRoverForAccount(id); err != nil {
response.Error = err.Error()
} else {
fmt.Printf("New rover spawned\taccount:%s\trover:%s\tpos:%+v\n", id, rover, pos)
fmt.Printf("New rover spawned\taccount:%s\trover:%s\attributes:%+v\n", id, rover, attribs)
response.Success = true
response.Position = pos
response.Attributes = attribs
}
return response, nil
@ -217,12 +217,12 @@ func HandleRover(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
} else if inst, err := s.accountant.GetRover(id); err != nil {
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
} else if pos, err := s.world.RoverPosition(inst); err != nil {
} else if attribs, err := s.world.RoverAttributes(inst); err != nil {
response.Error = fmt.Sprintf("Error getting radar from rover: %s", err)
} else {
fmt.Printf("Responded with rover\taccount:%s\trover:%+v\n", id, pos)
response.Position = pos
fmt.Printf("Responded with rover\taccount:%s\trover:%+v\n", id, attribs)
response.Attributes = attribs
response.Success = true
}

View file

@ -92,7 +92,7 @@ func TestHandleCommand(t *testing.T) {
// Spawn the rover rover for the account
_, inst, err := s.SpawnRoverForAccount(a.Id)
pos, err := s.world.RoverPosition(inst)
attribs, err := s.world.RoverAttributes(inst)
assert.NoError(t, err, "Couldn't get rover position")
data := rove.CommandData{
@ -127,10 +127,10 @@ func TestHandleCommand(t *testing.T) {
// Tick the command queues to progress the move command
s.world.ExecuteCommandQueues()
pos2, err := s.world.RoverPosition(inst)
attribs2, err := s.world.RoverAttributes(inst)
assert.NoError(t, err, "Couldn't get rover position")
pos.Add(game.Vector{X: 0.0, Y: attrib.Speed * 1}) // Should have moved north by the speed and duration
assert.Equal(t, pos, pos2, "Rover should have moved by bearing")
attribs.Pos.Add(game.Vector{X: 0.0, Y: attrib.Speed * 1}) // Should have moved north by the speed and duration
assert.Equal(t, attribs.Pos, attribs2.Pos, "Rover should have moved by bearing")
}
func TestHandleRadar(t *testing.T) {

View file

@ -258,10 +258,10 @@ 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.Vector, uuid.UUID, error) {
func (s *Server) SpawnRoverForAccount(accountid uuid.UUID) (game.RoverAttributes, uuid.UUID, error) {
inst := s.world.SpawnRover()
if pos, err := s.world.RoverPosition(inst); err != nil {
return game.Vector{}, uuid.UUID{}, fmt.Errorf("No position found for created rover")
if attribs, err := s.world.RoverAttributes(inst); err != nil {
return game.RoverAttributes{}, uuid.UUID{}, fmt.Errorf("No attributes found for created rover")
} else {
if err := s.accountant.AssignRover(accountid, inst); err != nil {
@ -270,9 +270,9 @@ func (s *Server) SpawnRoverForAccount(accountid uuid.UUID) (game.Vector, uuid.UU
fmt.Printf("Failed to destroy rover after failed rover assign: %s", err)
}
return game.Vector{}, uuid.UUID{}, err
return game.RoverAttributes{}, uuid.UUID{}, err
} else {
return pos, inst, nil
return attribs, inst, nil
}
}
}