-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathroles.py
More file actions
1275 lines (1075 loc) · 41.3 KB
/
Copy pathroles.py
File metadata and controls
1275 lines (1075 loc) · 41.3 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
"""
roles.py
Version: 5.1.0
Defines the behavior of all roles using a generic base class and specific subclasses.
"""
import random
# --- Roles ---
# Simplified keys, add manually to lobby.html
ROLE_ALPHA_WEREWOLF = "Alpha_Werewolf"
ROLE_BACKLASH_WEREWOLF = "Backlash_Werewolf"
ROLE_BODYGUARD = "Bodyguard"
ROLE_CUPID = "Cupid"
ROLE_DEMENTED_VILLAGER = "Demented_Villager"
ROLE_FOOL = "Fool"
ROLE_HONEYPOT = "Honeypot"
ROLE_HUNTER = "Hunter"
ROLE_LAWYER = "Lawyer"
ROLE_MARTYR = "Martyr"
ROLE_MAYOR = "Mayor"
ROLE_MONSTER = "Monster"
ROLE_PROSTITUTE = "Prostitute"
ROLE_RANDOM_SEER = "Random_Seer"
ROLE_REVEALER = "Revealer"
ROLE_SEER = "Seer"
ROLE_SERIAL_KILLER = "Serial_Killer"
ROLE_SORCERER = "Sorcerer"
ROLE_TOUGH_VILLAGER = "Tough_Villager"
ROLE_TOUGH_WEREWOLF = "Tough_Werewolf"
ROLE_VILLAGER = "Villager"
ROLE_WEREWOLF = "Werewolf"
ROLE_WILD_CHILD = "Wild_Child"
ROLE_WITCH = "Witch"
GOOD_MAYORS = [
ROLE_VILLAGER,
ROLE_DEMENTED_VILLAGER,
ROLE_FOOL,
ROLE_MONSTER,
ROLE_TOUGH_VILLAGER,
ROLE_MAYOR,
]
SPECIAL_WEREWOLVES = [ROLE_ALPHA_WEREWOLF, ROLE_TOUGH_WEREWOLF, ROLE_BACKLASH_WEREWOLF]
SOLO_LAST_MAN = [
ROLE_ALPHA_WEREWOLF,
ROLE_DEMENTED_VILLAGER,
ROLE_MONSTER,
ROLE_SERIAL_KILLER,
]
# 1. Global Registry to keep track of all available roles
AVAILABLE_ROLES = {}
def register_role(cls):
"""Decorator to automatically register a role class."""
AVAILABLE_ROLES[cls.__name__] = cls
return cls
# 2. The Base Generic Class
class Role:
name_key = "Unknown"
description_key = "desc_generic"
team = "Neutral" # Villager, Werewolf, Neutral
priority = 8 # 0 = First (e.g., Bodyguard), 50 = Last (e.g.,Werewolf)
VILLAGER_PROMPT_COUNT = 9
ui_short = "No description."
ui_long = "No description."
ui_rating = 0.0
ui_color = "#888888"
def __init__(self):
# Basic Metadata
self.is_night_active = False
self.player_id = None
def on_assign(self, player_obj):
"""
Called once when the role is assigned to the player.
Use this to apply permanent status effects.
"""
self.player_id = player_obj.id
pass
def on_night_start(self, player_obj, game_context):
"""Called at the very start of the night phase."""
pass
def get_valid_targets(self, game_context):
"""Returns a list of valid player IDs this role can target."""
return [p for p in game_context["players"] if p.is_alive]
def night_action(self, player_obj, target_player_obj, game_context):
"""
Logic for when the player performs their night action.
Returns a dict of action data to be stored in the game state.
"""
return {}
def get_night_ui_schema(self, player_obj, game_context):
idx = game_context.get("villager_prompt_index", 0)
safe_idx = idx % self.VILLAGER_PROMPT_COUNT
prompt_key = f"prompts.villager_{safe_idx}"
return {
"template": {
"header": f"roles.{self.name_key}.night.prompt",
"button": f"roles.{self.name_key}.night.button",
"success": f"roles.{self.name_key}.night.feedback",
"variables": {"prompt": prompt_key}, # Pass dynamic variables
},
"targets": [
{"id": p.id, "name": p.name}
for p in self.get_valid_targets(game_context)
],
"can_skip": True,
}
def check_win_condition(self, player_obj, game_context) -> bool:
"""
Custom win condition check.
Returns True if this specific player has satisfied their win condition.
"""
return False
def on_death(self, player_obj, game_context):
"""
Triggered when this player dies.
Can be used for Hunter (shoot someone) or Martyr (buff someone).
"""
# Future logic:
# if self.name_key == "hunter":
# game_context['engine'].trigger_hunter_event(player_obj)
return {}
def to_dict(self):
"""Serializes role info for the frontend."""
return {
"color": self.ui_color,
"description_key": self.description_key,
"is_night_active": self.is_night_active,
"long": self.ui_long,
"name_key": self.name_key,
"priority": self.priority,
"rating": self.ui_rating,
"short": self.ui_short,
"team": self.team,
}
# --- Specific Role Implementations ---
@register_role
class Villager(Role):
name_key = ROLE_VILLAGER
description_key = "desc_villager"
team = "Villagers"
ui_rating = 0.4
ui_color = "#4D00B3"
def __init__(self):
super().__init__()
self.is_night_active = False
def night_action(self, player_obj, target_player_obj, game_context):
"""
Logic for when the player performs their night action.
Returns a dict of action data to be stored in the game state.
"""
if not target_player_obj:
return {}
return {"action": "villager_vote", "target": target_player_obj.id}
@register_role
class Werewolf(Role):
name_key = ROLE_WEREWOLF
description_key = "desc_werewolf"
team = "Werewolves"
priority = 45 # Wolves attack after defensive roles
ui_rating = -0.6
ui_color = "#CC0033"
def __init__(self):
super().__init__()
self.is_night_active = True
def night_action(self, player_obj, target_player_obj, game_context):
# The engine will aggregate Werewolf votes, but the action is simply voting a target
# Werewolf Kill Logic: Unanimous for less than 5 active werewolves, else require >=80% of active werewolves to choose same victim.
return {"action": "kill_vote", "target": target_player_obj.id}
def get_night_ui_schema(self, player_obj, game_context):
return {
"template": {
# These keys map directly to the JSON structure we added
"header": f"roles.{self.name_key}.night.prompt",
"button": f"roles.{self.name_key}.night.button",
"success": f"roles.{self.name_key}.night.feedback",
},
"targets": [
{"id": p.id, "name": p.name}
for p in self.get_valid_targets(game_context)
],
"can_skip": True,
}
@register_role
class Seer(Role):
name_key = ROLE_SEER
description_key = "desc_seer"
team = "Villagers"
priority = 3 # Seer acts early
ui_rating = 1.0
ui_color = "#0000FF"
def __init__(self):
super().__init__()
self.is_night_active = True
def investigate(self, target_player):
"""Central logic for determining what the Seer sees."""
if (
target_player.role.team == "Werewolves"
or target_player.role.team == "Monster"
):
return ROLE_WEREWOLF
return ROLE_VILLAGER
def night_action(self, player_obj, target_player_obj, game_context):
# Return the information immediately to the engine to send back to user
result = self.investigate(target_player_obj)
return {
"action": "investigate",
"target": target_player_obj.id,
"result": result,
}
def get_night_ui_schema(self, player_obj, game_context):
return {
"template": {
# These keys map directly to the JSON structure we added
"header": f"roles.{self.name_key}.night.prompt",
"button": f"roles.{self.name_key}.night.button",
"success": f"roles.{self.name_key}.night.feedback",
},
"targets": [
{"id": p.id, "name": p.name}
for p in self.get_valid_targets(game_context)
],
"can_skip": True,
}
@register_role
class Alpha_Werewolf(Werewolf):
name_key = ROLE_ALPHA_WEREWOLF
ui_rating = -0.5
ui_color = "#C00040"
def __init__(self):
super().__init__()
def check_win_condition(self, player_obj, game_context):
# Wins if is the ONLY one left alive with max one non-monster alive
if not player_obj.is_alive:
return False
living_players = [p for p in game_context["players"] if p.is_alive]
if len(living_players) == 1:
return True
werewolves = [p for p in living_players if p.role.team == "Werewolves"]
if len(werewolves) > 1:
return False
non_monsters = [p for p in living_players if p.role.name_key != "Monster"]
return (
len(living_players) == 2 and len(non_monsters) == 2
) # werewolf is a nonmonster as well
@register_role
class Bodyguard(Role):
name_key = ROLE_BODYGUARD
description_key = "desc_bodyguard"
team = "Villagers"
priority = 17 # Priority PROTECT BEFORE ATTACK
ui_rating = 0.5
ui_color = "#4000C0"
def __init__(self):
super().__init__()
self.is_night_active = True
self.last_protected_id = None
def night_action(self, player_obj, target_player_obj, game_context):
if target_player_obj.id == self.last_protected_id:
return {}
self.last_protected_id = target_player_obj.id
print(f"Bodyguard protecting {target_player_obj.name}")
return {
"action": "Protect",
"effect": "protected",
"target": target_player_obj.id,
}
def get_valid_targets(self, game_context):
"""Returns a list of valid player IDs excluding last portected"""
all_living = [p for p in game_context["players"] if p.is_alive]
if self.last_protected_id:
return [p for p in all_living if p.id != self.last_protected_id]
return all_living
def get_night_ui_schema(self, player_obj, game_context):
return {
"template": {
# These keys map directly to the JSON structure we added
"header": f"roles.{self.name_key}.night.prompt",
"button": f"roles.{self.name_key}.night.button",
"success": f"roles.{self.name_key}.night.feedback",
},
"targets": [
{"id": p.id, "name": p.name}
for p in self.get_valid_targets(game_context)
],
"can_skip": True,
}
@register_role
class Cupid(Villager):
name_key = ROLE_CUPID
priority = 9 # Very early, before wolves
ui_rating = -0.2
ui_color = "#990066"
def __init__(self):
super().__init__()
self.is_night_active = True
def night_action(self, player_obj, target_player_obj, game_context):
if self.is_night_active:
self.is_night_active = False
# Validation: Cannot pick self
if target_player_obj.id == player_obj.id:
return {}
# 1. Get second lover from Metadata (provided by Engine)
metadata = game_context.get("current_action_metadata", {})
target_player_id2 = metadata.get("target_id2")
if not target_player_id2:
print("Cupid Error: Second target not found in metadata.")
return {}
target_player_obj2 = next(
(p for p in game_context["players"] if p.id == target_player_id2), None
)
if not target_player_obj2:
print("Cupid Error: Second target not found.")
return {}
target_player_obj.linked_partner_id = target_player_obj2.id
target_player_obj2.linked_partner_id = target_player_obj.id
print(
f"Cupid: {target_player_obj.name} linked with {target_player_obj2.name}"
)
return {
"action": "Link Lovers",
"target": target_player_obj.name,
"partner": target_player_obj2.name,
}
return {"action": "villager_vote", "target": target_player_obj.id}
def get_night_ui_schema(self, player_obj, game_context):
if not self.is_night_active:
return Villager.get_night_ui_schema(self, player_obj, game_context)
return {
"template": {
"header": f"roles.{self.name_key}.night.prompt",
"description": f"roles.{self.name_key}.night.description", # Extra field
"button": f"roles.{self.name_key}.night.button",
"success": f"roles.{self.name_key}.night.feedback",
},
"targets": [
{"id": p.id, "name": p.name}
for p in self.get_valid_targets(game_context)
],
"can_skip": False,
}
@register_role
class Demented(Villager):
name_key = ROLE_DEMENTED_VILLAGER
team = "Neutral" # Wins alone
ui_rating = 0.2
ui_color = "#660099"
def __init__(self):
super().__init__()
# Wins if last one alive
def check_win_condition(self, player_obj, game_context):
# win if alive and max one non serial killer villager alive
if not player_obj.is_alive:
return False
living_players = [p for p in game_context["players"] if p.is_alive]
if len(living_players) == 1:
return True
werewolves = [p for p in living_players if p.role.team == "Werewolves"]
if len(werewolves) > 0:
return False
KILL_DEMENTED = [
ROLE_MONSTER,
ROLE_HONEYPOT,
ROLE_HUNTER,
ROLE_SERIAL_KILLER,
ROLE_WILD_CHILD,
]
villagers = [p for p in living_players if p.role.name_key not in KILL_DEMENTED]
return len(living_players) == 2 and len(villagers) == 2
@register_role
class Fool(Villager):
name_key = ROLE_FOOL
team = "Neutral"
ui_rating = -0.2
ui_color = "#990066"
# Wins if lynched
def __init__(self):
super().__init__()
# Logic handled in game_engine.resolve_lynch_vote
@register_role
class Honeypot(Villager):
name_key = ROLE_HONEYPOT
ui_rating = 0
ui_color = "#800080"
def __init__(self):
super().__init__()
def on_death(self, player_obj, game_context):
reason = game_context.get("reason", "")
# 1. Lynch Retaliation: Kill a random "Yes" voter
if reason == "Lynched":
votes = game_context.get("lynch_votes", {})
yes_voters = [
pid
for pid, vote in votes.items()
if vote == "yes" and pid != player_obj.id
]
# Filter for ALIVE voters only
alive_yes_voters = [
pid
for pid in yes_voters
if any(p.id == pid and p.is_alive for p in game_context["players"])
]
if alive_yes_voters:
target_id = random.choice(alive_yes_voters)
target_player_obj = next(
(p for p in game_context["players"] if p.id == target_id), None
)
msg = "Honeypot Retaliation"
if target_player_obj:
msg = f"Honeypot retaliation: <strong>{target_player_obj.name}</strong> selected from lynch mob. They were a {target_player_obj.role.name_key}!"
print(msg)
return {"kill": target_id, "reason": msg}
# 2. Werewolf Retaliation: Kill a random Werewolf
elif reason == "Werewolf meat":
wolves = [
p
for p in game_context["players"]
if p.is_alive and p.role.team == "Werewolves"
]
if wolves:
target = random.choice(wolves)
msg = (
f"Honeypot retaliation: {target.name} selected from werewolf pack."
)
print(msg)
return {"kill": target.id, "reason": msg}
# 3. Witch Retaliation: Kill the Witch
elif reason == "Witch Poison":
witches = [
p
for p in game_context["players"]
if p.is_alive and p.role.name_key == "Witch"
]
if witches:
target = random.choice(witches)
msg = f"Honeypot retaliation: {target.name} is taking an acid bath."
print(msg)
return {"kill": target.id, "reason": msg}
# 4. Serial Killer Retaliation: Kill the Serial Killer
elif reason == "Serial Killer":
killers = [
p
for p in game_context["players"]
if p.is_alive and p.role.name_key == "Serial_Killer"
]
if killers:
target = random.choice(killers)
msg = (
f"Honeypot retaliation: {target.name} is sleeping with the fishies."
)
print(msg)
return {"kill": target.id, "reason": msg}
return {}
@register_role
class Hunter(Role):
name_key = ROLE_HUNTER
team = "Villagers"
priority = 48
ui_rating = 0.4
ui_color = "#4D00B3"
def __init__(self):
super().__init__()
self.is_night_active = True
self.failsafe_id = None
def night_action(self, player_obj, target_player_obj, game_context):
# Store the target, do NOT kill yet.
self.failsafe_id = target_player_obj.id
return {}
def on_death(self, player_obj, game_context):
# If I die, I take my failsafe target with me
if self.failsafe_id:
return {"kill": self.failsafe_id}
return {}
def get_valid_targets(self, game_context):
"""Returns a list of valid player IDs this role can target, exclude self."""
return [
p for p in game_context["players"] if p.is_alive and p.id != self.player_id
]
def get_night_ui_schema(self, player_obj, game_context):
return {
"template": {
# These keys map directly to the JSON structure we added
"header": f"roles.{self.name_key}.night.prompt",
"button": f"roles.{self.name_key}.night.button",
"success": f"roles.{self.name_key}.night.feedback",
},
"targets": [
{"id": p.id, "name": p.name}
for p in self.get_valid_targets(game_context)
],
"can_skip": True,
}
@register_role
class Backlash_Werewolf(Hunter):
# Same logic as Hunter, just Werewolf team
name_key = ROLE_BACKLASH_WEREWOLF
team = "Werewolves"
priority = 50
ui_rating = -1.0
ui_color = "#FF0000"
def __init__(self):
super().__init__()
self.failsafe_id = None
def get_night_ui_schema(self, player_obj, game_context):
return {
# We provide a UI with TWO dropdowns
"template": {
"header": f"roles.{self.name_key}.night.prompt",
"description": f"roles.{self.name_key}.night.description", # Extra field
"button": f"roles.{self.name_key}.night.button",
"success": f"roles.{self.name_key}.night.feedback",
},
"targets": [
{"id": p.id, "name": p.name}
for p in self.get_valid_targets(game_context)
],
"can_skip": True, # Wolves must vote!
}
def night_action(self, player_obj, target_player_obj, game_context):
# 1. Handle the Primary Selection (The Wolf Kill Vote)
# We use the parent logic to generate the standard kill vote
# 2. Handle the Secondary Selection (The Backlash Grudge)
# We retrieve the second dropdown's value from metadata
metadata = game_context.get("current_action_metadata", {})
backlash_id = metadata.get("target_id2")
if backlash_id:
self.failsafe_id = backlash_id
backlash_name = "Unknown"
found_player = next(
(p for p in game_context["players"] if p.id == backlash_id), None
)
if found_player:
backlash_name = found_player.name
print(f"Backlash Wolf {player_obj.name} marked {backlash_name} for death.")
return {"action": "kill_vote", "target": target_player_obj.id}
@register_role
class Lawyer(Villager):
name_key = ROLE_LAWYER
description_key = "desc_lawyer"
priority = 14 # Acts around the same time as Bodyguard
ui_rating = 0.2
ui_color = "#660099"
def __init__(self):
super().__init__()
self.is_night_active = True
def night_action(self, player_obj, target_player_obj, game_context):
# Apply the protection effect
return {
"action": "defend",
"effect": "no_lynch",
"target": target_player_obj.id,
"reason": "Lawyer Defense",
}
def get_night_ui_schema(self, player_obj, game_context):
return {
"template": {
# These keys map directly to the JSON structure we added
"header": f"roles.{self.name_key}.night.prompt",
"button": f"roles.{self.name_key}.night.button",
"success": f"roles.{self.name_key}.night.feedback",
},
"targets": [
{"id": p.id, "name": p.name}
for p in self.get_valid_targets(game_context)
],
"can_skip": True,
}
@register_role
class Martyr(Villager):
name_key = "Martyr"
ui_rating = 0.2
ui_color = "#660099"
def __init__(self):
super().__init__()
self.is_night_active = True
self.failsafe_id = None
def on_death(self, player_obj, game_context):
# Let's do: If I die, I give a "blessing" (armor) to a random living player.
lucky_person = next(
(
p
for p in game_context["players"]
if p.is_alive and p.id == self.failsafe_id
),
None,
)
if lucky_person:
lucky_person.status_effects.append("2nd_life")
print(f"Martyr died and blessed {lucky_person.name}")
return {}
def get_night_ui_schema(self, player_obj, game_context):
return {
"template": {
# These keys map directly to the JSON structure we added
"header": f"roles.{self.name_key}.night.prompt",
"button": f"roles.{self.name_key}.night.button",
"success": f"roles.{self.name_key}.night.feedback",
},
"targets": [
{"id": p.id, "name": p.name}
for p in self.get_valid_targets(game_context)
],
"can_skip": False,
}
def get_valid_targets(self, game_context):
"""Returns a list of valid player IDs this role can target, exclude self."""
return [
p for p in game_context["players"] if p.is_alive and p.id != self.player_id
]
def night_action(self, player_obj, target_player_obj, game_context):
self.failsafe_id = target_player_obj.id
return {}
@register_role
class Mayor(Villager):
# Mayor tag is transferable to not night active like villager, demented villager, fool, monster, tough_villager
name_key = ROLE_MAYOR
description_key = "desc_mayor"
priority = 12
ui_rating = 0.4
ui_color = "#4D00B3"
def __init__(self):
super().__init__()
self.is_night_active = True
self.next_mayor_id = "not_set_yet"
def night_action(self, player_obj, target_player_obj, game_context):
if self.is_night_active:
self.is_night_active = False
self.next_mayor_id = target_player_obj.id
return {
"type": "announcement",
"message": f"🗳️ Next mayor selected: <strong>{target_player_obj.name}</strong> promoted to <strong>Mayor-Elect!</strong>",
}
return {"action": "villager_vote", "target": target_player_obj.id}
def get_valid_targets(self, game_context):
"""Returns a list of valid player IDs this role can target, exclude self."""
return [
p for p in game_context["players"] if p.is_alive and p.id != self.player_id
]
def get_night_ui_schema(self, player_obj, game_context):
if self.next_mayor_id != "not_set_yet":
return Villager.get_night_ui_schema(self, player_obj, game_context)
return {
"template": {
# These keys map directly to the JSON structure we added
"header": f"roles.{self.name_key}.night.prompt",
"button": f"roles.{self.name_key}.night.button",
"success": f"roles.{self.name_key}.night.feedback",
},
"targets": [
{"id": p.id, "name": p.name}
for p in self.get_valid_targets(game_context)
],
"can_skip": True,
}
def on_night_start(self, player_obj, game_context):
"""if next_mayor is dead, choose new next_mayor"""
if self.next_mayor_id and self.next_mayor_id != "not_set_yet":
next_mayor = next(
(p for p in game_context["players"] if p.id == self.next_mayor_id), None
)
if next_mayor and not next_mayor.is_alive:
self.is_night_active = True
def on_death(self, player_obj, game_context):
if not self.next_mayor_id or self.next_mayor_id == "not_set_yet":
return {}
new_mayor = next(
(
p
for p in game_context["players"]
if p.is_alive and p.id == self.next_mayor_id
),
None,
)
if new_mayor:
new_mayor.role.next_mayor_id = "not_set_yet"
# only GOOD_MAYORS can pass on mayor title
if new_mayor.role.name_key in GOOD_MAYORS:
new_mayor.role.is_night_active = True
new_mayor.role.next_mayor_id = "not_set_yet"
# bind mayor functions
new_mayor.role.night_action = Mayor.night_action.__get__(
new_mayor.role, type(new_mayor.role)
)
new_mayor.role.get_night_ui_schema = Mayor.get_night_ui_schema.__get__(
new_mayor.role, type(new_mayor.role)
)
new_mayor.role.on_death = Mayor.on_death.__get__(
new_mayor.role, type(new_mayor.role)
)
new_mayor.role.on_night_start = Mayor.on_night_start.__get__(
new_mayor.role, type(new_mayor.role)
)
new_mayor.role.get_valid_targets = Mayor.get_valid_targets.__get__(
new_mayor.role, type(new_mayor.role)
)
# announce to all next mayor name has been elected
return {
"type": "announcement",
"message": f"🎩 The Mayor is dead! Long live Mayor <strong>{new_mayor.name}</strong>!",
}
return {}
@register_role
class Monster(Villager):
# seen as Werewolf, but cannot be killed by Werewolf
name_key = ROLE_MONSTER
team = "Monster"
ui_rating = 0.3
ui_color = "#5A00A6"
def __init__(self):
super().__init__()
def on_assign(self, player_obj):
# This is checked by the Engine when calculating deaths
player_obj.status_effects.append("immune_to_wolf")
def check_win_condition(self, player_obj, game_context):
# Monster win if alive and max one werewolf alive.
if not player_obj.is_alive:
return False
living_players = [p for p in game_context["players"] if p.is_alive]
if len(living_players) == 1:
return True
werewolves = [p for p in living_players if p.role.team == "Werewolves"]
if len(living_players) == 2 and len(werewolves) == 1:
return True
return False
@register_role
class Prostitute(Role):
name_key = ROLE_PROSTITUTE
priority = 5
team = "Villagers"
ui_rating = 0.4
ui_color = "#4D00B3"
def __init__(self):
super().__init__()
self.slept_with = set()
self.is_night_active = True
def night_action(self, player_obj, target_player_obj, game_context):
player_obj.visiting_id = target_player_obj.id
target_player_obj.visiting_id = player_obj.id
self.slept_with.add(target_player_obj.id)
print(f"Prostitute {player_obj.name} is visiting {target_player_obj.name}")
return {}
def check_win_condition(self, player_obj, game_context):
# Wins if sleeps with (Total - 2) players, dead or alive
# called in resolve_night_deaths
all_p = len(game_context["players"])
if len(self.slept_with) >= (all_p - 2):
return True
return False
def get_valid_targets(self, game_context):
"""Returns a list of valid player IDs this role can target, exclude self."""
return [
p for p in game_context["players"] if p.is_alive and p.id != self.player_id
]
def get_night_ui_schema(self, player_obj, game_context):
return {
"template": {
# These keys map directly to the JSON structure we added
"header": f"roles.{self.name_key}.night.prompt",
"button": f"roles.{self.name_key}.night.button",
"success": f"roles.{self.name_key}.night.feedback",
},
"targets": [
{"id": p.id, "name": p.name}
for p in self.get_valid_targets(game_context)
],
"can_skip": False,
}
@register_role
class Random_Seer(Seer):
name_key = ROLE_RANDOM_SEER
ui_rating = -0.1
ui_color = "#8D0073"
def __init__(self):
super().__init__()
# insane, naive, paranoid, normal
self.sanity = random.choice(["insane", "naive", "paranoid", "normal"])
def investigate(self, target_player):
actual = super().investigate(target_player) # "Werewolf" or "Villager"
if self.sanity == "paranoid":
return ROLE_WEREWOLF
elif self.sanity == "naive":
return ROLE_VILLAGER
elif self.sanity == "insane":
return ROLE_VILLAGER if actual == ROLE_WEREWOLF else ROLE_WEREWOLF
return actual # Normal
@register_role
class Revealer(Role):
name_key = ROLE_REVEALER
team = "Villagers"
priority = 25
ui_rating = 0.3
ui_color = "#5A00A6"
def __init__(self):
super().__init__()
self.is_night_active = True
def night_action(self, player_obj, target_player_obj, game_context):
# If wolf -> kill wolf. Else -> kill self.
if target_player_obj.role.team == "Werewolves":
return {
"action": "revealed_werewolf",
"reason": "revealed_werewolf",
}
else:
return {
"action": "revealed_wrongly",
"reason": "revealed_wrongly",
}
def get_valid_targets(self, game_context):
"""Returns a list of valid player IDs this role can target, exclude self."""
return [
p for p in game_context["players"] if p.is_alive and p.id != self.player_id
]
def get_night_ui_schema(self, player_obj, game_context):
return {
"template": {
# These keys map directly to the JSON structure we added
"header": f"roles.{self.name_key}.night.prompt",
"button": f"roles.{self.name_key}.night.button",
"success": f"roles.{self.name_key}.night.feedback",
},
"targets": [
{"id": p.id, "name": p.name}
for p in self.get_valid_targets(game_context)
],
"can_skip": True,
}
@register_role
class Serial_Killer(Role):
name_key = "Serial_Killer"
team = "Serial_Killer"
priority = 15 # Kills before wolves
ui_rating = -0.2
ui_color = "#990066"
def __init__(self):
super().__init__()
self.is_night_active = True
def night_action(self, player_obj, target_player_obj, game_context):
return {
"action": "direct_kill",
"target": target_player_obj.id,
"reason": "Serial Killer", # Custom death reason
}
def check_win_condition(self, player_obj, game_context):