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

@ -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")
}
}

View file

@ -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
View 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(),
}
}

View 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")
}
}

View file

@ -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)
}

View file

@ -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 {