Huge Instance -> Rover refactor, for clarification

This commit is contained in:
Marc Di Luzio 2020-06-04 21:17:43 +01:00
parent 33f25a7414
commit 0fbad15c01
10 changed files with 96 additions and 96 deletions

View file

@ -30,8 +30,8 @@ type RegisterResponse struct {
// ==============================
// API: /spawn method: POST
// Spawns the primary entity for an account
// Responds with the position of said entity
// Spawns the rover for an account
// Responds with the position of said rover
// SpawnData is the data to be sent for the spawn command
type SpawnData struct {

View file

@ -130,8 +130,8 @@ func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) error {
// log the data sent
fmt.Printf("\tspawn data: %v\n", data)
// Create a new instance
if pos, _, err := s.SpawnPrimaryForAccount(id); err != nil {
// Create a new rover
if pos, _, err := s.SpawnRoverForAccount(id); err != nil {
response.Error = err.Error()
} else {
response.Success = true
@ -168,8 +168,8 @@ func HandleCommands(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 if inst, err := s.accountant.GetPrimary(id); err != nil {
response.Error = fmt.Sprintf("Provided account has no primary: %s", err)
} else if inst, err := s.accountant.GetRover(id); err != nil {
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
} else {
// log the data sent

View file

@ -77,8 +77,8 @@ func TestHandleCommands(t *testing.T) {
a, err := s.accountant.RegisterAccount("test")
assert.NoError(t, err, "Error registering account")
// Spawn the primary instance for the account
_, inst, err := s.SpawnPrimaryForAccount(a.Id)
// Spawn the rover rover for the account
_, inst, err := s.SpawnRoverForAccount(a.Id)
data := CommandsData{
Id: a.Id.String(),
@ -107,7 +107,7 @@ func TestHandleCommands(t *testing.T) {
}
if _, err := s.world.GetPosition(inst); err != nil {
t.Error("Couldn't get position for the primary instance")
t.Error("Couldn't get position for the rover rover")
}
// TODO: Check position is correct

View file

@ -161,18 +161,18 @@ func (s *Server) wrapHandler(method string, handler Handler) func(w http.Respons
}
}
// SpawnPrimaryForAccount spawns the primary instance for an account
func (s *Server) SpawnPrimaryForAccount(accountid uuid.UUID) (game.Vector, uuid.UUID, error) {
// SpawnRoverForAccount spawns the rover rover for an account
func (s *Server) SpawnRoverForAccount(accountid uuid.UUID) (game.Vector, uuid.UUID, error) {
inst := uuid.New()
s.world.Spawn(inst)
s.world.SpawnRover(inst)
if pos, err := s.world.GetPosition(inst); err != nil {
return game.Vector{}, uuid.UUID{}, fmt.Errorf("No position found for created instance")
return game.Vector{}, uuid.UUID{}, fmt.Errorf("No position found for created rover")
} else {
if err := s.accountant.AssignPrimary(accountid, inst); err != nil {
// Try and clear up the instance
if err := s.world.DestroyInstance(inst); err != nil {
fmt.Printf("Failed to destroy instance after failed primary assign: %s", err)
if err := s.accountant.AssignRover(accountid, inst); err != nil {
// Try and clear up the rover
if err := s.world.DestroyRover(inst); err != nil {
fmt.Printf("Failed to destroy rover after failed rover assign: %s", err)
}
return game.Vector{}, uuid.UUID{}, err