|
|
@ -11,7 +11,7 @@ class Game:
|
|
|
|
Base class to run a single game of Werewolf
|
|
|
|
Base class to run a single game of Werewolf
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, role_code=None):
|
|
|
|
def __init__(self, role_code):
|
|
|
|
self.roles = []
|
|
|
|
self.roles = []
|
|
|
|
self.role_code = role_code
|
|
|
|
self.role_code = role_code
|
|
|
|
|
|
|
|
|
|
|
@ -42,6 +42,11 @@ class Game:
|
|
|
|
3. Check Initial role setup (including alerts)
|
|
|
|
3. Check Initial role setup (including alerts)
|
|
|
|
4. Start game
|
|
|
|
4. Start game
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
if len(self.players) != self.roles:
|
|
|
|
|
|
|
|
ctx.send("Players does not match roles, cannot start")
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _cycle(self):
|
|
|
|
async def _cycle(self):
|
|
|
@ -61,35 +66,70 @@ class Game:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
await self._at_start():
|
|
|
|
await self._at_start():
|
|
|
|
|
|
|
|
|
|
|
|
async def _at_game_start(self):
|
|
|
|
async def _at_game_start(self): # ID 0
|
|
|
|
|
|
|
|
if self.game_over:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
await self._notify(0)
|
|
|
|
|
|
|
|
|
|
|
|
asyncio.sleep(60)
|
|
|
|
asyncio.sleep(60)
|
|
|
|
await self._at_day_start()
|
|
|
|
await self._at_day_start()
|
|
|
|
|
|
|
|
|
|
|
|
async def _at_day_start(self):
|
|
|
|
async def _at_day_start(self): # ID 1
|
|
|
|
|
|
|
|
if self.game_over:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
await self._notify(1)
|
|
|
|
|
|
|
|
|
|
|
|
asyncio.sleep(240) # 4 minute days
|
|
|
|
asyncio.sleep(240) # 4 minute days
|
|
|
|
await self._at_day_end()
|
|
|
|
await self._at_day_end()
|
|
|
|
|
|
|
|
|
|
|
|
async def _at_vote(self):
|
|
|
|
async def _at_vote(self, target): # ID 2
|
|
|
|
|
|
|
|
if self.game_over:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
await self._notify(2, target)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _at_kill(self, target): # ID 3
|
|
|
|
|
|
|
|
if self.game_over:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
await self._notify(3, target)
|
|
|
|
|
|
|
|
|
|
|
|
async def _at_kill(self):
|
|
|
|
async def _at_hang(self, target): # ID 4
|
|
|
|
|
|
|
|
if self.game_over:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
await self._notify(4, target)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _at_day_end(self): # ID 5
|
|
|
|
|
|
|
|
if self.game_over:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
await self._notify(5)
|
|
|
|
|
|
|
|
|
|
|
|
async def _at_day_end(self):
|
|
|
|
|
|
|
|
asyncio.sleep(60)
|
|
|
|
asyncio.sleep(60)
|
|
|
|
await self._at_night_start()
|
|
|
|
await self._at_night_start()
|
|
|
|
|
|
|
|
|
|
|
|
async def _at_night_start(self):
|
|
|
|
async def _at_night_start(self): # ID 6
|
|
|
|
asyncio.sleep(240)
|
|
|
|
if self.game_over:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
await self._notify(6)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
asyncio.sleep(120)
|
|
|
|
|
|
|
|
# 2 minutes left
|
|
|
|
|
|
|
|
asyncio.sleep(90)
|
|
|
|
|
|
|
|
# 30 seconds left
|
|
|
|
|
|
|
|
asyncio.sleep(30)
|
|
|
|
|
|
|
|
|
|
|
|
await self._at_night_end()
|
|
|
|
await self._at_night_end()
|
|
|
|
|
|
|
|
|
|
|
|
async def _at_night_end(self):
|
|
|
|
async def _at_night_end(self): # ID 7
|
|
|
|
self._notify()
|
|
|
|
if self.game_over:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
await self._notify(7)
|
|
|
|
|
|
|
|
|
|
|
|
asyncio.sleep(15)
|
|
|
|
asyncio.sleep(15)
|
|
|
|
await self._at_day_start()
|
|
|
|
await self._at_day_start()
|
|
|
|
|
|
|
|
|
|
|
|
async def _notify(self, event):
|
|
|
|
async def _notify(self, event):
|
|
|
|
for i in range(10):
|
|
|
|
for i in range(10):
|
|
|
|
tasks = []
|
|
|
|
tasks = []
|
|
|
|
role_action = [role for role in self.roles if role.action==i]
|
|
|
|
role_order = [role for role in self.roles if role.priority==i]
|
|
|
|
for role in role_action:
|
|
|
|
for role in role_action:
|
|
|
|
tasks.append(asyncio.ensure_future(role.on_event(event))
|
|
|
|
tasks.append(asyncio.ensure_future(role.on_event(event))
|
|
|
|
# self.loop.create_task(role.on_event(event))
|
|
|
|
# self.loop.create_task(role.on_event(event))
|
|
|
@ -98,7 +138,7 @@ class Game:
|
|
|
|
|
|
|
|
|
|
|
|
async def join(self, member: discord.Member):
|
|
|
|
async def join(self, member: discord.Member):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Joins a game
|
|
|
|
Have a member join a game
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
if self.started:
|
|
|
|
if self.started:
|
|
|
|
return "**Game has already started!**"
|
|
|
|
return "**Game has already started!**"
|
|
|
@ -108,7 +148,27 @@ class Game:
|
|
|
|
|
|
|
|
|
|
|
|
self.started.append(member)
|
|
|
|
self.started.append(member)
|
|
|
|
|
|
|
|
|
|
|
|
return "{} has been added to the game, total players is **{}**".format(member.mention, len(self.players))
|
|
|
|
out = "{} has been added to the game, total players is **{}**".format(member.mention, len(self.players))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def quit(self, member: discord.Member):
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Have a member quit a game
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
player = await self.get_player_by_member(member)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not player:
|
|
|
|
|
|
|
|
return "You're not in a game!"
|
|
|
|
|
|
|
|
if self.started:
|
|
|
|
|
|
|
|
await self.kill()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if member in self.players:
|
|
|
|
|
|
|
|
return "{} is already in the game!".format(member.mention)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.started.append(member)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out = "{} has been added to the game, total players is **{}**".format(member.mention, len(self.players))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_roles(self, role_code=None):
|
|
|
|
async def get_roles(self, role_code=None):
|
|
|
|
if role_code:
|
|
|
|
if role_code:
|
|
|
|