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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue