fix middle_cards, fix async, discussion timer added

This commit is contained in:
Bibin Muttappillil 2020-04-15 17:23:24 +02:00
parent be93817063
commit e56a5d1629
4 changed files with 41 additions and 29 deletions

View File

@ -108,6 +108,7 @@ def error_handling(command):
@error_handling @error_handling
async def start(ctx): async def start(ctx):
game_instances[ctx.channel].game = bot.loop.create_task(game_instances[ctx.channel].round()) game_instances[ctx.channel].game = bot.loop.create_task(game_instances[ctx.channel].round())
await game_instances[ctx.channel].game
@game.command() @game.command()
@ -133,13 +134,6 @@ async def players(ctx):
async def roles(ctx): async def roles(ctx):
await game_instances[ctx.channel].set_roles(ctx.message.content.split()[3:]) # exclude commands await game_instances[ctx.channel].set_roles(ctx.message.content.split()[3:]) # exclude commands
# ONLY FOR TESTING
@game.command()
@game_running
@error_handling
async def vote(ctx):
await game_instances[ctx.channel].vote()
# smaller commands # smaller commands

View File

@ -13,6 +13,7 @@ class Game:
self.channel = channel self.channel = channel
self.player_list = [] self.player_list = []
self.role_list = [] self.role_list = []
self.discussion_time = 300 # seconds
async def send(self, message): async def send(self, message):
await self.channel.send(embed=discord.Embed(description=message, color=0x00ffff)) await self.channel.send(embed=discord.Embed(description=message, color=0x00ffff))
@ -43,7 +44,7 @@ class Game:
if r in [Werewolf, Mason]: if r in [Werewolf, Mason]:
self.role[r] = [] self.role[r] = []
else: else:
r(self) r(self).add_yourself()
self.voting_list = self.player_list + [No_player()] self.voting_list = self.player_list + [No_player()]
for c in self.voting_list: for c in self.voting_list:
c.reset() c.reset()
@ -52,8 +53,10 @@ class Game:
def distribute_roles(self): def distribute_roles(self):
shuffle(self.role_list) shuffle(self.role_list)
for i in range(len(self.player_list)): for i in range(len(self.player_list)):
self.role_list[i](self, self.player_list[i]) role_obj = self.role_list[i](self, self.player_list[i])
self.middle_card = self.role_list[-3:] self.player_list[i].setRole(role_obj)
role_obj.add_yourself()
self.middle_card = [r(self) for r in self.role_list[-3:]]
async def start_night(self): async def start_night(self):
await self.for_all_player(lambda p: p.send_normal("*The night has begun*")) await self.for_all_player(lambda p: p.send_normal("*The night has begun*"))
@ -78,14 +81,26 @@ class Game:
await self.send("The day has started") await self.send("The day has started")
async def discussion_timer(self): async def discussion_timer(self):
pass await self.send(f"You have {self.discussion_time / 60} minutes to discuss")
await asyncio.sleep(self.discussion_time / 2)
await self.send("Half of the time has passed")
await asyncio.sleep(self.discussion_time / 2 - 60)
await self.send("One minute remaining")
await asyncio.sleep(30)
await self.send("30 seconds remaining")
await asyncio.sleep(30)
async def early_vote(self):
await self.for_all_player(lambda p: p.ready_to_vote())
async def discussion_finished(self):
done, pending = await asyncio.wait([self.discussion_timer(), self.early_vote()], return_when=asyncio.FIRST_COMPLETED)
for p in pending:
p.cancel()
await asyncio.wait(pending)
async def vote(self): async def vote(self):
# vote
# replace with dm: await self.receive('$vote')
await self.send("Vote in DM") await self.send("Vote in DM")
await self.for_all_player(lambda p: p.cast_vote("Who do you want to kill?", self.voting_list)) await self.for_all_player(lambda p: p.cast_vote("Who do you want to kill?", self.voting_list))
def tally(self): def tally(self):
@ -151,15 +166,17 @@ class Game:
return werewolf_won, village_won, tanner_won, dead return werewolf_won, village_won, tanner_won, dead
async def result(self, werewolf_won, village_won, tanner_won, dead): async def result(self, werewolf_won, village_won, tanner_won, dead):
winnning = []
if werewolf_won: if werewolf_won:
winnning = ["Werewolves won!"] winnning.append("Werewolves")
if village_won: if village_won:
winnning = ["Village won!"] winnning.append("Village")
if tanner_won: if tanner_won:
winnning.append(f"{sum(1 for d in dead if d.day_role.is_role(Tanner))} tanner won") winnning.append(f"{sum(1 for d in dead if d.day_role.is_role(Tanner))} tanner")
if len(winnning) == 0:
winnning = ["No one"]
embed = discord.Embed(title=' and '.join(winnning), color=0x00ffff) embed = discord.Embed(title=f"{' and '.join(winnning)} won!", color=0x00ffff)
for p in self.player_list: for p in self.player_list:
won_emoji = ":trophy:" if p.won else ":frowning2:" won_emoji = ":trophy:" if p.won else ":frowning2:"
dead_emoji = ":skull:" if p.dead else ":no_mouth:" dead_emoji = ":skull:" if p.dead else ":no_mouth:"
@ -179,15 +196,12 @@ class Game:
self.distribute_roles() self.distribute_roles()
await self.start_night() await self.start_night()
await self.send_role() await self.send_role()
await self.night_phases() await self.night_phases()
await self.start_day() await self.start_day()
await self.discussion_finished()
await self.vote() await self.vote()
self.tally() self.tally()
await self.result(*self.who_won(self.who_dead())) await self.result(*self.who_won(self.who_dead()))
await self.send("Round ended") await self.send("Round ended")
finally: finally:
self.end() self.end()

View File

@ -85,6 +85,12 @@ class Player:
async def cast_vote(self, question, options): async def cast_vote(self, question, options):
self.vote = options[await self.get_choice(question, options)] self.vote = options[await self.get_choice(question, options)]
async def ready_to_vote(self):
def check(msg):
return msg.channel == self.dm and msg.author == self.member and msg.content.casefold() == "vote"
await self.game.bot.wait_for('message', check=check)
await self.send_confirmation("You are ready to vote")
class No_player(Player): class No_player(Player):
def name(self): def name(self):

View File

@ -7,8 +7,6 @@ class Role:
def __init__(self, game, player=No_player()): def __init__(self, game, player=No_player()):
self.game = game self.game = game
self.player = player self.player = player
self.player.setRole(self)
self.add_yourself()
def add_yourself(self): def add_yourself(self):
self.game.role[type(self)] = self self.game.role[type(self)] = self
@ -127,7 +125,7 @@ class Robber(Role):
self.choice = await self.player.get_choice("Which player do you want to rob?", self.player.other()) self.choice = await self.player.get_choice("Which player do you want to rob?", self.player.other())
@Role.no_player @Role.no_player
def simulate(self): async def simulate(self):
self.player.swap(self.player.other()[self.choice]) self.player.swap(self.player.other()[self.choice])
@Role.no_player @Role.no_player
@ -141,7 +139,7 @@ class Troublemaker(Role):
self.A, self.B = await self.player.get_double_choice("Who do you want to exchange? (send two numbers)", self.player.other()) self.A, self.B = await self.player.get_double_choice("Who do you want to exchange? (send two numbers)", self.player.other())
@Role.no_player @Role.no_player
def simulate(self): async def simulate(self):
self.player.other()[self.A].swap(self.player.other()[self.B]) self.player.other()[self.A].swap(self.player.other()[self.B])
@ -151,7 +149,7 @@ class Drunk(Role):
self.choice = await self.player.get_choice("Which card from the middle do you want to take?", ["left", "middle", "right"]) self.choice = await self.player.get_choice("Which card from the middle do you want to take?", ["left", "middle", "right"])
@Role.no_player @Role.no_player
def simulate(self): async def simulate(self):
self.player.day_role, self.game.middle_card[self.choice] = self.game.middle_card[self.choice], self.player.day_role # swap self.player.day_role, self.game.middle_card[self.choice] = self.game.middle_card[self.choice], self.player.day_role # swap