-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.py
179 lines (147 loc) · 5.53 KB
/
match.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
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
from selenium import webdriver as wd
import requests, threading, service as srv, player
class Match:
# CONSTANTS
ONLINE_SERVER = "http://127.0.0.1:5000"
THIS_SERVER = "http://192.168.0.65:5000"
FIREBASE_SERVER_KEY = "AAAAejrw0Vc:APA91bH-UEiG0Gl9TnLUUjIw44ps3ctL7tYpoEfZ0pqpPqbyo26bgMrmzgZ_wpfs1bGojbezj1qna" \
"YJ3_WmiCZBCpkF2QiSfETuc4afOG6E3bllxULSL9qE9nqwcuybaB2whGisOtJeK"
FIREBASE_URL = "https://fcm.googleapis.com/fcm/send"
MAX_PLAYERS = 8
# GAME INFO
players = dict()
player_queue = list()
player_turn = list()
played_chal = list([1,3,4])
number_played_challenge = 0
quiz = list()
admin = ''
current_categ = ""
current_chal = dict()
current_trivia = dict()
active = False
voice_hz = dict()
def __init__(self):
# INIT SELENIUM
self.driver = wd.Firefox()
self.driver.maximize_window()
threading.Thread(target=srv.homePage, args=(self.driver, self.THIS_SERVER)).start()
def newPlayer(self, new_player, token):
if len(self.players) < self.MAX_PLAYERS:
p = player.Player(new_player,token)
self.players[new_player] = p
return True
return False
def setAdmin(self, admin):
self.admin = str(admin)
def updateTrivia(self, info):
if info is None:
self.current_trivia = dict()
self.quiz = list()
else:
self.current_trivia = dict(info)
self.quiz.append(info['q_id'])
def updateChallenge(self, challenge):
self.current_chal = dict(challenge)
self.current_chal['active'] = False
self.number_played_challenge = self.number_played_challenge + 1
# Choose Randomly who have to play the challenge
def playerTurn(self, number):
flag = 1
if len(self.players) < number:
return False
elif len(self.player_queue) < number:
self.player_queue = srv.randomize(self.getPlayersName())
flag = -1
self.player_turn = list()
for i in range(0,number):
self.player_turn.append(self.player_queue.pop())
return flag
def setActiveMatch(self, status):
if (status is True) or (status is False):
self.active = status
# Check if an user exist among the current players
def containsPlayer(self, player):
tmp = str.lower(player)
for p in self.players:
if str.lower(self.players[p].getName()) == tmp:
return True
return False
# Send a notification to every user (except who clicked the button for the next challenge) in order to update
# mobile activity
def sendNotifications(self, players=list(), title="refresh", body="challenge"):
all = False
if len(players) == 0:
all = True
headers = {'Authorization': 'key=' + self.FIREBASE_SERVER_KEY, 'Content-Type': 'application/json'}
res = list()
if all:
for p in self.players:
fields = {'to': self.players[p].getToken(),
'notification': {
'title': title,
'body': body
}
}
res.append(requests.post(self.FIREBASE_URL, headers=headers, json=fields).text)
else:
for p in players:
fields = {'to': self.players[p].getToken(),
'notification': {
'title': title,
'body': body
}
}
res.append(requests.post(self.FIREBASE_URL, headers=headers, json=fields).text)
print(res)
return res
def getToken(self, player):
return str(self.players[player].getToken())
def setCategory(self, category):
self.current_categ = category
def getCategory(self):
return self.current_categ
def isActive(self, dictionary):
return dictionary['active']
def setActive(self, dictionary, bool):
dictionary['active'] = bool
def voiceHzResult(self, user, given_frequency):
self.voice_hz[user] = given_frequency
def getVoiceHzResult(self, key):
if key in self.voice_hz:
return self.voice_hz[key]
return -1
def getPlayersName(self):
"""players = list()
for p in self.players:
players.append(self.players[p].getName())"""
return list(self.players)
def getCurrentIdChal(self):
if "id" in self.current_chal:
return self.current_chal['id']
return -1
def getPlayer(self, name):
return self.players[name]
def updateScores(self):
for p in self.players:
self.players[p].updateScore()
def getPlayersScore(self):
scores = dict()
for p in self.players:
scores[self.players[p].getName()] = [self.players[p].getScore(), self.players[p].getCurrentScore()]
return scores
def sortedPlayerByScore(self):
sortedlist = list(self.players.values())
sortedlist.sort(key=lambda x: x.getScore(), reverse=True)
return sortedlist
if __name__ == '__main__':
match = Match()
"""match.newPlayer("Enzham")
match.newPlayer("Syrien")
match.newPlayer("Giangio")
match.setAdmin("Enzham")
match.playerTurn(2)
print(match.player_queue)"""
match.newPlayer("lorry03", "token")
match.newPlayer("syrien95", "token")
print(match.getPlayersScore())