-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.py
330 lines (281 loc) · 11.7 KB
/
sim.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
from nicegui import ui, app
from dataclasses import dataclass
from typing import List
import random
@dataclass
class Card:
title: str
kind: str
subcategory: str
text: str
image: str
id: str = ""
selected: bool = False
def __post_init__(self):
"""Generate a random ID 8 characters a-z"""
self.id = ''.join(random.choices("abcdefghijklmnopqrstuvwxyz", k=8))
@dataclass
class Stack:
"""Any stack of cards. It could be a deck, a hand, a discard pile, etc."""
cards: List[Card]
def draw(self):
return self.cards.pop()
def shuffle(self):
x = list(self.cards)
random.shuffle(x)
self.cards = x
def add_to_top(self, card):
self.cards.append(card)
def add_to_bottom(self, card):
self.cards.insert(0, card)
def remove(self, card):
"""Remove a card from anywhere in the stack."""
self.cards.remove(card)
@dataclass
class Player:
name: str
hand: Stack
table: Stack
tokens: int
@dataclass
class Game:
players: list[Player]
deck: Stack
discard: Stack
treasureDeck: Stack
treasureDiscard: Stack
currentPlayer: int = 0
maxTokens: int = 10
selected: Card = None
def deselect_card(self):
if self.selected:
self.selected.selected = False
self.selected = None
def select_card(self, card):
self.deselect_card()
self.selected = card
self.selected.selected = True
def move_card(self, card, destination):
self.find_stack(card).cards.remove(card)
destination.add_to_top(card)
if self.is_selected(card):
# this is a nice UX thing
self.deselect_card()
def find_stack(self, card):
for player in self.players:
for stack in (player.hand, player.table):
if card in stack.cards:
return stack
for stack in (self.deck, self.discard, self.treasureDeck, self.treasureDiscard):
if card in stack.cards:
return stack
def is_selected(self, card):
return self.selected and self.selected.id == card.id
def load_cards(file="cards.csv"):
# load cards from CSV
import csv
cards = []
with open(file, newline='') as f:
reader = csv.DictReader(f)
for row in reader:
count = int(row.pop('count', 1))
for _ in range(count):
cards.append(Card(**row))
return cards
def init_game():
NUM_INITIAL_CARDS = 5
all_cards = load_cards()
# build the deck and treasure deck
deck = Stack((x for x in all_cards if x.kind in ("resource", "quest", "event")))
treasureDeck = Stack((x for x in all_cards if x.kind == "treasure"))
# shuffle the decks
deck.shuffle()
treasureDeck.shuffle()
players = []
for i in (1, 2):
hand = Stack([deck.draw() for _ in range(NUM_INITIAL_CARDS)])
player = Player(f"Player {i}", hand, Stack([]), 0)
players.append(player)
return Game(players, deck, Stack([]), treasureDeck, Stack([]))
CARD_DISCARD = Card("Discard", "", "", "", "")
CARD_NONE = Card("", "", "", "", "")
def ui_gamecard(card, on_click_cb=None, visible=True):
def click():
if on_click_cb:
on_click_cb(card)
additionalStyles = "border: 2px solid green;" if card.selected else "border: 2px solid transparent;"
with ui.card().style(f"width: 10em; height: 15em; {additionalStyles}").on("click", click):
if card == CARD_NONE:
ui.space()
return
if card == CARD_DISCARD:
ui.space()
ui.icon("clear", size="1em").classes("mx-auto")
ui.space()
return
if visible or card.selected:
ui.badge(card.kind).classes("ml-auto")
ui.label(card.title)
if card.image:
ui.image(card.image)
if card.subcategory:
ui.label(card.subcategory)
ui.label(card.text).classes("text-xs")
else:
ui.space()
ui.icon("star", size="2em").classes("mx-auto")
ui.space()
UI_PLAYER_CARDS_HAND = 0
UI_PLAYER_CARDS_TABLE = 1
def ui_player(game, player_idx, select_card_cb, click_hand_cb, click_table_cb, mirror=False):
player = game.players[player_idx]
if mirror:
_ui_player_cards(player, select_card_cb, click_hand_cb, UI_PLAYER_CARDS_HAND)
_ui_player_cards(player, select_card_cb, click_table_cb, UI_PLAYER_CARDS_TABLE)
else:
_ui_player_cards(player, select_card_cb, click_table_cb, UI_PLAYER_CARDS_TABLE)
_ui_player_cards(player, select_card_cb, click_hand_cb, UI_PLAYER_CARDS_HAND)
def _ui_player_cards(player, card_cb, icon_cb=None, kind=UI_PLAYER_CARDS_HAND):
if not kind in (UI_PLAYER_CARDS_HAND, UI_PLAYER_CARDS_TABLE):
raise ValueError(f"Invalid kind: {kind}")
border_color = "red" if kind == UI_PLAYER_CARDS_HAND else "blue"
label_kind = "Hand" if kind == UI_PLAYER_CARDS_HAND else "Face-Up"
icon_kind = "o_front_hand" if kind == UI_PLAYER_CARDS_HAND else "o_square"
with ui.row().style(f"border: 1px solid {border_color};"):
with ui.column():
with ui.row().classes("items-center"):
with ui.card():
ui.label("Tokens")
ui.badge(player.tokens, color="primary").bind_text_from(player, "tokens").classes("mx-auto")
ui.button(icon="add") # TODO make this work!
#ui.button("", icon="add", on_click=lambda: player.tokens = min(player.tokens + 1, game.maxTokens)).classes("mx-auto")
#ui.badge(player.tokens, color="primary").bind_text_from(player, "tokens")
#ui.button(icon="add", on_click=lambda: player.tokens = min(player.tokens + 1, game.maxTokens))
#ui.button(icon="remove", on_click=lambda: player.tokens = max(player.tokens - 1, 0))
ui.icon(icon_kind, size="xl").on("click", icon_cb).props("color=accent")
stack = player.hand if kind == UI_PLAYER_CARDS_HAND else player.table
for card in reversed(stack.cards):
ui_gamecard(card, card_cb)
@ui.refreshable
def ui_common(game, deck_click_cb=None, discard_click_cb=None, treasure_click_cb=None, treasure_discard_click_cb=None):
with ui.row().classes("w-full").style("border: 1px solid blue;"): #.classes("mx-auto justify-between w-1/2"):
with ui.column().classes("w-1/4").style("border: 1px solid green;"):
ui.label("Deck")
with ui.row():
# face-down main deck
if len(game.deck.cards) > 0:
ui_gamecard(game.deck.cards[-1], deck_click_cb, visible=False)
else:
ui_gamecard(CARD_NONE) # TODO - shuffle discard back into deck
# face-up discard pile
if len(game.discard.cards) > 0:
ui_gamecard(game.discard.cards[-1], discard_click_cb)
else:
ui_gamecard(CARD_DISCARD, discard_click_cb)
with ui.column():
ui.space()
with ui.column().classes("w-1/4").style("border: 1px solid green;"):
ui.label("Treasure")
with ui.row():
if len(game.treasureDeck.cards) > 0:
ui_gamecard(game.treasureDeck.cards[-1], treasure_click_cb, visible=False)
else:
ui_gamecard(CARD_NONE) # TODO - shuffle treasure discard back into deck
if len(game.treasureDiscard.cards) > 0:
ui_gamecard(game.treasureDiscard.cards[-1], treasure_discard_click_cb)
else:
ui_gamecard(CARD_DISCARD, treasure_discard_click_cb)
def notify(msg):
ui.notify(msg, position="center", type='warning', timeout=500, animation=False)
if __name__ in ("__main__", "__mp_main__"):
game = init_game()
# this is an annoying hack
# the refreshable decorator is not working as I expect on repeated calls with different arguments
# so wrapping the calls in functions which can be refreshed
# this additional layer of indirection seems to work
def render_ui_player(i):
mirror = i == 1
ui_player(game=game,
player_idx=i,
select_card_cb=select_card,
click_hand_cb=make_select_hand_cb(i),
click_table_cb=make_select_table_cb(i),
mirror=mirror)
@ui.refreshable
def p1():
i = 0
render_ui_player(i)
@ui.refreshable
def p2():
i = 1
render_ui_player(i)
def refresh_all():
p2.refresh()
ui_common.refresh(game=game, discard_click_cb=discard_cb, treasure_discard_click_cb=treasure_discard_cb)
p1.refresh()
def select_card(card):
if game.is_selected(card):
game.deselect_card()
else:
game.select_card(card)
refresh_all()
def make_select_table_cb(player_idx):
def select_table(_):
if game.selected:
if not game.selected.kind in ("treasure", "quest"):
notify("You can only place Treasure or Quest cards on the table")
return
game.move_card(game.selected, game.players[player_idx].table)
refresh_all()
return select_table
def make_select_hand_cb(player_idx):
def select_hand(_):
if game.selected:
game.move_card(game.selected, game.players[player_idx].hand)
refresh_all()
return select_hand
def make_select_stack_cb(stack_name, allowed_kinds):
def select_stack(_):
# game reset will mess up our reference to the stack
# so we wrap the stack reference
stack = getattr(game, stack_name)
# if a card is selected, move it to the discard pile
# keep it selected in case we want to move it back
# otherwise, select the top discard card if present
top_discard = None
if len(stack.cards) > 0:
top_discard = stack.cards[-1]
if top_discard and game.is_selected(top_discard):
# clicking on the top discard which we already selected
game.deselect_card()
elif game.selected:
# clicking on discard with some other card selected
if not game.selected.kind in allowed_kinds:
notify(f"You can't place a {game.selected.kind} card there")
return
game.move_card(game.selected, stack)
else:
# no card selected, select the top discard card
if top_discard:
game.select_card(top_discard)
# finally
refresh_all()
return select_stack
deck_cb = make_select_stack_cb("deck", ("resource", "quest", "event"))
treasure_cb = make_select_stack_cb("treasureDeck", ("treasure",))
discard_cb = make_select_stack_cb("discard", ("resource", "quest", "event"))
treasure_discard_cb = make_select_stack_cb("treasureDiscard", ("treasure",))
# TODO next:
# - add tokens counters (could just be a slider?)
# - add handling for Quests / Treasure sections (or combine those in to a single "in front of player" section for simplicity?)
# - start writing cards!
# - (maybe) hide the opponent's hand from the current player
p2()
ui_common(game=game, deck_click_cb=deck_cb, discard_click_cb=discard_cb, treasure_click_cb=treasure_cb, treasure_discard_click_cb=treasure_discard_cb)
p1()
def reset_game():
global game
game = init_game()
refresh_all()
ui.button("New Game", on_click=reset_game)
ui.run()