From 2315bd9fc3e633875e6663e6452ce341ef666c3f Mon Sep 17 00:00:00 2001 From: bobloy Date: Thu, 19 Aug 2021 17:29:41 -0400 Subject: [PATCH] The rest converted??? --- conquest/conquest.py | 178 +++++++++++++++++++-------------------- conquest/conquestgame.py | 108 ++++++++++++++++++------ 2 files changed, 169 insertions(+), 117 deletions(-) diff --git a/conquest/conquest.py b/conquest/conquest.py index 47dd087..ec04403 100644 --- a/conquest/conquest.py +++ b/conquest/conquest.py @@ -18,6 +18,8 @@ from redbot.core.utils.predicates import MessagePredicate from conquest.conquestgame import ConquestGame from conquest.regioner import ConquestMap, MapMaker, composite_regions +ERROR_CONQUEST_SET_MAP = "No map is currently set. See `[p]conquest set map`" + class Conquest(commands.Cog): """ @@ -414,11 +416,12 @@ class Conquest(commands.Cog): @conquest_set.command(name="resetzoom") async def _conquest_set_resetzoom(self, ctx: Context): """Resets the zoom level of the current map""" - if self.current_games[ctx.guild.id] is None: - await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") + current_game = self.current_games[ctx.guild.id] + if current_game is None: + await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP) return - if not await self.current_games[ctx.guild.id].reset_zoom(): + if not await current_game.reset_zoom(): await ctx.maybe_send_embed(f"No zoom data found, reset not needed") await ctx.tick() @@ -431,15 +434,16 @@ class Conquest(commands.Cog): y: positive integer zoom: float greater than or equal to 1 """ - if self.current_games[ctx.guild.id] is None: - await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") + current_game = self.current_games[ctx.guild.id] + if current_game is None: + await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP) return if x < 0 or y < 0 or zoom < 1: await ctx.send_help() return - await self.current_games[ctx.guild.id].set_zoom(x, y, zoom) + await current_game.set_zoom(x, y, zoom) await ctx.tick() @@ -452,17 +456,18 @@ class Conquest(commands.Cog): y: positive integer zoom: float greater than or equal to 1 """ - if self.current_games[ctx.guild.id] is None: - await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") + current_game = self.current_games[ctx.guild.id] + if current_game is None: + await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP) return if x < 0 or y < 0 or zoom < 1: await ctx.send_help() return - # 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 + # UNUSED: out_of_date marks zoom as oudated, since this overwrite the temp + zoomed_path = await current_game.create_zoomed_map( + x, y, zoom, current_game.current_map, current_game.zoomed_current_map ) await ctx.send(file=discord.File(fp=zoomed_path, filename=f"test_zoom.{self.ext}")) @@ -470,22 +475,24 @@ class Conquest(commands.Cog): @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_games[ctx.guild.id] is None: - await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") + current_game = self.current_games[ctx.guild.id] + if current_game is None: + await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP) return - await self.current_games[ctx.guild.id].save_as(save_name) + await current_game.save_as(save_name) 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_games[ctx.guild.id] is None: - await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") + current_game = self.current_games[ctx.guild.id] + if current_game is None: + await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP) return - if not await self.current_games[ctx.guild.id].load_from(save_name): + if not await current_game.load_from(save_name): await ctx.maybe_send_embed(f"Saved map not found, check your spelling") return @@ -513,25 +520,27 @@ class Conquest(commands.Cog): map_dir, mapname, self.current_map_folder / ctx.guild.id / mapname ) - self.current_map = mapname - self.is_custom = is_custom - await self.config.current_map.set(self.current_map) # Save to config too - await self.config.is_custom.set(is_custom) - - await self.current_map_load() - - current_map_folder = await self._get_current_map_folder() - current_map = current_map_folder / f"current.{self.ext}" - - if not reset and current_map.exists(): - await ctx.maybe_send_embed( - "This map is already in progress, resuming from last game\n" - "Use `[p]conquest set map [mapname] True` to start a new game" - ) - else: - if not current_map_folder.exists(): - current_map_folder.mkdir() - copyfile(check_path / mapname / f"blank.{self.ext}", current_map) + # self.current_map = mapname + # self.is_custom = is_custom + # await self.config.current_map.set(self.current_map) # Save to config too + # await self.config.is_custom.set(is_custom) + # + # await self.current_map_load() + + await self.current_games[ctx.guild.id].resume_game(ctx, reset) + + # current_map_folder = await self._get_current_map_folder() + # current_map = current_map_folder / f"current.{self.ext}" + # + # if not reset and current_map.exists(): + # await ctx.maybe_send_embed( + # "This map is already in progress, resuming from last game\n" + # "Use `[p]conquest set map [mapname] True` to start a new game" + # ) + # else: + # if not current_map_folder.exists(): + # current_map_folder.mkdir() + # copyfile(check_path / mapname / f"blank.{self.ext}", current_map) await ctx.tick() @@ -540,66 +549,39 @@ class Conquest(commands.Cog): """ Send the current map. """ - if self.current_map is None: - await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") + current_game = self.current_games[ctx.guild.id] + if current_game is None: + await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP) return - - current_img = await self._get_current_map_folder() / f"current.{self.ext}" - - await self._send_maybe_zoomed_map(ctx, current_img, f"current_map.{self.ext}") + async with ctx.typing(): + map_file = await current_game.get_maybe_zoomed_map("current") + await ctx.send(file=map_file) @conquest.command("blank") async def _conquest_blank(self, ctx: Context): """ Print the blank version of the current map, for reference. """ - if self.current_map is None: - await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") + current_game = self.current_games[ctx.guild.id] + if current_game is None: + await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP) return - - current_blank_img = self._path_if_custom() / self.current_map / f"blank.{self.ext}" - - await self._send_maybe_zoomed_map(ctx, current_blank_img, f"blank_map.{self.ext}") + async with ctx.typing(): + map_file = await current_game.get_maybe_zoomed_map("blank") + await ctx.send(file=map_file) @conquest.command("numbered") async def _conquest_numbered(self, ctx: Context): """ Print the numbered version of the current map, for reference. """ - if self.current_map is None: - await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") + current_game = self.current_games[ctx.guild.id] + if current_game is None: + await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP) return async with ctx.typing(): - numbers_path = self._path_if_custom() / self.current_map / f"numbers.{self.ext}" - if not numbers_path.exists(): - await ctx.send( - file=discord.File( - fp=self._path_if_custom() / self.current_map / f"numbered.{self.ext}", - filename=f"numbered.{self.ext}", - ) - ) - return - - current_map_path = await self._get_current_map_folder() - current_map = Image.open(current_map_path / f"current.{self.ext}") - numbers = Image.open(numbers_path).convert("L") - - inverted_map = ImageOps.invert(current_map) - - loop = asyncio.get_running_loop() - current_numbered_img = await loop.run_in_executor( - None, Image.composite, current_map, inverted_map, numbers - ) - - current_numbered_img.save( - current_map_path / f"current_numbered.{self.ext}", self.ext_format - ) - - await self._send_maybe_zoomed_map( - ctx, - current_map_path / f"current_numbered.{self.ext}", - f"current_numbered.{self.ext}", - ) + map_file = await current_game.get_maybe_zoomed_map("numbered") + await ctx.send(file=map_file) @conquest.command(name="multitake") async def _conquest_multitake( @@ -610,8 +592,9 @@ class Conquest(commands.Cog): :param start_region: """ - if self.current_map is None: - await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") + current_game = self.current_games[ctx.guild.id] + if current_game is None: + await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP) return try: @@ -620,18 +603,22 @@ class Conquest(commands.Cog): await ctx.maybe_send_embed(f"Invalid color {color}") return - if end_region > self.map_data["region_max"] or start_region < 1: + if start_region < end_region: + start_region, end_region = end_region, start_region + + if end_region > current_game.region_max or start_region < 1: await ctx.maybe_send_embed( - f"Max region number is {self.map_data['region_max']}, minimum is 1" + f"Max region number is {current_game.region_max}, minimum is 1" ) return - if start_region < end_region: - start_region, end_region = end_region, start_region - regions = [r for r in range(start_region, end_region + 1)] - await self._process_take_regions(ctx, color, regions) + async with ctx.typing(): + await current_game._process_take_regions(color, regions) + map_file = await current_game.get_maybe_zoomed_map("current") + + await ctx.send(file=map_file) @conquest.command(name="take") async def _conquest_take(self, ctx: Context, regions: Greedy[int], *, color: str): @@ -645,8 +632,9 @@ class Conquest(commands.Cog): await ctx.send_help() return - if self.current_map is None: - await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") + current_game = self.current_games[ctx.guild.id] + if current_game is None: + await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP) return try: @@ -656,10 +644,14 @@ class Conquest(commands.Cog): return for region in regions: - if region > self.map_data["region_max"] or region < 1: + if region > current_game.region_max or region < 1: await ctx.maybe_send_embed( - f"Max region number is {self.map_data['region_max']}, minimum is 1" + f"Max region number is {current_game.region_max}, minimum is 1" ) return + async with ctx.typing(): + await current_game._process_take_regions(color, regions) + map_file = await current_game.get_maybe_zoomed_map("current") + + await ctx.send(file=map_file) - await self._process_take_regions(ctx, color, regions) diff --git a/conquest/conquestgame.py b/conquest/conquestgame.py index 124f2f5..c78e9ba 100644 --- a/conquest/conquestgame.py +++ b/conquest/conquestgame.py @@ -1,14 +1,18 @@ +import asyncio import json import pathlib from shutil import copyfile +from types import SimpleNamespace +from typing import Optional, Union import discord -from PIL import Image, ImageColor, ImageOps +from PIL import Image, ImageOps +from redbot.core import commands + from conquest.regioner import ConquestMap, composite_regions class ConquestGame: - ext = "PNG" ext_format = "PNG" @@ -20,10 +24,22 @@ class ConquestGame: self.current_map_folder = custom_map_path self.settings_json = self.current_map_folder / "settings.json" - self.current_map = self.current_map_folder / f"current.{self.ext}" - self.zoomed_map = self.current_map_folder / f"zoomed.{self.ext}" - self.zoom_is_out_of_date = True + self.current_filename = f"current.{self.ext}" + self.current_map = self.current_map_folder / self.current_filename + + self.zoomed_current_filename = f"current_zoomed.{self.ext}" + self.zoomed_current_map = self.current_map_folder / self.zoomed_current_filename + + self.numbered_current_filename = f"current_numbered.{self.ext}" + self.numbered_current_map = self.current_map_folder / self.numbered_current_filename + + self.zoomed_numbered_current_filename = f"current_zoomed_numbered.{self.ext}" + self.zoomed_numbered_current_map = self.current_map_folder / self.zoomed_numbered_current_filename + + self.region_max = self.source_map.region_max + + # self.zoom_is_out_of_date = {'current': True, 'blank': True} async def save_region(self, region): if not self.custom: @@ -31,7 +47,19 @@ class ConquestGame: pass # TODO: region data saving async def start_game(self): - pass + if not self.current_map_folder.exists(): + self.current_map_folder.mkdir() + copyfile(self.source_map.blank_path(), self.current_map) + + async def resume_game(self, ctx: commands.Context, reset: bool): + + if not reset and self.current_map.exists(): + await ctx.maybe_send_embed( + "This map is already in progress, resuming from last game\n" + "Use `[p]conquest set map [mapname] True` to start a new game" + ) + else: + await self.start_game() async def _process_take_regions(self, color, regions): im = Image.open(self.current_map) @@ -43,37 +71,69 @@ class ConquestGame: self.source_map.masks_path(), ) 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.current = True + + async def create_numbered_map(self): + if not self.source_map.numbers_path().exists(): # No numbers map, can't add numbers to current + return self.source_map.numbered_path() - async def create_zoomed_map(self, x, y, zoom, out_of_date=False, **kwargs): + current_map = Image.open(self.current_map) + numbers = Image.open(self.source_map.numbers_path()).convert("L") + + inverted_map = ImageOps.invert(current_map) + + loop = asyncio.get_running_loop() + current_numbered_img = await loop.run_in_executor( + None, Image.composite, current_map, inverted_map, numbers + ) + + current_numbered_img.save(self.numbered_current_map, self.ext_format) + + return self.numbered_current_map + + async def create_zoomed_map( + self, x, y, zoom, source_map: Union[Image.Image, pathlib.Path], target_path: pathlib.Path, **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 + # if out_of_date: + # self.zoom_is_out_of_date.current = True + + # if current_map is None: + # current_map = Image.open(self.current_map_folder) + # target_map = self.zoomed_current_map + + if not isinstance(source_map, Image.Image): + source_map = Image.open(source_map) + + w, h = source_map.size zoom2 = zoom * 2 - zoomed_map = current_map.crop((x - w / zoom2, y - h / zoom2, x + w / zoom2, y + h / zoom2)) + zoomed_map = source_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) + zoomed_map.save(target_path, self.ext_format) - return self.zoomed_map + return True - async def get_maybe_zoomed_map(self, filename): + async def get_maybe_zoomed_map(self, version): zoom_data = {"enabled": False} if self.settings_json.exists(): with self.settings_json.open() as zoom_json: zoom_data = json.load(zoom_json) - map_path = self.current_map + if version == "numbered": + map_path = self.create_numbered_map() + zoomed_path = self.zoomed_numbered_current_map + else: # version == "current" + map_path = self.current_map + zoomed_path = self.zoomed_current_map if zoom_data["enabled"]: # Send zoomed map instead of current map - 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 + # if self.zoom_is_out_of_date: + await self.create_zoomed_map(**zoom_data, source_map=map_path, target_map=zoomed_path) + map_path = zoomed_path + # self.zoom_is_out_of_date = False - return discord.File(fp=map_path, filename=filename) + return discord.File(fp=map_path) # lol file names async def reset_zoom(self): if not self.settings_json.exists(): @@ -82,7 +142,7 @@ class ConquestGame: with self.settings_json.open("w+") as zoom_json: json.dump({"enabled": False}, zoom_json, sort_keys=True, indent=4) - self.zoom_is_out_of_date = True + # self.zoom_is_out_of_date = True return True @@ -94,7 +154,7 @@ class ConquestGame: zoom_data["y"] = y zoom_data["zoom"] = zoom - self.zoom_is_out_of_date = True + # self.zoom_is_out_of_date = True with self.settings_json.open("w+") as zoom_json: json.dump(zoom_data, zoom_json, sort_keys=True, indent=4)