Add the concept of a player and the /register endpoint

This commit is contained in:
Marc Di Luzio 2020-05-31 00:06:14 +01:00
parent 8c4bf4f75f
commit eccb726f74
11 changed files with 129 additions and 36 deletions

View file

@ -19,13 +19,13 @@ func NewConnection(host string) *Connection {
}
}
// ServerStatus is a struct that contains information on the status of the server
type ServerStatus struct {
// StatusResponse is a struct that contains information on the status of the server
type StatusResponse struct {
Ready bool `json:"ready"`
}
// Status returns the current status of the server
func (c *Connection) Status() (status ServerStatus, err error) {
func (c *Connection) Status() (status StatusResponse, err error) {
url := url.URL{
Scheme: "http",
Host: c.host,
@ -33,12 +33,37 @@ func (c *Connection) Status() (status ServerStatus, err error) {
}
if resp, err := http.Get(url.String()); err != nil {
return ServerStatus{}, err
return StatusResponse{}, err
} else if resp.StatusCode != http.StatusOK {
return ServerStatus{}, fmt.Errorf("Status request returned %d", resp.StatusCode)
return StatusResponse{}, fmt.Errorf("Status request returned %d", resp.StatusCode)
} else {
err = json.NewDecoder(resp.Body).Decode(&status)
}
return
}
// RegisterResponse
type RegisterResponse struct {
Id string `json:"id"`
Success bool `json:"success"`
}
// Register registers a new player on the server
func (c *Connection) Register() (register RegisterResponse, err error) {
url := url.URL{
Scheme: "http",
Host: c.host,
Path: "register",
}
if resp, err := http.Get(url.String()); err != nil {
return RegisterResponse{}, err
} else if resp.StatusCode != http.StatusOK {
return RegisterResponse{}, fmt.Errorf("Status request returned %d", resp.StatusCode)
} else {
err = json.NewDecoder(resp.Body).Decode(&register)
}
return
}

29
pkg/rove/rove_test.go Normal file
View file

@ -0,0 +1,29 @@
// +build integration
package rove
import (
"testing"
)
var serverUrl = "localhost:8080"
func TestStatus(t *testing.T) {
conn := NewConnection(serverUrl)
if status, err := conn.Status(); err != nil {
t.Errorf("Status returned error: %s", err)
} else if !status.Ready {
t.Error("Server did not return that it was ready")
}
}
func TestRegister(t *testing.T) {
conn := NewConnection(serverUrl)
if reg, err := conn.Register(); err != nil {
t.Errorf("Register returned error: %s", err)
} else if !reg.Success {
t.Error("Server did not success for Register")
} else if len(reg.Id) == 0 {
t.Error("Server returned empty registration ID")
}
}

View file

@ -1,5 +1,7 @@
package rovegame
import "github.com/google/uuid"
// World describes a self contained universe and everything in it
type World struct {
instances []Instance
@ -7,7 +9,7 @@ type World struct {
// Instance describes a single entity or instance of an entity in the world
type Instance struct {
id int
id uuid.UUID
}
// NewWorld creates a new world object
@ -16,9 +18,8 @@ func NewWorld() *World {
}
// Adds an instance to the game
func (w *World) CreateInstance() int {
// Simple ID to start with
id := len(w.instances)
func (w *World) CreateInstance() uuid.UUID {
id := uuid.New()
// Initialise the instance
instance := Instance{