-
Notifications
You must be signed in to change notification settings - Fork 1
/
games.py
131 lines (108 loc) · 3.77 KB
/
games.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
from datetime import datetime
import random
from flask import render_template, json, g, session, abort, request, flash, redirect, url_for
import bleach
from application import app
from ranking import player2_won
from data.games import save_game
from data.challenges import link_challenge_to_game
from data.shouts import save_shout
@app.route('/games')
def show_games():
return render_template(
'show_games.html',
games=list(reversed(g.games)),
players=g.ranking,
swaps=g.swaps,
)
@app.route('/games/data')
def show_game_data_json():
return json.jsonify(game_details=g.game_details)
@app.route('/games/<player>')
def show_games_for_player(player):
return render_template(
'show_games.html',
games=[
game for game
in list(reversed(g.games))
if game.challenger.name == player
or game.defender.name == player
],
players=g.ranking,
swaps=g.swaps,
filtered_player=player,
)
@app.route('/games/<player>/vs/<other_player>')
def show_games_for_players(player, other_player):
return render_template(
'show_games.html',
games=[
game for game
in list(reversed(g.games))
if game.challenger.name in[player, other_player]
and game.defender.name in [player, other_player]
],
players=g.ranking,
swaps=g.swaps,
filtered_player=player,
filtered_player2=other_player,
)
@app.route('/game/add', methods=['POST'])
def add_game():
if not session.get('logged_in'):
abort(401)
if request.form['player1'] not in g.ranking:
abort(401)
if request.form['player2'] not in g.ranking:
abort(401)
scores = (
(request.form['player1_score1'], request.form['player2_score1']),
(request.form['player1_score2'], request.form['player2_score2']),
(request.form['player1_score3'], request.form['player2_score3']),
)
for game in scores:
for score in game:
if len(score) > 0 and not score.isnumeric():
abort(401)
comment = bleach.clean(
request.form.get('comment', ''),
tags=app.config['ALLOWED_TAGS']['game_comment'],
attributes=app.config['ALLOWED_ATTRS']['game_comment'],
)
game = save_game(
request.form['player1'],
request.form['player2'],
scores,
comment,
date=datetime.utcnow()
)
flash("Game result was saved")
link_challenge_to_game(game)
flash("Open challenge (if any) was removed")
challenger_lost = player2_won(
[scores[0][0], scores[1][0], scores[2][0]],
[scores[0][1], scores[1][1], scores[2][1]]
)
if challenger_lost:
winner = game.defender
shout_message = '<b>{player1}</b> could not win from {nick} <b>{player2}</b> {score[0][0]}-{score[0][1]} {score[1][0]}-{score[1][1]} {score[2][0]}{dash}{score[2][1]}{comment}'
else:
winner = game.challenger
shout_message = '{nick}<b>{player1}</b> beat <b>{player2}</b> {score[0][0]}-{score[0][1]} {score[1][0]}-{score[1][1]} {score[2][0]}{dash}{score[2][1]}{comment}'
if len(comment) > 0:
comment = '<div class="gamecomment">{comment}</div>'.format(comment=comment)
# pick one of the winner's nicknames
nickname = ''
if any(winner.tags):
nickname = " '<span class=\"tag\">{random_tag}</span>' ".format(
random_tag=random.choice(winner.tags).tag
)
save_shout(None, shout_message.format(
player1=request.form['player1'],
player2=request.form['player2'],
nick=nickname,
score=scores,
dash='-' if request.form['player1_score3'] else '',
comment=comment
))
return redirect(url_for('show_games'))