-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (146 loc) · 4.81 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/Frezzle/noxes/game_manager"
"github.com/Frezzle/noxes/tictactoe"
)
func main() {
http.HandleFunc("/game", getGameHandler)
http.HandleFunc("/games", getAllGamesHandler)
http.HandleFunc("/game/create", createGameHandler)
http.HandleFunc("/game/move", makeMoveHandler)
// TODO: more endpoints can be added to accomodate auth: (including updating above endpoints to require auth)
// http.HandleFunc("/game/join", ...)
// http.HandleFunc("/game/quit", ...)
// http.HandleFunc("/user/register", ...)
// http.HandleFunc("/user/login", ...)
port := os.Getenv("PORT")
if port == "" {
port = "9876"
}
address := "0.0.0.0:" + port
log.Printf("Listening on %s", address)
log.Fatal(http.ListenAndServe(address, nil))
}
// TODO: Update model to use different data types to avoid so many type conversions.
type GameJSON struct {
ID string `json:"id"`
Board string `json:"board"`
NextTurn string `json:"nextTurn"`
GameOver string `json:"gameOver"`
Winner string `json:"winner"`
}
type GamesJSON []GameJSON
type IdJSON struct {
ID string `json:"id"`
}
var gm = game_manager.NewGameManager()
func getGameHandler(w http.ResponseWriter, r *http.Request) { // TODO: GET body should not be used; get ID from somewhere else (e.g. URL, header, other).
if !verifyRequest("GET", w, r) {
return
}
var gameIdJSON IdJSON
err := json.NewDecoder(r.Body).Decode(&gameIdJSON)
if err != nil {
http.Error(w, fmt.Sprintf(`{"error":"%s"}`, err.Error()), http.StatusBadRequest)
return
}
gameId, convErr := strconv.ParseInt(gameIdJSON.ID, 10, 32)
if convErr != nil {
http.Error(w, `{"error":"bad id provided"}`, http.StatusBadRequest)
return
}
game, findGameErr := gm.GetGameById(int(gameId))
if findGameErr != nil {
http.Error(w, `{"error":"game does not exist"}`, http.StatusBadRequest)
return
}
var result = GameJSON{
ID: gameIdJSON.ID,
Board: game.GetBoard(),
NextTurn: string(game.GetNextTurn()),
GameOver: strconv.FormatBool(game.IsGameOver()),
Winner: string(game.GetWinner()),
}
json.NewEncoder(w).Encode(result)
}
func getAllGamesHandler(w http.ResponseWriter, r *http.Request) {
if !verifyRequest("GET", w, r) {
return
}
games := gm.GetAllGames()
gamesJSON := make([]GameJSON, 0, len(games))
for _, game := range games {
gamesJSON = append(gamesJSON, GameJSON{ // TODO: refactor into own function.
ID: strconv.FormatInt(int64(game.GetId()), 10),
Board: game.GetBoard(),
NextTurn: string(game.GetNextTurn()),
GameOver: strconv.FormatBool(game.IsGameOver()),
Winner: string(game.GetWinner()),
})
}
json.NewEncoder(w).Encode(gamesJSON)
}
func createGameHandler(w http.ResponseWriter, r *http.Request) {
if !verifyRequest("POST", w, r) {
return
}
gameId := gm.CreateNewGame()
var result = IdJSON{ID: strconv.FormatInt(int64(gameId), 10)}
json.NewEncoder(w).Encode(result)
}
type MakeMoveJSON struct {
GameID string `json:"gameId"`
Location string `json:"location"`
Player string `json:"player"`
}
func makeMoveHandler(w http.ResponseWriter, r *http.Request) {
if !verifyRequest("POST", w, r) {
return
}
var makeMoveJSON MakeMoveJSON
err := json.NewDecoder(r.Body).Decode(&makeMoveJSON)
if err != nil {
http.Error(w, fmt.Sprintf(`{"error":"%s"}`, err.Error()), http.StatusBadRequest)
return
} else if makeMoveJSON.GameID == "" || makeMoveJSON.Location == "" || makeMoveJSON.Player == "" { // TODO: refactor into own function to use on all API calls
http.Error(w, `{"error":"'gameId', 'location' and 'player' fields required"}`, http.StatusBadRequest)
return
}
gameId, convErr := strconv.ParseInt(makeMoveJSON.GameID, 10, 32) // TODO: refactor into own function.
if convErr != nil {
http.Error(w, `{"error":"bad gameId provided"}`, http.StatusBadRequest)
return
}
game, findGameErr := gm.GetGameById(int(gameId)) // TODO: refactor into own function.
if findGameErr != nil {
http.Error(w, `{"error":"game does not exist"}`, http.StatusBadRequest)
return
}
location, convErr := strconv.ParseInt(makeMoveJSON.Location, 10, 32)
if convErr != nil {
http.Error(w, `{"error":"bad location provided"}`, http.StatusBadRequest)
return
}
err = gm.MakeMove(game.GetId(), tictactoe.Cell(makeMoveJSON.Player), tictactoe.Location(location))
if err != nil {
http.Error(w, fmt.Sprintf(`{"error":"%s"}`, err.Error()), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
}
func verifyRequest(supportedMethod string, w http.ResponseWriter, r *http.Request) bool {
if r.Method != supportedMethod {
http.Error(w, fmt.Sprintf(`{"error":"Invalid request method: %s"}`, r.Method), http.StatusMethodNotAllowed)
return false
} else if r.Body == nil {
http.Error(w, `{"error":"Please send a request body"}`, http.StatusBadRequest)
return false
}
return true
}