-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
486 lines (402 loc) · 19.7 KB
/
app.py
File metadata and controls
486 lines (402 loc) · 19.7 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/usr/bin/env python3
"""
app.py — Flask web backend for Dubito.
Run: python3 app.py
Deps: pip install flask
"""
import importlib
import random
import uuid
from flask import Flask, jsonify, render_template, request
from dubito.player import Player
from dubito.handlers import GameHandler, StatsHandler, generate_player_data
from dubito.game_data import TurnOutput, TurnData
from dubito.core_game import initialize
from bots.manual import rule_based, probability
from bots.llms import claude as claude_bots
app = Flask(__name__)
# ── Bot registry ──────────────────────────────────────────────────────────────
def _try(module, names):
try:
m = importlib.import_module(module)
return {n: getattr(m, n) for n in names if hasattr(m, n)}
except (ImportError, ModuleNotFoundError):
return {}
ALL_BOTS: dict[str, type] = {
"AlwaysTruthful": rule_based.AlwaysTruthful,
"JustPutCards": rule_based.JustPutCards,
"MrDoubt": rule_based.MrDoubt,
"MrNoDoubt": rule_based.MrNoDoubt,
"RandomBoi": rule_based.RandomBoi,
"StefaBot": rule_based.StefaBot,
"AdaptyBoi": probability.AdaptyBoi,
"SusBoi": probability.SusBoi,
"UsualBot": probability.UsualBot,
"RiskCounter": probability.RiskCounter,
"ClaudeBot": claude_bots.ClaudeBot,
**_try("bots.llms.chatgpt", ["ChatGPTBot", "ChatGPT_thinking"]),
**_try("bots.llms.gemini", ["GeminiBot"]),
}
# ── Card name helpers ─────────────────────────────────────────────────────────
NAMES = {0: "★", 1: "A", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6",
7: "7", 8: "8", 9: "9", 10: "10", 11: "J", 12: "Q", 13: "K"}
LONG = {0: "Joker", 1: "Ace", 11: "Jack", 12: "Queen", 13: "King"}
def cn(v): return NAMES.get(v, str(v))
def cln(v): return LONG.get(v, cn(v))
# ── Human player ──────────────────────────────────────────────────────────────
class HumanPlayer(Player):
def play(self, p: TurnData) -> TurnOutput:
raise NotImplementedError("Human player plays via the web UI")
def add_cards(self, cards: list[int]) -> None:
# Append in arrival order — no sorting, so the player sees exactly
# which cards they just picked up.
self.cards.hand.extend(cards)
# ── In-memory session store ───────────────────────────────────────────────────
_sessions: dict[str, "GameSession"] = {}
# ── Game session ──────────────────────────────────────────────────────────────
class GameSession:
"""Manages a single interactive game between one human and N bots."""
def __init__(self, all_players: list[Player], show_names: bool) -> None:
self.id = str(uuid.uuid4())[:8]
self.gh = GameHandler(all_players=all_players, deck_size=14)
self.sh = StatsHandler(all_players=all_players)
self.human = next(p for p in all_players if isinstance(p, HumanPlayer))
self.all_players = all_players
self.prev_player = all_players[-1]
self.this_player = all_players[0]
self.show_names = show_names
self.messages: list[str] = []
self._correct_doubt = False
# ── Label helpers ─────────────────────────────────────────────────────────
def _lbl(self, p: Player) -> str:
if isinstance(p, HumanPlayer):
return "You"
return p.__class__.__name__ if self.show_names else f"Bot {p.id}"
def _display_name(self, p: Player) -> str:
if isinstance(p, HumanPlayer):
return "YOU"
return p.__class__.__name__ if self.show_names else f"Bot {p.id}"
# ── Board snapshot ────────────────────────────────────────────────────────
def snap(self) -> dict:
"""Lightweight snapshot of board state + card counts + human hand."""
gh = self.gh
return {
"board_cards": gh.n_cards_board(),
"last_n_played": len(gh.board.latests) if gh.board.latests else 0,
"declared_number": gh.board.number if gh.board.number else None,
"declared_name": cln(gh.board.number) if gh.board.number else None,
"is_first_hand": gh.is_first_hand(),
"players_cards": {str(p.id): len(p.cards) for p in self.all_players},
"human_hand": list(self.human.cards.hand),
}
# ── Output resolution ─────────────────────────────────────────────────────
def process_output(
self, output: TurnOutput, this_player: Player, prev_player: Player
) -> tuple[list[dict], bool]:
"""
Apply a TurnOutput to the game state.
Returns:
resolve_events: frame dicts for doubt resolution, discards, and wins.
correct_doubt: True when the doubter was right (they earn a free turn).
"""
gh = self.gh
sh = self.sh
resolve_events: list[dict] = []
correct_doubt = False
if isinstance(this_player, HumanPlayer):
sh.increase_turns_played(this_player, gh.is_first_hand())
if output.doubt:
sh.increase_player_doubts(this_player)
latest_cards = list(gh.get_latest_played_cards())
revealed = ', '.join(cn(c) for c in latest_cards)
jokers = gh.jokers_in_latest()
takes = "take" if isinstance(this_player, HumanPlayer) else "takes"
if gh.is_honest() and jokers:
rest = [c for c in gh.get_board() if c != 0]
this_player.add_cards(rest)
sh.increase_player_honesty(prev_player)
gh.reset_board()
resolve_events.append({
"msg": (
f"Joker! {self._lbl(prev_player)} played [{revealed}] — protected. "
f"{self._lbl(this_player)} {takes} {len(rest)} card(s)."
),
"event_type": "joker",
"actor_id": this_player.id,
"target_id": this_player.id,
"revealed_cards": latest_cards,
**self.snap(),
})
elif gh.is_honest():
n = gh.n_cards_board()
this_player.add_cards(gh.get_board())
sh.increase_player_honesty(prev_player)
gh.reset_board()
resolve_events.append({
"msg": (
f"{self._lbl(prev_player)} was honest — played [{revealed}]. "
f"{self._lbl(this_player)} takes {n} card(s). Ouch."
),
"event_type": "take_honest",
"actor_id": this_player.id,
"target_id": this_player.id,
"revealed_cards": latest_cards,
**self.snap(),
})
else:
n = gh.n_cards_board()
correct_doubt = True
prev_player.add_cards(gh.get_board())
sh.increase_player_dishonesty(prev_player)
gh.reset_board()
resolve_events.append({
"msg": (
f"{self._lbl(prev_player)} was bluffing — actually played [{revealed}]. "
f"They take {n} card(s). Free turn for {self._lbl(this_player)}!"
),
"event_type": "take_bluff",
"actor_id": this_player.id,
"target_id": prev_player.id,
"revealed_cards": latest_cards,
**self.snap(),
})
else:
if gh.is_first_hand():
val = output.number or 1
if val == 0:
pool = gh.board.availables or output.cards
val = random.choice(pool)
gh.set_current_number(val)
gh.set_board_cards(output.cards)
# Discard phase
for p in gh.playing_players():
disc = p.discard_cards()
if disc:
gh.set_discarded_cards(disc)
resolve_events.append({
"msg": f"{self._lbl(p)} discards four {cn(disc[0])}s.",
"event_type": "discard",
"actor_id": p.id,
"target_id": p.id,
**self.snap(),
})
# Win check
for winner in [p for p in gh.playing_players() if p.has_no_cards()]:
gh.set_winners(winner)
resolve_events.append({
"msg": f"{self._lbl(winner)} wins!",
"event_type": "win",
"actor_id": winner.id,
"target_id": winner.id,
**self.snap(),
})
if gh.n_playing_players() == 2:
losers = gh.playing_players()
resolve_events.append({
"msg": f"Game over — {self._lbl(losers[0])} and {self._lbl(losers[1])} lose!",
"event_type": "game_over",
"actor_id": None,
"target_id": None,
**self.snap(),
})
return resolve_events, correct_doubt
# ── Bot auto-play ─────────────────────────────────────────────────────────
def advance_bots(self) -> list[dict]:
"""
Auto-play all consecutive bot turns until it's the human's turn (or game over).
Returns animation frames, one per game event.
"""
gh = self.gh
all_frames: list[dict] = []
while True:
if gh.n_playing_players() <= 2 or gh.turn.counter >= 1000:
break
if self._correct_doubt:
self._correct_doubt = False
else:
self.prev_player, self.this_player = gh.next_turn()
this_player = self.this_player
if isinstance(this_player, HumanPlayer):
break
ip = generate_player_data(gh, self.sh)
is_first = gh.is_first_hand()
output = this_player.play(ip)
self.sh.increase_turns_played(this_player, is_first)
if output.doubt:
pre_snap = self.snap()
resolve_events, correct_doubt = self.process_output(output, this_player, self.prev_player)
doubt_msg = f"{self._lbl(this_player)} doubts {self._lbl(self.prev_player)}!"
self.messages.append(doubt_msg)
all_frames.append({
"msg": doubt_msg,
"event_type": "doubt",
"actor_id": this_player.id,
"target_id": self.prev_player.id,
**pre_snap,
})
else:
resolve_events, correct_doubt = self.process_output(output, this_player, self.prev_player)
play_msg = (
f"{self._lbl(this_player)} plays {len(output.cards)} card(s), "
f"declares {cln(output.number)}s."
if is_first else
f"{self._lbl(this_player)} plays {len(output.cards)} card(s) "
f"claiming {cln(gh.board.number)}s."
)
self.messages.append(play_msg)
all_frames.append({
"msg": play_msg,
"event_type": "play",
"actor_id": this_player.id,
"target_id": None,
**self.snap(),
})
for e in resolve_events:
self.messages.append(e["msg"])
all_frames.append(e)
self._correct_doubt = correct_doubt
return all_frames
# ── Human actions ─────────────────────────────────────────────────────────
def play_cards(self, card_indices: list[int], number: int | None) -> list[dict]:
"""Execute a human play. Returns all animation frames."""
gh = self.gh
is_first = gh.is_first_hand()
cards = [self.human.cards.hand[i] for i in card_indices]
for i in sorted(card_indices, reverse=True):
self.human.cards.hand.pop(i)
if is_first:
play_msg = f"You play {len(cards)} card(s) and declare {cln(number)}s."
output = TurnOutput(doubt=False, number=number, cards=cards)
else:
play_msg = f"You play {len(cards)} card(s) claiming {cln(gh.board.number)}s."
output = TurnOutput(doubt=False, number=None, cards=cards)
resolve_events, correct_doubt = self.process_output(output, self.human, self.prev_player)
self.messages.append(play_msg)
frames: list[dict] = [{
"msg": play_msg,
"event_type": "play",
"actor_id": self.human.id,
"target_id": None,
**self.snap(),
}]
for e in resolve_events:
self.messages.append(e["msg"])
frames.append(e)
self._correct_doubt = correct_doubt
return frames + self.advance_bots()
def call_doubt(self) -> list[dict]:
"""Execute a human doubt. Returns all animation frames."""
pre_snap = self.snap()
doubt_msg = f"You doubt {self._lbl(self.prev_player)}!"
output = TurnOutput(doubt=True, number=None, cards=None)
resolve_events, correct_doubt = self.process_output(output, self.human, self.prev_player)
self.messages.append(doubt_msg)
frames: list[dict] = [{
"msg": doubt_msg,
"event_type": "doubt",
"actor_id": self.human.id,
"target_id": self.prev_player.id,
**pre_snap,
}]
for e in resolve_events:
self.messages.append(e["msg"])
frames.append(e)
self._correct_doubt = correct_doubt
return frames + self.advance_bots()
# ── Serialization ─────────────────────────────────────────────────────────
def serialize(self) -> dict:
"""Return a JSON-serializable snapshot of the full game state."""
gh = self.gh
playing = gh.playing_players()
winners = gh.get_winners()
is_over = gh.n_playing_players() <= 2 or gh.turn.counter >= 1000
players_info = [
{
"id": p.id,
"display_name": self._display_name(p),
"is_human": isinstance(p, HumanPlayer),
"n_cards": len(p.cards),
"is_current": p is self.this_player,
"is_prev": p is self.prev_player and p is not self.this_player,
"is_playing": p in playing or p in winners,
}
for p in self.all_players
]
winner_set = set(winners)
non_winners = sorted(
[p for p in gh.playing_players() if p not in winner_set],
key=lambda p: len(p.cards),
)
standings = [
{"name": self._display_name(w), "bot_type": None if isinstance(w, HumanPlayer) else w.__class__.__name__, "cards": 0, "is_human": isinstance(w, HumanPlayer)}
for w in winners
] + [
{"name": self._display_name(p), "bot_type": None if isinstance(p, HumanPlayer) else p.__class__.__name__, "cards": len(p.cards), "is_human": isinstance(p, HumanPlayer)}
for p in non_winners
]
return {
"game_id": self.id,
"status": "game_over" if is_over else "player_turn",
"turn": gh.turn.counter,
"is_first_hand": gh.is_first_hand(),
"board_cards": gh.n_cards_board(),
"last_n_played": len(gh.board.latests) if gh.board.latests else 0,
"declared_number": gh.board.number if gh.board.number else None,
"declared_name": cln(gh.board.number) if gh.board.number else None,
"streak": gh.turn.streak,
"available_numbers": gh.board.availables,
"hand": list(self.human.cards.hand),
"players": players_info,
"messages": self.messages,
"standings": standings,
"timed_out": gh.turn.counter >= 1000,
}
# ── Routes ────────────────────────────────────────────────────────────────────
@app.route("/")
def index():
return render_template("index.html")
@app.route("/api/bots")
def api_bots():
return jsonify(list(ALL_BOTS.keys()))
@app.route("/api/game", methods=["POST"])
def api_create_game():
body = request.get_json(force=True)
n_players = max(3, min(8, int(body.get("n_players", 4))))
bot_pool = body.get("bot_pool", list(ALL_BOTS.keys()))
show_names = bool(body.get("show_names", True))
pool = [ALL_BOTS[n] for n in bot_pool if n in ALL_BOTS] or list(ALL_BOTS.values())
human = HumanPlayer(1)
bots = [random.choice(pool)(i + 2) for i in range(n_players - 1)]
all_players = [human] + bots
random.shuffle(all_players)
for i, p in enumerate(all_players):
p.id = i + 1
initialize(all_players, n_jollies=2)
session = GameSession(all_players, show_names)
_sessions[session.id] = session
init_frames = session.advance_bots()
return jsonify(session.serialize() | {"frames": init_frames})
@app.route("/api/game/<gid>/play", methods=["POST"])
def api_play(gid: str):
session = _sessions.get(gid)
if not session:
return jsonify({"error": "game not found"}), 404
body = request.get_json(force=True)
card_indices = sorted(body.get("card_indices", []))
number = body.get("number", None)
if not card_indices:
return jsonify({"error": "no cards selected"}), 400
frames = session.play_cards(card_indices, number)
return jsonify(session.serialize() | {"frames": frames})
@app.route("/api/game/<gid>/doubt", methods=["POST"])
def api_doubt(gid: str):
session = _sessions.get(gid)
if not session:
return jsonify({"error": "game not found"}), 404
if session.gh.is_first_hand():
return jsonify({"error": "cannot doubt on first hand"}), 400
frames = session.call_doubt()
return jsonify(session.serialize() | {"frames": frames})
# ── Entry point ───────────────────────────────────────────────────────────────
if __name__ == "__main__":
app.run(port=5001, debug=True)