Add the concept of a player and the /register endpoint
This commit is contained in:
parent
8c4bf4f75f
commit
eccb726f74
11 changed files with 129 additions and 36 deletions
|
@ -1,20 +0,0 @@
|
|||
// +build integration
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/rove"
|
||||
)
|
||||
|
||||
var serverUrl = "localhost:8080"
|
||||
|
||||
func TestServerStatus(t *testing.T) {
|
||||
conn := rove.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")
|
||||
}
|
||||
}
|
|
@ -8,6 +8,8 @@ import (
|
|||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/rovegame"
|
||||
)
|
||||
|
||||
var port = flag.Int("port", 8080, "The port to host on")
|
||||
|
@ -16,6 +18,14 @@ func main() {
|
|||
|
||||
fmt.Println("Initialising...")
|
||||
|
||||
// Set up the world
|
||||
world := rovegame.NewWorld()
|
||||
fmt.Printf("World created\n\t%+v\n", world)
|
||||
|
||||
// Create a new router
|
||||
router := NewRouter()
|
||||
fmt.Printf("Router Created\n")
|
||||
|
||||
// Set up the close handler
|
||||
c := make(chan os.Signal)
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
|
@ -25,9 +35,6 @@ func main() {
|
|||
os.Exit(0)
|
||||
}()
|
||||
|
||||
// Create a new router
|
||||
router := NewRouter()
|
||||
|
||||
fmt.Println("Initialised")
|
||||
|
||||
// Listen and serve the http requests
|
||||
|
|
13
cmd/rove-server/player.go
Normal file
13
cmd/rove-server/player.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package main
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type Player struct {
|
||||
id uuid.UUID
|
||||
}
|
||||
|
||||
func NewPlayer() Player {
|
||||
return Player{
|
||||
id: uuid.New(),
|
||||
}
|
||||
}
|
13
cmd/rove-server/player_test.go
Normal file
13
cmd/rove-server/player_test.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewPlayer(t *testing.T) {
|
||||
a := NewPlayer()
|
||||
b := NewPlayer()
|
||||
if a.id == b.id {
|
||||
t.Error("Player IDs matched")
|
||||
}
|
||||
}
|
|
@ -15,6 +15,7 @@ func NewRouter() (router *mux.Router) {
|
|||
|
||||
// Set up the handlers
|
||||
router.HandleFunc("/status", HandleStatus)
|
||||
router.HandleFunc("/register", HandleRegister)
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -23,7 +24,7 @@ func NewRouter() (router *mux.Router) {
|
|||
func HandleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("%s\t%s", r.Method, r.RequestURI)
|
||||
|
||||
var status = rove.ServerStatus{
|
||||
var response = rove.StatusResponse{
|
||||
Ready: true,
|
||||
}
|
||||
|
||||
|
@ -32,5 +33,24 @@ func HandleStatus(w http.ResponseWriter, r *http.Request) {
|
|||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
// Reply with the current status
|
||||
json.NewEncoder(w).Encode(status)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// HandleRegister handles HTTP requests to the /register endpoint
|
||||
func HandleRegister(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("%s\t%s", r.Method, r.RequestURI)
|
||||
|
||||
// TODO: Add this user to the server
|
||||
player := NewPlayer()
|
||||
var response = rove.RegisterResponse{
|
||||
Success: true,
|
||||
Id: player.id.String(),
|
||||
}
|
||||
|
||||
// Be a good citizen and set the header for the return
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
// Reply with the current status
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ func TestHandleStatus(t *testing.T) {
|
|||
|
||||
HandleStatus(response, request)
|
||||
|
||||
var status rove.ServerStatus
|
||||
var status rove.StatusResponse
|
||||
json.NewDecoder(response.Body).Decode(&status)
|
||||
|
||||
if status.Ready != true {
|
||||
|
|
5
go.mod
5
go.mod
|
@ -2,4 +2,7 @@ module github.com/mdiluz/rove
|
|||
|
||||
go 1.14
|
||||
|
||||
require github.com/gorilla/mux v1.7.4
|
||||
require (
|
||||
github.com/google/uuid v1.1.1
|
||||
github.com/gorilla/mux v1.7.4
|
||||
)
|
||||
|
|
2
go.sum
2
go.sum
|
@ -1,2 +1,4 @@
|
|||
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
||||
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
|
||||
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||
|
|
|
@ -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(®ister)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
29
pkg/rove/rove_test.go
Normal file
29
pkg/rove/rove_test.go
Normal 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")
|
||||
}
|
||||
}
|
|
@ -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{
|
||||
|
|
Loading…
Add table
Reference in a new issue