-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
82 lines (66 loc) · 1.8 KB
/
game.py
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
import random
import api
import math
import changetext
import operator
maxRound = 5
currentRound = -1
record = {}
randomPhotos = []
currentTags = []
def setUpGame():
global randomPhotos
allPhotos = api.getAllPaths()
randomIndices = []
while(len(randomIndices) < maxRound):
index = random.randrange(len(allPhotos))
if index not in randomIndices:
randomIndices.append(index)
randomPhotos.append(allPhotos[index])
def addUser(number,username):
global record
record[number] = {}
record[number]["lastSuccessfulRound"] = 0
record[number]["score"] = 0
record[number]["username"] = username
def nextRound():
#send time request to flask server
global randomPhotos
global currentRound
global currentTags
changetext.changePath(randomPhotos[currentRound])
currentRound +=1
currentTags = api.getAllTags(randomPhotos[currentRound])
# print(randomPhotos[currentRound],currentTags)
def endRound():
changetext.changeText(getTops())
def makeGuess(number,guess):
global currentRound
global record
if currentRound > record.get(number).get("lastSuccessfulRound"):
index = isGood(guess)
if (index >= 0):
record[number]["score"] += getScore(index)
record[number]["lastSuccessfulRound"] = currentRound
def getTops():
global record
newDict = {}
for user in record:
newDict[record[user]["username"]] = record[user]["score"]
sortedRanking = sorted(newDict.items(), key=operator.itemgetter(1))
return sortedRanking[:10]
def isGood(guess):
global currentTags
for i in range(5):
if guess.lower() == currentTags[i].lower(): return i
return -1
def getScore(index):
return math.floor(3-(index/2.0))
# setUpGame()
# for i in range(5):
# addUser(i,"Urmom{}".format(i))
# while(currentRound < maxRound-1):
# nextRound()
# makeGuess(0,"sea")
# for user in record.keys():
# print(user, record[user]["score"])