Repository navigation
Expand file tree
/
Copy pathhanabi_client.py
More file actions
1501 lines (1310 loc) · 62.5 KB
/
Copy pathhanabi_client.py
File metadata and controls
1501 lines (1310 loc) · 62.5 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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import websocket
from constants import ACTION, COLOR_CLUE, RANK_CLUE
from game_state import GameState
from conventions.encoder import EncoderV1GameState, EncoderV2GameState
from conventions.h_group import HGroupGameState
from conventions.ref_sieve import RefSieveGameState
from conventions.reactor import ReactorGameState
import traceback
from typing import Dict, Type
def is_int(x):
try:
int(x)
return True
except Exception:
return False
class HanabiClient:
def __init__(
self,
url,
cookie,
bot_to_join: str,
convention: str,
disconnect_on_game_end: bool,
table_name: str,
max_num_players: int
):
self.bot_to_join = bot_to_join
self.convention_name = (
convention.replace("_", "").replace("-", "").replace(" ", "").lower()
)
self.game_state_cls: Type[GameState] = {
"encoderv1": EncoderV1GameState,
"encoderv2": EncoderV2GameState,
"hgroup": HGroupGameState,
"refsieve": RefSieveGameState,
"reactor": ReactorGameState,
}[self.convention_name]
self.disconnect_on_game_end = disconnect_on_game_end
self.table_name = table_name
self.max_num_players = max_num_players
# Initialize all class variables
self.commandHandlers = {}
self.tables = {}
self.username = ""
self.ws = None
self.action_time = False
self.everyone_connected = False
self.games: Dict[int, GameState] = {}
# Initialize the website command handlers (for the lobby)
self.commandHandlers["welcome"] = self.welcome
self.commandHandlers["warning"] = self.warning
self.commandHandlers["error"] = self.error
self.commandHandlers["chat"] = self.chat
self.commandHandlers["table"] = self.table
self.commandHandlers["tableList"] = self.table_list
self.commandHandlers["tableGone"] = self.table_gone
self.commandHandlers["tableStart"] = self.table_start
# Initialize the website command handlers (for the game)
self.commandHandlers["init"] = self.init
self.commandHandlers["gameAction"] = self.game_action
self.commandHandlers["gameActionList"] = self.game_action_list
self.commandHandlers["databaseID"] = self.database_id
self.commandHandlers["connected"] = self.connected
self.commandHandlers["clock"] = self.clock
self.commandHandlers["user"] = self.user
self.commandHandlers["noteListPlayer"] = self.note_list_player
self.commandHandlers["chatTyping"] = self.chat_typing
# Start the WebSocket client
print(f'Connecting to "{url}".')
self.ws = websocket.WebSocketApp(
url,
on_message=lambda ws, message: self.websocket_message(ws, message),
on_error=lambda ws, error: self.websocket_error(ws, error),
on_open=lambda ws: self.websocket_open(ws),
on_close=lambda ws: self.websocket_close(ws),
cookie=cookie,
)
self.ws.run_forever()
def websocket_message(self, ws, message):
# WebSocket messages from the server come in the format of:
# commandName {"field_name":"value"}
# For more information, see:
# https://github.com/Hanabi-Live/hanabi-live/blob/main/server/src/actions.go
result = message.split(" ", 1) # Split it into two things
if len(result) != 1 and len(result) != 2:
print("error: received an invalid WebSocket message:")
print(message)
return
command = result[0]
try:
data = json.loads(result[1])
except:
print(f'error: the JSON data for the command of "{command}" was invalid')
return
if command in self.commandHandlers:
if command not in {"gameAction", "clock", "warning", "user"}:
print('debug: got command "' + command + '"')
try:
self.commandHandlers[command](data)
except Exception as e:
en = e.__class__.__name__
print("**************************\n" * 3)
print(f'error: command handler for "{command}" raised {en}, details:\n')
traceback.print_exc()
print("**************************\n" * 3)
return
else:
print(f'debug: ignoring command "{command}"')
def websocket_error(self, ws, error):
print(f"Encountered a WebSocket error ({error.__class__.__name__}), details:")
print(error)
def websocket_close(self, ws):
print("WebSocket connection closed.")
def websocket_open(self, ws):
print("Successfully established WebSocket connection.")
# --------------------------------
# Website Command Handlers (Lobby)
# --------------------------------
def welcome(self, data):
# The "welcome" message is the first message that the server sends us
# once we have established a connection
# It contains our username, settings, and so forth
self.username = data["username"]
if self.bot_to_join == "create":
self.chat_create_table()
def error(self, data):
# Either we have done something wrong,
# or something has gone wrong on the server
print(data)
def warning(self, data):
# We have done something wrong
print(data)
def chat(self, data):
# We only care about private messages
if data["recipient"] != self.username:
return
# We only care about private messages that start with a forward slash
if not data["msg"].startswith("/"):
return
args = data["msg"][1:].split(" ")
command = args[0]
if command == "join":
player_name = args[1] if len(args) > 1 else None
self.chat_join(data, player_name)
elif command == "create":
self.chat_create_table()
elif command == "start":
self.chat_start()
elif command in {"setvariant", "set_variant"}:
variant_name = (
(
args[1]
.replace("_", " ")
.replace("+", " & ")
.replace("6s", "(6 Suits)")
.replace("5s", "(5 Suits)")
.replace("4s", "(4 Suits)")
.replace("3s", "(3 Suits)")
)
if len(args) > 1
else None
)
self.chat_set_variant(variant_name)
elif command == "terminate":
table_id = args[1] if len(args) > 1 else None
self.chat_terminate(table_id)
elif command == "reattend":
table_id = args[1] if len(args) > 1 else 0
self.chat_reattend(table_id)
elif command == "restart":
self.chat_restart()
else:
msg = "That is not a valid command."
self.chat_reply(msg, data["who"])
def chat_join(self, data, player_name=None):
# Someone sent a private message to the bot and requested that we join
# their game
# Find the table that the current user is currently in
table_id = None
if is_int(player_name):
self.send("tableJoin", {"tableID": int(player_name)})
return
if player_name is None:
player_name = data.get("who", "")
print(player_name)
print("-----------")
for table in self.tables.values():
# Ignore games that have already started (and shared replays)
if table["running"]:
continue
if player_name in table["players"]:
if len(table["players"]) == 6:
msg = "Your game is full. Please make room for me before requesting that I join your game."
self.chat_reply(msg, data["who"])
return
table_id = table["id"]
break
if table_id is None:
msg = "Please create a table first before requesting that I join your game."
self.chat_reply(msg, data["who"])
return
self.send("tableJoin", {"tableID": table_id})
def chat_reattend(self, table_id: int):
self.send("reattend", {"tableID": table_id})
def chat_create_table(self):
self.send(
"tableCreate",
{"name": self.table_name, "maxPlayers": self.max_num_players},
)
def chat_set_variant(self, variant_name: str):
if variant_name is not None:
for table in self.tables.values():
if self.username in table["players"]:
self.send(
"tableSetVariant",
{
"tableID": table["id"],
"options": {"variantName": variant_name},
},
)
def chat_start(self):
for table in self.tables.values():
if self.username in table["players"]:
self.send("tableStart", {"tableID": table["id"]})
def chat_restart(self):
for table in self.tables.values():
if self.username in table["players"]:
self.s
self.send("tableRestart", {"tableID": table["id"], "hidePregame": True})
def chat_terminate(self, table_id=None):
if table_id is None:
for table in self.tables.values():
if self.username in table["players"]:
table_id = table["id"]
self.send("terminateTable", {"tableID": table_id})
def table(self, data):
self.tables[data["id"]] = data
if (
self.bot_to_join is not None
and self.bot_to_join != "create"
and self.bot_to_join in data["players"]
):
self.send("tableJoin", {"tableID": data["id"]})
def table_list(self, data_list):
for data in data_list:
self.table(data)
def table_gone(self, data):
del self.tables[data["tableID"]]
def table_start(self, data):
# The server has told us that a game that we are in is starting
# So, the next step is to request some high-level information about the
# game (e.g. number of players)
# The server will respond with an "init" command
self.send("getGameInfo1", {"tableID": data["tableID"]})
# -------------------------------
# Website Command Handlers (Game)
# -------------------------------
def init(self, data):
print("Init data:")
print(data)
print()
"""
{
'tableID': 29600,
'playerNames': ['yagami_green', 'yagami_light', 'yagami_blue'],
'ourPlayerIndex': 2, 'spectating': False, 'shadowing': False, 'replay': False,
'databaseID': -1, 'hasCustomSeed': False, 'seed': 'p3v177s1',
'datetimeStarted': '2023-07-03T20:36:19.812308097Z', 'datetimeFinished': '0001-01-01T00:00:00Z',
'options': {
'numPlayers': 3, 'startingPlayer': 0, 'variantID': 0, 'variantName': 'Omni (5 Suits)',
'timed': False, 'timeBase': 0, 'timePerTurn': 0, 'speedrun': False, 'cardCycle': False,
'deckPlays': False, 'emptyClues': False, 'oneExtraCard': False, 'oneLessCard': False,
'allOrNothing': False, 'detrimentalCharacters': False
}, 'characterAssignments': [], 'characterMetadata': [], 'sharedReplay': False,
'sharedReplayLeader': 'yagami_light', 'sharedReplaySegment': 0, 'sharedReplayEffMod': 0,
'paused': False, 'pausePlayerIndex': -1, 'pauseQueued': False
}
"""
self.action_time = False
self.everyone_connected = False
# Make a new game state and store it on the "games" dictionary
state = self.game_state_cls(
variant_name=data["options"]["variantName"],
player_names=data["playerNames"],
our_player_index=data["ourPlayerIndex"],
)
self.games[data["tableID"]] = state
# At this point, the JavaScript client would have enough information to
# load and display the game UI; for our purposes, we do not need to
# load a UI, so we can just jump directly to the next step
# Now, we request the specific actions that have taken place thus far
# in the game (which will come in a "gameActionList")
self.send("getGameInfo2", {"tableID": data["tableID"]})
def _go(self, data):
if data["tableID"] not in self.games:
print("NO STATE FOUND FOR TABLE ID = " + str(data["tableID"]) + "!")
return
state = self.games[data["tableID"]]
if (
(state.current_player_index == state.our_player_index)
& self.action_time
& self.everyone_connected
):
print("Player " + str(state.our_player_index) + " DECIDING ACTION!")
self.decide_action(data["tableID"])
self.action_time = False
def game_action(self, data):
# We just received a new action for an ongoing game
self.handle_action(data["action"], data["tableID"])
self._go(data)
def game_action_list(self, data):
for action in data["list"]:
self.handle_action(action, data["tableID"])
# Let the server know that we have finished "loading the UI"
# (so that our name does not appear as red / disconnected)
self.send("loaded", {"tableID": data["tableID"]})
self._go(data)
def handle_action(self, data, table_id):
_type = data["type"]
print(f'debug: got a game action of "{_type}" for table {table_id}: {data}')
# Local variables
state = self.games[table_id]
if data["type"] == "draw":
card = state.handle_draw(
data["playerIndex"], data["order"], data["suitIndex"], data["rank"]
)
elif data["type"] == "play":
# state.print()
state.handle_play(
data["playerIndex"], data["order"], data["suitIndex"], data["rank"]
)
elif data["type"] == "discard":
# state.print()
state.handle_discard(
data["playerIndex"], data["order"], data["suitIndex"], data["rank"]
)
elif data["type"] == "clue":
# state.print()
state.handle_clue(
data["giver"],
data["target"],
data["clue"]["type"],
data["clue"]["value"],
data["list"],
)
elif data["type"] == "turn":
state.turn = data["num"]
state.current_player_index = data["currentPlayerIndex"]
state.print()
elif data["type"] == "strike":
state.bombs = data["num"]
state.handle_strike(data["order"])
elif data["type"] == "status":
state.clue_tokens = data["clues"]
state.max_score = data["maxScore"]
elif data["type"] == "gameOver":
if self.disconnect_on_game_end:
raise SystemExit
def database_id(self, data):
# Games are transformed into shared replays after they are completed
# The server sends a "databaseID" message when the game has ended
# Use this as a signal to leave the shared replay
self.send(
"tableUnattend",
{
"tableID": data["tableID"],
},
)
# Delete the game state for the game to free up memory
del self.games[data["tableID"]]
def connected(self, data):
print("Connected: " + str(data))
self.everyone_connected = sum(data["list"]) == len(data["list"])
print("self.everyone_connected = " + str(self.everyone_connected))
state = self.games[data["tableID"]]
if state.turn == 0 and self.everyone_connected:
state.print()
def clock(self, data):
"""
{
'tableID': 2345, 'times': [-2268, -1015, -1039], 'activePlayerIndex': 1,
'timeTaken': 0
}
"""
self.action_time = True
self._go(data)
def user(self, data):
"""
{
'userID': 81697, 'name': 'kingCLUE', 'status': 0, 'tableID': 0,
'hyphenated': False, 'inactive': False
}
"""
pass
def note_list_player(self, data):
print("Note List Player: " + str(data))
def chat_typing(self, data):
print("Chat Typing: ", str(data))
self.action_time = True
self._go(data)
def play(self, order, table_id):
print(f"Playing order: {order}")
self.send("action", {"tableID": table_id, "type": ACTION.PLAY, "target": order})
def discard(self, order, table_id):
print(f"Discarding order: {order}")
self.send(
"action", {"tableID": table_id, "type": ACTION.DISCARD, "target": order}
)
def clue(self, target_index, clue_type, clue_value, table_id):
_type = {COLOR_CLUE: ACTION.COLOR_CLUE, RANK_CLUE: ACTION.RANK_CLUE}[clue_type]
print(f"Giving {_type._name_} with value {clue_value} to {target_index}")
self.send(
"action",
{
"tableID": table_id,
"type": _type,
"target": target_index,
"value": clue_value,
},
)
def write_note(self, table_id, order, note):
self.send("note", {"tableID": table_id, "order": order, "note": note})
def decide_action(self, table_id):
# The server expects to be told about actions in the following format:
# https://github.com/Hanabi-Live/hanabi-live/blob/main/server/src/command.go
state = self.games[table_id]
if isinstance(state, EncoderV1GameState):
self.encoder_v1(state, table_id)
elif isinstance(state, EncoderV2GameState):
self.encoder_v2(state, table_id)
elif isinstance(state, HGroupGameState):
self.hgroup(state, table_id)
elif isinstance(state, RefSieveGameState):
self.ref_sieve(state, table_id)
elif isinstance(state, ReactorGameState):
self.reactor(state, table_id)
else:
raise ValueError(type(state))
for order, note in state.notes.items():
self.write_note(table_id, order, note)
def hgroup(self, state: HGroupGameState, table_id: int):
good_actions = {
player_index: state.get_good_actions(player_index)
for player_index in range(state.num_players)
}
my_good_actions = good_actions[state.our_player_index]
next_player_good_actions = good_actions[state.next_player_index]
print(f"{state.our_player_name} POV - good actions:")
for player_index, orders in good_actions.items():
print(player_index, orders)
next_player_has_safe_action = False
my_chop_order = state.get_chop_order(state.our_player_index)
np_chop_order = state.get_chop_order(state.next_player_index)
for action_type, orders in next_player_good_actions.items():
if action_type == "seen_in_other_hand":
# TODO: handle this at later levels
continue
if len(orders):
next_player_has_safe_action = True
break
if not next_player_has_safe_action and state.clue_tokens >= 1:
if np_chop_order is not None:
np_chop = state.get_card(np_chop_order)
if (np_chop.suit_index, np_chop.rank) in state.playables:
self.clue(
state.next_player_index,
COLOR_CLUE,
np_chop.suit_index,
table_id,
)
return
elif (np_chop.suit_index, np_chop.rank) in state.criticals:
self.clue(
state.next_player_index, RANK_CLUE, np_chop.rank, table_id
)
return
# play if nothing urgent to do
if len(my_good_actions["playable"]):
# sort playables by lowest possible rank of candidates
sorted_playables = sorted(
my_good_actions["playable"],
key=lambda order: min([x[1] for x in state.get_candidates(order)]),
)
playable_fives = [
order
for order in my_good_actions["playable"]
if min([x[1] for x in state.get_candidates(order)]) == 5
]
if len(playable_fives):
self.play(playable_fives[0], table_id)
return
unique_playables = [
order
for order in sorted_playables
if order not in my_good_actions["dupe_in_other_hand"]
]
if len(unique_playables):
self.play(unique_playables[0], table_id)
return
# all the playables we have are duped in someone else's hand
# figure out where the duped card is and how to best resolve it
for playable_order in sorted_playables:
playable_candidates = state.get_candidates(playable_order)
if state.pace <= state.num_players - 2:
print("PACE IS TOO LOW, NEED TO PLAY!!!")
self.play(playable_order, table_id)
return
if len(playable_candidates) >= 2:
print("IDK WHAT THIS IS BUT ITS DUPED")
self.discard(playable_order, table_id)
return
suit_index, rank = list(playable_candidates)[0]
for player_index, hand in state.hands.items():
if player_index == state.our_player_index:
continue
for i, card in enumerate(hand):
if (suit_index, rank) != (card.suit_index, card.rank):
continue
candidates = state.all_candidates_list[player_index][i]
candidates_minus_my_play = candidates.difference(
{(suit_index, rank)}
)
if state.is_trash(candidates_minus_my_play):
print("OTHER GUY WILL KNOW ITS TRASH AFTER I PLAY THIS")
self.play(playable_order, table_id)
return
what_other_guy_sees = state.get_all_other_players_clued_cards(
player_index
)
unique_candidates_after_my_play = (
candidates_minus_my_play.difference(what_other_guy_sees)
)
if not len(unique_candidates_after_my_play) or state.is_trash(
unique_candidates_after_my_play
):
print("OTHER GUY WILL KNOW ITS DUPED AFTER I PLAY THIS")
self.play(playable_order, table_id)
return
if state.clue_tokens < 8:
for trashable in [
"trash",
"dupe_in_own_hand",
"dupe_in_other_hand",
"dupe_in_other_hand_or_trash",
]:
if len(my_good_actions[trashable]):
self.discard(my_good_actions[trashable][0], table_id)
return
self.discard(my_chop_order, table_id)
return
if np_chop_order is not None:
burn_clue_card = state.get_card(np_chop_order)
else:
burn_clue_card = state.hands[state.next_player_index][0]
self.clue(state.next_player_index, RANK_CLUE, burn_clue_card.rank, table_id)
def reactor(self, state: ReactorGameState, table_id: int):
stable_clues = state.get_stable_clues()
reactive_clues = state.get_reactive_clues()
print('------------------')
print('Players play/discard/chop:')
for pindex in range(state.num_players):
print(pindex, state.play_orders[pindex], state.discard_orders[pindex], state.get_chop_order(pindex))
print('Play orders')
print(state.play_orders)
print('Discard orders')
print(state.discard_orders)
print('Unresolved reactions')
print(state.unresolved_reactions)
print('All stable clues:')
for (clue_value, clue_type, target_index), y in stable_clues.items():
assert y in {"SAFE_ACTION", "DIRECT_PLAY", "REF_PLAY", "REF_DISCARD", "LOCK"}
print("COLOR" if clue_type == COLOR_CLUE else "RANK", clue_value, 'to', target_index, ':', y)
print('All reactive clues:')
for (clue_value, clue_type, target_index), y in reactive_clues.items():
print("COLOR" if clue_type == COLOR_CLUE else "RANK", clue_value, 'to', target_index, ':', y)
print('------------------')
# TODO: fold into a function
ur = state.unresolved_reactions[state.our_player_index]
if ur is not None:
target_index = ur.ordering[1]
target_orders = ur.player_slot_orders[target_index]
reacter_orders = ur.player_slot_orders[state.our_player_index]
pslot = ur.get_reactive_playable_human_slot()
tslot = ur.get_reactive_trash_human_slot()
print(f'Responding to reactive where {target_index} is target')
print(f'pslot = {pslot}, tslot = {tslot}')
if ur.play_parity == 0:
if pslot is not None:
reacter_slot = (ur.focused_slot - pslot - 1) % len(target_orders) + 1
order_to_play = reacter_orders[reacter_slot - 1]
print(f'!!1 Playing slot {reacter_slot}')
self.play(order_to_play, table_id)
state.unresolved_reactions[state.our_player_index] = None
return
for reacter_slot in [1,5,4,3,2]:
order_to_play = reacter_orders[reacter_slot - 1]
order_to_play_candidates = state.get_candidates(order_to_play)
fslot = (ur.focused_slot - reacter_slot - 1) % len(target_orders) + 1
fcard = state.hands[target_index][-fslot]
if fcard.to_tuple() in state.one_away_from_playables:
if state.get_next_playable_card_tuple(fcard.suit_index) not in order_to_play_candidates:
continue
else:
print(f'!!2 Playing slot {reacter_slot}')
self.play(order_to_play, table_id)
state.unresolved_reactions[state.our_player_index] = None
return
elif ur.play_parity == 1:
if pslot is not None:
reacter_slot = (ur.focused_slot - pslot - 1) % len(target_orders) + 1
order_to_discard = reacter_orders[reacter_slot - 1]
print(f'!!3 Discarding slot {reacter_slot}')
self.discard(order_to_discard, table_id)
state.unresolved_reactions[state.our_player_index] = None
return
if tslot is not None:
reacter_slot = (ur.focused_slot - tslot - 1) % len(target_orders) + 1
order_to_play = reacter_orders[reacter_slot - 1]
print(f'!!4 Playing slot {reacter_slot}')
self.play(order_to_play, table_id)
state.unresolved_reactions[state.our_player_index] = None
return
if len(state.our_play_orders):
print(f'!!5 Playing order {state.our_play_orders[0]}')
self.play(state.our_play_orders[0], table_id)
return
if len(state.our_discard_orders) and (state.clue_tokens < 8):
print(f'!!6 Discarding order {state.our_discard_orders[0]}')
self.discard(state.our_discard_orders[0], table_id)
return
if len(reactive_clues) and state.clue_tokens >= 2:
for (clue_value, clue_type, target_index), clue_str in reactive_clues.items():
if clue_str in {'2P0D_PLAY', '2P0D_FINESSE'}:
self.clue(target_index, clue_type, clue_value, table_id)
return
for (clue_value, clue_type, target_index), clue_str in reactive_clues.items():
if clue_str in {'1P1D_DISCARD', '1P1D_PLAY'}:
self.clue(target_index, clue_type, clue_value, table_id)
return
if len(stable_clues) and state.clue_tokens >= 1:
for (clue_value, clue_type, target_index), clue_str in stable_clues.items():
if clue_str != 'LOCK':
self.clue(target_index, clue_type, clue_value, table_id)
return
if (state.clue_tokens < 8):
chop_order = state.get_chop_order(state.our_player_index)
if chop_order is not None:
self.discard(chop_order, table_id)
return
self.play(state.our_hand[-1].order, table_id)
def ref_sieve(self, state: RefSieveGameState, table_id: int):
ref_sieve_clues = state.get_ref_sieve_clues()
print('Players play/discard/chop:')
for pindex in range(state.num_players):
print(pindex, state.play_orders[pindex], state.discard_orders[pindex], state.get_chop_order(pindex))
print('All ref sieve clues:')
for x, y in ref_sieve_clues.items():
assert y in {"SAFE_ACTION", "DIRECT_PLAY", "REF_PLAY", "REF_DISCARD", "LOCK"}
print(x, y)
bob = state.next_player_index
bob_chop_order = state.get_chop_order(bob)
if bob_chop_order is not None:
bob_chop_card = state.get_card(bob_chop_order)
urgent_card_on_bobs_chop = (state.is_playable_card(bob_chop_card) and bob_chop_order not in state.clued_card_orders) or state.is_critical_card(bob_chop_card)
bob_no_safe_actions = (
len(state.play_orders[bob]) +
len(state.discard_orders[bob]) == 0
)
clues_to_bob = {x: y for x, y in ref_sieve_clues.items() if x[-1] == bob}
if (state.clue_tokens >= 1) and (state.num_cards_in_deck >= 2) and urgent_card_on_bobs_chop and bob_no_safe_actions:
print('Bob has a crit/playable on chop and no safe actions!')
play_clues_to_bob = [x for x, y in clues_to_bob.items() if y in {"DIRECT_PLAY", "REF_PLAY"}]
safe_action_clues_to_bob = [x for x, y in clues_to_bob.items() if y in {"SAFE_ACTION"}]
discard_clues_to_bob = [x for x, y in clues_to_bob.items() if y in {"REF_DISCARD"}]
if len(play_clues_to_bob):
play_clues_ranked = sorted(
play_clues_to_bob, key=lambda x: state.evaluate_clue_score(x[0], x[1], x[2])
)
for clue_type, clue_value, target_index in play_clues_ranked:
self.clue(target_index, clue_type, clue_value, table_id)
return
if len(safe_action_clues_to_bob):
clue_type, clue_value, _ = safe_action_clues_to_bob[0]
self.clue(bob, clue_type, clue_value, table_id)
return
if len(discard_clues_to_bob):
# find a clue that gets rid of trash
for clue_type, clue_value, _ in discard_clues_to_bob:
discard_index = state.get_index_of_ref_discard_target(bob, clue_type, clue_value)
if discard_index is None:
continue
discard_target_card = state.hands[bob][discard_index]
touched_cards = state.get_touched_cards(clue_type, clue_value, bob)
newly_touched_cards = [card for card in touched_cards if card.order not in state.clued_card_orders]
does_not_bad_touch = True
for card in newly_touched_cards:
if state.is_weak_trash_card(card):
does_not_bad_touch = False
if does_not_bad_touch and state.is_weak_trash_card(discard_target_card):
print('Found kt to remove in Bobs hand!')
self.clue(bob, clue_type, clue_value, table_id)
return
# otherwise find a clue that doesn't get rid of a crit
for clue_type, clue_value, _ in discard_clues_to_bob:
discard_index = state.get_index_of_ref_discard_target(bob, clue_type, clue_value)
if discard_index is None:
continue
discard_target_card = state.hands[bob][discard_index]
if not state.is_critical_card(discard_target_card):
print('No kt to remove in Bobs hand, saving crits instead')
self.clue(bob, clue_type, clue_value, table_id)
return
if len(state.our_play_orders):
self.play(state.our_play_orders[0], table_id)
return
play_clues = [x for x, y in ref_sieve_clues.items() if y in {"DIRECT_PLAY", "REF_PLAY"}]
safe_action_clues = [x for x, y in ref_sieve_clues.items() if y in {"SAFE_ACTION"}]
discard_clues = [x for x, y in ref_sieve_clues.items() if y in {"REF_DISCARD"}]
lock_clues = [x for x, y in ref_sieve_clues.items() if y in {"LOCK"}]
if (state.clue_tokens >= 2):
print('Give some sort of useful clue')
if len(play_clues):
play_clues_ranked = sorted(
play_clues, key=lambda x: state.evaluate_clue_score(x[0], x[1], x[2])
)
for clue_type, clue_value, target_index in play_clues_ranked:
self.clue(target_index, clue_type, clue_value, table_id)
return
if len(safe_action_clues):
for clue_type, clue_value, target_index in safe_action_clues:
touched_cards = state.get_touched_cards(clue_type, clue_value, target_index)
newly_touched_cards = [card for card in touched_cards if card.order not in state.clued_card_orders]
for card in newly_touched_cards:
if state.is_weak_trash_card(card):
continue
self.clue(target_index, clue_type, clue_value, table_id)
return
if len(discard_clues):
players_with_good_chops = [
pindex for pindex in range(state.num_players)
if pindex != state.our_player_index
and state.get_chop_order(pindex) is not None
and not state.is_weak_trash_card(state.get_card(state.get_chop_order(pindex)))
]
good_discard_clues = [x for x in discard_clues if x[-1] in players_with_good_chops]
for clue_type, clue_value, target_index in good_discard_clues:
discard_index = state.get_index_of_ref_discard_target(target_index, clue_type, clue_value)
if discard_index is None:
continue
discard_target_card = state.hands[target_index][discard_index]
touched_cards = state.get_touched_cards(clue_type, clue_value, target_index)
newly_touched_cards = [card for card in touched_cards if card.order not in state.clued_card_orders]
does_not_bad_touch = True
for card in newly_touched_cards:
if state.is_weak_trash_card(card):
does_not_bad_touch = False
if does_not_bad_touch and state.is_weak_trash_card(discard_target_card):
self.clue(target_index, clue_type, clue_value, table_id)
return
if len(state.our_discard_orders):
self.discard(state.our_discard_orders[0], table_id)
return
if (state.clue_tokens < 8):
chop_order = state.get_chop_order(state.our_player_index)
if chop_order is not None:
self.discard(chop_order, table_id)
return
self.play(state.our_hand[-1].order, table_id)
def encoder_v2(self, state: EncoderV2GameState, table_id: int):
# ragequit
if state.pace < 0 or state.max_score < 5 * len(state.stacks):
self.play(state.our_hand[-1].order, table_id)
return
# TODO: implement elim
good_actions = {
player_index: state.get_good_actions(player_index)
for player_index in range(state.num_players)
}
my_good_actions = good_actions[state.our_player_index]
print(state.our_player_name + " good actions:")
for action_type, orders in good_actions.items():
print(action_type, orders)
max_crits = 0
for player_index in range(state.num_players):
if player_index == state.our_player_index:
continue
num_crits = sum(
[state.is_critical_card(card) for card in state.hands[player_index]]
)
max_crits = max(max_crits, num_crits)
if state.cannot_play:
print(f"CANNOT PLAY! {max_crits} crits > {state.num_cards_in_deck} cards")
if len(my_good_actions["playable"]) and not state.cannot_play:
# sort playables by lowest possible rank of candidates
sorted_playables = sorted(
my_good_actions["playable"],
key=lambda order: min([x[1] for x in state.get_candidates(order)]),
)
num_crits_i_have = sum([state.is_critical(x) for x in state.our_candidates])
# TODO: clean this up and put this into the encoder game state
# priority 0
fk_orders = state.get_fully_known_card_orders(
state.our_player_index, keyed_on_order=True
)
for order in sorted_playables:
if order in fk_orders:
identity = fk_orders[order]
next_playable = (identity[0], identity[1] + 1)
all_others_hc_cards = state.get_all_other_players_hat_clued_cards()
dire_circumstances = (
identity not in state.criticals
and num_crits_i_have > state.num_cards_in_deck
)
duped_in_another_hand = (
order in my_good_actions["dupe_in_other_hand"]
and state.num_cards_in_deck != 1
)
if dire_circumstances:
print(f"Would love to play {identity} but cannot")
elif duped_in_another_hand:
print(f"Not playing {identity} prio 0 when duped in other hand")
if (
next_playable in all_others_hc_cards
and not dire_circumstances
and not duped_in_another_hand
):
print("PRIO 0")
self.play(order, table_id)
return
# priority 1
key_crits = [
order
for order in sorted_playables
if state.is_critical(state.get_candidates(order))
and max([x[1] for x in state.get_candidates(order)]) <= 3
]
if len(key_crits):
print("PRIO 1")
self.play(key_crits[0], table_id)
return
# priority 2
playable_fives = [
order
for order in my_good_actions["playable"]
if min([x[1] for x in state.get_candidates(order)]) == 5
]
if len(playable_fives):
print("PRIO 2")
self.play(playable_fives[0], table_id)
return
# priority 3
unique_playables = [
order
for order in sorted_playables
if order not in my_good_actions["dupe_in_other_hand"]
]
if len(unique_playables):
print("PRIO 3")
self.play(unique_playables[0], table_id)
return