diff --git a/json_helpers.go b/json_helpers.go new file mode 100644 index 0000000000000000000000000000000000000000..b983a161f25d7c885f4cfec7c195727c934c77b7 --- /dev/null +++ b/json_helpers.go @@ -0,0 +1,15 @@ +package main + +import ( + "io/ioutil" + "log" + "net/http" +) + +func readJsonFromRequest(r *http.Request) []byte { + data, err := ioutil.ReadAll(r.Body) + if err != nil { + log.Println("could not read json from body") + } + return data +} diff --git a/main.go b/main.go index f82685d25e171ecee09e0ac85f04234749792d24..94ef43203dd301fbec3d0fafd2d4da6f89723dab 100644 --- a/main.go +++ b/main.go @@ -21,25 +21,22 @@ type Game struct { Player2 Player } -// GameData stores the "database" of all games -type GameData struct { - Store map[int]*Game -} - // Move represents a move in the game type Move struct { GameID int `json:"game_id"` + Player int `json:"player"` MoveData string `json:"move"` GameOver bool `json:"game_over"` } var ( - data *GameData + gameData map[int]Game ) /************** MAIN FUNCTION **********************/ func main() { + gameData = make(map[int]Game) http.HandleFunc("/", hello) @@ -68,6 +65,10 @@ func move(w http.ResponseWriter, r *http.Request) { log.Println("could not decode json data into Move struct") } + if move.GameOver { + fmt.Println("Game over!") + } + fmt.Fprintf(w, move.MoveData) }