-
Notifications
You must be signed in to change notification settings - Fork 0
/
elo_ball_app.py
450 lines (366 loc) · 14.6 KB
/
elo_ball_app.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
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
from flask import Flask, jsonify, request, make_response
from passwords import MYSQL_PASSWORD
import config
from peewee import MySQLDatabase, Model, CharField
from json import loads, dumps
from datetime import datetime, timedelta
from itertools import chain
import requests as r
app = Flask(__name__)
### Database conenctions and things
db_creds = {'host':'yaiir.mysql.pythonanywhere-services.com',
'user':"yaiir",
'passwd':MYSQL_PASSWORD,
'database':config.db_name}
db = MySQLDatabase(**db_creds)
@app.before_request
def _db_connect():
db.connect()
@app.teardown_request
def _db_close(exc):
if not db.is_closed():
db.close()
class BaseModel(Model):
class Meta:
database = db
class Games(BaseModel):
result = CharField()
account_id = CharField()
### API stuff
class GameError(Exception):
pass
class GameList(object):
def __init__(self, days=None):
self.games = self.get_all_games(days)
def get_all_games(self, days):
all_games = Games.select()
out = []
for row in all_games:
result = loads(row.result)
result['id'] = row.id
out += [result]
out.sort(key=lambda x: x['timestamp'])
if days:
date_ago = datetime.now().date() - timedelta(days=int(days))
date_ago_iso = date_ago.isoformat()
filtered = []
for game in out:
if game['timestamp'] > date_ago_iso:
filtered += [game]
out = filtered
return out
class PlayerList(object):
def __init__(self, games):
self.games = games
player_set = chain(*[game['winners'] + game['losers'] for game in games.games])
self.players = {player:dict() for player in player_set}
self.games_list = self._games_list()
def _games_list(self):
games_list = []
for game in self.games.games:
games_list += [[game['winners'], game['losers'], game['timestamp']]]
return games_list
def add_records(self):
for player in self.players:
self.players[player]['record'] = {'wins':0, 'losses':0}
for winners, losers, timestamp in self.games_list:
for player in winners:
self.players[player]['record']['wins'] += 1
for player in losers:
self.players[player]['record']['losses'] += 1
return self
def _calc_winner_change(self, sum_winners_elo, sum_losers_elo):
Rw = 10 ** (sum_winners_elo/400)
Rl = 10 ** (sum_losers_elo/400)
Ew = Rw / (Rw + Rl)
winner_change = 32 * (1 - Ew)
return winner_change
def add_elo(self):
for player in self.players:
self.players[player]['elo'] = {'current':1500, 'history':[]}
for winners, losers, timestamp in self.games_list:
winners_av = sum([int(self.players[winner]['elo']['current']) for winner in winners])/len(winners)
losers_av = sum([int(self.players[loser]['elo']['current']) for loser in losers])/len(losers)
winner_gain = self._calc_winner_change(winners_av, losers_av)
for winner in winners:
self.players[winner]['elo']['current'] += winner_gain
self.players[winner]['elo']['history'] += [(timestamp, self.players[winner]['elo']['current'] )]
for loser in losers:
self.players[loser]['elo']['current'] -= winner_gain
self.players[loser]['elo']['history'] += [(timestamp, self.players[loser]['elo']['current'] )]
return self
def ready_for_returning(self):
out = []
players = self.players
for player in players:
info = players[player]
info['id'] = player
out += [info]
return out
class SingleGame(object):
def __init__(self, game):
self.validated_game(game)
self.game = game
def validated_game(self, body):
all_players = body['winners'] + body['losers']
no_duplicates = (len(set(all_players)) == len(all_players))
all_teams = all([body['winners'], body['losers']])
if (no_duplicates and all_teams):
pass
else:
raise GameError
def prep_create_game(self):
try:
self.game['timestamp']
except KeyError:
self.game['timestamp'] = datetime.now().isoformat()
prepped_game = {'result':dumps(self.game), 'account_id':'default'}
return prepped_game
def create(self):
prepped_game = self.prep_create_game()
self.created_game = Games.create(**prepped_game)
return self
def get_days_from_req(request):
return request.args.get('days')
@app.route('/')
def hello_world():
return 'Howdy from Flask staging'
@app.route('/games', methods=['GET', 'POST'])
def games():
if request.method == 'POST':
body = request.get_json()
try:
SingleGame(body).create()
return jsonify(GameList().games)
except GameError:
return make_response(jsonify({'error':'check the teams reported'}), 400)
except:
return make_response(jsonify({'error':'server fuckup'}), 500)
else:
days = get_days_from_req(request)
return jsonify(GameList(days).games)
@app.route('/games/<game_id>', methods=['DELETE'])
def delete_games(game_id):
# TODO move this into SingleGame(), can then return metadata of deleted game
Games.get( Games.id == game_id ).delete_instance()
return jsonify(GameList().games)
@app.route('/players', methods=['GET'])
def get_players():
days = get_days_from_req(request)
game_list = GameList(days)
player_list = PlayerList(game_list).add_records().add_elo().ready_for_returning()
return jsonify(player_list)
##### SLACK INTEGRATION - should be separate but keeping together for python anywhere limitations
from re import findall, sub
from tabulate import tabulate
import pandas as pd
class SlackSingleGame(object):
def __init__(self, winners, losers, timestamp, game_id=None):
self.winners = winners
self.losers = losers
self.timestamp = timestamp
self.game_id = game_id
def _dictify(self):
out = {
'winners': self.winners,
'losers': self.losers,
'timestamp': self.timestamp,
'id': self.game_id
}
return out
def create(self):
r.post(config.base_url + '/games', json=self._dictify())
return self
def delete(self):
r.delete(config.base_url + '/games/{}'.format(self.game_id))
return self
def pprint(self):
return self._dictify()
class SlackPlayerList(object):
def __init__(self, days=None):
query_url = config.base_url + '/players'
if days:
query_url = config.base_url + '/players?days={}'.format(days)
raw = r.get(query_url).json()
raw.sort(key=lambda x: x['record']['losses'])
raw.sort(key=lambda x: x['record']['wins'], reverse=True)
raw.sort(key=lambda x: x['elo']['current'], reverse=True)
self.players = raw
def filter_player_list(self, user_ids):
return [row for row in self.players if (row['id'] in user_ids)]
def _prep_pprint(self, users=None):
out = [['Player', 'Elo', 'Diff', 'Wins', 'Losses']]
for record in self.players:
name = self._replace_mentions_with_username(record['id'])
elo, wins, losses = round(record['elo']['current']), record['record']['wins'], record['record']['losses']
try:
if record['id'] in users:
last_game_ts = record['elo']['history'][-1][0]
history = record['elo']['history']
first_game_of_set = min([i for i, (ts, elo) in enumerate(history) if (ts == last_game_ts)])
prev_game_ts, prev_game_elo = record['elo']['history'][first_game_of_set - 1]
diff = elo - prev_game_elo
if prev_game_ts == last_game_ts:
diff = elo - 1500
diff = round(diff)
else:
diff = 0
except TypeError:
diff = 0
out += [[name, elo, diff, wins, losses]]
return out
def pprint(self, users=None):
out = self._prep_pprint(users)
tabulated = tabulate(out, tablefmt='simple', headers='firstrow')
out = {
"response_type": "in_channel",
"text": '```{}```'.format(tabulated)
}
return out
def _replace_mentions_with_username(self, text):
return sub('\<(.*?)\|', '', text).replace('>', '')
class SlackGameList(object):
def __init__(self):
self.raw_games = r.get(config.base_url + '/games').json()
self.raw_games.sort(key=lambda x: x['timestamp'], reverse=True)
self.raw_games = self.raw_games[:30]
self.games = [SlackSingleGame(game['winners'], game['losers'], game['timestamp'], game['id']) for game in self.raw_games]
def delete(self, game_id):
for game in self.games:
if str(game.game_id) == str(game_id):
game.delete()
return game.pprint()
def pprint(self, text='all the games'):
attachments = []
for game in self.games:
attachments += [{
"title": '{} beat {} at {}'.format(game.winners, game.losers, game.timestamp),
"callback_id": "delete_game",
"attachment_type": "default",
"actions": [
{
"name": "delete",
"text": "Delete Game",
"type": "button",
"value": game.game_id
}
]
}]
out = {
"text": 'all the games',
"response_type": "in_channel",
"attachments": attachments
}
return out
class SlackChanges(object):
def __init__(self):
raw_data = r.get(config.base_url + '/players').json()
df = pd.DataFrame(raw_data)
def munge_history(row):
hist = row['history']
hist_dict = {time: elo for time, elo in hist}
return hist_dict
df ['history'] = df['elo'].apply(munge_history)
df = df.set_index('id')
df = pd.DataFrame({name: df.loc[name]['history'] for name in df.index}).sort_index()
df = df.fillna(value=1500, limit=1)
df = df.fillna(method='ffill')
df.index = pd.to_datetime(df.index)
tg = pd.Grouper(freq='w')
df = df.groupby(tg).last().sort_index(ascending=False)
df.index = df.index.strftime(date_format = '%Y-%m-%d')
self.df = df
def _prep_pprint(self):
df = self.df
df = df.diff(-1).head(6).round(1).transpose().sort_values(df.index[0], ascending=False)
df = df.head(3).append(df.tail(3))
df['Player'] = df.index
df['Player'] = df['Player'].apply(lambda x: self._replace_mentions_with_username(x))
df = df[['Player'] + df.columns.tolist()[:-1]]
headers = ['Player', 'Current'] + list(df.columns)[2:]
table = df.values.tolist()
return [headers] + table
def _replace_mentions_with_username(self, text):
return sub('\<(.*?)\|', '', text).replace('>', '')
def pprint(self):
out = self._prep_pprint()
tabulated = tabulate(out, tablefmt='simple', headers='firstrow')
out = {
"response_type": "in_channel",
"text": '```{}```'.format(tabulated)
}
return out
class SlackCommand(object):
def __init__(self, request):
self.request = request
self.raw_text = request.form['text']
self.cleaned_text = request.form['text'].replace(' ', '').replace('beat', '1-0')
self.com_type = self._calc_type()
self.users = self._extract_all_users_from_text(self.cleaned_text)
if self.com_type == 'report':
self.report = self._clean_report()
def _calc_type(self):
if '-' in self.cleaned_text:
return 'report'
elif 'game' in self.cleaned_text:
return 'gamelist'
elif 'changes' in self.cleaned_text:
return 'changes'
else:
self._set_result_days()
return 'playerlist'
def _set_result_days(self):
if self.cleaned_text == '':
self.result_days = 30
elif self.cleaned_text == 'all':
self.result_days = None
else:
try:
self.result_days = int(self.cleaned_text)
except ValueError:
self.result_days = 30 # Default days in Slack
def _clean_report(self):
a, b = self.cleaned_text.split('-')
team_a, wins_a = a[:-1], int(a[-1])
team_b, wins_b = b[1:], int(b[0])
out = {
'a':{'members':self._extract_all_users_from_text(team_a), 'wins':wins_a},
'b':{'members':self._extract_all_users_from_text(team_b), 'wins':wins_b}
}
return out
def _extract_all_users_from_text(self, text):
list_of_users = findall('\<(.*?)\>', text)
list_with_tags = ['<{}>'.format(user) for user in list_of_users]
return list_with_tags
def create(self):
report = self.report
timestamp = datetime.now().isoformat()
for _ in range(report['a']['wins']):
winners, losers = report['a']['members'], report['b']['members']
SlackSingleGame(winners, losers, timestamp).create()
for _ in range(report['b']['wins']):
winners, losers = report['b']['members'], report['a']['members']
SlackSingleGame(winners, losers, timestamp).create()
return self
@app.route("/slack", methods=['POST'])
def slack():
command = SlackCommand(request)
if command.com_type == 'report':
command.create()
out = SlackPlayerList(days=30).pprint(command.users)
return jsonify(out)
if command.com_type == 'gamelist':
out = SlackGameList().pprint()
return jsonify(out)
if command.com_type == 'changes':
out = SlackChanges().pprint()
return jsonify(out)
if command.com_type == 'playerlist':
out = SlackPlayerList(days=command.result_days).pprint()
return jsonify(out)
@app.route("/slack/actions", methods=['GET', 'POST'])
def slack_action():
payload = loads(request.form['payload'])
game_id = payload['actions'][0]['value']
deleted_metadata = SlackGameList().delete(game_id)
return jsonify({'deleted game':deleted_metadata})