~johanvandegriff/games.johanv.xyz

ref: b98c836c0a9753be78225c9530039f5fe9a32d0f games.johanv.xyz/boggle.py -rw-r--r-- 36.8 KiB
b98c836cJohan Vandegriff added instructions to correct mistakes, fixed file naming 10 months ago
                                                                                
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
from flask import render_template
import sys, cgi, json, datetime, re, time, decimal, subprocess, random, threading, os, pymongo, traceback, base64
from functools import reduce

from nav import nav #file in same dir
from BoggleCVPipeline import processImage, BoggleError

ROOT_DIR = "/srv/boggle"
# GAMES_FILE = ROOT_DIR + "/games.json"
# GAMES_LOCK_FILE = ROOT_DIR + "/games.json.lock"

IMAGE_UPLOAD_DIR = ROOT_DIR + '/upload-board/'

DEFINITIONS_FILE = ROOT_DIR + '/lists/CollinsScrabbleWords2019WithDefinitions.json'
WORD_LIST_FILE = ROOT_DIR + '/lists/CollinsScrabbleWords2019.json'

# remove games that are finished after this many seconds
ARCHIVE_TIMEOUT = 30 #archive a COMPLETED game but keep in the lobby
REMOVE_COMPLETED_FROM_LOBBY_TIMEOUT = 5*60 #remove a COMPLETED game from the lobby
REMOVE_STALE_FROM_LOBBY_TIMEOUT = 2*60*60 #remove a NOT STARTED game from lobby

EXTRA_SECONDS_TO_SUBMIT = 1

#all the dice found in boggle deluxe
BOGGLE_DICE = [
        ['O','O','O','T','T','U'],
        ['D','H','H','N','O','T'],
        ['N','O','U','T','O','W'],
        ['A','A','A','S','F','R'],
        ['A','E','E','M','U','G'],
        ['A','E','N','N','M','G'],
        ['A','D','E','N','N','N'],
        ['D','D','N','R','L','O'],
        ['C','C','T','S','N','W'],
        ['F','S','I','P','R','Y'],
        ['A','E','E','E','E','M'],
        ['I','R','R','P','H','Y'],
        ['E','O','T','T','T','M'],
        ['A','A','F','I','S','R'],
        ['D','O','L','H','N','R'],
        ['C','E','S','P','T','I'],
        ['A','A','E','E','E','E'],
        ['E','I','I','I','T','T'],
        ['A','F','I','S','R','Y'],
        ['C','E','P','I','T','L'],
        ['E','N','S','S','S','U'],
        ['C','E','I','I','L','T'],
        ['D','H','H','O','L','R'],
        ['G','O','V','R','R','W'],
        ['K','Z','X','B','J','Qu']
]

#the boggle alphabet
ALPHABET = ['A','B','C','D','E','F','G','H','I','J','K','L','M',
            'N','O','P','Qu','R','S','T','U','V','W','X','Y','Z']

DB_NAME = "boggle"
COLL_NAME = "games"

with open("/srv/boggle/mongodb-url.txt", 'r') as f:
    DB_URL = f.read()

dbClient = pymongo.MongoClient(DB_URL)
db = dbClient[DB_NAME]
coll = db[COLL_NAME]

wordList = json.load(open(WORD_LIST_FILE,'r'))
allDefinitions = json.load(open(DEFINITIONS_FILE,'r'))


#invitations to play again. the key is the game ID that players are invited from,
#  and the value is the new game they are invited to.
invitations = {}


#replace quotes
def rq(s):
  return s.replace("'", "'").replace('"', '"')
  #return cgi.escape(s).replace("'", "'").replace('"', '"')

def create(size=5, letters=None, minutes=3, board=None):
    if letters is None:
        if size >=5:
            letters = 4
        elif size == 4:
            letters = 3
        else:
            letters = 2

    dice = [] #no dice

    #until we have enough dice for this board size
    while len(dice) < size*size:
        #add a complete set of standard dice
        dice.extend(BOGGLE_DICE[:])
    #if the size is 5 or less, only 1 set is required
    #if the size is 6 or 7, 2 sets are required, etc.

    random.shuffle(dice)

    wasImported = True #the board was passed in from some other method
    if board is None:
        wasImported = False #the board was randomly generated rather than given
        #construct the board, nested item-by-item
        board = [[dice[i*size+j][random.randint(0,5)] for j in range(size)] for i in range(size)]

    #construct a dict with all the info
    game = {
        "size": size,
        "letters": letters,
        "minutes": minutes,
        "board": board,
        "wasImported": wasImported,
        "isStarted": False,
        "isDone": False,
        "isArchived": False,
        "players": [],
        "timeCreatedSeconds": int(time.time())
    }
    return game

def calculatePoints(word):
    length = len(word)
    if length > 8: length = 8
    return [0,1,1,1,1,2,3,5,11][length]

#x, y: the current position on the board
#word: the current word
#used: the spots the word has letters from 
# def solve_aux(x, y, word, used, board, words, found, size):
#     myused = used[:]

#     myused.append([x,y]); #use the current spot
#     myword = word + board[x][y].lower() #add on to the word

#     if myword in words: #if the word is in the list of possible words
#         found.append(myword)
#         # found[myword] = myused
#         words.remove(myword)
#     if not any(re.search("^" + myword, word) for word in words):
#         return
#     for newx in range(x-1,x+2): # +2 because range() is exclusive on the upper bound
#         for newy in range(y-1,y+2):
#             if (newx>=0 and newy>=0 and newx<size and newy<size and not [newx,newy] in myused):
#                 solve_aux(newx, newy, myword, myused, board, words, found, size)

def solve(game):
    start = time.time()
    board = game["board"]
    minWordLength = game["letters"]
    size = len(board)

    quantities = [] #the quantities of each letter

    excluded = [] #which letters are not on the board
    for letter in ALPHABET:
        excluded.append(letter) #copy the alphabet to excluded
        quantities.append(0) #fill quantities with 0's
        for letter2 in ALPHABET:
            excluded.append(letter + letter2)
    for x in range(size):
        for y in range(size):
            letter = board[x][y]
            quantities[ALPHABET.index(letter)] += 1 #increase the quantity of the letter
            if letter in excluded: #if the letter is in excluded
                excluded.remove(letter) #remove the letter from excluded
            if letter == 'Qu' and 'U' in excluded: #if the letter is in excluded
                excluded.remove('U') #remove the letter from excluded
            for newx in range(x-1,x+2): # +2 because range() is exclusive on the upper bound
                for newy in range(y-1,y+2):
                    if (newx>=0 and newy>=0 and newx<size and newy<size
                            and not (newx == x and newy == y)):
                        newletter = board[newx][newy]
                        seq = letter + newletter
                        if seq in excluded: #if the sequence is in excluded
                            excluded.remove(seq) #remove the sequence from excluded
                        if letter == 'Qu':
                            seq = 'U' + newletter
                        if seq in excluded: #if the sequence is in excluded
                            excluded.remove(seq) #remove the sequence from excluded

    #increase the number of U's by the number of Qu's
    quantities[ALPHABET.index('U')] += quantities[ALPHABET.index('Qu')]

    # words = [] #the list of possible words
    found = []
    
    wordList = json.load(open(WORD_LIST_FILE,'r'))
    for line in wordList: #sort through each word
        if len(line) >= minWordLength: #if the word is long enough
            #if the word does not have too many of any letter
            if not any(line.count(ALPHABET[i].lower()) > quantities[i] for i in range(len(ALPHABET))):
                #if there are no letters not on the board in this word
                if not any(letter.lower() in line for letter in excluded):
                    # words.append(line); #add the word to the list of possible words
                    if isWordValid(game, line, None):
                        found.append(line)

    score = 0
    #found = []
    # found = {}

    # for x in range(size):
    #     for y in range(size):
    #         used = []
    #         word = ""
    #         solve_aux(x, y, word, used, board, words, found, size)
    
    score = 0
    for word in found:
        score += calculatePoints(word)
    
    game["words"] = found
    # game["paths"] = found
    game["maxScore"] = score
    game["maxWords"] = len(found)
    game["secondsToSolve"] = time.time() - start
    return game

def isWordValidAux(board, word, x, y, used, size):
    if len(word) == 0: return True
    myused = used[:]
    myused.append([x,y]); #use the current spot
    l = board[x][y].lower()
    if l == "qu":
        if len(word) < 2 or word[0:2] != "qu": return False
        if word == "qu": return True
        newword = word[2:]
    else:
        if word[0] != l: return False
        if word == l: return True
        newword = word[1:]
            
    for newx in range(x-1,x+2): # +2 because range() is exclusive on the upper bound
        for newy in range(y-1,y+2):
            if (newx>=0 and newy>=0 and newx<size and newy<size and not [newx,newy] in myused):
                if isWordValidAux(board, newword, newx, newy, myused, size):
                    return True
    return False


def isWordValid(game, word, wordList):
    if len(word) < game["letters"] or (wordList is not None and not word in wordList):
        return False
    board = game["board"]
    size = len(board)
    if len(word) <= size*size:
        for x in range(size):
            for y in range(size):
                if isWordValidAux(board, word, x, y, [], size):
                     return True
    return False


def getGameByID(id):
    return coll.find_one({"_id": id})

def deleteGameByID(id):
    coll.delete_one({"_id": id})

def getAllIDs():
    return [x["_id"] for x in coll.find({}, {"_id": 1})]
def newGameID():
    allIDs = getAllIDs()
    if len(allIDs) == 0: #if there are no IDs, max([]) will fail
        return 0 #so return 1 so it starts with game #0
    else:
        return max(getAllIDs())+1

def updateAttr(game, attr):
    coll.update_one({"_id": game["_id"]}, {"$set": {attr: game[attr]}})


class BackgroundSolver(object):
    def __init__(self, game):
        self.game = game
        thread = threading.Thread(target=self.run, args=())
        thread.daemon = True
        thread.start()

    def run(self):
        self.game = solve(self.game)
        # lockGamesFile()
        # games = loadGamesFile()
        new_game = getGameByID(self.game["_id"])
        new_game["words"] = self.game["words"]
        new_game["maxScore"] = self.game["maxScore"]
        new_game["maxWords"] = self.game["maxWords"]
        new_game["secondsToSolve"] = self.game["secondsToSolve"]
            
        if  new_game["maxWords"] == 0:
            new_game["percentFound"] = 100
        elif not "numWordsPlayersFound" in new_game:
            new_game["percentFound"] = 0
        else:
            new_game["percentFound"] = new_game["numWordsPlayersFound"] / new_game["maxWords"] * 100

        coll.update_one({"_id": new_game["_id"]}, {"$set": {"words": new_game["words"], "maxScore": new_game["maxScore"], "maxWords": new_game["maxWords"], "secondsToSolve": new_game["secondsToSolve"], "percentFound": new_game["percentFound"]}})

        # saveGamesFile(games)
        # unlockGamesFile()

def processAllTypedWords(game):
    if not "playerData" in game:
        game["playerData"] = {}
    if not "typedWords" in game:
        game["typedWords"] = {}
    for username in game["players"]:
        if username in game["typedWords"]:
            words = game["typedWords"][username]
        else:
            words = []
        
        if not username in game["players"]:
            game["players"].append(username)
        
        wordsDict = {}
        
        for word in words:
            if not word in wordsDict:
                if "words" in game: #if the board is already solved
                    if word in game["words"]: #check the list of solved words
                        wordsDict[word] = False #false means it's not a duplicate
                else: # if the board has not been solved yet
                    if isWordValid(game, word, wordList): #manually check using the board
                        wordsDict[word] = False #false means it's not a duplicate

        for player in game["playerData"]:
            playerData = game["playerData"][player]
            for word in wordsDict:
                if word in playerData["words"]:
                    wordsDict[word] = True #true means it is a duplicate
                    if not playerData["words"][word]:
                        playerData["words"][word] = True
                        playerData["score"] -= calculatePoints(word)
    
        score = 0
        for word in wordsDict:
            if not wordsDict[word]:
                score += calculatePoints(word)
        game["playerData"][username] = {
            "words": wordsDict,
            "score": score,
            "numWords": len(wordsDict)
        }

    winners = []
    winScore = -1
    playerWords = set([])
    dupes = []
    for player in game["playerData"]:
        playerData = game["playerData"][player]
        playerWords = playerWords.union(playerData["words"])
        playerScore = playerData["score"]
        if playerScore > winScore:
            winners = [player]
            winScore = playerScore
        elif playerScore == winScore and not player in winners:
            winners.append(player)
        for word in playerData["words"]:
            if playerData["words"][word] and not word in dupes:
                dupes.append(word)
    game["numWordsPlayersFound"] = len(playerWords)
    game["duplicates"] = len(dupes)
    if ("maxWords" not in game) or game["maxWords"] == 0:
        game["percentFound"] = 100
    else:
        game["percentFound"] = game["numWordsPlayersFound"] / game["maxWords"] * 100
    game["winners"] = winners
    game["winScore"] = winScore
    coll.update_one({"_id": game["_id"]}, {"$set": {"playerData": game["playerData"], "typedWords": game["typedWords"], "numWordsPlayersFound": game["numWordsPlayersFound"], "duplicates": game["duplicates"], "percentFound": game["percentFound"], "winners": game["winners"], "winScore": game["winScore"]}})
    return game

def updateGame(game):
    changed = False

    elapsedSinceCreatedSeconds = int(time.time()) - game["timeCreatedSeconds"]
    if "timeStartedSeconds" in game:
        elapsedSinceStartedSeconds = int(time.time()) - game["timeStartedSeconds"]
    else:
        elapsedSinceStartedSeconds = 0
    
    #check for stale games in the lobby
    if not game["isStarted"] and elapsedSinceCreatedSeconds > REMOVE_STALE_FROM_LOBBY_TIMEOUT:
        deleteGameByID(game["_id"])
        print("stale game #{} removed from lobby".format(game["_id"]))
        return None

    game["secondsLeft"] = game["minutes"]*60 - elapsedSinceStartedSeconds
    if game["secondsLeft"] <= -EXTRA_SECONDS_TO_SUBMIT and not game["isDone"]:
        game["isDone"] = True
        game = processAllTypedWords(game)
        changed = True
    # if the game has been over for longer than the archive timeout, and it's not already archived, and it's solved
    if game["secondsLeft"] <= -ARCHIVE_TIMEOUT and not game["isArchived"] and "words" in game:
        game["isArchived"] = True
        changed = True
    if changed:
        coll.update_one({"_id": game["_id"]}, {"$set": {"secondsLeft": game["secondsLeft"], "isDone": game["isDone"], "isArchived": game["isArchived"]}})
    return game

def toIntOrDefault(v, default):
    try:
        return int(v)
    except ValueError:
        return default

def toFloatOrDefault(v, default):
    try:
        return float(v)
    except ValueError:
        return default

def filterUsername(username):
    #only alphanumeric, max length is 32 chars
    return re.sub('[^a-zA-Z\d]', '', username)[:32]

def filterWord(word):
    #only letters, convert to lowercase
    return re.sub('[^a-z]', '', word.lower())

def getAllGames():
    games = coll.find()
    games = [updateGame(game) for game in games]
    games = [game for game in games if game] #remove entries that are "None"
    return games

def boardFrom5x5Str(board_flat):
    board = []
    for i in range(5):
        row = []
        for j in range(5):
            n = i*5+j
            letter = board_flat[n]
            if letter == "Q":
                letter = "Qu"
            row.append(letter)
        board.append(row)
    return board

tmp_images = {}

"""
This method doesn't return html, but JSON data for JS to digest.
It is called with AJAX requests, and the data will be formatted
to update part of the page without reloading the whole thing.
"""
def request_data(form, files):
    request = filterWord(form["request"])
    if request == "game":
        if "id" in form:
            id = toIntOrDefault(form["id"], -1)
            # lockGamesFile()
            # games = loadGamesFile()
            print("game request with id {}".format(id))
            game = getGameByID(id)
            if game is None:
                print("game not found")
                # unlockGamesFile()
                return json.dumps({})
            else:
                game = updateGame(game)
                if game is None:
                    return json.dumps({})
                print("game found")
                # if changed:
                #     saveGamesFile(games)
                # unlockGamesFile()
                return json.dumps({"game": game})
        else:
            print("game request with no id")
            return json.dumps({})
    if request == "games":
        # lockGamesFile()
        # games = loadGamesFile()
        # anyChanged = False
        games = getAllGames()
        # for id in getAllIDs():
        #     updateGame(getGameByID(id))
            # if changed:
            #     anyChanged = True
        # if anyChanged:
        #     saveGamesFile(games)
        # unlockGamesFile()
        if "page" in form:
            page = filterWord(form["page"])
            if page == "lobby":
                games = [game for game in games if game["secondsLeft"] > -REMOVE_COMPLETED_FROM_LOBBY_TIMEOUT]
            elif page == "stats":
                games = [game for game in games if game["isArchived"]]
        return json.dumps({"games": games})
    if request == "basic" and "id" in form:
        id = toIntOrDefault(form["id"], -1)
        # lockGamesFile()
        # games = loadGamesFile()
        # unlockGamesFile()
        game = getGameByID(id)
        if game is not None:
            game = updateGame(game)
        if game is not None:
            return json.dumps({"isStarted": game["isStarted"], "players": game["players"]})
    if request == "savewords" and "id" in form and "words" in form and "username" in form:
        id = toIntOrDefault(form["id"], -1)
        words = form["words"].split(",")
        words = [filterWord(word) for word in words]
        username = filterUsername(form["username"])
        # lockGamesFile()
        # games = loadGamesFile()
        game = getGameByID(id)
        if game is not None:
            game = updateGame(game)
        if not "typedWords" in game:
            game["typedWords"] = {}
            changed = True
        typedWords = game["typedWords"]
        if not game["isDone"]:
            if not username in typedWords:
                typedWords[username] = []
                changed = True
            myTypedWords = typedWords[username]
            for word in words:
                if not word in myTypedWords:
                    myTypedWords.append(word)
                    changed = True
        if changed:
            updateAttr(game, "typedWords")
            # coll.update_one({"_id": game["_id"]}, {"$set": {"typedWords": typedWords})
            # saveGamesFile(games)
        # unlockGamesFile()
        return json.dumps({"typedWords": typedWords[username]})
    if request == "definitions" and "id" in form:
        id = toIntOrDefault(form["id"], -1)
        game = getGameByID(id)
        if game is not None:
            definitions = {}
            for word in game["words"]:
                definitions[word] = allDefinitions[word]
            return json.dumps({"definitions": definitions})
    if request == "invitation" and "id" in form:
        id = toIntOrDefault(form["id"], -1)
        toDelete = []
        for inv in invitations:
            newGame = getGameByID(invitations[inv])
            #If the new game is archived, the invitation is useless and can be removed from the list
            if newGame is None or newGame["isArchived"]:
                toDelete.append(inv)
        for inv in toDelete:
            del invitations[inv]
        print("invitations:", invitations)
        if id in invitations:
            invitedTo = invitations[id]
            return json.dumps({"invitation": invitedTo})
    if request == "upload":
        response = {}
        # create image directory if not found
        if not os.path.isdir(IMAGE_UPLOAD_DIR):
            os.mkdir(IMAGE_UPLOAD_DIR)
        print(files)
        print(len(files))
        if len(files) > 0:
            upload = files.getlist("upload")[0]
            print("filename: {}".format(upload.filename))
            if upload.filename == "blob":
                upload.filename = str(datetime.datetime.now().strftime("%Y%m%d_%H-%M-%S"))
                print("new filename: {}".format(upload.filename))
            filename = upload.filename.replace("'","") #filter out '
            destination = IMAGE_UPLOAD_DIR + filename
            i=0
            destination_extra = destination
            spl = os.path.splitext(destination)
            while os.path.exists(destination_extra):
                destination_extra = spl[0] + "_" + str(i) + spl[1]
                i += 1
            destination = destination_extra
            # save file
            print("File saved to to:", destination)
            upload.save(destination)
            message = "File uploaded"
            try:
                lettersGuessed, confidence = processImage(destination)
                response["board"] = boardFrom5x5Str(lettersGuessed)
                message += "; " + lettersGuessed
                message += "; " + str(confidence)
            except Exception as e:
                message += "; " + str(e) + "; " + repr(e) + "; " + traceback.format_exc()
            with open(destination+".txt", "w") as f:
                print("Message saved to to:", destination+".txt")
                f.write(message)
        else:
            message = "no file provided"
        response["message"] = message
        if os.path.exists(destination+"_warpedimage.png"):
            key = str(random.randint(1000000000000000,9999999999999999))
            tmp_images[key] = destination
            response["warpedimage"] = "?request=tmpimage&key="+key
        return json.dumps(response)
    if request == "tmpimage":
        if "key" in form and form["key"] in tmp_images:
            filename = tmp_images[form["key"]]+"_warpedimage.png"
            with open(filename, 'rb') as f:
                return f.read()

    return json.dumps({})

def do_action(form):
    # these are guaranteed by the function that calls this
    action = filterWord(form["action"])
    username = filterUsername(form["username"])

    if action == "create":
        board = None
        if "preset" in form:
            preset = form["preset"]
            if preset == "5x5":
                size = 5
                letters = 4
                minutes = 3
            elif preset == "4x4":
                size = 4
                letters = 3
                minutes = 3
            elif preset == "random":
                size = random.randint(2,8)
                letters = [0,0,
                    random.randint(2,3), #2
                    random.randint(2,4), #3
                    random.randint(2,5), #4
                    random.randint(2,6), #5
                    random.randint(2,7), #6
                    random.randint(2,7), #7
                    random.randint(2,7)  #8
                ]
                letters = letters[size]
                minutes = random.randint(0,10)
                if minutes == 0: minutes = 0.5
            elif preset == "5x5bogglecv" and "board" in form:
                board = boardFrom5x5Str(form["board"].split(','))
                print(board)
                size = 5
                letters = 4
                minutes = 3
                if "letters" in form:
                    letters = toIntOrDefault(form["letters"], 4)
                if "minutes" in form:
                    minutes = toFloatOrDefault(form["minutes"], 3)
                if "key" in form:
                    board_file = tmp_images[form["key"]] + "_board_corrected.txt"
                    with open(board_file, 'w') as f:
                        f.write(str(board))
            elif preset == "custom" and "size" in form and "letters" in form and "minutes" in form:
                size = form["size"]
                matched = re.match("^(\d+)x(\d+)$", size)
                if matched is None:
                    size = 5
                else:
                    size = int(matched.groups()[0])
                if size < 5:
                    lettersDefault = 3
                else:
                    lettersDefault = 4
                letters = toIntOrDefault(form["letters"], lettersDefault)
                minutes = toFloatOrDefault(form["minutes"], 3)
            else:
                return "lobby", None
            # lockGamesFile()
            # games = loadGamesFile()
            print("creating game: size={}, letters={}, minutes={}".format(size, letters, minutes))
            game = create(size=size, letters=letters, minutes=minutes, board=board) #usually board is None
            game["_id"] = newGameID()
            BackgroundSolver(game)
            game["players"].append(username)
            print(game)
            coll.insert_one(game)
            # games.append(game)
            # saveGamesFile(games)
            # unlockGamesFile()
            if "inviteFrom" in form:
                inviteFrom = toIntOrDefault(form["inviteFrom"], -1)
                if inviteFrom >= 0:
                    invitations[inviteFrom] = game["_id"]
                print("invitations:", invitations)
            return "pregame", game["_id"]
        return "lobby", None

    if action == "join" and "id" in form:
        id = toIntOrDefault(form["id"], -1)
        # lockGamesFile()
        # games = loadGamesFile()
        game = getGameByID(id)
        if game is None:
            # unlockGamesFile()
            return "lobby", None
        # can't join after the game is finished
        if game["isDone"]:
            # unlockGamesFile()
            return "view", id
        elif not username in game["players"]:
            game["players"].append(username)
            updateAttr(game, "players")
            # coll.update_one({"_id": game["_id"]}, {"$set": {"players": game["players"]})
            # saveGamesFile(games)
        # unlockGamesFile()
        if game["isStarted"]:
            return "play", id
        else:
            return "pregame", id

    if action == "start" and "id" in form:
        id = toIntOrDefault(form["id"], -1)
        # lockGamesFile()
        # games = loadGamesFile()
        game = getGameByID(id)
        if game is None:
            # unlockGamesFile()
            return "lobby", None
        players = game["players"]
        if len(players) > 0 and players[0] == username:
            # player is the host, start the game
            if not game["isStarted"]:
                game["isStarted"] = True
                game["timeStartedSeconds"] = int(time.time())
                coll.update_one({"_id": game["_id"]}, {"$set": {"isStarted": game["isStarted"], "timeStartedSeconds": game["timeStartedSeconds"]}})
                # saveGamesFile(games)
        # unlockGamesFile()
        if not game["isStarted"]:
            return "pregame", id
        elif not game["isDone"]:
            return "play", id
        else:
            return "view", id

    if action == "cancel" and "id" in form:
        id = toIntOrDefault(form["id"], -1)
        # lockGamesFile()
        # games = loadGamesFile()
        game = getGameByID(id)
        # works only in the pregame and play phases (i.e. isDone = False)
        if game is not None and not game["isDone"]:
            # remove this player from the game, promoting the next player to host.
            game["players"].remove(username)
            # if there are no more players, delete the game
            if len(game["players"]) == 0:
                games = deleteGameByID(id)
            else:
                updateAttr(game, "players")
            # saveGamesFile(games)
        # unlockGamesFile()
        return "lobby", None
    return "lobby", None


def histogram(games, maxGame, prop):
    hs = maxGame[prop]
    bins = []
    data = []
    if hs < 10:
        for i in range(int(hs)+1):
            bins.append(i)
            data.append(0)
    else:
        for i in range(1,10+1): #1 to 10 inclusive
            bins.append(round(i*hs)/10)
            data.append(0)
    
    for game in games:
        s = game[prop]
        for j in range(len(bins)):
            if round(s*10)/10 <= bins[j]:
                data[j] += 1
                break
    
    return {"labels": bins, "series": [data]}

def text2bool(text):
    return text.lower() == "true"

def get_param(form, kwargs, name, converter=lambda text: text, fallback=""):
    if name in form:
        kwargs[name] = converter(form[name])
    else:
        kwargs[name] = fallback

def load_page(form, page=None, id=None):
    if page is None:
        if "page" in form:
            page = filterWord(form["page"])
        else:
            page = "lobby"
    if not "username" in form and not page in ("stats", "json", "view"):
        page = "login"

    if id is None and "id" in form:
        id = toIntOrDefault(form["id"], -1)
    
    if "prev" in form:
        prev = filterWord(form["prev"])
    else:
        prev = "lobby"

    kwargs = {
        "nav": nav,
        "active": "boggle",
        "stylesheet": "boggle",
        "formMethod": "get",
        "id": id
    }


    if page == "login":
        return render_template("boggle/login.html", page=page, **kwargs)

    get_param(form, kwargs, "username", filterUsername)

    if page == "pregame" and id is not None:
        return render_template("boggle/pregame.html", **kwargs)

    if page == "play" and id is not None:
        return render_template("boggle/play.html", **kwargs)

    if page == "view" and id is not None:
        return render_template("boggle/view.html", prev=prev, **kwargs)

    if page == "stats":
        games = getAllGames()
        games = [game for game in games if game["isArchived"]]

        #exclude games that were from BoggleCV from the highscore
        games = [game for game in games if not ("wasImported" in game and game["wasImported"])]

        get_param(form, kwargs, "selectedUsed", text2bool, False)
        
        if kwargs["selectedUsed"]:
            fallback = ""
        else:
            fallback = "checked"

        showLabels = [
            "show2x2", "show3x3", "show4x4", "show5x5", "show6x6", "show7x7", "showOtherSizes",
            "show2L", "show3L", "show4L", "show5L", "show6L", "show7L", "showOtherL",
            "show30Sec", "show1Min", "show2Min", "show3Min", "show4Min", "show5Min",
            "show6Min", "show7Min", "show8Min", "show9Min", "show10Min", "showOtherMin"
        ]

        labelBool = {}
        numChecked = 0
        for label in showLabels:
            get_param(form, kwargs, label, lambda text: "checked" if text == "on" else "", fallback)
            labelBool[label] = kwargs[label] == "checked"
            if labelBool[label]:
                numChecked += 1

        kwargs["selectAll"] = "checked" if numChecked >= len(labelBool)/2 else ""
        # print(labelBool)

        gamesFiltered = {}
        gamesFiltered["5x5"] = [game for game in games if game["size"] == 5 and game["letters"] == 4 and game["minutes"] == 3]
        gamesFiltered["4x4"] = [game for game in games if game["size"] == 4 and game["letters"] == 3 and game["minutes"] == 3]
        gamesFiltered["Sel"] = []
        for game in games:
            if (
                    labelBool["show2x2"] and game["size"] == 2 or
                    labelBool["show3x3"] and game["size"] == 3 or
                    labelBool["show4x4"] and game["size"] == 4 or
                    labelBool["show5x5"] and game["size"] == 5 or
                    labelBool["show6x6"] and game["size"] == 6 or
                    labelBool["show7x7"] and game["size"] == 7 or
                    labelBool["showOtherSizes"] and not game["size"] in [2,3,4,5,6,7]
                ) and (
                    labelBool["show2L"] and game["letters"] == 2 or
                    labelBool["show3L"] and game["letters"] == 3 or
                    labelBool["show4L"] and game["letters"] == 4 or
                    labelBool["show5L"] and game["letters"] == 5 or
                    labelBool["show6L"] and game["letters"] == 6 or
                    labelBool["show7L"] and game["letters"] == 7 or
                    labelBool["showOtherL"] and not game["letters"] in [2,3,4,5,6,7]
                ) and (
                    labelBool["show30Sec"] and game["minutes"] == 0.5 or
                    labelBool["show1Min"] and game["minutes"] == 1 or
                    labelBool["show2Min"] and game["minutes"] == 2 or
                    labelBool["show3Min"] and game["minutes"] == 3 or
                    labelBool["show4Min"] and game["minutes"] == 4 or
                    labelBool["show5Min"] and game["minutes"] == 5 or
                    labelBool["show6Min"] and game["minutes"] == 6 or
                    labelBool["show7Min"] and game["minutes"] == 7 or
                    labelBool["show8Min"] and game["minutes"] == 8 or
                    labelBool["show9Min"] and game["minutes"] == 9 or
                    labelBool["show10Min"] and game["minutes"] == 10 or
                    labelBool["showOtherMin"] and not game["minutes"] in [0.5,1,2,3,4,5,6,7,8,9,10]
                ):
                gamesFiltered["Sel"].append(game)
        gamesFiltered["All"] = games

        for gameType in ["5x5", "4x4", "Sel", "All"]:
            kwargs["has"+gameType] = (len(gamesFiltered[gameType]) > 0)
            if kwargs["has"+gameType]:
                for prop in ["winScore", "numWordsPlayersFound", "percentFound", "maxScore", "maxWords", "duplicates", "secondsToSolve"]:
                    kwargs[prop+gameType] = reduce(lambda prev, curr: prev if prev[prop] > curr[prop] else curr, gamesFiltered[gameType]) 
                for prop in ["maxScore", "maxWords"]:
                    kwargs["low_"+prop+gameType] = reduce(lambda prev, curr: prev if prev[prop] < curr[prop] else curr, gamesFiltered[gameType])

        get_param(form, kwargs, "sortCol", int, 0)
        get_param(form, kwargs, "isAscending", text2bool, False)

        #these correspond to the columns on the stats page
        lambdas = [
            lambda game: game["_id"],
            lambda game: game["players"][0].lower(),
            lambda game: len(game["players"]),
            lambda game: game["size"],
            lambda game: game["letters"],
            lambda game: game["minutes"],
            lambda game: game["maxWords"],
            lambda game: game["numWordsPlayersFound"],
            lambda game: game["percentFound"],
            lambda game: game["maxScore"],
            lambda game: game["winScore"],
            lambda game: game["winners"][0].lower(),
        ]
        
        if kwargs["hasSel"]:
            for prop in ["winScore", "maxScore", "numWordsPlayersFound", "maxWords", "percentFound", "duplicates", "secondsToSolve"]:
                kwargs[prop+"Chart"] = histogram(gamesFiltered["Sel"], kwargs[prop+"Sel"], prop)

            kwargs["sizeChart"] = histogram(gamesFiltered["Sel"], {"size": 7}, "size")
            print(kwargs["sizeChart"])
            kwargs["sizeChart"]["labels"] = ["2x2", "3x3", "4x4", "5x5", "6x6", "7x7"]
            kwargs["sizeChart"]["series"][0] = kwargs["sizeChart"]["series"][0][2:] #remove 1st 2 items
            print(kwargs["sizeChart"])

        games_sorted = sorted(gamesFiltered["Sel"], key=lambdas[kwargs["sortCol"]], reverse=(not kwargs["isAscending"]))

        # print(kwargs)
        return render_template("boggle/stats.html", prev=prev, games=games_sorted, **kwargs)

    if page == "json":
        return json.dumps([x for x in coll.find()], indent=2)
    
    if page == "upload":
        return render_template("boggle/upload.html", **kwargs)

    if page == "lobby":
        return render_template("boggle/lobby.html", remove_completed_from_lobby_timeout=REMOVE_COMPLETED_FROM_LOBBY_TIMEOUT, **kwargs)

    return "error with page '" + str(page) + "'"


"""
This method processes all requests coming in, and passes
them to the correct function.

There are 3 important fields:
action - an action to be taken before loading the page,
    such as leaving a game or submitting a list of words
page - the page to be returned, such as the login, lobby,
    pregame, play, and stats pages
request - ask for a certain type of data, such as current
    games, past games, data on 1 particular game, or word
    definition. This overrides the page, so if a request,
    page, and action are all specified, the server will
    do the action, then return the requested data,
    ignoring the page variable.
"""
def app(request):
    # form = request.args | request.form
    form = {}
    for k, v in request.args.items():
        form[k] = v
    for k, v in request.form.items():
        form[k] = v

    print("boggle http request:", form)
    page = None
    id = None
    if "action" in form and "username" in form:
        page, id = do_action(form)
    
    if "request" in form:
        return request_data(form, request.files)
    else:
        return load_page(form, page, id)