-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·111 lines (82 loc) · 2.38 KB
/
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
#!/bin/env python3
from functools import partial
from argparse import ArgumentParser, ArgumentTypeError
from rich.console import Console
from rich.text import Text
from src.game import Game
from src.history import History
from src.letters import Letters
from ui.app import SpellingBee
def str_of_len(length: int):
def x(l, s):
if len(s) == l:
return s
raise ArgumentTypeError(f"Argument must have length of {l}")
return partial(x, length)
def parser() -> ArgumentParser:
parser = ArgumentParser()
subparsers = parser.add_subparsers()
play_parser = subparsers.add_parser("play")
play_parser.set_defaults(func = play)
play_parser.add_argument(
"letters",
type=str_of_len(7),
help="The letters of the hive. " \
"The first letter is the center of the hive."
)
play_parser.add_argument(
"--new-game", "-n",
action="store_true",
help="Create a new game. " \
"The previous answers will be deleted."
)
list_parser = subparsers.add_parser("list")
list_parser.set_defaults(func = list_saves)
list_parser.add_argument(
"--sort-by-score", "-s",
action="store_true",
help="Show games from best to worst."
)
return parser
def play(args):
letters = Letters.fromstr(args.letters)
history = History()
if args.new_game:
history.reset(letters)
# Load game from history
game = history.load(letters) or Game(letters)
# Launch app
game = SpellingBee(game).run()
# Save game to history
if game and game.score:
history.save(game)
def show(game: Game) -> None:
name = Text(str(game.letters))
name.stylize("yellow", 0, 1)
console = Console()
console.print(name, "|", game.score)
def list_saves(args):
history = History()
games = list(
history.load(save) for save in history.list()
)
if args.sort_by_score:
# Sort from best score to worst
games.sort(
reverse=True,
key=lambda x: x.score if x else 0
)
else:
# Sort alphabetically
games.sort(
key=lambda x: str(x.letters) if x else ""
)
for game in games:
if not game:
continue
show(game)
def main():
args = parser().parse_args()
args.func(args)
if __name__ == "__main__":
main()