From cc4990093e648eb1ffa4a7e4c33f16656e7210b1 Mon Sep 17 00:00:00 2001 From: Elnath Date: Sun, 13 Jun 2021 19:47:03 +0200 Subject: [PATCH] Added possibility to automatically end vote once everybody has voted --- GameFiles/Game.py | 15 ++++++++++++++- SecretBot.py | 12 ++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/GameFiles/Game.py b/GameFiles/Game.py index fc67f47..6b09ce7 100644 --- a/GameFiles/Game.py +++ b/GameFiles/Game.py @@ -168,6 +168,15 @@ class Game: vote_count[self.config["vote"][str(player_id)]] += 1 return vote_count[True] > vote_count[False] + @game_started + def get_auto_end_vote(self) -> bool: + return self.config["auto_end_vote"] + + @game_started + def set_auto_end_vote(self, auto_end_vote: bool): + self.config["auto_end_vote"] = auto_end_vote + self.save_function() # We do not use save_on_success decorator since this is not a coroutine + @game_started def get_chaos(self) -> int: return self.config["chaos"] @@ -195,6 +204,7 @@ class Game: self.config["players"] = players self.config["player_info"] = {str(player): {} for player in self.config["players"]} self.config["vote"] = None + self.config["auto_end_vote"] = True self.config["deck"] = [Policy.FASCIST.value] * 11 + [Policy.LIBERAL.value] * 6 random.shuffle(self.config["deck"]) self.config["discard"] = [] @@ -340,7 +350,8 @@ class Game: message_content.append(f":red_square: <@{player_id}> has voted NEIN") else: # Player has voted but the vote should not be revealed message_content.append(f":white_large_square: <@{player_id}> has voted") - + if self.get_auto_end_vote() and not self.config["vote"]["revealed"]: + message_content.append("The vote will automatically end once everybody has voted") message_content_str = "\n".join(message_content) if self.config["vote"]["message"] is None: self.config["vote"]["message"] = (await self.get_votes_channel().send(message_content_str, allowed_mentions = discord.AllowedMentions.none())).id @@ -353,6 +364,8 @@ class Game: logging.debug(f"[{self.guild.name}] Casting vote with value {vote} for user {user.display_name}") self.config["vote"][str(user.id)] = vote await self.update_vote_message() + if self.get_auto_end_vote() and all(self.config["vote"][str(player_id)] is not None for player_id in self.get_players_id()): + asyncio.create_task(self.stop_vote()) @vote_running @save_on_success diff --git a/SecretBot.py b/SecretBot.py index c1a7087..9049ef6 100755 --- a/SecretBot.py +++ b/SecretBot.py @@ -244,6 +244,18 @@ class SecretBot(commands.Cog): await game.stop_vote() await ctx.message.delete() + @commands.command("AutoEndVote", help = "Set whether votes of the current game should end automatically when everybody has voted", usage = "true|false|get") + async def auto_end_vote(self, ctx: commands.Context, value): + game = await self.get_running_game_or_error_message(ctx) + await self.check_is_administrator_or_gm(ctx) + if value in ["true", "false"]: + game.set_auto_end_vote(value == "true") + await ctx.reply(":white_check_mark: Done.") + elif value == "get": + await ctx.reply(f"AutoEndVote currently set to {str(game.get_auto_end_vote()).lower()}.") + else: + await ctx.reply(":dizzy_face: You should give either 'true', 'false' or 'get'") + @commands.command("Legislate", help = "Start the legislative session by drawing three policies") async def draw_policies(self, ctx: commands.Context): game = await self.get_running_game_or_error_message(ctx)