From eccb726f74d389bf83845b06dd12285e801205be Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 31 May 2020 00:06:14 +0100 Subject: [PATCH] Add the concept of a player and the /register endpoint --- cmd/rove-server/integration_test.go | 20 ----------------- cmd/rove-server/main.go | 13 ++++++++--- cmd/rove-server/player.go | 13 +++++++++++ cmd/rove-server/player_test.go | 13 +++++++++++ cmd/rove-server/router.go | 24 ++++++++++++++++++-- cmd/rove-server/router_test.go | 2 +- go.mod | 5 ++++- go.sum | 2 ++ pkg/rove/rove.go | 35 ++++++++++++++++++++++++----- pkg/rove/rove_test.go | 29 ++++++++++++++++++++++++ pkg/rovegame/world.go | 9 ++++---- 11 files changed, 129 insertions(+), 36 deletions(-) delete mode 100644 cmd/rove-server/integration_test.go create mode 100644 cmd/rove-server/player.go create mode 100644 cmd/rove-server/player_test.go create mode 100644 pkg/rove/rove_test.go diff --git a/cmd/rove-server/integration_test.go b/cmd/rove-server/integration_test.go deleted file mode 100644 index 33b562d..0000000 --- a/cmd/rove-server/integration_test.go +++ /dev/null @@ -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") - } -} diff --git a/cmd/rove-server/main.go b/cmd/rove-server/main.go index 48148c9..b243814 100644 --- a/cmd/rove-server/main.go +++ b/cmd/rove-server/main.go @@ -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 diff --git a/cmd/rove-server/player.go b/cmd/rove-server/player.go new file mode 100644 index 0000000..42e2f3a --- /dev/null +++ b/cmd/rove-server/player.go @@ -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(), + } +} diff --git a/cmd/rove-server/player_test.go b/cmd/rove-server/player_test.go new file mode 100644 index 0000000..b26d265 --- /dev/null +++ b/cmd/rove-server/player_test.go @@ -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") + } +} diff --git a/cmd/rove-server/router.go b/cmd/rove-server/router.go index ecb1846..59d24f9 100644 --- a/cmd/rove-server/router.go +++ b/cmd/rove-server/router.go @@ -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) } diff --git a/cmd/rove-server/router_test.go b/cmd/rove-server/router_test.go index 35643c2..de7ae5b 100644 --- a/cmd/rove-server/router_test.go +++ b/cmd/rove-server/router_test.go @@ -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 { diff --git a/go.mod b/go.mod index ede3023..25e663c 100644 --- a/go.mod +++ b/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 +) diff --git a/go.sum b/go.sum index abb0613..ee6c4bc 100644 --- a/go.sum +++ b/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= diff --git a/pkg/rove/rove.go b/pkg/rove/rove.go index 1f3c01f..2c91901 100644 --- a/pkg/rove/rove.go +++ b/pkg/rove/rove.go @@ -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 +} diff --git a/pkg/rove/rove_test.go b/pkg/rove/rove_test.go new file mode 100644 index 0000000..2a8e4ce --- /dev/null +++ b/pkg/rove/rove_test.go @@ -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") + } +} diff --git a/pkg/rovegame/world.go b/pkg/rovegame/world.go index 3b78cc9..1e1a4e3 100644 --- a/pkg/rovegame/world.go +++ b/pkg/rovegame/world.go @@ -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{