Implement vote swap

This commit is contained in:
Yohann D'ANELLO 2021-11-12 14:15:39 +01:00
parent c53656c427
commit 881b97218e
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
3 changed files with 45 additions and 5 deletions

View File

@ -286,6 +286,11 @@ async def close(ctx: commands.Context):
vote.vote = Vote.ALLY vote.vote = Vote.ALLY
await ctx.send(f"L'équipe **{' et '.join(player.name for player in vote.players)}** " 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.") f"n'a pas voté en salle **{room.room.value}** et s'est alliée par défaut.")
if vote.swapped:
vote.vote = Vote.ALLY if vote.vote == Vote.BETRAY else Vote.BETRAY
await ctx.send(f"L'équipe **{' et '.join(player.name for player in vote.players)}** "
f"en salle **{room.room.value}** a vu son vote échangé. "
f"Nouveau vote : **{vote.vote.value}**")
for player in game.players.values(): for player in game.players.values():
channel = bot.get_channel(player.private_channel_id) channel = bot.get_channel(player.private_channel_id)
@ -376,7 +381,7 @@ async def setup(ctx: commands.Context, room: Room):
@bot.command(help="Falsification des votes") @bot.command(help="Falsification des votes")
@commands.has_permissions(administrator=True) @commands.has_permissions(administrator=True)
async def cheat(ctx: commands.Context, player_name: str, vote: Vote | None): async def vote(ctx: commands.Context, player_name: str, vote: Vote | None):
game: Game = Game.INSTANCE game: Game = Game.INSTANCE
if game.state != GameState.VOTING: if game.state != GameState.VOTING:
return await ctx.reply("Les votes ne sont pas ouverts.") return await ctx.reply("Les votes ne sont pas ouverts.")
@ -398,6 +403,35 @@ async def cheat(ctx: commands.Context, player_name: str, vote: Vote | None):
await ctx.reply(f"Le vote de **{current_player.name}** a bien été falsifié (nouveau vote : *{vote}**).") await ctx.reply(f"Le vote de **{current_player.name}** a bien été falsifié (nouveau vote : *{vote}**).")
@bot.command(help="Préparation d'une salle")
@commands.has_any_role('IA', 'Dan')
async def swap(ctx: commands.Context, player_name: str):
"""
Exchange the vote of the given player.
"""
game: Game = Game.INSTANCE
current_round = game.rounds[-1]
if game.state != GameState.VOTING:
return await ctx.reply("Les votes ne sont pas ouverts.")
for player in game.players.values():
if player.name.lower() == player_name.lower():
current_player = player
break
else:
return await ctx.reply("Le joueur n'existe pas.")
for room in current_round.rooms:
for v in room.votes:
if current_player in v.players:
v.swapped ^= True
game.save('game.save')
await ctx.reply(f"Le vote de **{current_player.name}** a bien été inversé. S'il était déjà inversé, "
"alors il est de retour à la normale.")
@bot.command(help="Reboot") @bot.command(help="Reboot")
@commands.has_permissions(administrator=True) @commands.has_permissions(administrator=True)
async def reboot(ctx: commands.Context): async def reboot(ctx: commands.Context):
@ -425,7 +459,7 @@ class VoteView(disnake.ui.View):
await interaction.response.send_message("Votre vote a bien été pris en compte.", ephemeral=True) await interaction.response.send_message("Votre vote a bien été pris en compte.", ephemeral=True)
self.vote(interaction, Vote.ALLY) await self.vote(interaction, Vote.ALLY)
@disnake.ui.button(label="Trahir", style=disnake.ButtonStyle.red) @disnake.ui.button(label="Trahir", style=disnake.ButtonStyle.red)
async def cancel(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction): async def cancel(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
@ -434,9 +468,9 @@ class VoteView(disnake.ui.View):
await interaction.response.send_message("Votre vote a bien été pris en compte.", ephemeral=True) await interaction.response.send_message("Votre vote a bien été pris en compte.", ephemeral=True)
self.vote(interaction, Vote.BETRAY) await self.vote(interaction, Vote.BETRAY)
def vote(self, interaction: disnake.MessageInteraction, vote: Vote) -> None: async def vote(self, interaction: disnake.MessageInteraction, vote: Vote) -> None:
game = Game.INSTANCE game = Game.INSTANCE
current_round = game.rounds[-1] current_round = game.rounds[-1]
current_player: Player | None = None current_player: Player | None = None

View File

@ -70,6 +70,7 @@ class RoundVote:
player1: Player | None = None player1: Player | None = None
player2: Player | None = None player2: Player | None = None
vote: Vote | None = None vote: Vote | None = None
swapped: bool = field(default=False, init=False)
@property @property
def players(self) -> Iterable[Player]: def players(self) -> Iterable[Player]:

View File

@ -58,7 +58,12 @@
{% endif %} {% endif %}
<td>{{ vote.player1.name|default('<em>personne</em>')|safe }}{% if vote.player2 %}, {{ vote.player2.name }}{% endif %}</td> <td>{{ vote.player1.name|default('<em>personne</em>')|safe }}{% if vote.player2 %}, {{ vote.player2.name }}{% endif %}</td>
{% if round.round != game.rounds|length or game.state.value == 2 or admin %} {% if round.round != game.rounds|length or game.state.value == 2 or admin %}
<td>{{ vote.vote.value|default('<em>Pas de vote</em>')|safe }}</td> <td>
{{ vote.vote.value|default('<em>Pas de vote</em>')|safe }}
{% if admin and vote.swapped %}
<span class="badge bg-danger">Échange</span>
{% endif %}
</td>
{% else %} {% else %}
<td><em>Vote en cours ...</em></td> <td><em>Vote en cours ...</em></td>
{% endif %} {% endif %}