-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
303 lines (249 loc) · 10.7 KB
/
main.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
import functools
import sqlite3
import textwrap
from collections import defaultdict
from datetime import datetime
from typing import Iterable, Callable, Concatenate
import click
import yaml
from gametournament import db, tournament_tools
from gametournament.constants import DEFAULT_DURATION_MULTIPLIER, DEFAULT_RANK_MULTIPLIER
from gametournament.db import DB_FILE
from gametournament.models import TourneyScore, Player, Tournament
from gametournament.point_scorer import PointScorer, PointFormula
from gametournament.rank_scorer import RankScorer, RankFormula
def require_dbfile[**P, R](func: Callable[Concatenate[sqlite3.Connection, P], R]) -> Callable[P, R]:
@functools.wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
if not db.DB_FILE.exists():
raise click.Abort("You need to run the init command!")
with db.get_connection() as connection:
return func(connection, *args, **kwargs)
return wrapper
def require_current_tournament[**P, R](func: Callable[Concatenate[Tournament, P], R]) -> Callable[P, R]:
@functools.wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
tournament = tournament_tools.get_current_tournament()
if tournament is None:
raise click.Abort("No currently selected tournament")
return func(tournament, *args, **kwargs)
return wrapper
@click.group()
def cli():
"""This is a CLI Tool for creating and running rankings for a Board Game Tournament.
It creates a "meta-score" for each game, whether there are points for the game or only
just a set of rankings.
Rather than being an "elimination" tournament, it calculates an average meta-score across
all games.
In order to run a tournament, you need to run the "init" command to set up the database.
After that, you can use the "add-scores" command
"""
pass
@cli.command(short_help="Sets up the tournament database.")
def init():
click.echo("Setting up tournament...")
if DB_FILE.exists():
click.confirm("This will replace the current database. Are you sure you want to proceed?", abort=True)
with db.get_connection() as connection:
db.create_tables(connection)
@cli.group(short_help="Commands related to tournaments")
def tournament():
pass
@tournament.command(short_help="Creates a new tournament")
@click.argument("name")
@click.option(
'-r',
'--rank-multiplier',
type=click.FLOAT,
default=DEFAULT_RANK_MULTIPLIER,
show_default=True,
help="Multiplier to apply to the INVERSE rank for a game",
prompt=True
)
@click.option(
'-d',
'--duration-multiplier',
type=click.FLOAT,
default=DEFAULT_DURATION_MULTIPLIER,
show_default=True,
help="Multiplier to apply to the number of game hours. Set this to 0 if you don't want to apply duration bonus.",
prompt=True,
)
@click.option(
'--bonus/--no-bonus',
default=True,
show_default=True,
help="Whether to apply a bonus or penalty to metascores on basis of standard deviations from average",
prompt=True,
)
@require_dbfile
def new(
connection: sqlite3.Connection,
name: str,
rank_multiplier: float,
duration_multiplier: float,
bonus: bool,
):
tournament = Tournament(
name=name,
start_date=datetime.now(),
rank_multiplier=rank_multiplier,
duration_multiplier=duration_multiplier,
apply_bonus_or_penalty=bonus,
)
tournament = db.create_tournament(connection, tournament)
players = []
while True:
player = click.prompt("Enter player name or hit enter if finished", default="", show_default=False)
if player.strip() == "":
break
players.append(player)
db.insert_players(connection, tournament['id'], players)
tournament_tools.set_current_tournament(tournament)
@tournament.command(short_help="Gets current tournament info")
@require_dbfile
@require_current_tournament
def get(tournament: Tournament, connection: sqlite3.Connection):
click.echo("Current Tournament:")
click.echo("-" * 20)
as_yaml = yaml.dump(tournament)
indented = textwrap.indent(as_yaml, '>> ')
click.echo(indented)
click.echo("Tournament Players:")
click.echo("-" * 20)
players = db.get_players(connection, tournament['id'])
for player in players:
click.echo(f">> {player['name']}")
@tournament.command(short_help="Selects a pre-existing tournament as the current tournament")
@require_dbfile
def select(connection: sqlite3.Connection):
tournaments = db.get_tournaments(connection)
tournament_map = {}
tournament_selection = "Select tournament by id\n"
if len(tournaments) == 0:
click.echo("No tournaments to select")
raise click.Abort()
for tournament in tournaments:
tournament_map[tournament['id']] = tournament
tournament_selection += f'* {tournament["start_date"]}\tID: {tournament["id"]}\t{tournament["name"]}\n'
tournament_selection_lines = tournament_selection.splitlines()
if len(tournament_selection_lines) > 10:
click.echo_via_pager(tournament_selection_lines)
else:
click.echo(tournament_selection)
tournament_id = click.prompt("Which id do you want to select?", type=click.INT)
tournament = tournament_map[tournament_id]
click.echo(f"Setting tournament named \"{tournament["name"]}\" as current tournament.")
tournament_tools.set_current_tournament(tournament)
@tournament.command(short_help="Adds a player to a tournament")
@click.argument("player-name")
@require_dbfile
@require_current_tournament
def add_player(tournament: Tournament, connection: sqlite3.Connection, player_name):
player_name = player_name.strip()
if player_name == "":
raise click.Abort("Invalid player name!")
db.insert_players(connection, tournament['id'], [player_name])
click.echo(f"Player {player_name} added to tournament: {tournament['name']}")
@tournament.command(short_help="Displays the scoring formulae for the tournament")
@require_current_tournament
def show_formulae(tournament: Tournament):
point_formula = PointFormula(tournament).show()
rank_formula = RankFormula(tournament).show()
click.echo(f"Metascore formula for point-based games:\n\t{point_formula}")
click.echo(f"\nMetascore formula for rank-based games:\n\t{rank_formula}")
@cli.group(short_help="Commands for working with scores")
def scores():
pass
@scores.command(short_help="Adds scores for a game")
@require_dbfile
@require_current_tournament
def record(tournament: Tournament, connection: sqlite3.Connection):
all_players = db.get_players(connection, tournament['id'])
game = click.prompt("What game was it?")
hours = click.prompt("How many hours did you play it?", type=float)
if click.confirm("Did all players play?", default=True):
players = all_players
else:
players = []
for player in all_players:
if click.confirm(f"Did {player['name']} play?"):
players.append(player)
if click.confirm("Did the game have points?", default=True):
scorer = PointScorer(tournament, players, hours)
else:
scorer = RankScorer(tournament, players, hours)
scores = scorer.score()
formula = scorer.get_formula()
click.echo(f"Metascore formula:\n\t{formula}")
player_lookup = {player['id']: player['name'] for player in players}
click.echo(f"\n{'-' * 20}\nHere are the meta-scores for that game:")
pretty_print_game_scores(player_lookup, scores.values())
click.confirm(f"\n{'-' * 20}\nDo you want to record these scores?", default=True, abort=True)
db.record_scores(connection, tournament['id'], game, hours, scores.values())
current_totals = db.get_scores(connection, tournament['id'])
output_scores(current_totals)
def pretty_print_game_scores(player_lookup: dict[int, str], scores: Iterable[TourneyScore]):
pretty_scores = {player_lookup[score['player_id']]: score for score in scores}
for key, value in pretty_scores.items():
click.echo(f"{key} -> {value['tournament_score']}")
@scores.command(short_help="Gets the current rankings/scores for the tournament")
@require_dbfile
@require_current_tournament
def get(tournament: Tournament, connection: sqlite3.Connection):
current_totals = db.get_scores(connection, tournament['id'])
output_scores(current_totals)
@tournament.command(short_help="Gets ALL scores currently entered for the tournament")
@require_dbfile
@require_current_tournament
def log(tournament: Tournament, connection: sqlite3.Connection):
records = db.get_all_records(connection, tournament['id'])
for record in records:
string = ""
for key, value in dict(record).items():
string += f"| {key}: {value} "
click.echo(string)
@scores.command(short_help="Recalculate all scores")
@require_dbfile
@require_current_tournament
def recalc(tournament: Tournament, connection: sqlite3.Connection):
records = db.get_all_records(connection, tournament['id'])
player_lookup = {}
games_to_hours = {}
scores_by_game = defaultdict(list)
for record in records:
scores_by_game[record['game']].append(TourneyScore(
player_id=record['player_id'],
game_score=record['points_or_rank'],
tournament_score=record['score'],
game_score_type=record['game_score_type'],
score_id=record['score_id']
))
player_lookup[record['player_id']] = record['name']
games_to_hours[record['game']] = record['hours']
new_scores = {}
for game, scores in scores_by_game.items():
players = [
Player(id=score['player_id'], name=player_lookup[score['player_id']])
for score in scores
]
if scores[0]['game_score_type'] == 'points':
scorer = PointScorer(tournament, players, games_to_hours[game])
else:
scorer = RankScorer(tournament, players, games_to_hours[game])
new_scores[game] = scorer.recalculate(scores)
for game, scores in new_scores.items():
click.echo(f"\n-----\nHere's the score for game {game}")
pretty_print_game_scores(player_lookup, scores)
click.confirm(f"\n{'-' * 20}\nDo you want to record these scores?", default=True, abort=True)
with connection:
for game, scores in new_scores.items():
db.update_scores(connection, scores)
current_totals = db.get_scores(connection, tournament['id'])
output_scores(current_totals)
def output_scores(current_totals):
click.echo(f"\n{'-' * 20}\nHere are the running total scores:")
for player, score, game_count, avg_score in current_totals:
click.echo(f'{player["name"]} -> avg: {round(avg_score, 3)}, total: {round(score, 3)}, games: {game_count}')
if __name__ == '__main__':
cli.main()