-
Notifications
You must be signed in to change notification settings - Fork 0
/
mafia.py
executable file
·437 lines (361 loc) · 14 KB
/
mafia.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
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
from __future__ import annotations
import asyncio
from roomHandler import PermissionDeniedError
from typing import Callable, Dict, Generator, Iterable, List, Literal, Tuple, Union
from dataclasses import dataclass
import datetime
import random
import uuid
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from roomHandler import Room
PLAYERID = str
ROOMID = uuid.UUID
TARGETID = uuid.UUID
ROLES = {
"m": "Мафія",
"p": "Мирний",
"d": "Доктор",
"k": "Маніяк",
"s": "Шериф",
"g": "Путана"
}
TIMINGS = {
"perform":40,
"vote":40,
"connect":20
}
LOC = {
"mafwin": "Мафія виграла",
"mafdefeat": "Мирні перемогли мафію",
"perform": "Ніч",
"vote": "Голосування"
}
playersMin = 6
class PlayerNotFoundError(Exception):
def __init__(self, UUID):
self.uuid = UUID
super().__init__(f"Error! Player with UUID:${self.uuid} not found!")
def __repr__(self):
return f"Error! Player with UUID:${self.uuid} not found!"
class PlayerRAW:
# на просто id мы обосремося
def __init__(self, name: str, gid: PLAYERID, avatar: str = None, *args, **kwargs):
self.name = name
self.id = gid # ця штука заповнюється даними реєстрації на стороні серверу
self.avatar = avatar # only for other players. doesnt impact logic
def __repr__(self):
return "Player %s,id: %s" % (self.name,self.id)
def __hash__(self):
return hash(self.id)
class Player(PlayerRAW):
def __init__(self, raw: PlayerRAW, role: str):
self.__name = raw.name
self.avatar = raw.avatar
self.__id = raw.id # ебкое ООП
self.__role = role # инкапсуляция в действии
self.__killed = False
self.__whore = False # путана
self.__uuid = uuid.uuid4()
def __unicode__(self):
return "Player:%s, Role:%s" % (self.name, self.role)
def __str__(self):
return "Player:%s, Role:%s" % (self.name, self.role)
def __repr__(self):
return "Player:%s, Role:%s" % (self.name, self.role)
@property
def role(self) -> str:
return self.__role
@property
def roleNameFull(self) -> str:
"""
Returns:
str: [description of role in Russian]
"""
return ROLES[self.__role]
@property
def id(self) -> PLAYERID:
return self.__id
@property
def name(self) -> str:
return self.__name
@property
def UUID(self) -> TARGETID:
return self.__uuid
@property
def isKilled(self) -> bool:
return self.__killed
@property
def isFucked(self) -> bool:
return self.__whore
@property
def jsonable(self) -> dict: # TODO: добавить role если игрок сдох
ret={
"name": self.name,
"avatar": self.avatar,
"id": str(self.UUID),
"isKilled": self.isKilled
}
if self.isKilled:
ret["role"]=self.roleNameFull
return ret
@property
def jsonableP(self) -> dict:
"""It is reply to getme
Returns:
dict: id, role,desc(role name), isKilled
"""
return {
"id": str(self.UUID),
"role": self.__role,
"desc": self.roleNameFull,
"isKilled": self.isKilled
}
@property
def isPerformable(self) -> bool:
"""
Returns True if gamer can perform (if gamer is not regular person)
"""
return False if self.__role == 'p' else True
def setKilled(self, state: bool):
self.__killed = state
def setWhore(self, state: bool):
self.__whore = state
def checkUser(self, id, role) -> bool:
return True if (self.id == id and self.role == role) else False
def canDo(self, id, role) -> bool: # TODO:обрезание
return True if (not self.__whore and self.checkUser(id, role)) else False
class Players(object):
"""
Player container with sweet methods
"""
def __init__(self, data: set[PlayerRAW]):
cardlist = [
['m', 'p', 'p', 'p', 'p', 's'], # 6
['m','p','s','k','d','m','g'],#debug only!
#['m', 'm', 'p', 'p', 'p', 'p', 's'],
['m', 'm', 'p', 'p', 'p', 'p', 'p', 's'],
['m', 'm', 'p', 'p', 'p', 'p', 'p', 's', 'k'],
['m', 'm', 'p', 'p', 'p', 'p', 'p', 'p', 's', 'k'], # 10
['m', 'm', 'p', 'p', 'p', 'p', 'p', 'p', 's', 'k', 'd'],
['m', 'm', 'm', 'p', 'p', 'p', 'p', 'p', 'p', 's', 'k', 'd'],
['m', 'm', 'm', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 's', 'k', 'd'],
['m', 'm', 'm', 'p', 'p', 'p', 'p', 'p',
'p', 'p', 's', 'k', 'd', 'g'], # 14
# Mafia Person Doctor Killer Sheriff Girl
# x means killed
]
self.players: set[Player] = set()
choosed = cardlist[len(data)-playersMin]
random.shuffle(choosed)
for x, y in zip(data, choosed):
self.players.add(Player(x, y))
@property
def jsonable(self) -> List[Dict]:
return [x.jsonable for x in self.players]
def getMafias(self) -> Generator[Player, None, None]:
for player in self.players:
if player.isKilled:pass
elif player.role == "m":
yield player
def getMafiasCount(self) -> int:
return len(list(self.getMafias()))
def getGood(self) -> Generator[Player, None, None]:
for player in self.players:
if player.isKilled:pass
elif player.role in ["p", "s", "d", "g"]:
yield player
def getAlive(self) -> Generator[Player, None, None]:
for player in self.players:
if not player.isKilled:
yield player
def getAliveCount(self) -> int:
return len(list(self.getAlive()))
def getGoodCount(self) -> int:
return len(list(self.getGood()))
def getPerformable(self,countKilled=False) -> Generator[Player, None, None]:
for player in self.players:
if player.isKilled and not countKilled:pass
elif player.role in ['m', 's', 'k', 'd', 'g']:
yield player
def getPerformableCount(self,countKilled=False) -> int:
return len(list(self.getPerformable(countKilled)))
def getByTID(self, UUID: TARGETID) -> Player:
for player in self.players:
if str(player.UUID) == str(UUID):
return player
raise PlayerNotFoundError(UUID)
def getByGID(self, UUID: PLAYERID) -> Player:
for player in self.players:
if str(player.id) == str(UUID):
return player
raise PlayerNotFoundError(UUID)
def getByRole(self, role: str) -> Player:
for player in self.getPerformable(countKilled=True):
if player.role == role:
return player
raise PlayerNotFoundError(role)
def kill(self, player: Player):
player.setKilled(True)
def heal(self, player: Player):
player.setKilled(False)
def whore(self, player: Player):
player.setWhore(True)
class Doings(Players):
def do_mafkill(self, targets: List[TARGETID]):
# все засыпают, просипается мафия. кого убить и т.д/////////////////////////////////////////////////////////////////////////////////////////////////////
if self.getMafiasCount() > 1: # голосовалка, не соблазнят компанию
random.shuffle(targets)
elif next(self.getMafias()).isFucked:
return
self.kill(self.getByTID(targets[0]))
def do_sherif(self, target: TARGETID) -> str:
# мафия сделала вибор, просипается шериф ////////////////////////////////////////////////////////////////////////////////////////////////////////////
if self.getByRole("s").isFucked:
return "BLOCKED"
return self.getByTID(target).role
def do_killer(self, target: TARGETID):
# просипается маньяк
if self.getByRole("k").isFucked:
return
self.kill(self.getByTID(target))
def do_doctor(self, target: TARGETID):
if self.getByRole("d").isFucked:
return
self.heal(self.getByTID(target))
def do_girl(self, target: TARGETID):
# а теперь путана, она действует всегда
self.whore(self.getByTID(target))
@property
def isFinished(self) -> Union[bool, None]:
"""
Returns:
False if mafia won, True if players won, None if game is still available
"""
if self.getMafiasCount() == 0:
return False
elif self.getGoodCount() == 0:
return True
else:
return None
@dataclass
class PerformOne:
player: Player
targetid: TARGETID
class PerformResult:
sherifflock=False
def __init__(self): # List потомушо вонючая мафия может выбрать ≥1 игрока
self.__data: Dict[str, Union[List[TARGETID]]] = {}
def set(self, value: PerformOne):
if value.player.role in self.__data:
self.__data[value.player.role].append(value.targetid)
else:
self.__data[value.player.role] = [value.targetid]
def __getitem__(self, role: str) -> Union[List[TARGETID], None]:
return self.__data.get(role)
def __len__(self) -> int:
return len(self.__data)
class Timer:
begin: float
duration: int
def currtime(self) -> float:
"i hate long names"
return datetime.datetime.timestamp(datetime.datetime.now())
def start(self, duration: int) -> Timer:
"""duration: seconds"""
self.begin = self.currtime()
self.duration = duration
return self
@property
def expiredate(self):
return self.begin+self.duration
@property
def isExpired(self) -> bool:
if self.currtime() >= self.expiredate:
return True
return False
def onExpire(self, callback: Callable):
asyncio.ensure_future(self.aonExpire(callback))
async def aonExpire(self, callback: Callable):
await asyncio.sleep(self.duration + (self.currtime()-self.begin))
callback()
class Game(Doings):
__result:Union[PerformResult,None]
__vote:Union[Dict,None]
__timer:Timer
__status:Union[Literal["perform"],Literal["vote"],Literal["pause"]]
@property
def status(self)->Tuple[Union[Literal["perform"],Literal["vote"],Literal["pause"]],int]:
return self.__status,int(self.__timer.expiredate)
@property
def result(self)->PerformResult:
if self.__result==None:
raise PermissionDeniedError("not allowed perform")
return self.__result
@property
def vote(self)->Dict:
if self.__vote==None:
raise PermissionDeniedError("not allowed vote")
return self.__vote
def performData(self, gid: PLAYERID, pid: TARGETID)->Union[None,str]:
"""returns role for sheriff. """
self.result.set(PerformOne(self.getByGID(gid), pid))
if self.getByGID(gid).role=="s" and not self.result.sherifflock:
self.result.sherifflock=True
return self.do_sherif(pid)
def voteData(self, gid: PLAYERID, pid: TARGETID):
self.vote[gid] = pid
async def getPerformData(self):
self.__status="perform"
self.__timer=Timer().start(TIMINGS["perform"])
await self.__room.send({"msg": "Update"})
self.__result = PerformResult()
pc = self.getPerformableCount()
while len(self.result) < pc and not self.__timer.isExpired:
await asyncio.sleep(1) # TODO: зафигачить таймер
return self.result
async def getVoteData(self):
self.__status="vote"
self.__timer=Timer().start(TIMINGS["vote"])
await self.__room.send({"msg": "Update"})
self.__vote = dict()
while len(self.vote.values()) < self.getAliveCount() and not self.__timer.isExpired:
await asyncio.sleep(1) # TODO: зафигачить таймер
return self.vote
def parsePerform(self):
data: PerformResult = self.result
if data["g"]:
self.do_girl(data["g"][0])
if data["k"]:
self.do_killer(data["k"][0])
mafias = data["m"]
if mafias != None: # TODO:Settings
self.do_mafkill(mafias)
if data["d"]:
self.do_doctor(data["d"][0])
def parseVote(self):
values_list = list(self.vote.values())
if len(values_list)<=0:
return
self.kill(self.getByTID(max(set(values_list), key=values_list.count)))
def finishCheck(self):
if self.isFinished != None:
self.__room.finish(self.isFinished)
async def startMainloop(self, room: Room):
self.__room = room
super(Game, self).__init__(self.__room.players)
# room.познакомитьИгроков()
while self.isFinished == None:
await room.checkConnectivity()
await room.send({"msg": "DoPerform"})
result = await self.getPerformData()
print(result)
self.parsePerform()
self.__result=None
self.finishCheck()
await room.send({"msg": "DoVote"})
result = await self.getVoteData()
print(result)
self.parseVote()
self.__vote=None
self.finishCheck()