nonebot-plugin-werewolf 1.1.1__py3-none-any.whl → 1.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. nonebot_plugin_werewolf/__init__.py +8 -4
  2. nonebot_plugin_werewolf/_timeout.py +110 -0
  3. nonebot_plugin_werewolf/config.py +15 -18
  4. nonebot_plugin_werewolf/constant.py +18 -3
  5. nonebot_plugin_werewolf/exception.py +1 -1
  6. nonebot_plugin_werewolf/game.py +91 -110
  7. nonebot_plugin_werewolf/matchers/__init__.py +2 -0
  8. nonebot_plugin_werewolf/matchers/message_in_game.py +15 -0
  9. nonebot_plugin_werewolf/{ob11_ext.py → matchers/ob11_ext.py} +26 -20
  10. nonebot_plugin_werewolf/matchers/start_game.py +56 -0
  11. nonebot_plugin_werewolf/player_set.py +9 -7
  12. nonebot_plugin_werewolf/players/__init__.py +10 -0
  13. nonebot_plugin_werewolf/players/can_shoot.py +59 -0
  14. nonebot_plugin_werewolf/players/civilian.py +7 -0
  15. nonebot_plugin_werewolf/players/guard.py +37 -0
  16. nonebot_plugin_werewolf/players/hunter.py +8 -0
  17. nonebot_plugin_werewolf/players/idiot.py +44 -0
  18. nonebot_plugin_werewolf/players/joker.py +21 -0
  19. nonebot_plugin_werewolf/players/player.py +161 -0
  20. nonebot_plugin_werewolf/players/prophet.py +30 -0
  21. nonebot_plugin_werewolf/players/werewolf.py +67 -0
  22. nonebot_plugin_werewolf/players/witch.py +72 -0
  23. nonebot_plugin_werewolf/players/wolfking.py +14 -0
  24. nonebot_plugin_werewolf/utils.py +83 -65
  25. {nonebot_plugin_werewolf-1.1.1.dist-info → nonebot_plugin_werewolf-1.1.3.dist-info}/METADATA +20 -8
  26. nonebot_plugin_werewolf-1.1.3.dist-info/RECORD +29 -0
  27. {nonebot_plugin_werewolf-1.1.1.dist-info → nonebot_plugin_werewolf-1.1.3.dist-info}/WHEEL +1 -1
  28. nonebot_plugin_werewolf/matchers.py +0 -63
  29. nonebot_plugin_werewolf/player.py +0 -455
  30. nonebot_plugin_werewolf-1.1.1.dist-info/RECORD +0 -15
  31. {nonebot_plugin_werewolf-1.1.1.dist-info → nonebot_plugin_werewolf-1.1.3.dist-info}/LICENSE +0 -0
  32. {nonebot_plugin_werewolf-1.1.1.dist-info → nonebot_plugin_werewolf-1.1.3.dist-info}/top_level.txt +0 -0
@@ -1,455 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import asyncio
4
- import asyncio.timeouts
5
- from dataclasses import dataclass
6
- from typing import TYPE_CHECKING, ClassVar, TypeVar, final
7
-
8
- from nonebot.log import logger
9
- from nonebot_plugin_alconna.uniseg import Receipt, Target, UniMessage
10
- from typing_extensions import override
11
-
12
- from .constant import GameStatus, KillReason, Role, RoleGroup, role_name_conv
13
- from .exception import GameFinishedError
14
- from .utils import InputStore, check_index
15
-
16
- if TYPE_CHECKING:
17
- from collections.abc import Callable
18
-
19
- from nonebot.adapters import Bot
20
-
21
- from .game import Game
22
- from .player_set import PlayerSet
23
-
24
-
25
- P = TypeVar("P", bound=type["Player"])
26
- PLAYER_CLASS: dict[Role, type[Player]] = {}
27
-
28
-
29
- def register_role(role: Role, role_group: RoleGroup, /) -> Callable[[P], P]:
30
- def decorator(cls: P, /) -> P:
31
- cls.role = role
32
- cls.role_group = role_group
33
- PLAYER_CLASS[role] = cls
34
- return cls
35
-
36
- return decorator
37
-
38
-
39
- @dataclass
40
- class KillInfo:
41
- reason: KillReason
42
- killers: PlayerSet
43
-
44
-
45
- class Player:
46
- role: ClassVar[Role]
47
- role_group: ClassVar[RoleGroup]
48
-
49
- bot: Bot
50
- game: Game
51
- user: Target
52
- name: str
53
- alive: bool = True
54
- killed: asyncio.Event
55
- kill_info: KillInfo | None = None
56
- selected: Player | None = None
57
-
58
- @final
59
- def __init__(self, bot: Bot, game: Game, user: Target, name: str) -> None:
60
- self.bot = bot
61
- self.game = game
62
- self.user = user
63
- self.name = name
64
- self.killed = asyncio.Event()
65
-
66
- @final
67
- @classmethod
68
- def new(
69
- cls,
70
- role: Role,
71
- bot: Bot,
72
- game: Game,
73
- user: Target,
74
- name: str,
75
- ) -> Player:
76
- if role not in PLAYER_CLASS:
77
- raise ValueError(f"Unexpected role: {role!r}")
78
-
79
- return PLAYER_CLASS[role](bot, game, user, name)
80
-
81
- def __repr__(self) -> str:
82
- return f"<{self.role_name}: user={self.name!r} alive={self.alive}>"
83
-
84
- @property
85
- def user_id(self) -> str:
86
- return self.user.id
87
-
88
- @property
89
- def role_name(self) -> str:
90
- return role_name_conv[self.role]
91
-
92
- @final
93
- def _log(self, text: str) -> None:
94
- text = text.replace("\n", "\\n")
95
- logger.opt(colors=True).info(
96
- f"<b><e>{self.game.group.id}</e></b> | "
97
- f"[<b><m>{self.role_name}</m></b>] "
98
- f"<y>{self.name}</y>(<e>{self.user_id}</e>) | "
99
- f"{text}",
100
- )
101
-
102
- @final
103
- async def send(self, message: str | UniMessage) -> Receipt:
104
- if isinstance(message, str):
105
- message = UniMessage.text(message)
106
-
107
- self._log(f"<g>Send</g> | {message}")
108
- return await message.send(target=self.user, bot=self.bot)
109
-
110
- @final
111
- async def receive(self, prompt: str | UniMessage | None = None) -> UniMessage:
112
- if prompt:
113
- await self.send(prompt)
114
-
115
- result = await InputStore.fetch(self.user.id)
116
- self._log(f"<y>Recv</y> | {result}")
117
- return result
118
-
119
- @final
120
- async def receive_text(self) -> str:
121
- return (await self.receive()).extract_plain_text()
122
-
123
- async def interact(self) -> None:
124
- return
125
-
126
- async def notify_role(self) -> None:
127
- await self.send(f"你的身份: {self.role_name}")
128
-
129
- async def kill(self, reason: KillReason, *killers: Player) -> bool:
130
- from .player_set import PlayerSet
131
-
132
- self.alive = False
133
- self.kill_info = KillInfo(reason, PlayerSet(killers))
134
- return True
135
-
136
- async def post_kill(self) -> None:
137
- self.killed.set()
138
-
139
- async def vote(self, players: PlayerSet) -> tuple[Player, Player] | None:
140
- await self.send(
141
- f"请选择需要投票的玩家:\n{players.show()}"
142
- "\n\n发送编号选择玩家\n发送 “/stop” 弃票"
143
- )
144
-
145
- while True:
146
- text = await self.receive_text()
147
- if text == "/stop":
148
- await self.send("你选择了弃票")
149
- return None
150
- index = check_index(text, len(players))
151
- if index is not None:
152
- selected = index - 1
153
- break
154
- await self.send("输入错误,请发送编号选择玩家")
155
-
156
- player = players[selected]
157
- await self.send(f"投票的玩家: {player.name}")
158
- return self, player
159
-
160
-
161
- class CanShoot(Player):
162
- @override
163
- async def post_kill(self) -> None:
164
- if self.kill_info and self.kill_info.reason == KillReason.Poison:
165
- await self.send("你昨晚被女巫毒杀,无法使用技能")
166
- return await super().post_kill()
167
-
168
- await self.game.send(
169
- UniMessage.text(f"{self.role_name} ")
170
- .at(self.user_id)
171
- .text(f" 死了\n请{self.role_name}决定击杀目标...")
172
- )
173
-
174
- self.game.state.shoot = (None, None)
175
- shoot = await self.shoot()
176
-
177
- if shoot is not None:
178
- self.game.state.shoot = (self, shoot)
179
- await self.send(
180
- UniMessage.text(f"{self.role_name} ")
181
- .at(self.user_id)
182
- .text(" 射杀了玩家 ")
183
- .at(shoot.user_id)
184
- )
185
- await shoot.kill(KillReason.Shoot, self)
186
- else:
187
- await self.send(f"{self.role_name}选择了取消技能")
188
- return await super().post_kill()
189
-
190
- async def shoot(self) -> Player | None:
191
- players = self.game.players.alive().exclude(self)
192
- await self.send(
193
- "请选择需要射杀的玩家:\n"
194
- + players.show()
195
- + "\n\n发送编号选择玩家"
196
- + "\n发送 “/stop” 取消技能"
197
- )
198
-
199
- while True:
200
- text = await self.receive_text()
201
- if text == "/stop":
202
- await self.send("已取消技能")
203
- return None
204
- index = check_index(text, len(players))
205
- if index is not None:
206
- selected = index - 1
207
- break
208
- await self.send("输入错误,请发送编号选择玩家")
209
-
210
- await self.send(f"选择射杀的玩家: {players[selected].name}")
211
- return players[selected]
212
-
213
-
214
- @register_role(Role.Werewolf, RoleGroup.Werewolf)
215
- class Werewolf(Player):
216
- @override
217
- async def notify_role(self) -> None:
218
- await super().notify_role()
219
- partners = self.game.players.alive().select(RoleGroup.Werewolf).exclude(self)
220
- if partners:
221
- await self.send(
222
- "你的队友:\n"
223
- + "\n".join(f" {p.role_name}: {p.name}" for p in partners)
224
- )
225
-
226
- @override
227
- async def interact(self) -> None:
228
- players = self.game.players.alive()
229
- partners = players.select(RoleGroup.Werewolf).exclude(self)
230
-
231
- # 避免阻塞
232
- def broadcast(msg: str | UniMessage) -> asyncio.Task[None]:
233
- return asyncio.create_task(partners.broadcast(msg))
234
-
235
- msg = UniMessage()
236
- if partners:
237
- msg = (
238
- msg.text("你的队友:\n")
239
- .text("\n".join(f" {p.role_name}: {p.name}" for p in partners))
240
- .text("\n所有私聊消息将被转发至队友\n\n")
241
- )
242
- await self.send(
243
- msg.text("请选择今晚的目标:\n")
244
- .text(players.show())
245
- .text("\n\n发送编号选择玩家")
246
- .text("\n发送 “/stop” 结束回合")
247
- .text("\n\n意见未统一将空刀")
248
- )
249
-
250
- selected = None
251
- finished = False
252
- while selected is None or not finished:
253
- input_msg = await self.receive()
254
- text = input_msg.extract_plain_text()
255
- index = check_index(text, len(players))
256
- if index is not None:
257
- selected = index - 1
258
- msg = f"当前选择玩家: {players[selected].name}"
259
- await self.send(f"{msg}\n发送 “/stop” 结束回合")
260
- broadcast(f"队友 {self.name} {msg}")
261
- if text == "/stop":
262
- if selected is not None:
263
- finished = True
264
- await self.send("你已结束当前回合")
265
- broadcast(f"队友 {self.name} 结束当前回合")
266
- else:
267
- await self.send("当前未选择玩家,无法结束回合")
268
- broadcast(UniMessage.text(f"队友 {self.name}:\n") + input_msg)
269
-
270
- self.selected = players[selected]
271
-
272
-
273
- @register_role(Role.WolfKing, RoleGroup.Werewolf)
274
- class WolfKing(CanShoot, Werewolf):
275
- @override
276
- async def notify_role(self) -> None:
277
- await super().notify_role()
278
- await self.send("作为狼王,你可以在死后射杀一名玩家")
279
-
280
-
281
- @register_role(Role.Prophet, RoleGroup.GoodGuy)
282
- class Prophet(Player):
283
- @override
284
- async def interact(self) -> None:
285
- players = self.game.players.alive().exclude(self)
286
- await self.send(
287
- UniMessage.text("请选择需要查验身份的玩家:\n")
288
- .text(players.show())
289
- .text("\n\n发送编号选择玩家")
290
- )
291
-
292
- while True:
293
- text = await self.receive_text()
294
- index = check_index(text, len(players))
295
- if index is not None:
296
- selected = index - 1
297
- break
298
- await self.send("输入错误,请发送编号选择玩家")
299
-
300
- player = players[selected]
301
- result = "狼人" if player.role_group == RoleGroup.Werewolf else "好人"
302
- await self.send(f"玩家 {player.name} 的阵营是『{result}』")
303
-
304
-
305
- @register_role(Role.Witch, RoleGroup.GoodGuy)
306
- class Witch(Player):
307
- antidote: int = 1
308
- poison: int = 1
309
-
310
- async def handle_killed(self) -> bool:
311
- msg = UniMessage()
312
- if (killed := self.game.state.killed) is not None:
313
- msg.text(f"今晚 {killed.name} 被刀了\n\n")
314
- else:
315
- await self.send("今晚没有人被刀")
316
- return False
317
-
318
- if not self.antidote:
319
- await self.send(msg.text("你已经用过解药了"))
320
- return False
321
-
322
- await self.send(msg.text("使用解药请发送 “1”\n不使用解药请发送 “/stop”"))
323
-
324
- while True:
325
- text = await self.receive_text()
326
- if text == "1":
327
- self.antidote = 0
328
- self.game.state.antidote.add(killed)
329
- await self.send(f"你对 {killed.name} 使用了解药,回合结束")
330
- return True
331
- if text == "/stop":
332
- return False
333
- await self.send("输入错误: 请输入 “1” 或 “/stop”")
334
-
335
- @override
336
- async def interact(self) -> None:
337
- if await self.handle_killed():
338
- return
339
-
340
- if not self.poison:
341
- await self.send("你没有可以使用的药水,回合结束")
342
- return
343
-
344
- players = self.game.players.alive()
345
- await self.send(
346
- UniMessage.text("你有一瓶毒药\n")
347
- .text("玩家列表:\n")
348
- .text(players.show())
349
- .text("\n\n发送玩家编号使用毒药")
350
- .text("\n发送 “/stop” 结束回合(不使用药水)")
351
- )
352
-
353
- while True:
354
- text = await self.receive_text()
355
- index = check_index(text, len(players))
356
- if index is not None:
357
- selected = index - 1
358
- break
359
- if text == "/stop":
360
- await self.send("你选择不使用毒药,回合结束")
361
- return
362
- await self.send("输入错误: 请发送玩家编号或 “/stop”")
363
-
364
- self.poison = 0
365
- player = players[selected]
366
- self.game.state.poison.add((self, player))
367
- await self.send(f"当前回合选择对玩家 {player.name} 使用毒药\n回合结束")
368
-
369
-
370
- @register_role(Role.Hunter, RoleGroup.GoodGuy)
371
- class Hunter(CanShoot, Player):
372
- pass
373
-
374
-
375
- @register_role(Role.Guard, RoleGroup.GoodGuy)
376
- class Guard(Player):
377
- @override
378
- async def interact(self) -> None:
379
- players = self.game.players.alive()
380
- await self.send(
381
- UniMessage.text("请选择需要保护的玩家:\n")
382
- .text(players.show())
383
- .text("\n\n发送编号选择玩家")
384
- .text("\n发送 “/stop” 结束回合")
385
- )
386
-
387
- while True:
388
- text = await self.receive_text()
389
- if text == "/stop":
390
- await self.send("你选择了取消,回合结束")
391
- return
392
- index = check_index(text, len(players))
393
- if index is not None:
394
- selected = index - 1
395
- if players[selected] is self.selected:
396
- await self.send("守卫不能连续两晚保护同一目标")
397
- continue
398
- break
399
- await self.send("输入错误,请发送编号选择玩家")
400
-
401
- self.selected = players[selected]
402
- self.game.state.protected.add(self.selected)
403
- await self.send(f"本回合保护的玩家: {self.selected.name}")
404
-
405
-
406
- @register_role(Role.Idiot, RoleGroup.GoodGuy)
407
- class Idiot(Player):
408
- voted: bool = False
409
-
410
- @override
411
- async def notify_role(self) -> None:
412
- await super().notify_role()
413
- await self.send(
414
- "作为白痴,你可以在首次被投票放逐时免疫放逐,但在之后的投票中无法继续投票"
415
- )
416
-
417
- @override
418
- async def kill(self, reason: KillReason, *killers: Player) -> bool:
419
- if reason == KillReason.Vote and not self.voted:
420
- self.voted = True
421
- await self.game.send(
422
- UniMessage.at(self.user_id)
423
- .text(" 的身份是白痴\n")
424
- .text("免疫本次投票放逐,且接下来无法参与投票"),
425
- )
426
- return False
427
- return await super().kill(reason, *killers)
428
-
429
- @override
430
- async def vote(self, players: PlayerSet) -> tuple[Player, Player] | None:
431
- if self.voted:
432
- await self.send("你已经发动过白痴身份的技能,无法参与本次投票")
433
- return None
434
- return await super().vote(players)
435
-
436
-
437
- @register_role(Role.Joker, RoleGroup.Others)
438
- class Joker(Player):
439
- @override
440
- async def notify_role(self) -> None:
441
- await super().notify_role()
442
- await self.send("你的胜利条件: 被投票放逐")
443
-
444
- @override
445
- async def kill(self, reason: KillReason, *killers: Player) -> bool:
446
- await super().kill(reason, *killers)
447
- if reason == KillReason.Vote:
448
- self.game.killed_players.append(self)
449
- raise GameFinishedError(GameStatus.Joker)
450
- return True
451
-
452
-
453
- @register_role(Role.Civilian, RoleGroup.GoodGuy)
454
- class Civilian(Player):
455
- pass
@@ -1,15 +0,0 @@
1
- nonebot_plugin_werewolf/__init__.py,sha256=I6lFyJgJ4Tz4t8gS2lrsx-7paN2h_PNoGjGR8ryvSeU,734
2
- nonebot_plugin_werewolf/config.py,sha256=gl_ujiY-8fxQ5hN9_gjL1WpJJR5UOBIRBGofbLcQzaE,2900
3
- nonebot_plugin_werewolf/constant.py,sha256=1XNTcTicil90WSviI2e33FX0LstWXl1aV2ugbhsDlIk,1909
4
- nonebot_plugin_werewolf/exception.py,sha256=Ui8rB1sBg6_p8JHSvrjCpUaXJlgrM_9XsLJ09k8F1bg,391
5
- nonebot_plugin_werewolf/game.py,sha256=Y9MQASaNdm2uuNw37IjdQEQzikWmbql_PWLHaL7lcB8,17414
6
- nonebot_plugin_werewolf/matchers.py,sha256=5FioDfARlxTEibGL1JMKUMzmTzOwljjWKAJxBnyzaYs,2106
7
- nonebot_plugin_werewolf/ob11_ext.py,sha256=P8uc3AdN5K5MzJaK80WDK85VKFg_CK5avDHu7ueMkho,2418
8
- nonebot_plugin_werewolf/player.py,sha256=ifqTBfNepQY5FCY7hZoUA6DcObkcFEiNmKEeRkitJqY,14749
9
- nonebot_plugin_werewolf/player_set.py,sha256=AI8v6KjH9ACtP4lvrd5IJfayan0qALmpTRLC3s_3DFw,2754
10
- nonebot_plugin_werewolf/utils.py,sha256=l7oNSK471SV5CaB1eEVyZm10XSBFf_TwNotvb30U6vE,6835
11
- nonebot_plugin_werewolf-1.1.1.dist-info/LICENSE,sha256=B_WbEqjGr6GYVNfEJPY31T1Opik7OtgOkhRs4Ig3e2M,1064
12
- nonebot_plugin_werewolf-1.1.1.dist-info/METADATA,sha256=5ikh-mdZTIObaFs9w7qgzrgsx31lCxB5Ag--C8stl0g,10447
13
- nonebot_plugin_werewolf-1.1.1.dist-info/WHEEL,sha256=cVxcB9AmuTcXqmwrtPhNK88dr7IR_b6qagTj0UvIEbY,91
14
- nonebot_plugin_werewolf-1.1.1.dist-info/top_level.txt,sha256=wLTfg8sTKbH9lLT9LtU118C9cTspEBJareLsrYM52YA,24
15
- nonebot_plugin_werewolf-1.1.1.dist-info/RECORD,,