Pull out API definitions and testing into it's own package
This commit is contained in:
parent
5a2d35aca8
commit
3474e6ca8c
3 changed files with 25 additions and 22 deletions
|
@ -7,6 +7,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/mdiluz/rove/pkg/rove"
|
||||||
"github.com/mdiluz/rove/pkg/version"
|
"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) {
|
func HandleStatus(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
||||||
|
|
||||||
// Simply return the current server status
|
// Simply return the current server status
|
||||||
return StatusResponse{
|
return rove.StatusResponse{
|
||||||
Ready: true,
|
Ready: true,
|
||||||
Version: version.Version,
|
Version: version.Version,
|
||||||
}, nil
|
}, nil
|
||||||
|
@ -61,12 +62,12 @@ func HandleStatus(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error)
|
||||||
|
|
||||||
// HandleRegister handles /register endpoint
|
// HandleRegister handles /register endpoint
|
||||||
func HandleRegister(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
func HandleRegister(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
||||||
var response = RegisterResponse{
|
var response = rove.RegisterResponse{
|
||||||
Success: false,
|
Success: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode the registration info, verify it and register the account
|
// Decode the registration info, verify it and register the account
|
||||||
var data RegisterData
|
var data rove.RegisterData
|
||||||
err := json.NewDecoder(b).Decode(&data)
|
err := json.NewDecoder(b).Decode(&data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to decode json: %s\n", err)
|
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
|
// HandleSpawn will spawn the player entity for the associated account
|
||||||
func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
||||||
var response = SpawnResponse{
|
var response = rove.SpawnResponse{
|
||||||
Success: false,
|
Success: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode the spawn info, verify it and spawn the rover for this account
|
// 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 {
|
if err := json.NewDecoder(b).Decode(&data); err != nil {
|
||||||
fmt.Printf("Failed to decode json: %s\n", err)
|
fmt.Printf("Failed to decode json: %s\n", err)
|
||||||
response.Error = err.Error()
|
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
|
// HandleSpawn will spawn the player entity for the associated account
|
||||||
func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
||||||
var response = CommandsResponse{
|
var response = rove.CommandsResponse{
|
||||||
Success: false,
|
Success: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode the commands, verify them and the account, and execute the commands
|
// 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 {
|
if err := json.NewDecoder(b).Decode(&data); err != nil {
|
||||||
fmt.Printf("Failed to decode json: %s\n", err)
|
fmt.Printf("Failed to decode json: %s\n", err)
|
||||||
response.Error = err.Error()
|
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
|
// HandleRadar handles the radar request
|
||||||
func HandleRadar(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
func HandleRadar(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
||||||
var response = RadarResponse{
|
var response = rove.RadarResponse{
|
||||||
Success: false,
|
Success: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode the radar message, verify it, and respond with the radar info
|
// 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 {
|
if err := json.NewDecoder(b).Decode(&data); err != nil {
|
||||||
fmt.Printf("Failed to decode json: %s\n", err)
|
fmt.Printf("Failed to decode json: %s\n", err)
|
||||||
response.Error = err.Error()
|
response.Error = err.Error()
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/mdiluz/rove/pkg/game"
|
"github.com/mdiluz/rove/pkg/game"
|
||||||
|
"github.com/mdiluz/rove/pkg/rove"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,7 +19,7 @@ func TestHandleStatus(t *testing.T) {
|
||||||
s := NewServer()
|
s := NewServer()
|
||||||
s.wrapHandler(http.MethodGet, HandleStatus)(response, request)
|
s.wrapHandler(http.MethodGet, HandleStatus)(response, request)
|
||||||
|
|
||||||
var status StatusResponse
|
var status rove.StatusResponse
|
||||||
json.NewDecoder(response.Body).Decode(&status)
|
json.NewDecoder(response.Body).Decode(&status)
|
||||||
|
|
||||||
if status.Ready != true {
|
if status.Ready != true {
|
||||||
|
@ -31,7 +32,7 @@ func TestHandleStatus(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleRegister(t *testing.T) {
|
func TestHandleRegister(t *testing.T) {
|
||||||
data := RegisterData{Name: "one"}
|
data := rove.RegisterData{Name: "one"}
|
||||||
b, err := json.Marshal(data)
|
b, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
@ -43,7 +44,7 @@ func TestHandleRegister(t *testing.T) {
|
||||||
s := NewServer()
|
s := NewServer()
|
||||||
s.wrapHandler(http.MethodPost, HandleRegister)(response, request)
|
s.wrapHandler(http.MethodPost, HandleRegister)(response, request)
|
||||||
|
|
||||||
var status RegisterResponse
|
var status rove.RegisterResponse
|
||||||
json.NewDecoder(response.Body).Decode(&status)
|
json.NewDecoder(response.Body).Decode(&status)
|
||||||
|
|
||||||
if status.Success != true {
|
if status.Success != true {
|
||||||
|
@ -55,7 +56,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{Id: a.Id.String()}
|
data := rove.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")
|
||||||
|
@ -65,7 +66,7 @@ func TestHandleSpawn(t *testing.T) {
|
||||||
|
|
||||||
s.wrapHandler(http.MethodPost, HandleSpawn)(response, request)
|
s.wrapHandler(http.MethodPost, HandleSpawn)(response, request)
|
||||||
|
|
||||||
var status SpawnResponse
|
var status rove.SpawnResponse
|
||||||
json.NewDecoder(response.Body).Decode(&status)
|
json.NewDecoder(response.Body).Decode(&status)
|
||||||
|
|
||||||
if status.Success != true {
|
if status.Success != true {
|
||||||
|
@ -84,11 +85,11 @@ func TestHandleCommands(t *testing.T) {
|
||||||
pos, err := s.world.RoverPosition(inst)
|
pos, err := s.world.RoverPosition(inst)
|
||||||
assert.NoError(t, err, "Couldn't get rover position")
|
assert.NoError(t, err, "Couldn't get rover position")
|
||||||
|
|
||||||
data := CommandsData{
|
data := rove.CommandsData{
|
||||||
Id: a.Id.String(),
|
Id: a.Id.String(),
|
||||||
Commands: []Command{
|
Commands: []rove.Command{
|
||||||
{
|
{
|
||||||
Command: CommandMove,
|
Command: rove.CommandMove,
|
||||||
Bearing: "N",
|
Bearing: "N",
|
||||||
Duration: 1,
|
Duration: 1,
|
||||||
},
|
},
|
||||||
|
@ -103,7 +104,7 @@ func TestHandleCommands(t *testing.T) {
|
||||||
|
|
||||||
s.wrapHandler(http.MethodPost, HandleCommands)(response, request)
|
s.wrapHandler(http.MethodPost, HandleCommands)(response, request)
|
||||||
|
|
||||||
var status CommandsResponse
|
var status rove.CommandsResponse
|
||||||
json.NewDecoder(response.Body).Decode(&status)
|
json.NewDecoder(response.Body).Decode(&status)
|
||||||
|
|
||||||
if status.Success != true {
|
if status.Success != true {
|
||||||
|
@ -127,7 +128,7 @@ func TestHandleRadar(t *testing.T) {
|
||||||
// Spawn the rover rover for the account
|
// Spawn the rover rover for the account
|
||||||
_, _, err = s.SpawnRoverForAccount(a.Id)
|
_, _, err = s.SpawnRoverForAccount(a.Id)
|
||||||
|
|
||||||
data := RadarData{
|
data := rove.RadarData{
|
||||||
Id: a.Id.String(),
|
Id: a.Id.String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,7 +140,7 @@ func TestHandleRadar(t *testing.T) {
|
||||||
|
|
||||||
s.wrapHandler(http.MethodPost, HandleRadar)(response, request)
|
s.wrapHandler(http.MethodPost, HandleRadar)(response, request)
|
||||||
|
|
||||||
var status RadarResponse
|
var status rove.RadarResponse
|
||||||
json.NewDecoder(response.Body).Decode(&status)
|
json.NewDecoder(response.Body).Decode(&status)
|
||||||
|
|
||||||
if status.Success != true {
|
if status.Success != true {
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/mdiluz/rove/pkg/accounts"
|
"github.com/mdiluz/rove/pkg/accounts"
|
||||||
"github.com/mdiluz/rove/pkg/game"
|
"github.com/mdiluz/rove/pkg/game"
|
||||||
"github.com/mdiluz/rove/pkg/persistence"
|
"github.com/mdiluz/rove/pkg/persistence"
|
||||||
|
"github.com/mdiluz/rove/pkg/rove"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -182,11 +183,11 @@ func (s *Server) SpawnRoverForAccount(accountid uuid.UUID) (game.Vector, uuid.UU
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConvertCommands converts server commands to game commands
|
// 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
|
var cmds []game.Command
|
||||||
for _, c := range commands {
|
for _, c := range commands {
|
||||||
switch c.Command {
|
switch c.Command {
|
||||||
case CommandMove:
|
case rove.CommandMove:
|
||||||
if bearing, err := game.DirectionFromString(c.Bearing); err != nil {
|
if bearing, err := game.DirectionFromString(c.Bearing); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue