Pull out API definitions and testing into it's own package

This commit is contained in:
Marc Di Luzio 2020-06-05 17:24:27 +01:00
parent 5a2d35aca8
commit 3474e6ca8c
3 changed files with 25 additions and 22 deletions

View file

@ -7,6 +7,7 @@ import (
"net/http"
"github.com/google/uuid"
"github.com/mdiluz/rove/pkg/rove"
"github.com/mdiluz/rove/pkg/version"
)
@ -53,7 +54,7 @@ var Routes = []Route{
func HandleStatus(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
// Simply return the current server status
return StatusResponse{
return rove.StatusResponse{
Ready: true,
Version: version.Version,
}, nil
@ -61,12 +62,12 @@ func HandleStatus(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error)
// HandleRegister handles /register endpoint
func HandleRegister(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
var response = RegisterResponse{
var response = rove.RegisterResponse{
Success: false,
}
// Decode the registration info, verify it and register the account
var data RegisterData
var data rove.RegisterData
err := json.NewDecoder(b).Decode(&data)
if err != nil {
fmt.Printf("Failed to decode json: %s\n", err)
@ -88,12 +89,12 @@ func HandleRegister(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error
// HandleSpawn will spawn the player entity for the associated account
func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
var response = SpawnResponse{
var response = rove.SpawnResponse{
Success: false,
}
// Decode the spawn info, verify it and spawn the rover for this account
var data SpawnData
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()
@ -117,12 +118,12 @@ func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
// HandleSpawn will spawn the player entity for the associated account
func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
var response = CommandsResponse{
var response = rove.CommandsResponse{
Success: false,
}
// Decode the commands, verify them and the account, and execute the commands
var data CommandsData
var data rove.CommandsData
if err := json.NewDecoder(b).Decode(&data); err != nil {
fmt.Printf("Failed to decode json: %s\n", err)
response.Error = err.Error()
@ -151,12 +152,12 @@ func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error
// HandleRadar handles the radar request
func HandleRadar(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
var response = RadarResponse{
var response = rove.RadarResponse{
Success: false,
}
// Decode the radar message, verify it, and respond with the radar info
var data CommandsData
var data rove.CommandsData
if err := json.NewDecoder(b).Decode(&data); err != nil {
fmt.Printf("Failed to decode json: %s\n", err)
response.Error = err.Error()

View file

@ -8,6 +8,7 @@ import (
"testing"
"github.com/mdiluz/rove/pkg/game"
"github.com/mdiluz/rove/pkg/rove"
"github.com/stretchr/testify/assert"
)
@ -18,7 +19,7 @@ func TestHandleStatus(t *testing.T) {
s := NewServer()
s.wrapHandler(http.MethodGet, HandleStatus)(response, request)
var status StatusResponse
var status rove.StatusResponse
json.NewDecoder(response.Body).Decode(&status)
if status.Ready != true {
@ -31,7 +32,7 @@ func TestHandleStatus(t *testing.T) {
}
func TestHandleRegister(t *testing.T) {
data := RegisterData{Name: "one"}
data := rove.RegisterData{Name: "one"}
b, err := json.Marshal(data)
if err != nil {
t.Error(err)
@ -43,7 +44,7 @@ func TestHandleRegister(t *testing.T) {
s := NewServer()
s.wrapHandler(http.MethodPost, HandleRegister)(response, request)
var status RegisterResponse
var status rove.RegisterResponse
json.NewDecoder(response.Body).Decode(&status)
if status.Success != true {
@ -55,7 +56,7 @@ func TestHandleSpawn(t *testing.T) {
s := NewServer()
a, err := s.accountant.RegisterAccount("test")
assert.NoError(t, err, "Error registering account")
data := SpawnData{Id: a.Id.String()}
data := rove.SpawnData{Id: a.Id.String()}
b, err := json.Marshal(data)
assert.NoError(t, err, "Error marshalling data")
@ -65,7 +66,7 @@ func TestHandleSpawn(t *testing.T) {
s.wrapHandler(http.MethodPost, HandleSpawn)(response, request)
var status SpawnResponse
var status rove.SpawnResponse
json.NewDecoder(response.Body).Decode(&status)
if status.Success != true {
@ -84,11 +85,11 @@ func TestHandleCommands(t *testing.T) {
pos, err := s.world.RoverPosition(inst)
assert.NoError(t, err, "Couldn't get rover position")
data := CommandsData{
data := rove.CommandsData{
Id: a.Id.String(),
Commands: []Command{
Commands: []rove.Command{
{
Command: CommandMove,
Command: rove.CommandMove,
Bearing: "N",
Duration: 1,
},
@ -103,7 +104,7 @@ func TestHandleCommands(t *testing.T) {
s.wrapHandler(http.MethodPost, HandleCommands)(response, request)
var status CommandsResponse
var status rove.CommandsResponse
json.NewDecoder(response.Body).Decode(&status)
if status.Success != true {
@ -127,7 +128,7 @@ func TestHandleRadar(t *testing.T) {
// Spawn the rover rover for the account
_, _, err = s.SpawnRoverForAccount(a.Id)
data := RadarData{
data := rove.RadarData{
Id: a.Id.String(),
}
@ -139,7 +140,7 @@ func TestHandleRadar(t *testing.T) {
s.wrapHandler(http.MethodPost, HandleRadar)(response, request)
var status RadarResponse
var status rove.RadarResponse
json.NewDecoder(response.Body).Decode(&status)
if status.Success != true {

View file

@ -14,6 +14,7 @@ import (
"github.com/mdiluz/rove/pkg/accounts"
"github.com/mdiluz/rove/pkg/game"
"github.com/mdiluz/rove/pkg/persistence"
"github.com/mdiluz/rove/pkg/rove"
)
const (
@ -182,11 +183,11 @@ func (s *Server) SpawnRoverForAccount(accountid uuid.UUID) (game.Vector, uuid.UU
}
// ConvertCommands converts server commands to game commands
func (s *Server) ConvertCommands(commands []Command, inst uuid.UUID) ([]game.Command, error) {
func (s *Server) ConvertCommands(commands []rove.Command, inst uuid.UUID) ([]game.Command, error) {
var cmds []game.Command
for _, c := range commands {
switch c.Command {
case CommandMove:
case rove.CommandMove:
if bearing, err := game.DirectionFromString(c.Bearing); err != nil {
return nil, err
} else {