Simplify API calls and pull them out to their own file
This commit is contained in:
parent
79914ba728
commit
4126da61cc
3 changed files with 95 additions and 85 deletions
88
pkg/server/api.go
Normal file
88
pkg/server/api.go
Normal file
|
@ -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"`
|
||||||
|
}
|
|
@ -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
|
// HandleStatus handles the /status request
|
||||||
func HandleStatus(s *Server, b io.ReadCloser, w io.Writer) error {
|
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
|
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
|
// HandleRegister handles /register endpoint
|
||||||
func HandleRegister(s *Server, b io.ReadCloser, w io.Writer) error {
|
func HandleRegister(s *Server, b io.ReadCloser, w io.Writer) error {
|
||||||
|
|
||||||
// Set up the response
|
// Set up the response
|
||||||
var response = RegisterResponse{
|
var response = RegisterResponse{
|
||||||
BasicResponse: BasicResponse{
|
Success: false,
|
||||||
Success: false,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull out the registration info
|
// Pull out the registration info
|
||||||
|
@ -138,25 +107,11 @@ func HandleRegister(s *Server, b io.ReadCloser, w io.Writer) error {
|
||||||
return nil
|
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
|
// HandleSpawn will spawn the player entity for the associated account
|
||||||
func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) error {
|
func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) error {
|
||||||
// Set up the response
|
// Set up the response
|
||||||
var response = SpawnResponse{
|
var response = SpawnResponse{
|
||||||
BasicResponse: BasicResponse{
|
Success: false,
|
||||||
Success: false,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull out the incoming info
|
// Pull out the incoming info
|
||||||
|
@ -193,31 +148,10 @@ func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) error {
|
||||||
return nil
|
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
|
// HandleSpawn will spawn the player entity for the associated account
|
||||||
func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) error {
|
func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) error {
|
||||||
// Set up the response
|
// Set up the response
|
||||||
var response = BasicResponse{
|
var response = CommandsResponse{
|
||||||
Success: false,
|
Success: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,23 +200,11 @@ func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) error {
|
||||||
return nil
|
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
|
// HandleView handles the view request
|
||||||
func HandleView(s *Server, b io.ReadCloser, w io.Writer) error {
|
func HandleView(s *Server, b io.ReadCloser, w io.Writer) error {
|
||||||
// Set up the response
|
// Set up the response
|
||||||
var response = ViewResponse{
|
var response = ViewResponse{
|
||||||
BasicResponse: BasicResponse{
|
Success: false,
|
||||||
Success: false,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull out the incoming info
|
// Pull out the incoming info
|
||||||
|
|
|
@ -55,7 +55,7 @@ func TestHandleSpawn(t *testing.T) {
|
||||||
s := NewServer()
|
s := NewServer()
|
||||||
a, err := s.accountant.RegisterAccount("test")
|
a, err := s.accountant.RegisterAccount("test")
|
||||||
assert.NoError(t, err, "Error registering account")
|
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)
|
b, err := json.Marshal(data)
|
||||||
assert.NoError(t, err, "Error marshalling 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}
|
move := game.Vector{X: 1, Y: 2, Z: 3}
|
||||||
|
|
||||||
data := CommandsData{
|
data := CommandsData{
|
||||||
BasicAccountData: BasicAccountData{Id: a.Id.String()},
|
Id: a.Id.String(),
|
||||||
Commands: []Command{
|
Commands: []Command{
|
||||||
{
|
{
|
||||||
Command: CommandMove,
|
Command: CommandMove,
|
||||||
|
@ -101,7 +101,7 @@ func TestHandleCommands(t *testing.T) {
|
||||||
|
|
||||||
s.wrapHandler(http.MethodPost, HandleCommands)(response, request)
|
s.wrapHandler(http.MethodPost, HandleCommands)(response, request)
|
||||||
|
|
||||||
var status BasicResponse
|
var status CommandsResponse
|
||||||
json.NewDecoder(response.Body).Decode(&status)
|
json.NewDecoder(response.Body).Decode(&status)
|
||||||
|
|
||||||
if status.Success != true {
|
if status.Success != true {
|
||||||
|
|
Loading…
Add table
Reference in a new issue