/
player_utils.py
290 lines (254 loc) · 10.1 KB
/
player_utils.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
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
import logging
from operator import itemgetter
from google.appengine.ext import ndb
from models import PlayerModel, MatchModel
from utils import calculate_winner, number_to_word
def update_player_name(key, fname, lname):
user = PlayerModel.get_by_id(key)
user.first_name = fname
user.last_name = lname
user.put()
def compare_players(user, opponent):
pass
def get_most_games(type="total", top=5):
"""
Gets the top X number of people with the chosen type of game
@param type: Which type of games to calculate. The default is "total", which
will just get all games. Accepted inputs: "wins" and "losses"
@param top: Number of people to collect
"""
players = PlayerModel.query()
player_games = []
for player in players:
email = player.key.id()
games = 0
new_player = {
"email":email,
"data":0,
"full_name":get_player_full_name(email)
}
if type == "wins":
games = player.gamesWon
elif type == "losses":
games = player.gamesPlayed - player.gamesWon
elif type == "total":
games = player.gamesPlayed
new_player['data'] = games
player_games.append(new_player)
sorted_players = sorted(player_games, key=itemgetter('data'))
if len(sorted_players) < top:
top = len(sorted_players)
return sorted_players[len(sorted_players)-top:]
def get_highest_goals(against=False, top=5):
"""
Pass in a boolean to tell the method to calculate goals against instead of
the default which is goals for.
@return: a list of dictionaries containing the top however many people with
the highest goals
@param against: Pass True if you want this to count goals against
@param top: Pass in the number of "top" people you want (e.g. Top 5)
"""
players = PlayerModel.query()
player_goals = []
for player in players:
email = player.key.id()
matches = MatchModel.query(ndb.OR(MatchModel.player1 == email,
MatchModel.player2 == email))
goals = 0
new_player = {
'email': email,
'data': 0,
'full_name': get_player_full_name(email)
}
for match in matches:
if against:
if match.player1 == email:
goals += match.scores[0].player2
goals += match.scores[1].player2
goals += match.scores[2].player2
elif match.player2 == email:
goals += match.scores[0].player1
goals += match.scores[1].player1
goals += match.scores[2].player1
elif not against:
if match.player1 == email:
goals += match.scores[0].player1
goals += match.scores[1].player1
goals += match.scores[2].player1
elif match.player2 == email:
goals += match.scores[0].player2
goals += match.scores[1].player2
goals += match.scores[2].player2
new_player["data"] += goals
player_goals.append(new_player)
sorted_players = sorted(player_goals, key=itemgetter('data'))
if len(sorted_players) < top:
top = len(sorted_players)
return sorted_players[len(sorted_players)-top:]
def get_player_full_name(email):
"""
Returns full name like "first last"
@param email: Email/key of the person who's name you want
"""
user = PlayerModel.get_by_id(email)
return user.first_name + " " + user.last_name
def calc_player_info(player, key):
""" Returns information for the player viewing page """
name = player.first_name
last = player.last_name
skill = player.skillScore
total_wins = player.gamesWon
games = player.gamesPlayed
total_losses = games - total_wins
email = key.pairs()[0][1]
qry = MatchModel.query(ndb.OR(MatchModel.player1 == email,
MatchModel.player2 == email))
match_results = qry.order(-MatchModel.gameDate).fetch(5)
last_five_wins = 0
last_five_losses = 0
for result in match_results:
wins = 0
losses = 0
for i in range(0, len(result.scores)):
winner = calculate_winner(result.scores[i].player1, result.scores[i].player2)
if winner == "player1":
if result.player1 == email:
wins += 1
elif result.player2 == email:
losses += 1
elif winner == "player2":
if result.player1 == email:
losses += 1
elif result.player2 == email:
wins += 1
if wins > losses:
last_five_wins += 1
elif losses > wins:
last_five_losses += 1
total_games = number_to_word(last_five_wins + last_five_losses)
last_five_games = {"wins" : last_five_wins,
"losses" : last_five_losses,
"total" : total_games}
try:
win_ratio = round(((total_wins / (games * 1.0)) * 100), 2)
except ZeroDivisionError:
win_ratio = 0
qry = MatchModel.query(ndb.OR(MatchModel.player1 == email,
MatchModel.player2 == email))
match_results = qry.order(-MatchModel.gameDate).fetch()
streak_wins = 0
streak_losses = 0
first = True
last_result = "wins"
final_result = ""
for result in match_results:
wins = 0
losses = 0
if first:
for i in range(0, len(result.scores)):
winner = calculate_winner(result.scores[i].player1, result.scores[i].player2)
if winner == "player1":
if result.player1 == email:
wins += 1
elif result.player2 == email:
losses += 1
elif winner == "player2":
if result.player1 == email:
losses += 1
elif result.player2 == email:
wins += 1
if wins > losses:
last_result = "wins"
streak_wins += 1
elif losses > wins:
last_result = "losses"
streak_losses += 1
first = False
else:
for i in range(0, len(result.scores)):
winner = calculate_winner(result.scores[i].player1, result.scores[i].player2)
if winner == "player1":
if result.player1 == email:
wins += 1
elif result.player2 == email:
losses += 1
elif winner == "player2":
if result.player1 == email:
losses += 1
elif result.player2 == email:
wins += 1
if wins > losses:
if last_result == "wins":
streak_wins += 1
else:
final_result = "Currently on a {} game losing streak".format(number_to_word(streak_losses))
break
elif losses > wins:
if last_result == "losses":
streak_losses += 1
else:
final_result = "Currently on a {} game winning streak".format(number_to_word(streak_wins))
break
else:
if len(match_results) == 0:
final_result = "Hasn't played any games yet!"
if final_result == "":
#Need to calculate it
if last_result == "wins":
final_result = "Currently on a {} game winning streak".format(number_to_word(streak_wins))
elif last_result == "losses":
final_result = "Currently on a {} game losing streak".format(number_to_word(streak_losses))
player = {
"email": email,
"name": name,
"last": last,
"skill": skill,
"wins": total_wins,
"losses": total_losses,
"games": games,
"win_ratio": win_ratio,
"last_five_games": last_five_games,
"streak": final_result
}
return player
def match_history(email):
"""
"""
qry = MatchModel.query(ndb.OR(MatchModel.player1 == email,
MatchModel.player2 == email))
match_results = qry.order(-MatchModel.gameDate).fetch()
history = []
count = 0
for result in match_results:
count += 1
wins = 0
losses = 0
for i in range(0, len(result.scores)):
winner = calculate_winner(result.scores[i].player1, result.scores[i].player2)
if winner == "player1":
if result.player1 == email:
wins += 1
elif result.player2 == email:
losses += 1
elif winner == "player2":
if result.player1 == email:
losses += 1
elif result.player2 == email:
wins += 1
if result.player1 == email:
scores = str(result.scores[0].player1) + "-" + str(result.scores[0].player2) + ", " \
+ str(result.scores[1].player1) + "-" + str(result.scores[1].player2) + ", " \
+ str(result.scores[2].player1) + "-" + str(result.scores[2].player2)
else:
scores = str(result.scores[0].player2) + "-" + str(result.scores[0].player1) + ", " \
+ str(result.scores[1].player2) + "-" + str(result.scores[1].player1) + ", " \
+ str(result.scores[2].player2) + "-" + str(result.scores[2].player1)
if wins > losses:
history.append([count, str(result.gameDate.strftime('%h %d, %Y @ %I:%S')), \
True, scores, get_player_full_name(result.player1), \
get_player_full_name(result.player2)])
elif losses > wins:
history.append([count, str(result.gameDate.strftime('%h %d, %Y @ %I:%S')), \
False, scores, get_player_full_name(result.player1), \
get_player_full_name(result.player2)])
return history