From 4126da61cc580bfc476b326c65a5a98354425752 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Thu, 4 Jun 2020 18:36:26 +0100 Subject: [PATCH] Simplify API calls and pull them out to their own file --- pkg/server/api.go | 88 +++++++++++++++++++++++++++++++++++++++ pkg/server/routes.go | 86 ++------------------------------------ pkg/server/routes_test.go | 6 +-- 3 files changed, 95 insertions(+), 85 deletions(-) create mode 100644 pkg/server/api.go diff --git a/pkg/server/api.go b/pkg/server/api.go new file mode 100644 index 0000000..2b6c704 --- /dev/null +++ b/pkg/server/api.go @@ -0,0 +1,88 @@ +package server + +import "github.com/mdiluz/rove/pkg/game" + +// ================ +// API: /status + +// StatusResponse is a struct that contains information on the status of the server +type StatusResponse struct { + Ready bool `json:"ready"` + Version string `json:"version"` +} + +// ================ +// API: /register + +// RegisterData describes the data to send when registering +type RegisterData struct { + Name string `json:"name"` +} + +// RegisterResponse describes the response to a register request +type RegisterResponse struct { + Success bool `json:"success"` + Error string `json:"error"` + + Id string `json:"id"` +} + +// ================ +// API: /spawn + +// SpawnData is the data to be sent for the spawn command +type SpawnData struct { + Id string `json:"id"` +} + +// SpawnResponse is the data to respond with on a spawn command +type SpawnResponse struct { + Success bool `json:"success"` + Error string `json:"error"` + + Position game.Vector `json:"position"` +} + +// ================ +// API: /commands + +// CommandsData is a set of commands to execute in order +type CommandsData struct { + Id string `json:"id"` + Commands []Command `json:"commands"` +} + +// CommandsResponse is the response to be sent back +type CommandsResponse struct { + Success bool `json:"success"` + Error string `json:"error"` +} + +const ( + // CommandMove describes a single move command + CommandMove = "move" +) + +// Command describes a single command to execute +// it contains the type, and then any members used for each command type +type Command struct { + // Command is the main command string + Command string `json:"command"` + + // Used for CommandMove + Vector game.Vector `json:"vector"` +} + +// ================ +// API: /view + +// ViewData describes the input data to request an accounts current view +type ViewData struct { + Id string `json:"id"` +} + +// ViewResponse describes the response to a /view call +type ViewResponse struct { + Success bool `json:"success"` + Error string `json:"error"` +} diff --git a/pkg/server/routes.go b/pkg/server/routes.go index 1e503d1..9a4fa45 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -50,12 +50,6 @@ var Routes = []Route{ }, } -// StatusResponse is a struct that contains information on the status of the server -type StatusResponse struct { - Ready bool `json:"ready"` - Version string `json:"version"` -} - // HandleStatus handles the /status request func HandleStatus(s *Server, b io.ReadCloser, w io.Writer) error { @@ -71,37 +65,12 @@ func HandleStatus(s *Server, b io.ReadCloser, w io.Writer) error { return nil } -// BasicResponse describes the minimum dataset for a response -type BasicResponse struct { - Success bool `json:"success"` - Error string `json:"error"` -} - -// BasicAccountData describes the data to be sent for an account specific post -type BasicAccountData struct { - Id string `json:"id"` -} - -// RegisterData describes the data to send when registering -type RegisterData struct { - Name string `json:"name"` -} - -// RegisterResponse describes the response to a register request -type RegisterResponse struct { - BasicResponse - - Id string `json:"id"` -} - // HandleRegister handles /register endpoint func HandleRegister(s *Server, b io.ReadCloser, w io.Writer) error { // Set up the response var response = RegisterResponse{ - BasicResponse: BasicResponse{ - Success: false, - }, + Success: false, } // Pull out the registration info @@ -138,25 +107,11 @@ func HandleRegister(s *Server, b io.ReadCloser, w io.Writer) error { return nil } -// SpawnData is the data to be sent for the spawn command -type SpawnData struct { - BasicAccountData -} - -// SpawnResponse is the data to respond with on a spawn command -type SpawnResponse struct { - BasicResponse - - Position game.Vector `json:"position"` -} - // HandleSpawn will spawn the player entity for the associated account func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) error { // Set up the response var response = SpawnResponse{ - BasicResponse: BasicResponse{ - Success: false, - }, + Success: false, } // Pull out the incoming info @@ -193,31 +148,10 @@ func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) error { return nil } -const ( - // CommandMove describes a single move command - CommandMove = "move" -) - -// Command describes a single command to execute -// it contains the type, and then any members used for each command type -type Command struct { - // Command is the main command string - Command string `json:"command"` - - // Used for CommandMove - Vector game.Vector `json:"vector"` -} - -// CommandsData is a set of commands to execute in order -type CommandsData struct { - BasicAccountData - Commands []Command `json:"commands"` -} - // HandleSpawn will spawn the player entity for the associated account func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) error { // Set up the response - var response = BasicResponse{ + var response = CommandsResponse{ Success: false, } @@ -266,23 +200,11 @@ func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) error { return nil } -// ViewData describes the input data to request an accounts current view -type ViewData struct { - BasicAccountData -} - -// ViewResponse describes the response to a /view call -type ViewResponse struct { - BasicResponse -} - // HandleView handles the view request func HandleView(s *Server, b io.ReadCloser, w io.Writer) error { // Set up the response var response = ViewResponse{ - BasicResponse: BasicResponse{ - Success: false, - }, + Success: false, } // Pull out the incoming info diff --git a/pkg/server/routes_test.go b/pkg/server/routes_test.go index 3bffa97..cd47bb6 100644 --- a/pkg/server/routes_test.go +++ b/pkg/server/routes_test.go @@ -55,7 +55,7 @@ func TestHandleSpawn(t *testing.T) { s := NewServer() a, err := s.accountant.RegisterAccount("test") assert.NoError(t, err, "Error registering account") - data := SpawnData{BasicAccountData: BasicAccountData{Id: a.Id.String()}} + data := SpawnData{Id: a.Id.String()} b, err := json.Marshal(data) assert.NoError(t, err, "Error marshalling data") @@ -84,7 +84,7 @@ func TestHandleCommands(t *testing.T) { move := game.Vector{X: 1, Y: 2, Z: 3} data := CommandsData{ - BasicAccountData: BasicAccountData{Id: a.Id.String()}, + Id: a.Id.String(), Commands: []Command{ { Command: CommandMove, @@ -101,7 +101,7 @@ func TestHandleCommands(t *testing.T) { s.wrapHandler(http.MethodPost, HandleCommands)(response, request) - var status BasicResponse + var status CommandsResponse json.NewDecoder(response.Body).Decode(&status) if status.Success != true {