Small improvements on vote casting

This commit is contained in:
Elnath 2021-06-20 21:08:19 +02:00
parent 3b8fc9e76d
commit b92ab9ce2f
2 changed files with 19 additions and 8 deletions

View File

@ -175,6 +175,10 @@ class Game:
def is_vote_running(self) -> bool: def is_vote_running(self) -> bool:
return self.config["vote"] is not None 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 @vote_running
def is_vote_passing(self) -> bool: def is_vote_passing(self) -> bool:
vote_count = defaultdict(int) vote_count = defaultdict(int)
@ -343,6 +347,7 @@ class Game:
"chancellor": chancellor.id, "chancellor": chancellor.id,
"message": None, "message": None,
"revealed": False, "revealed": False,
"can_cast_votes": True,
} }
self.config["vote"].update({str(player_id): None for player_id in self.get_players_id()}) self.config["vote"].update({str(player_id): None for player_id in self.get_players_id()})
await self.update_vote_message() await self.update_vote_message()
@ -357,9 +362,11 @@ class Game:
"Do you want to elect the following government?", "Do you want to elect the following government?",
f":crown: <@{president}> as president", f":crown: <@{president}> as president",
f":person_in_tuxedo: <@{chancellor}> as chancellor", 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(): for player_id in self.get_players_id():
player_vote = self.config["vote"][str(player_id)] player_vote = self.config["vote"][str(player_id)]
if player_vote is None: if player_vote is None:
@ -372,8 +379,10 @@ class Game:
message_content.append(f":red_square: <@{player_id}> has voted NEIN") message_content.append(f":red_square: <@{player_id}> has voted NEIN")
else: # Player has voted but the vote should not be revealed else: # Player has voted but the vote should not be revealed
message_content.append(f":white_large_square: <@{player_id}> has voted") 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") 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) message_content_str = "\n".join(message_content)
if self.config["vote"]["message"] is None: 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 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 @save_on_success
async def cast_vote(self, user: discord.Member, vote: Union[bool, None]): 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}") 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 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()): 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()) asyncio.create_task(self.stop_vote())
await self.update_vote_message()
@vote_running @vote_running
@save_on_success @save_on_success
async def stop_vote(self): async def stop_vote(self):
logging.debug(f"[{self.guild.name}] Stopping the vote") 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 self.config["vote"]["revealed"] = True
await self.update_vote_message() await self.update_vote_message()
await self.get_votes_channel().send("**The vote has ended**") passed = self.is_vote_passing()
announcement_content = [ announcement_content = [
f"<@&{self.get_player_role_id()}> <@&{self.get_gm_role_id()}> the vote has ended!", 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**" 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 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") 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 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 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 # After announcing a non-passing vote, we increase the chaos and announce it
if not passed: if not passed:
await self.increase_chaos() await self.increase_chaos()

View File

@ -207,7 +207,7 @@ class SecretBot(commands.Cog):
async def cast_vote(self, ctx: commands.Context, vote: bool): async def cast_vote(self, ctx: commands.Context, vote: bool):
game = self.games_file[ctx.guild] 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.author.id in game.get_players_id():
if ctx.channel == game.get_player_channel(ctx.author): if ctx.channel == game.get_player_channel(ctx.author):
await game.cast_vote(ctx.author, vote) await game.cast_vote(ctx.author, vote)