forked from jbowens/codenames
-
Notifications
You must be signed in to change notification settings - Fork 18
/
server.go
409 lines (349 loc) · 9.51 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package codenames
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"path"
"sort"
"strings"
"sync"
"time"
"github.com/jbowens/assets"
"github.com/jbowens/dictionary"
)
type Server struct {
Server http.Server
AssetsPath string
tpl *template.Template
jslib assets.Bundle
js assets.Bundle
css assets.Bundle
images assets.Bundle
other assets.Bundle
gameIDWords []string
mu sync.Mutex
games map[string]*Game
imagePaths []string
mux *http.ServeMux
}
func (s *Server) getGame(gameID, stateID string) (*Game, bool) {
g, ok := s.games[gameID]
if ok {
return g, ok
}
state, ok := decodeGameState(stateID)
if !ok {
return nil, false
}
g = newGame(gameID, s.imagePaths, state)
s.games[gameID] = g
return g, true
}
func (s *Server) getImagePaths(rw http.ResponseWriter, imagesLink string) ([]string, error) {
if imagesLink == "" {
// No link was given, use the server's default images.
return s.imagePaths, nil
}
fmt.Printf("Trying to use custom images from %s\n", imagesLink)
rs, err := http.Get(imagesLink)
if err != nil {
http.Error(rw, "Problem with provided link", 400)
return nil, err
}
defer rs.Body.Close()
bodyBytes, err := ioutil.ReadAll(rs.Body)
if err != nil {
http.Error(rw, "Problem with provided link", 400)
return nil, err
}
bodyString := string(bodyBytes)
if strings.HasSuffix(imagesLink, "txt") {
fmt.Printf("Text file based source\n")
// We assume that the text file is links line by line.
// They can either be full paths like:
// https://server.com/image.jpg
// Or paths relative to the text file location like:
// image.jpg
// Which refers to https://server.com/image.jpg
// if the text file was for example here:
// https://server.com/directorylisting.txt
links := strings.Split(bodyString, "\n")
validLinks := make([]string, 0, len(links))
// Remove any zero-length links.
for _, link := range links {
if len(strings.TrimSpace(link)) > 0 {
validLinks = append(validLinks, link)
}
}
// Testing if the links are relative or absolute site links
var absolute bool
if strings.Contains(validLinks[0], "http") {
absolute = true
} else {
absolute = false
}
if absolute {
return validLinks, nil
} else {
splitted := strings.Split(imagesLink, "/")
base := strings.Join(splitted[:len(splitted)-1], "/")
for index, link := range validLinks {
validLinks[index] = base + "/" + link
}
return validLinks, nil
}
} else {
fmt.Printf("Directory based source\n")
// The user has given us a non-text file.
// We assume it's a directory listing, specifically the one nginx produces.
splitted := strings.Split(imagesLink, "/")
base := strings.Join(splitted[:len(splitted)-1], "/")
lines := strings.Split(bodyString, "\n")
var links []string
for _, line := range lines {
if !strings.Contains(line, "<a href=\"") {
continue
}
if strings.Contains(line, "..") {
continue
}
relativeLink := strings.Split(strings.Split(" "+line, "<a href=\"")[1], "\">")[0]
links = append(links, base+"/"+relativeLink)
}
return links, nil
}
// We should never get to here.
return nil, nil
}
// GET /game/<id>
func (s *Server) handleRetrieveGame(rw http.ResponseWriter, req *http.Request) {
s.mu.Lock()
defer s.mu.Unlock()
err := req.ParseForm()
if err != nil {
http.Error(rw, "Error decoding query string", 400)
return
}
gameID := path.Base(req.URL.Path)
g, ok := s.getGame(gameID, req.Form.Get("state_id"))
if ok {
writeGame(rw, g)
return
}
imagePaths, err := s.getImagePaths(rw, req.Form.Get("newGameImagesLink"))
if err != nil {
fmt.Printf("Could not load in custom images\n")
http.Error(rw, "Unknown error encountered with custom images", 400)
return
}
if len(imagePaths) < 20 {
fmt.Printf("Insufficient images in custom source\n")
http.Error(rw, "Insufficient images (20 needed) available in custom source", 400)
return
}
// Now that we know the link is good, let's try to record the image link used.
f, err := os.OpenFile("successful_links.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
}
defer f.Close()
if _, err := f.WriteString(req.Form.Get("newGameImagesLink")); err != nil {
fmt.Println(err)
}
if err == nil {
fmt.Printf("Successfully wrote images link to file\n")
} else {
fmt.Println(err)
}
fmt.Printf("Making new game!\n")
g = newGame(gameID, imagePaths, randomState())
s.games[gameID] = g
writeGame(rw, g)
}
// POST /guess
func (s *Server) handleGuess(rw http.ResponseWriter, req *http.Request) {
var request struct {
GameID string `json:"game_id"`
StateID string `json:"state_id"`
Index int `json:"index"`
}
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&request); err != nil {
http.Error(rw, "Error decoding", 400)
return
}
s.mu.Lock()
defer s.mu.Unlock()
g, ok := s.getGame(request.GameID, request.StateID)
if !ok {
http.Error(rw, "No such game", 404)
return
}
if err := g.Guess(request.Index); err != nil {
http.Error(rw, err.Error(), 400)
return
}
writeGame(rw, g)
}
// POST /end-turn
func (s *Server) handleEndTurn(rw http.ResponseWriter, req *http.Request) {
var request struct {
GameID string `json:"game_id"`
StateID string `json:"state_id"`
}
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&request); err != nil {
http.Error(rw, "Error decoding", 400)
return
}
s.mu.Lock()
defer s.mu.Unlock()
g, ok := s.getGame(request.GameID, request.StateID)
if !ok {
http.Error(rw, "No such game", 404)
return
}
if err := g.NextTurn(); err != nil {
http.Error(rw, err.Error(), 400)
return
}
writeGame(rw, g)
}
func (s *Server) handleNextGame(rw http.ResponseWriter, req *http.Request) {
var request struct {
GameID string `json:"game_id"`
}
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&request); err != nil {
http.Error(rw, "Error decoding", 400)
return
}
s.mu.Lock()
defer s.mu.Unlock()
// Find the existing game so we can fetch the images it used.
g, exists := s.games[request.GameID]
if !exists {
http.Error(rw, "Invalid game", 404)
return
}
// Create a new game with the same ID and source images from the past game but with a random state.
g = newGame(request.GameID, g.Images, randomState())
s.games[request.GameID] = g
writeGame(rw, g)
}
type statsResponse struct {
InProgress int `json:"games_in_progress"`
}
func (s *Server) handleStats(rw http.ResponseWriter, req *http.Request) {
var inProgress int
s.mu.Lock()
defer s.mu.Unlock()
for _, g := range s.games {
if g.WinningTeam == nil {
inProgress++
}
}
writeJSON(rw, statsResponse{inProgress})
}
func (s *Server) cleanupOldGames() {
s.mu.Lock()
defer s.mu.Unlock()
for id, g := range s.games {
if g.WinningTeam != nil && g.CreatedAt.Add(12*time.Hour).Before(time.Now()) {
delete(s.games, id)
fmt.Printf("Removed completed game %s\n", id)
continue
}
if g.CreatedAt.Add(24 * time.Hour).Before(time.Now()) {
delete(s.games, id)
fmt.Printf("Removed expired game %s\n", id)
continue
}
}
}
func (s *Server) Start() error {
gameIDs, err := dictionary.Load(fmt.Sprintf("%s/game-id-words.txt", s.AssetsPath))
if err != nil {
return err
}
var imagesAssetPath = fmt.Sprintf("%s/images", s.AssetsPath)
s.images, err = assets.Development(imagesAssetPath)
if err != nil {
return err
}
// Hardcoding 20 is easier than defining a constants file.
if len(s.images.RelativePaths()) < 20 {
fmt.Fprintf(os.Stderr,
"Error: You need at least %d images in %s\n",
20,
imagesAssetPath,
)
os.Exit(1)
}
s.tpl, err = template.New("index").Parse(tpl)
if err != nil {
return err
}
s.jslib, err = assets.Development(fmt.Sprintf("%s/jslib", s.AssetsPath))
if err != nil {
return err
}
s.js, err = assets.Development(fmt.Sprintf("%s/javascript", s.AssetsPath))
if err != nil {
return err
}
s.css, err = assets.Development(fmt.Sprintf("%s/stylesheets", s.AssetsPath))
if err != nil {
return err
}
s.other, err = assets.Development(fmt.Sprintf("%s/other", s.AssetsPath))
if err != nil {
return err
}
s.mux = http.NewServeMux()
s.mux.HandleFunc("/stats", s.handleStats)
s.mux.HandleFunc("/next-game", s.handleNextGame)
s.mux.HandleFunc("/end-turn", s.handleEndTurn)
s.mux.HandleFunc("/guess", s.handleGuess)
s.mux.HandleFunc("/game/", s.handleRetrieveGame)
s.mux.Handle("/js/lib/", http.StripPrefix("/js/lib/", s.jslib))
s.mux.Handle("/js/", http.StripPrefix("/js/", s.js))
s.mux.Handle("/css/", http.StripPrefix("/css/", s.css))
s.mux.Handle("/images/", http.StripPrefix("/images/", s.images))
s.mux.Handle("/other/", http.StripPrefix("/other/", s.other))
s.mux.HandleFunc("/", s.handleIndex)
gameIDs = dictionary.Filter(gameIDs, func(s string) bool { return len(s) > 3 })
s.gameIDWords = gameIDs.Words()
s.games = make(map[string]*Game)
s.imagePaths = s.images.RelativePaths()
for index, element := range s.imagePaths {
s.imagePaths[index] = "images/" + element
}
sort.Strings(s.imagePaths)
s.Server.Handler = s.mux
go func() {
for range time.Tick(10 * time.Minute) {
s.cleanupOldGames()
}
}()
fmt.Printf("Server running!\n")
return s.Server.ListenAndServe()
}
func writeGame(rw http.ResponseWriter, g *Game) {
writeJSON(rw, struct {
*Game
StateID string `json:"state_id"`
}{g, g.GameState.ID()})
}
func writeJSON(rw http.ResponseWriter, resp interface{}) {
j, err := json.Marshal(resp)
if err != nil {
http.Error(rw, "unable to marshal response: "+err.Error(), 500)
return
}
rw.Header().Set("Content-Type", "application/json")
rw.Write(j)
}