diff --git a/orochi/bot.py b/orochi/bot.py index 00c6d47..e22dd1c 100644 --- a/orochi/bot.py +++ b/orochi/bot.py @@ -1,4 +1,3 @@ -import os from functools import partial import disnake @@ -90,23 +89,23 @@ async def on_ready(): role, overwrite=PermissionOverwrite(read_message_history=True, read_messages=True) ) - game = Game.load('game.save') + game = Game.load() if not game: game = Game() for player in config.PLAYERS: game.register_player(player, config.vote_channels[player.lower()]) - game.save('game.save') + game.save() # Update private channel id if necessary for player in list(game.players.values()): if player.private_channel_id != config.vote_channels[player.name.lower()]: game.register_player(player.name, config.vote_channels[player.name.lower()]) - game.save('game.save') + game.save() # Setup first round if not exists if not game.rounds: game.rounds.append(game.default_first_round()) - game.save('game.save') + game.save() if not config.telepathy_channel: channel: TextChannel = await secret_category.create_text_channel("bigbrain") @@ -187,14 +186,14 @@ async def on_ready(): @bot.command(help="Sauvegarde la partie") @commands.has_permissions(administrator=True) -async def save(ctx: commands.Context, filename: str = 'game.save'): +async def save(ctx: commands.Context, filename: str | None = None): Game.INSTANCE.save(filename) await ctx.reply("La partie a été sauvegardée.") @bot.command(help="Recharger la partie") @commands.has_permissions(administrator=True) -async def load(ctx: commands.Context, filename: str = 'game.save'): +async def load(ctx: commands.Context, filename: str | None = None): game = Game.load(filename) if not game: return await ctx.reply("Une erreur est survenue : le fichier n'existe pas ?") @@ -306,7 +305,7 @@ async def close(ctx: commands.Context): "Mais ... Attendrez-vous vos camarades ?") await ctx.reply("Les votes ont bien été fermés.") - game.save('game.save') + game.save() @bot.command(help="Préparation du tour suivant") @@ -336,7 +335,7 @@ async def prepare(ctx: commands.Context): vote2=RoundVote(), ), )) - game.save('game.save') + game.save() await ctx.reply("Le tour suivant est en préparation. Utilisez `!setup A|B|C` pour paramétrer les salles A, B ou C. " "Dan peut faire la même chose.") @@ -399,7 +398,7 @@ async def vote(ctx: commands.Context, player_name: str, vote: Vote | None): if current_player in v.players: v.vote = vote - game.save('game.save') + game.save() await ctx.reply(f"Le vote de **{current_player.name}** a bien été falsifié (nouveau vote : *{vote}**).") @@ -427,7 +426,7 @@ async def swap(ctx: commands.Context, player_name: str): if current_player in v.players: v.swapped ^= True - game.save('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.") @@ -449,7 +448,7 @@ async def override(ctx: commands.Context, player_name: str, target_score: int): game.score_overrides[current_player] = target_score - game.save('game.save') + game.save() await ctx.reply(f"Le score de **{current_player.name}** a bien été modifié. " f"Il sera maintenant de **{target_score}**.") @@ -462,13 +461,13 @@ async def reboot(ctx: commands.Context): channel = bot.get_channel(player.private_channel_id) await channel.send("REB0OT.") - os.rename('game.save', 'game-prereboot.save') + game.save('game-prereboot.save') game = Game() for player in bot.config.PLAYERS: game.register_player(player, bot.config.vote_channels[player.lower()]) game.rounds.append(game.default_first_round()) - game.save('game.save') + game.save() await ctx.reply("REB0OT.") @@ -516,7 +515,7 @@ class VoteView(disnake.ui.View): if current_vote.vote is None: current_vote.vote = vote - game.save('game.save') + game.save() class PrepareRoomView(disnake.ui.View): @@ -592,7 +591,7 @@ class PrepareRoomView(disnake.ui.View): "Vous pouvez redéfinir la salle tant que le tour n'est pas lancé. " "Utilisez !preview pour un récapitulatif des tours.") - game.save('game.save') + game.save() game: Game = Game.INSTANCE current_round = game.rounds[-1] diff --git a/orochi/config.py b/orochi/config.py index 19b93a6..929b56b 100644 --- a/orochi/config.py +++ b/orochi/config.py @@ -30,14 +30,14 @@ class Config: player_roles: dict[str, int] = field(default_factory=dict) @classmethod - def load(cls, filename=None) -> "Config": + def load(cls, filename: str | None = None) -> "Config": if filename is None: filename = Path(__file__).parent.parent / 'config.yml' with open(filename) as config_file: return Config(**(yaml.safe_load(config_file) or {})) - def save(self, filename=None) -> None: + def save(self, filename: str | None = None) -> None: if filename is None: filename = Path(__file__).parent.parent / 'config.yml' diff --git a/orochi/models.py b/orochi/models.py index 328514e..d53621b 100644 --- a/orochi/models.py +++ b/orochi/models.py @@ -1,6 +1,7 @@ import pickle from dataclasses import dataclass, field from enum import Enum +from pathlib import Path from typing import ClassVar, Iterable, Generator @@ -160,18 +161,22 @@ class Game: player2=self.players['Oji'])), ) - def save(self, filename: str) -> None: + def save(self, filename: str | None = None) -> None: """ Uses pickle to save the current state of the game. """ + if filename is None: + filename = Path(__file__).parent.parent / 'game.save' with open(filename, 'wb') as f: pickle.dump(self, f) @classmethod - def load(cls, filename: str) -> "Game | None": + def load(cls, filename: str | None = None) -> "Game | None": """ Reload the game from a saved file. """ + if filename is None: + filename = Path(__file__).parent.parent / 'game.save' try: with open(filename, 'rb') as f: game = pickle.load(f)