From de6dc03ddad96ac114dba5069ab3970fb27666b5 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 8 Nov 2021 22:10:08 +0100 Subject: [PATCH] Implement vote closing --- orochi/bot.py | 35 +++++++++++++++++++++++++++++++++++ orochi/models.py | 8 ++++++++ orochi/templates/list.html | 19 ++++++++++++++++++- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/orochi/bot.py b/orochi/bot.py index 19145e8..6fa42c3 100644 --- a/orochi/bot.py +++ b/orochi/bot.py @@ -212,6 +212,7 @@ async def brother(ctx: commands.Context, *, message: str): @bot.command(help="Ouvrir les votes") +@commands.has_permissions(administrator=True) async def open(ctx: commands.Context): game: Game = Game.INSTANCE current_round = game.rounds[-1] @@ -258,15 +259,49 @@ async def open(ctx: commands.Context): await ctx.reply("Les salles de vote sont ouvertes, les joueur⋅se⋅s peuvent désormais voter.") +@bot.command(help="Fermer les votes") +@commands.has_permissions(administrator=True) +async def close(ctx: commands.Context): + game: Game = Game.INSTANCE + if game.state != GameState.VOTING: + await ctx.reply("Les votes ne sont pas ouverts.") + return + + current_round = game.rounds[-1] + for room in current_round.rooms: + for vote in room.votes: + if vote.vote is None: + vote.vote = Vote.ALLY + await ctx.send(f"L'équipe **{' et '.join(player.name for player in vote.players)}** " + f"n'a pas voté en salle {room.room.value} et s'est alliée par défaut.") + + for player in game.players.values(): + channel = bot.get_channel(player.private_channel_id) + await channel.send("Les votes sont à présent clos ! " + "Rendez-vous dans la salle principale pour découvrir les scores.") + if player.score <= 0: + await channel.send("Tiens ! Vous êtes morts :)") + + await ctx.reply("Les votes ont bien été fermés.") + game.state = GameState.RESULTS + game.save('game.save') + + class VoteView(disnake.ui.View): @disnake.ui.button(label="S'allier", style=disnake.ButtonStyle.green) async def confirm(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction): + if Game.INSTANCE.state != GameState.VOTING: + return await interaction.response.send_message("Les votes ne sont pas ouverts.", ephemeral=True) + await interaction.response.send_message("Votre vote a bien été pris en compte.", ephemeral=True) self.vote(interaction, Vote.ALLY) @disnake.ui.button(label="Trahir", style=disnake.ButtonStyle.red) async def cancel(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction): + if Game.INSTANCE.state != GameState.VOTING: + return await interaction.response.send_message("Les votes ne sont pas ouverts.", ephemeral=True) + await interaction.response.send_message("Votre vote a bien été pris en compte.", ephemeral=True) self.vote(interaction, Vote.BETRAY) diff --git a/orochi/models.py b/orochi/models.py index 1f5adbf..26ca70e 100644 --- a/orochi/models.py +++ b/orochi/models.py @@ -40,6 +40,10 @@ class Player: s = 3 for vote in self.round_votes: + if vote.room.round.round == len(Game.INSTANCE.rounds) and Game.INSTANCE.state != GameState.RESULTS: + # Don't compute temporary scores + break + room = vote.room other_vote = room.vote1 if room.vote1 is not vote else room.vote2 match vote.vote, other_vote.vote: @@ -52,6 +56,10 @@ class Player: case Vote.BETRAY, Vote.BETRAY: pass + if s <= 0: + # Player died + return s + return s diff --git a/orochi/templates/list.html b/orochi/templates/list.html index d7b93ad..24c2356 100644 --- a/orochi/templates/list.html +++ b/orochi/templates/list.html @@ -36,6 +36,23 @@ + +

Récapitulatif par tour

{% for round in game.rounds %} @@ -58,7 +75,7 @@ {{ room.room.value }} {% endif %} {{ vote.player1.name }}{% if vote.player2 %}, {{ vote.player2.name }}{% endif %} - {% if round.round != game.rounds|length or admin %} + {% if round.round != game.rounds|length or game.state.value == 2 or admin %} {{ vote.vote.value|default('Pas de vote') }} {% else %} Vote en cours ...