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)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to decode json: %s\n", err)
|
||||
|
||||
response.Error = err.Error()
|
||||
|
||||
} else if len(data.Name) == 0 {
|
||||
response.Error = "Cannot register empty name"
|
||||
|
||||
} else if acc, err := s.accountant.RegisterAccount(data.Name); err != nil {
|
||||
response.Error = err.Error()
|
||||
|
||||
} else {
|
||||
// log the data sent
|
||||
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 err == nil {
|
||||
response.Success = true
|
||||
response.Id = acc.Id.String()
|
||||
} else {
|
||||
response.Error = err.Error()
|
||||
}
|
||||
response.Success = true
|
||||
}
|
||||
|
||||
// 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 {
|
||||
response.Error = "Provided account ID was invalid"
|
||||
|
||||
} else if pos, _, err := s.SpawnRoverForAccount(id); err != nil {
|
||||
response.Error = err.Error()
|
||||
|
||||
} else {
|
||||
// log the data sent
|
||||
fmt.Printf("\tspawn data: %v\n", data)
|
||||
|
||||
// Create a new rover
|
||||
if pos, _, err := s.SpawnRoverForAccount(id); err != nil {
|
||||
response.Error = err.Error()
|
||||
} else {
|
||||
response.Success = true
|
||||
response.Position = pos
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Log the response
|
||||
|
@ -171,7 +166,7 @@ func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) error {
|
|||
Success: false,
|
||||
}
|
||||
|
||||
// Pull out the incoming info
|
||||
// Go through each possible validation step for the data
|
||||
var data CommandsData
|
||||
if err := json.NewDecoder(b).Decode(&data); err != nil {
|
||||
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 {
|
||||
response.Error = fmt.Sprintf("Couldn't convert commands: %s", err)
|
||||
|
||||
} else {
|
||||
// log the data sent
|
||||
fmt.Printf("\tcommands data: %v\n", data)
|
||||
|
||||
// Execute the commands
|
||||
if err := s.world.Execute(cmds...); err != nil {
|
||||
} else if err := s.world.Execute(cmds...); err != nil {
|
||||
response.Error = fmt.Sprintf("Failed to execute commands: %s", err)
|
||||
|
||||
} else {
|
||||
response.Success = true
|
||||
}
|
||||
}
|
||||
|
||||
// Log the response
|
||||
fmt.Printf("\tresponse: %+v\n", response)
|
||||
|
|
Loading…
Add table
Reference in a new issue