-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
187 lines (165 loc) · 4.72 KB
/
server.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
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"errors"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"time"
)
var airplaneRooms map[string]AirplaneRoom
var airplaneGames map[string]AirplaneGame
var rpsRooms map[string]RPSRoom
var rpsGames map[string]RPSGame
var tictactoeRooms map[string]TicTacToeBoxRoom
var tictactoeGames map[string]TicTacToeBoxGame
var CookieNotSet error = errors.New("Cookie didn't set")
// setupResponse() is to deal with CORS Cross Origin Resource Sharing
/* CORS does pre-flight requests sending an OPTIONS request to any URL,
so to handle a POST request you first need to handle an OPTIONS request to that same URL.
*/
func setupResponse(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}
func isRoomExist(roomid string, roomName string) bool {
switch roomName {
case "AirplaneRoom":
if _, ok := airplaneRooms[roomid]; ok {
return true
}
return false
case "RPSRoom":
if _, ok := rpsRooms[roomid]; ok {
return true
}
return false
case "TicTacToeBoxRoom":
if _, ok := tictactoeRooms[roomid]; ok {
return true
}
return false
}
return false
}
func generateRoomid(roomName string) string {
for index := 0; ; index++ {
roomid := getRandomInt(1000000)
if !isRoomExist(roomid, roomName) {
return roomid
}
}
}
func (room AirplaneRoom) removeExpiredRoom() {
for roomid, room := range airplaneRooms {
if time.Now().After(room.expire) {
delete(airplaneRooms, roomid)
delete(airplaneGames, roomid)
}
}
}
func (room RPSRoom) removeExpiredRoom() {
for roomid, room := range rpsRooms {
if time.Now().After(room.expire) {
delete(rpsRooms, roomid)
delete(rpsGames, roomid)
}
}
}
func (room TicTacToeBoxRoom) removeExpiredRoom() {
for roomid, room := range tictactoeRooms {
if time.Now().After(room.expire) {
delete(tictactoeRooms, roomid)
delete(tictactoeGames, roomid)
}
}
}
func getPlayerNow(user string, player1 string, player2 string) string {
switch user {
case player1:
return "player1"
case player2:
return "player2"
default:
return ""
}
}
func setCookie(w http.ResponseWriter) string {
value := getRandomInt(1000000)
expire := getExpireTime()
cookie := http.Cookie{
Name: "gameUser",
Value: value,
Expires: expire,
Path: "/",
}
http.SetCookie(w, &cookie)
return value
}
func getCookie(r *http.Request) (string, error) {
cookie, err := r.Cookie("gameUser")
if err != nil {
return "", err
}
if cookie == nil {
return "", CookieNotSet
}
return cookie.Value, nil
}
// Return a random number in the string type
func getRandomInt(i int) string {
return strconv.Itoa(rand.Intn(i))
}
// Send one day later as an expire day
func getExpireTime() time.Time {
return time.Now().AddDate(0, 0, 1)
}
func logError(err error) {
if err != nil {
log.Println("ERROR:", err)
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
// Enabling CORS on a Go Web Server
setupResponse(w)
if r.Method == "OPTIONS" {
return
}
b, err := os.ReadFile("frontend/dist/index.html")
logError(err)
w.Write(b)
}
func main() {
// http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("frontend/dist/js/"))))
// http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("frontend/dist/css/"))))
// Airplane
airplaneRooms = make(map[string]AirplaneRoom, 0)
airplaneGames = make(map[string]AirplaneGame, 0)
// Rock paper scissor
rpsRooms = make(map[string]RPSRoom, 0)
rpsGames = make(map[string]RPSGame, 0)
// TicTacToe
tictactoeRooms = make(map[string]TicTacToeBoxRoom, 0)
tictactoeGames = make(map[string]TicTacToeBoxGame, 0)
// Index
// http.HandleFunc("/", indexHandler)
// http.HandleFunc("/Game", indexHandler)
// Airplane
http.HandleFunc("/api/FindAirplane/Game", airplaneInitHandler)
http.HandleFunc("/api/FindAirplane/Game/room", airplaneGameHandler)
// http.HandleFunc("/api/FindAirplane/restart", airplaneRestartHandler)
// Rock paper scissor
// http.HandleFunc("/api/RockPaperScissor/Game", rpsInitHandler)
// http.HandleFunc("/api/RockPaperScissor/Game/wait", rpsWaitForPlayer2Handler)
// http.HandleFunc("/api/RockPaperScissor/Game/room", rpsGameHandler)
// http.HandleFunc("/api/RockPaperScissor/Game/roundend", rpsRoundHandler)
// TicTacToe
http.HandleFunc("/api/TicTacToeBox/Game", tictactoeBoxInitHandler)
http.HandleFunc("/api/TicTacToeBox/Game/wait", tictactoeBoxWaitHandler)
http.HandleFunc("/api/TicTacToeBox/Game/room", tictactoeBoxGameHandler)
fmt.Println(http.ListenAndServe(":3000", nil))
}