From 3e25c8a13f869e4d554dbad5f4b136afbb8b53cb Mon Sep 17 00:00:00 2001 From: bobloy Date: Fri, 23 Jul 2021 16:02:26 -0400 Subject: [PATCH] Saving and loading --- conquest/conquest.py | 40 +++++++++++++++------------------------- conquest/conquestgame.py | 21 ++++++++++++++++++--- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/conquest/conquest.py b/conquest/conquest.py index 9b7eec6..60f214e 100644 --- a/conquest/conquest.py +++ b/conquest/conquest.py @@ -58,7 +58,9 @@ class Conquest(commands.Cog): self.asset_path: Optional[pathlib.Path] = None - self.current_games: Dict[int, Optional[ConquestGame]] = defaultdict(lambda: None) # key: guild_id + self.current_games: Dict[int, Optional[ConquestGame]] = defaultdict( + lambda: None + ) # key: guild_id self.map_data = {} # key, value = guild.id, ConquestGame self.mm: Optional[MapMaker] = None @@ -121,8 +123,6 @@ class Conquest(commands.Cog): async def _get_current_map_folder(self, guild): return self.current_map_folder / guild.id / self.current_map - - async def _mm_save_map(self, map_name, target_save): return await self.mm.change_name(map_name, target_save) @@ -419,9 +419,7 @@ class Conquest(commands.Cog): return if not await self.current_games[ctx.guild.id].reset_zoom(): - await ctx.maybe_send_embed( - f"No zoom data found, reset not needed" - ) + await ctx.maybe_send_embed(f"No zoom data found, reset not needed") await ctx.tick() @conquest_set.command(name="zoom") @@ -462,43 +460,35 @@ class Conquest(commands.Cog): await ctx.send_help() return - zoomed_path = await self.current_games[ctx.guild.id].create_zoomed_map(x, y, zoom, out_of_date=True) + # out_of_date marks zoom as oudated, since this overwrite the temp + zoomed_path = await self.current_games[ctx.guild.id].create_zoomed_map( + x, y, zoom, out_of_date=True + ) - await ctx.send(file=discord.File(fp=zoomed_path, filename=f"current_zoomed.{self.ext}")) + await ctx.send(file=discord.File(fp=zoomed_path, filename=f"test_zoom.{self.ext}")) @conquest_set.command(name="save") async def _conquest_set_save(self, ctx: Context, *, save_name): """Save the current map to be loaded later""" - if self.current_map is None: + if self.current_games[ctx.guild.id] is None: await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") return - current_map_folder = await self._get_current_map_folder() - current_map = current_map_folder / f"current.{self.ext}" + await self.current_games[ctx.guild.id].save_as(save_name) - if not current_map_folder.exists() or not current_map.exists(): - await ctx.maybe_send_embed("Current map doesn't exist! Try setting a new one") - return - - copyfile(current_map, current_map_folder / f"{save_name}.{self.ext}") await ctx.tick() @conquest_set.command(name="load") async def _conquest_set_load(self, ctx: Context, *, save_name): """Load a saved map to be the current map""" - if self.current_map is None: + if self.current_games[ctx.guild.id] is None: await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") return - current_map_folder = await self._get_current_map_folder() - current_map = current_map_folder / f"current.{self.ext}" - saved_map = current_map_folder / f"{save_name}.{self.ext}" - - if not current_map_folder.exists() or not saved_map.exists(): - await ctx.maybe_send_embed(f"Saved map not found in the {self.current_map} folder") + if not await self.current_games[ctx.guild.id].load_from(save_name): + await ctx.maybe_send_embed(f"Saved map not found, check your spelling") return - copyfile(saved_map, current_map) await ctx.tick() @conquest_set.command(name="map") @@ -668,4 +658,4 @@ class Conquest(commands.Cog): ) return - await self._process_take_regions(ctx, color,regions) + await self._process_take_regions(ctx, color, regions) diff --git a/conquest/conquestgame.py b/conquest/conquestgame.py index ecc5fcf..124f2f5 100644 --- a/conquest/conquestgame.py +++ b/conquest/conquestgame.py @@ -1,5 +1,6 @@ import json import pathlib +from shutil import copyfile import discord from PIL import Image, ImageColor, ImageOps @@ -44,15 +45,17 @@ class ConquestGame: out.save(self.current_map, self.ext_format) # Overwrite current map with new map self.zoom_is_out_of_date = True - async def create_zoomed_map(self, x, y, zoom, **kwargs): + async def create_zoomed_map(self, x, y, zoom, out_of_date=False, **kwargs): + """Pass out_of_date when created a zoomed map based on something other than the settings json""" + if out_of_date: + self.zoom_is_out_of_date = True current_map = Image.open(self.current_map_folder) - w, h = current_map.size zoom2 = zoom * 2 zoomed_map = current_map.crop((x - w / zoom2, y - h / zoom2, x + w / zoom2, y + h / zoom2)) # zoomed_map = zoomed_map.resize((w, h), Image.LANCZOS) zoomed_map.save(self.zoomed_map, self.ext_format) - self.zoom_is_out_of_date = False + return self.zoomed_map async def get_maybe_zoomed_map(self, filename): @@ -68,6 +71,7 @@ class ConquestGame: map_path = self.zoomed_map if self.zoom_is_out_of_date: await self.create_zoomed_map(**zoom_data) + self.zoom_is_out_of_date = False return discord.File(fp=map_path, filename=filename) @@ -95,3 +99,14 @@ class ConquestGame: with self.settings_json.open("w+") as zoom_json: json.dump(zoom_data, zoom_json, sort_keys=True, indent=4) + async def save_as(self, save_name): + copyfile(self.current_map, self.current_map_folder / f"{save_name}.{self.ext}") + + async def load_from(self, save_name): + saved_map = self.current_map_folder / f"{save_name}.{self.ext}" + + if not saved_map.exists(): + return False + + copyfile(saved_map, self.current_map) # Overwrite current map with saved map + return True