Small command handling fixes
This commit is contained in:
parent
be0f4f1aff
commit
bd83621add
1 changed files with 18 additions and 28 deletions
|
@ -78,24 +78,21 @@ func HandleRegister(s *Server, b io.ReadCloser, w io.Writer) error {
|
||||||
err := json.NewDecoder(b).Decode(&data)
|
err := json.NewDecoder(b).Decode(&data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to decode json: %s\n", err)
|
fmt.Printf("Failed to decode json: %s\n", err)
|
||||||
|
|
||||||
response.Error = err.Error()
|
response.Error = err.Error()
|
||||||
|
|
||||||
} else if len(data.Name) == 0 {
|
} else if len(data.Name) == 0 {
|
||||||
response.Error = "Cannot register empty name"
|
response.Error = "Cannot register empty name"
|
||||||
|
|
||||||
|
} else if acc, err := s.accountant.RegisterAccount(data.Name); err != nil {
|
||||||
|
response.Error = err.Error()
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// log the data sent
|
// log the data sent
|
||||||
fmt.Printf("\tdata: %+v\n", data)
|
fmt.Printf("\tdata: %+v\n", data)
|
||||||
|
|
||||||
// Register the account with the server
|
|
||||||
acc, err := s.accountant.RegisterAccount(data.Name)
|
|
||||||
|
|
||||||
// If we didn't fail, respond with the account ID string
|
// If we didn't fail, respond with the account ID string
|
||||||
if err == nil {
|
|
||||||
response.Success = true
|
|
||||||
response.Id = acc.Id.String()
|
response.Id = acc.Id.String()
|
||||||
} else {
|
response.Success = true
|
||||||
response.Error = err.Error()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log the response
|
// Log the response
|
||||||
|
@ -126,17 +123,15 @@ func HandleSpawn(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 = "Provided account ID was invalid"
|
response.Error = "Provided account ID was invalid"
|
||||||
|
|
||||||
|
} else if pos, _, err := s.SpawnRoverForAccount(id); err != nil {
|
||||||
|
response.Error = err.Error()
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// log the data sent
|
|
||||||
fmt.Printf("\tspawn data: %v\n", data)
|
|
||||||
|
|
||||||
// Create a new rover
|
// Create a new rover
|
||||||
if pos, _, err := s.SpawnRoverForAccount(id); err != nil {
|
|
||||||
response.Error = err.Error()
|
|
||||||
} else {
|
|
||||||
response.Success = true
|
response.Success = true
|
||||||
response.Position = pos
|
response.Position = pos
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log the response
|
// Log the response
|
||||||
|
@ -171,7 +166,7 @@ func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) error {
|
||||||
Success: false,
|
Success: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull out the incoming info
|
// Go through each possible validation step for the data
|
||||||
var data CommandsData
|
var data CommandsData
|
||||||
if err := json.NewDecoder(b).Decode(&data); err != nil {
|
if err := json.NewDecoder(b).Decode(&data); err != nil {
|
||||||
fmt.Printf("Failed to decode json: %s\n", err)
|
fmt.Printf("Failed to decode json: %s\n", err)
|
||||||
|
@ -189,17 +184,12 @@ func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) error {
|
||||||
} else if cmds, err := s.ConvertCommands(data.Commands, inst); err != nil {
|
} else if cmds, err := s.ConvertCommands(data.Commands, inst); err != nil {
|
||||||
response.Error = fmt.Sprintf("Couldn't convert commands: %s", err)
|
response.Error = fmt.Sprintf("Couldn't convert commands: %s", err)
|
||||||
|
|
||||||
} else {
|
} else if err := s.world.Execute(cmds...); err != nil {
|
||||||
// log the data sent
|
|
||||||
fmt.Printf("\tcommands data: %v\n", data)
|
|
||||||
|
|
||||||
// Execute the commands
|
|
||||||
if err := s.world.Execute(cmds...); err != nil {
|
|
||||||
response.Error = fmt.Sprintf("Failed to execute commands: %s", err)
|
response.Error = fmt.Sprintf("Failed to execute commands: %s", err)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
response.Success = true
|
response.Success = true
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Log the response
|
// Log the response
|
||||||
fmt.Printf("\tresponse: %+v\n", response)
|
fmt.Printf("\tresponse: %+v\n", response)
|
||||||
|
|
Loading…
Add table
Reference in a new issue