Try and clean up a failed primary instance creation

This commit is contained in:
Marc Di Luzio 2020-06-03 12:31:52 +01:00
parent 78774329a8
commit a0657b32e7
3 changed files with 53 additions and 10 deletions

View file

@ -139,6 +139,9 @@ func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
// Log the response
fmt.Printf("\tresponse: %+v\n", response)
// Reply with the current status
json.NewEncoder(w).Encode(response)
}
@ -157,6 +160,12 @@ type SpawnResponse struct {
// HandleSpawn will spawn the player entity for the associated account
func (s *Server) HandleSpawn(w http.ResponseWriter, r *http.Request) {
// Verify we're hit with a get request
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
fmt.Printf("%s\t%s\n", r.Method, r.RequestURI)
// Set up the response
@ -166,35 +175,38 @@ func (s *Server) HandleSpawn(w http.ResponseWriter, r *http.Request) {
},
}
// Verify we're hit with a get request
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// Pull out the incoming info
var data SpawnData
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
fmt.Printf("Failed to decode json: %s\n", err)
response.Error = err.Error()
} else if len(data.Id) == 0 {
response.Error = "No account ID provided"
} else if id, err := uuid.Parse(data.Id); err != nil {
response.Error = "Provided account ID was invalid"
} else {
// log the data sent
fmt.Printf("\tdata: %v\n", data)
fmt.Printf("\tspawn data: %v\n", data)
// Create a new instance
inst := s.world.CreateInstance()
if pos, err := s.world.GetPosition(inst); err != nil {
response.Error = fmt.Sprint("No position found for created instance")
} else {
if err := s.accountant.AssignPrimary(id, inst); err != nil {
response.Error = err.Error()
// 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)
}
} else {
// Reply with valid data
response.Success = true
response.Position = pos
}
@ -205,6 +217,9 @@ func (s *Server) HandleSpawn(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
// Log the response
fmt.Printf("\tresponse: %+v\n", response)
// Reply with the current status
json.NewEncoder(w).Encode(response)
}