Saving and loading

conquest_develop
bobloy 4 years ago
parent d987018664
commit 3e25c8a13f

@ -58,7 +58,9 @@ class Conquest(commands.Cog):
self.asset_path: Optional[pathlib.Path] = None 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.map_data = {} # key, value = guild.id, ConquestGame
self.mm: Optional[MapMaker] = None self.mm: Optional[MapMaker] = None
@ -121,8 +123,6 @@ class Conquest(commands.Cog):
async def _get_current_map_folder(self, guild): async def _get_current_map_folder(self, guild):
return self.current_map_folder / guild.id / self.current_map return self.current_map_folder / guild.id / self.current_map
async def _mm_save_map(self, map_name, target_save): async def _mm_save_map(self, map_name, target_save):
return await self.mm.change_name(map_name, target_save) return await self.mm.change_name(map_name, target_save)
@ -419,9 +419,7 @@ class Conquest(commands.Cog):
return return
if not await self.current_games[ctx.guild.id].reset_zoom(): if not await self.current_games[ctx.guild.id].reset_zoom():
await ctx.maybe_send_embed( await ctx.maybe_send_embed(f"No zoom data found, reset not needed")
f"No zoom data found, reset not needed"
)
await ctx.tick() await ctx.tick()
@conquest_set.command(name="zoom") @conquest_set.command(name="zoom")
@ -462,43 +460,35 @@ class Conquest(commands.Cog):
await ctx.send_help() await ctx.send_help()
return 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") @conquest_set.command(name="save")
async def _conquest_set_save(self, ctx: Context, *, save_name): async def _conquest_set_save(self, ctx: Context, *, save_name):
"""Save the current map to be loaded later""" """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`") await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`")
return return
current_map_folder = await self._get_current_map_folder() await self.current_games[ctx.guild.id].save_as(save_name)
current_map = current_map_folder / f"current.{self.ext}"
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() await ctx.tick()
@conquest_set.command(name="load") @conquest_set.command(name="load")
async def _conquest_set_load(self, ctx: Context, *, save_name): async def _conquest_set_load(self, ctx: Context, *, save_name):
"""Load a saved map to be the current map""" """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`") await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`")
return return
current_map_folder = await self._get_current_map_folder() if not await self.current_games[ctx.guild.id].load_from(save_name):
current_map = current_map_folder / f"current.{self.ext}" await ctx.maybe_send_embed(f"Saved map not found, check your spelling")
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")
return return
copyfile(saved_map, current_map)
await ctx.tick() await ctx.tick()
@conquest_set.command(name="map") @conquest_set.command(name="map")
@ -668,4 +658,4 @@ class Conquest(commands.Cog):
) )
return return
await self._process_take_regions(ctx, color,regions) await self._process_take_regions(ctx, color, regions)

@ -1,5 +1,6 @@
import json import json
import pathlib import pathlib
from shutil import copyfile
import discord import discord
from PIL import Image, ImageColor, ImageOps 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 out.save(self.current_map, self.ext_format) # Overwrite current map with new map
self.zoom_is_out_of_date = True 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) current_map = Image.open(self.current_map_folder)
w, h = current_map.size w, h = current_map.size
zoom2 = zoom * 2 zoom2 = zoom * 2
zoomed_map = current_map.crop((x - w / zoom2, y - h / zoom2, x + w / zoom2, y + h / zoom2)) 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 = zoomed_map.resize((w, h), Image.LANCZOS)
zoomed_map.save(self.zoomed_map, self.ext_format) zoomed_map.save(self.zoomed_map, self.ext_format)
self.zoom_is_out_of_date = False
return self.zoomed_map return self.zoomed_map
async def get_maybe_zoomed_map(self, filename): async def get_maybe_zoomed_map(self, filename):
@ -68,6 +71,7 @@ class ConquestGame:
map_path = self.zoomed_map map_path = self.zoomed_map
if self.zoom_is_out_of_date: if self.zoom_is_out_of_date:
await self.create_zoomed_map(**zoom_data) await self.create_zoomed_map(**zoom_data)
self.zoom_is_out_of_date = False
return discord.File(fp=map_path, filename=filename) return discord.File(fp=map_path, filename=filename)
@ -95,3 +99,14 @@ class ConquestGame:
with self.settings_json.open("w+") as zoom_json: with self.settings_json.open("w+") as zoom_json:
json.dump(zoom_data, zoom_json, sort_keys=True, indent=4) 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

Loading…
Cancel
Save