From b92ab9ce2fcb851e6d31ab03c94e25544521caf3 Mon Sep 17 00:00:00 2001 From: Elnath Date: Sun, 20 Jun 2021 21:08:19 +0200 Subject: [PATCH] Small improvements on vote casting --- GameFiles/Game.py | 25 ++++++++++++++++++------- SecretBot.py | 2 +- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/GameFiles/Game.py b/GameFiles/Game.py index 036ac12..1e573a7 100644 --- a/GameFiles/Game.py +++ b/GameFiles/Game.py @@ -175,6 +175,10 @@ class Game: def is_vote_running(self) -> bool: return self.config["vote"] is not None + @game_started + def can_cast_votes(self) -> bool: + return self.is_vote_running() and self.config["vote"]["can_cast_votes"] + @vote_running def is_vote_passing(self) -> bool: vote_count = defaultdict(int) @@ -343,6 +347,7 @@ class Game: "chancellor": chancellor.id, "message": None, "revealed": False, + "can_cast_votes": True, } self.config["vote"].update({str(player_id): None for player_id in self.get_players_id()}) await self.update_vote_message() @@ -357,9 +362,11 @@ class Game: "Do you want to elect the following government?", f":crown: <@{president}> as president", f":person_in_tuxedo: <@{chancellor}> as chancellor", - "You can vote by typing `!ja` or `!nein` in your channel" - "", ] + if self.can_cast_votes(): + message_content.append("You can vote by typing `!ja` or `!nein` in your channel") + message_content.append("") # Just to mark a separation between the message header and the votes + for player_id in self.get_players_id(): player_vote = self.config["vote"][str(player_id)] if player_vote is None: @@ -372,8 +379,10 @@ 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"]: + if self.get_auto_end_vote() and self.can_cast_votes(): message_content.append("The vote will automatically end once everybody has voted") + if not self.can_cast_votes(): + message_content.append("**The vote has ended**") 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 @@ -384,19 +393,21 @@ class Game: @save_on_success async def cast_vote(self, user: discord.Member, vote: Union[bool, None]): logging.debug(f"[{self.guild.name}] Casting vote with value {vote} for user {user.display_name}") + if not self.can_cast_votes(): + raise RuntimeError("Votes can not be casted right now") 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()) + await self.update_vote_message() @vote_running @save_on_success async def stop_vote(self): logging.debug(f"[{self.guild.name}] Stopping the vote") - passed = self.is_vote_passing() + self.config["vote"]["can_cast_votes"] = False self.config["vote"]["revealed"] = True await self.update_vote_message() - await self.get_votes_channel().send("**The vote has ended**") + passed = self.is_vote_passing() announcement_content = [ f"<@&{self.get_player_role_id()}> <@&{self.get_gm_role_id()}> the vote has ended!", f"{':green_square:' if passed else ':red_square:'} The vote has **{'' if passed else 'not '}passed**" @@ -408,8 +419,8 @@ class Game: if self.config["chaos"] > 0: # If there was some chaos announcement_content.append(":relaxed: The country has calmed and the chaos counter has been reset") self.config["chaos"] = 0 # Anyway, the chaos is reset by a successful vote - await self.get_announcements_channel().send("\n".join(announcement_content), allowed_mentions = discord.AllowedMentions(roles = True)) self.config["vote"] = None + await self.get_announcements_channel().send("\n".join(announcement_content), allowed_mentions = discord.AllowedMentions(roles = True)) # After announcing a non-passing vote, we increase the chaos and announce it if not passed: await self.increase_chaos() diff --git a/SecretBot.py b/SecretBot.py index a138309..780932a 100755 --- a/SecretBot.py +++ b/SecretBot.py @@ -207,7 +207,7 @@ class SecretBot(commands.Cog): async def cast_vote(self, ctx: commands.Context, vote: bool): game = self.games_file[ctx.guild] - if game.is_started() and game.is_vote_running(): + if game.is_started() and game.is_vote_running() and game.can_cast_votes(): if ctx.author.id in game.get_players_id(): if ctx.channel == game.get_player_channel(ctx.author): await game.cast_vote(ctx.author, vote)