From d11c704fc89f895d54b445bfcd4c4f091ec17325 Mon Sep 17 00:00:00 2001 From: Alex Constantin-Gomez <ac3419@ic.ac.uk> Date: Sat, 8 Feb 2020 16:00:00 +0000 Subject: [PATCH] added generateID() - not thread safe --- main.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 94ef432..fde4051 100644 --- a/main.go +++ b/main.go @@ -17,8 +17,8 @@ type Player struct { // Game represents a game object in the database type Game struct { ID int - Player1 Player - Player2 Player + Player1 string + Player2 string } // Move represents a move in the game @@ -30,12 +30,14 @@ type Move struct { } var ( - gameData map[int]Game + gameData map[int]Game + idCounter int ) /************** MAIN FUNCTION **********************/ func main() { + // Initialise global variables gameData = make(map[int]Game) http.HandleFunc("/", hello) @@ -73,9 +75,25 @@ func move(w http.ResponseWriter, r *http.Request) { } func createGame(w http.ResponseWriter, r *http.Request) { + //jsonData := readJsonFromRequest(r) + + newGameID := generateID() + newGame := Game{ + ID: newGameID, + // Player1: , + // Player2: , + } + gameData[newGameID] = newGame + fmt.Fprintf(w, "hello") } func joinGame(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "hello") } + +/************** Helper functions *******************/ + +func generateID() int { + return idCounter + 1 +} -- GitLab