-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
242 lines (228 loc) · 10.3 KB
/
server.py
File metadata and controls
242 lines (228 loc) · 10.3 KB
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
import flask
from flask import Flask, request
from db import DB, DeclineTransaction
from utils import get_match_uuid
import tbt_config
import uuid
import typing
import core
import utils
#FIXME: game_scoring.html 在手機上面會超出螢幕
#FIXME: game_scoring.html 局數偶數時,發球顯示錯誤。 (應該是 JS 的問題)
#TODO: set knockout to imcomplete
QR_CODE_PAGE = ['ranking', 'new_match', 'register', 'groups', 'schedules']
app = Flask(__name__)
def get_db() -> DB:
if not hasattr(flask.g, 'db'):
flask.g.db = DB()
return flask.g.db
def render_template(template_name: str, **context: typing.Any):
return flask.render_template(template_name,
competition_title=tbt_config.COMPETITION_TITLE,
**context)
@app.route('/schedules')
def schedules():
db = get_db()
schedules = db.load('schedules', load_last=True)[0].get('matches', [])
return render_template('schedules.html', schedules=schedules)
@app.route('/groups')
def groups():
if tbt_config.COMPETITION_FORMAT != "group":
return "目前賽制不是循環賽"
db = get_db()
groups = db.load('groups', load_last=True)[0].get('groups', [])
return render_template('groups.html', groups=groups)
@app.route('/ranking')
def ranking():
db = get_db()
is_ranking_empty = db.is_empty('ranking')
if not is_ranking_empty:
ranking = db.load('ranking', load_last=True)[0].get('ranking', [])
return render_template('ranking.html', ranking=ranking)
competition_over = False
if tbt_config.COMPETITION_FORMAT == "knockout":
kntree = db.load('kntree', load_last=True)[0]
tree = kntree.get('tree', [])
if all(node is not None for node in tree):
competition_over = True
if tbt_config.COMPETITION_FORMAT == "group":
schedules = db.load('schedules', load_last=True)[0]
if len(schedules.get('matches', [])) == 0:
competition_over = True
player_data = None
if competition_over and is_ranking_empty:
ranking = None
players = db.load('players')
matches = db.load('matches')
games = db.load('games')
player_data = utils.compile_player_data(players, matches, games)
if tbt_config.COMPETITION_FORMAT == "group":
groups = db.load('groups', load_last=True)[0].get('groups', {})
ranking = core.rank_by_group(player_data, groups)[1]
if tbt_config.COMPETITION_FORMAT == "knockout":
kntree = db.load('kntree', load_last=True)[0]
ranking = core.rank_by_knockout(kntree.get('tree', []))
compiled_ranking = []
assert ranking is not None
for r in ranking:
pid = player_data.nickname_id[r]
compiled_ranking.append({
'nickname': r,
'tw_match': int(player_data.tw_match[pid]),
'tw_game': int(player_data.tw_game[pid]),
'tl_game': int(player_data.tl_game[pid]),
'tw_point': int(player_data.tw_point[pid]),
'tl_point': int(player_data.tl_point[pid]),
})
db.save('ranking', {'ranking': compiled_ranking})
if competition_over:
return render_template('ranking.html', ranking=ranking)
return render_template('ranking.html', ranking=None)
@app.route('/game_scoring/<uuid:match_id_uuid>', methods=['GET', 'POST'])
def game_scoring(match_id_uuid: uuid.UUID):
match_id = str(match_id_uuid)
db = get_db()
if request.method == 'POST':
score1 = int(request.form['score1'])
score2 = int(request.form['score2'])
winner = int(request.form['winner'])
matches = db.load('matches')
match_info = next((m for m in matches if m['match_id'] == match_id), dict())
match_over = False
def t_gip(*datas: list[dict[str, typing.Any]]) -> dict[str, dict[str, typing.Any]]:
nonlocal match_over
gip = datas[0][0]
games = datas[1]
if gip.get(match_id) is None:
raise DeclineTransaction("ERROR: 這個比賽組合不存在")
if score1 < 0 or score2 < 0 or (score1 < tbt_config.WINNING_SCORE and score2 < tbt_config.WINNING_SCORE):
raise DeclineTransaction(f"ERROR: 分數必須是非負數,且至少有一方達到 {tbt_config.WINNING_SCORE} 分")
if abs(score1 - score2) < 2:
raise DeclineTransaction("ERROR: 必須領先對手至少兩分")
games.append({
'match_id': match_id,
'score1': score1,
'score2': score2,
'winner': winner
})
gip[match_id] += 1
game_results = filter(lambda g: g['match_id'] == match_id, games)
game_wins = [0, 0]
for result in game_results:
game_wins[result['winner'] - 1] += 1
if game_wins[0] == (tbt_config.GAMES_PER_MATH // 2) + 1 or game_wins[1] == (tbt_config.GAMES_PER_MATH // 2) + 1:
match_over = True
del gip[match_id]
return {
'game_in_progress': gip,
'games': {
'match_id': match_id,
'score1': score1,
'score2': score2,
'winner': winner
}
}
def t_schedules_kntree(*datas: list[dict[str, typing.Any]]) -> dict[str, dict[str, typing.Any]]:
kntree = datas[0][0]
schedules = datas[1][0]
winner_nn = match_info['participant1'] if winner == 1 else match_info['participant2']
new_kntree, new_schedules = core.update_knockout_info(kntree.get('tree', []), schedules.get('matches', []), winner_nn)
return {
'kntree': {'tree': new_kntree},
'schedules': {'matches': new_schedules}
}
res = db.transact(['game_in_progress', 'games'], t_gip, load_last_list=[True, False])
if match_over and tbt_config.COMPETITION_FORMAT == "knockout":
db.transact(['kntree', 'schedules'], t_schedules_kntree, load_last_list=[True, True])
if res is not None:
return res
if match_over:
return render_template('game_result.html', score1=score1, score2=score2)
return render_template('game_result.html', score1=score1, score2=score2, match_id=match_id)
else:
gip = db.load('game_in_progress', load_last=True)[0]
game_round = gip.get(match_id)
if game_round is None:
return "ERROR: 這個比賽組合不存在"
if game_round >= tbt_config.GAMES_PER_MATH + 1:
return "ERROR: 這個比賽已經結束"
game_info_list = db.load('matches')
game_info = next((g for g in game_info_list if g['match_id'] == match_id), None)
if game_info is None:
return "ERROR: 找不到比賽資訊"
return render_template('game_scoring.html',
match_id=match_id,
game_info=game_info,
game_round=game_round,
games_per_match=tbt_config.GAMES_PER_MATH,
winning_score=tbt_config.WINNING_SCORE)
@app.route('/new_match', methods=['GET', 'POST'])
def new_match():
db = get_db()
if request.method == 'POST':
participant1 = request.form['participant1']
participant2 = request.form['participant2']
first = request.form['first']
match_id = get_match_uuid(participant1, participant2)
def t_schedules_gip(*datas: list[dict[str, typing.Any]]) -> dict[str, dict[str, typing.Any]]:
schedules = datas[0][0]
gip = datas[1][0]
expected_matches = schedules.get('matches', [])
if [participant1, participant2] not in expected_matches and [participant2, participant1] not in expected_matches:
raise DeclineTransaction("ERROR: 這個組合不在賽程中")
app.logger.debug(f"New game request: {request.form}")
if participant1 == participant2:
raise DeclineTransaction("ERROR: 不能與自己發起比賽比賽😃")
if gip.get(match_id) is not None:
raise DeclineTransaction("ERROR: 這個組合正在進行比賽")
gip[match_id] = 1
if [participant1, participant2] in expected_matches:
schedules['matches'].remove([participant1, participant2])
else:
schedules['matches'].remove([participant2, participant1])
return {
'schedules': schedules,
'game_in_progress': gip,
'matches': {
'match_id': match_id,
'participant1': participant1,
'participant2': participant2,
'first': first
}
}
res = db.transact(['schedules', 'game_in_progress'], t_schedules_gip, load_last_list=[True, True])
if res is not None:
return res
return render_template('new_match_result.html',
participant1=participant1,
participant2=participant2,
first=first,
match_id=match_id)
else:
players = db.load('players')
return render_template('new_match.html', players=players)
@app.route('/register', methods=['GET', 'POST'])
def register():
db = get_db()
if request.method == 'POST':
nickname = request.form['nickname']
def t_players(*datas: list[dict[str, str]]) -> dict[str, dict[str, str]]:
players = datas[0]
if any(p['nickname'] == nickname for p in players):
raise DeclineTransaction("這個暱稱已經被註冊過了")
return {
'players': {'nickname': nickname}
}
res = db.transact(['players'], t_players)
if res is not None:
return res
return render_template('register_result.html', message="註冊成功: " + nickname)
else:
return render_template('register.html')
@app.route('/')
def homepage():
return render_template('homepage.html')
def main():
print("Hello from tbt-sys!")
if __name__ == "__main__":
main()