-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframework.py
More file actions
275 lines (240 loc) · 8.93 KB
/
framework.py
File metadata and controls
275 lines (240 loc) · 8.93 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
from collections import Counter
import random
class Identity:
"""
Represents a participant in the game with a specific role, managing the participant's actions, state, and interactions with the game and other participants.
"""
item_map = {
'gun': 'shoot gun',
}
def __init__(self, role, game, client, name):
"""
Initializes a new Identity instance.
Args:
role (str): The role of the identity.
game (Game): The game object to which this identity belongs.
client: The client interface handling communications.
"""
self.role = role
self.actions = Game.role_actions[role]
self.game = game
self.items = []
self.alive = True
self.blocked = False
self.protected = False
self.client = client
self.name = name
self.transcript = []
self.alignment = Game.role_alignments[role]
def speak(self, message):
"""
Adds a message to the game's transcript as spoken by this identity.
Args:
message (str): The message to be spoken.
"""
self.game.broadcast(f"{self.name} says: {message}")
def heard(self, message):
"""
Adds a message to the game's transcript as heard by this identity.
Args:
message (str): The message to be heard.
"""
self.transcript.append(f"{message}")
def listen(self):
"""
Generates and returns a summary of the game's current state from this identity's perspective.
Returns:
str: A summary of the current state.
"""
text = f"Game history: {self.transcript}"
return text
def take_action(self, action, target_name):
"""
Executes a specified action against a target identity if possible.
Args:
action (str): The action to be taken.
target (Identity): The target of the action.
"""
# target_index = self.game.names.index(target_name)
target_index = next((index for index, name in enumerate(
self.game.names) if name.lower() == target_name.lower()), None)
target = self.game.identities[target_index]
if self.blocked or action not in self.actions or not target.alive:
return
if action == 'give gun':
target.items.append('gun')
if action == 'shoot gun':
target.alive = False
if action == 'block':
target.blocked = True
if action == 'heal':
target.protected = True
if action == 'investigate':
self.heard(f'{target_name} is {target.role}')
if action == 'kill':
target.get_killed()
def night_turn(self):
"""
Handles the actions and interactions for this identity during the night phase of the game.
"""
if self.alive:
self.client.send(self.listen())
response = self.client.act()
if response:
if self.role != 'Villager':
action, target = map(str.lower, response.split())
self.take_action(action, target)
self.game.callback("night_turn", {
"player": self.name,
"action": action,
"target": target
})
def day_turn(self):
"""
Handles the actions and interactions for this identity during the day phase of the game.
"""
if self.alive:
self.client.send(
f'{self.listen()}. It is Day. Say something and vote.')
response = self.client.respond()
if response:
self.speak(response)
self.game.callback("day_turn", {
"player": self.name,
"response": response
})
def get_killed(self):
"""
Marks this identity as not alive if it is not protected.
"""
if not self.protected:
self.alive = False
self.client.got_killed()
self.game.broadcast(f'{self.name} has been killed.')
self.game.callback("got_killed", self.name)
if result := self.game.game_over():
self.game.callback("game_over", self.game.game_over())
else:
self.game.broadcast(f'{self.name} was attacked but survived.')
self.game.callback("got_attacked_saved", self.name)
def vote(self):
if self.alive:
self.client.send(
f'{self.listen()} Who would you like to vote for?')
candidate = self.client.vote()
self.game.callback(
"vote", {"voter": self.name, "candidate": candidate})
return candidate
class Game:
"""
Manages the entire game, including the sequence of events, game rounds, and the status of all identities.
"""
role_actions = {
'Mafia': ['kill'],
'Villager': [],
'Arms Dealer': ['give gun'],
'Doctor': ['heal'],
'Cop': ['investigate'],
'Bartender': ['block'],
}
role_alignments = {
'Mafia': 'Mafia',
'Villager': 'Town',
'Arms Dealer': 'Town',
'Doctor': 'Town',
'Cop': 'Town',
'Bartender': 'Town',
}
acting_order = ['Bartender', 'Arms Dealer',
'Cop', 'Doctor', 'Mafia', 'Villager']
def __init__(self, roles, names, clients, callback):
"""
Initializes a new Game instance with specified roles.
Args:
roles (list): A list of roles to be assigned to the identities in the game.
"""
roles = sorted(roles, key=self.acting_order.index)
self.identities = [Identity(role, self, None, names[index])
for index, role in enumerate(roles)]
self.transcript = []
self.names = names
self.roles = roles
self.day_count = 1
# General-purpose callback for external updates. Params: message_type, data
self.callback = callback
if not self.callback:
self.callback = lambda x, y: None
for identity, client in zip(self.identities, clients):
identity.client = client
client.connect(identity)
self.callback("game_start", [{
"role": ident.role,
"name": ident.name,
"alignment": ident.alignment,
"alive": True
} for ident in self.identities])
def broadcast(self, message):
for identity in self.identities:
identity.transcript.append(message)
def night(self):
"""
Executes the night phase of the game, allowing each identity to perform night actions.
"""
for identity in self.identities:
if identity.alive:
identity.night_turn()
for identity in self.identities:
identity.blocked = False
identity.protected = False
def day(self):
"""
Executes the day phase of the game, involving discussion and voting processes among identities.
"""
for speaking_round in range(2):
order = random.sample(self.identities, len(self.identities))
for identity in order:
if identity.alive:
identity.day_turn()
votes = [identity.vote() for identity in self.identities]
vote_counts = Counter(votes)
max_votes = max(vote_counts.values())
most_voted = [name for name, \
count in vote_counts.items() if count == max_votes]
chosen = random.choice(most_voted)
if chosen == 'nobody':
return
self.identities[self.names.index(chosen)].get_killed()
self.broadcast(f'{chosen} has been lynched.')
def start_round(self):
"""
Starts a new round of the game, consisting of both night and day phases.
"""
self.callback("night", self.day_count)
self.night()
self.callback("day", self.day_count)
self.day()
self.day_count += 1
def play(self):
"""
Begins and manages the sequence of rounds until the game is over.
"""
while not (result := self.game_over()):
self.start_round()
return result
def game_over(self):
"""
Determines whether the game has ended based on the number of Mafia and Town members still alive.
Returns:
bool: True if the game is over, False otherwise.
"""
mafia_count = sum(
1 for identity in self.identities if identity.role == 'Mafia' and identity.alive)
town_count = sum(
1 for identity in self.identities if identity.role != 'Mafia' and identity.alive)
if mafia_count >= town_count:
result = "Mafia"
if mafia_count == 0:
result = "Town"
else:
result = None
return result