Remove /spawn POST endpoint
This was increasing complexity for no added benefit /register now performs the spawn in 4 lines of code
This commit is contained in:
parent
6fb7ee598d
commit
b3b369f608
6 changed files with 4 additions and 147 deletions
|
@ -65,21 +65,6 @@ func TestServer_Register(t *testing.T) {
|
|||
assert.False(t, r3.Success)
|
||||
}
|
||||
|
||||
func TestServer_Spawn(t *testing.T) {
|
||||
d1 := rove.RegisterData{
|
||||
Name: uuid.New().String(),
|
||||
}
|
||||
r1, err := serv.Register(d1)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, r1.Success)
|
||||
assert.NotZero(t, len(r1.Id))
|
||||
|
||||
s := rove.SpawnData{}
|
||||
r2, err := serv.Spawn(r1.Id, s)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, r2.Success)
|
||||
}
|
||||
|
||||
func TestServer_Command(t *testing.T) {
|
||||
d1 := rove.RegisterData{
|
||||
Name: uuid.New().String(),
|
||||
|
@ -89,11 +74,6 @@ func TestServer_Command(t *testing.T) {
|
|||
assert.True(t, r1.Success)
|
||||
assert.NotZero(t, len(r1.Id))
|
||||
|
||||
s := rove.SpawnData{}
|
||||
r2, err := serv.Spawn(r1.Id, s)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, r2.Success)
|
||||
|
||||
c := rove.CommandData{
|
||||
Commands: []game.Command{
|
||||
{
|
||||
|
@ -117,11 +97,6 @@ func TestServer_Radar(t *testing.T) {
|
|||
assert.True(t, r1.Success)
|
||||
assert.NotZero(t, len(r1.Id))
|
||||
|
||||
s := rove.SpawnData{}
|
||||
r2, err := serv.Spawn(r1.Id, s)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, r2.Success)
|
||||
|
||||
r3, err := serv.Radar(r1.Id)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, r3.Success)
|
||||
|
@ -136,11 +111,6 @@ func TestServer_Rover(t *testing.T) {
|
|||
assert.True(t, r1.Success)
|
||||
assert.NotZero(t, len(r1.Id))
|
||||
|
||||
s := rove.SpawnData{}
|
||||
r2, err := serv.Spawn(r1.Id, s)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, r2.Success)
|
||||
|
||||
r3, err := serv.Rover(r1.Id)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, r3.Success)
|
||||
|
|
|
@ -33,11 +33,6 @@ var Routes = []Route{
|
|||
method: http.MethodPost,
|
||||
handler: HandleRegister,
|
||||
},
|
||||
{
|
||||
path: "/{account}/spawn",
|
||||
method: http.MethodPost,
|
||||
handler: HandleSpawn,
|
||||
},
|
||||
{
|
||||
path: "/{account}/command",
|
||||
method: http.MethodPost,
|
||||
|
@ -92,8 +87,11 @@ func HandleRegister(s *Server, vars map[string]string, b io.ReadCloser, w io.Wri
|
|||
} else if acc, err := s.accountant.RegisterAccount(data.Name); err != nil {
|
||||
response.Error = err.Error()
|
||||
|
||||
} else if _, _, err := s.SpawnRoverForAccount(acc.Id); err != nil {
|
||||
response.Error = err.Error()
|
||||
|
||||
} else if err := s.SaveAll(); err != nil {
|
||||
response.Error = fmt.Sprintf("Internal server error when saving accounts: %s", err)
|
||||
response.Error = fmt.Sprintf("Internal server error when saving: %s", err)
|
||||
|
||||
} else {
|
||||
// Save out the new accounts
|
||||
|
@ -105,41 +103,6 @@ func HandleRegister(s *Server, vars map[string]string, b io.ReadCloser, w io.Wri
|
|||
return response, nil
|
||||
}
|
||||
|
||||
// HandleSpawn will spawn the player entity for the associated account
|
||||
func HandleSpawn(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
||||
var response = rove.SpawnResponse{
|
||||
Success: false,
|
||||
}
|
||||
|
||||
id := vars["account"]
|
||||
|
||||
// Decode the spawn info, verify it and spawn the rover for this account
|
||||
var data rove.SpawnData
|
||||
if err := json.NewDecoder(b).Decode(&data); err != nil {
|
||||
fmt.Printf("Failed to decode json: %s\n", err)
|
||||
response.Error = err.Error()
|
||||
|
||||
} else if len(id) == 0 {
|
||||
response.Error = "No account ID provided"
|
||||
|
||||
} else if id, err := uuid.Parse(id); err != nil {
|
||||
response.Error = "Provided account ID was invalid"
|
||||
|
||||
} else if attribs, _, err := s.SpawnRoverForAccount(id); err != nil {
|
||||
response.Error = err.Error()
|
||||
|
||||
} else if err := s.SaveWorld(); err != nil {
|
||||
response.Error = fmt.Sprintf("Internal server error when saving world: %s", err)
|
||||
|
||||
} else {
|
||||
response.Success = true
|
||||
response.Attributes = attribs
|
||||
}
|
||||
|
||||
fmt.Printf("spawn response \taccount:%s\tresponse:%+v\n", id, response)
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// HandleSpawn will spawn the player entity for the associated account
|
||||
func HandleCommand(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
||||
var response = rove.CommandResponse{
|
||||
|
|
|
@ -59,31 +59,6 @@ func TestHandleRegister(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestHandleSpawn(t *testing.T) {
|
||||
s := NewServer()
|
||||
s.Initialise(true)
|
||||
a, err := s.accountant.RegisterAccount("test")
|
||||
assert.NoError(t, err, "Error registering account")
|
||||
data := rove.SpawnData{}
|
||||
|
||||
b, err := json.Marshal(data)
|
||||
assert.NoError(t, err, "Error marshalling data")
|
||||
|
||||
request, _ := http.NewRequest(http.MethodPost, path.Join("/", a.Id.String(), "/spawn"), bytes.NewReader(b))
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
s.router.ServeHTTP(response, request)
|
||||
assert.Equal(t, http.StatusOK, response.Code)
|
||||
|
||||
var status rove.SpawnResponse
|
||||
json.NewDecoder(response.Body).Decode(&status)
|
||||
assert.Equal(t, http.StatusOK, response.Code)
|
||||
|
||||
if status.Success != true {
|
||||
t.Errorf("got false for /spawn: %s", status.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleCommand(t *testing.T) {
|
||||
s := NewServer()
|
||||
s.Initialise(false) // Leave the world empty with no obstacles
|
||||
|
|
|
@ -21,7 +21,6 @@ func Usage() {
|
|||
fmt.Fprintln(os.Stderr, "\nCommands:")
|
||||
fmt.Fprintln(os.Stderr, "\tstatus \tprints the server status")
|
||||
fmt.Fprintln(os.Stderr, "\tregister\tregisters an account and stores it (use with -name)")
|
||||
fmt.Fprintln(os.Stderr, "\tspawn \tspawns a rover for the current account")
|
||||
fmt.Fprintln(os.Stderr, "\tmove \tissues move command to rover")
|
||||
fmt.Fprintln(os.Stderr, "\tradar \tgathers radar data for the current rover")
|
||||
fmt.Fprintln(os.Stderr, "\trover \tgets data for current rover")
|
||||
|
@ -129,23 +128,6 @@ func InnerMain(command string) error {
|
|||
fmt.Printf("Registered account with id: %s\n", response.Id)
|
||||
config.Accounts[config.Host] = response.Id
|
||||
}
|
||||
case "spawn":
|
||||
d := rove.SpawnData{}
|
||||
if err := verifyId(account); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
response, err := server.Spawn(account, d)
|
||||
switch {
|
||||
case err != nil:
|
||||
return err
|
||||
|
||||
case !response.Success:
|
||||
return fmt.Errorf("Server returned failure: %s", response.Error)
|
||||
|
||||
default:
|
||||
fmt.Printf("Spawned rover with attributes %+v\n", response.Attributes)
|
||||
}
|
||||
|
||||
case "move":
|
||||
d := rove.CommandData{
|
||||
|
|
|
@ -31,7 +31,6 @@ func Test_InnerMain(t *testing.T) {
|
|||
assert.Error(t, InnerMain("register"))
|
||||
|
||||
// These methods should fail without an account
|
||||
assert.Error(t, InnerMain("spawn"))
|
||||
assert.Error(t, InnerMain("move"))
|
||||
assert.Error(t, InnerMain("radar"))
|
||||
assert.Error(t, InnerMain("rover"))
|
||||
|
@ -42,14 +41,6 @@ func Test_InnerMain(t *testing.T) {
|
|||
// Perform the register
|
||||
assert.NoError(t, InnerMain("register"))
|
||||
|
||||
// We've not spawned a rover yet so these should fail
|
||||
assert.Error(t, InnerMain("move"))
|
||||
assert.Error(t, InnerMain("radar"))
|
||||
assert.Error(t, InnerMain("rover"))
|
||||
|
||||
// Spawn a rover
|
||||
assert.NoError(t, InnerMain("spawn"))
|
||||
|
||||
// These should now work
|
||||
assert.NoError(t, InnerMain("radar"))
|
||||
assert.NoError(t, InnerMain("rover"))
|
||||
|
|
|
@ -46,30 +46,6 @@ type RegisterResponse struct {
|
|||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
// ==============================
|
||||
// API: /{account}/spawn method: POST
|
||||
|
||||
// Spawn spawns the rover for an account
|
||||
// Responds with the position of said rover
|
||||
func (s Server) Spawn(account string, d SpawnData) (r SpawnResponse, err error) {
|
||||
err = s.Post(path.Join(account, "spawn"), d, &r)
|
||||
return
|
||||
}
|
||||
|
||||
// SpawnData is the data to be sent for the spawn command
|
||||
type SpawnData struct {
|
||||
// Empty for now, reserved for data
|
||||
}
|
||||
|
||||
// SpawnResponse is the data to respond with on a spawn command
|
||||
type SpawnResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Error string `json:"error,omitempty"`
|
||||
|
||||
// The attributes of the spawned entity
|
||||
Attributes game.RoverAttributes `json:"attributes"`
|
||||
}
|
||||
|
||||
// ==============================
|
||||
// API: /{account}/command method: POST
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue