From 978d6f862756116baf2b5b180729fb1439a74196 Mon Sep 17 00:00:00 2001 From: bobloy Date: Fri, 20 Aug 2021 15:21:31 -0400 Subject: [PATCH] rest converted, time to fix maps --- conquest/conquest.py | 47 ++++++++++++++++++++++++---------------- conquest/conquestgame.py | 21 ++++++++++++++---- conquest/regioner.py | 7 +++++- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/conquest/conquest.py b/conquest/conquest.py index ec04403..36a0c2e 100644 --- a/conquest/conquest.py +++ b/conquest/conquest.py @@ -1,5 +1,6 @@ import asyncio import json +import logging import pathlib from collections import defaultdict from io import BytesIO @@ -20,6 +21,8 @@ from conquest.regioner import ConquestMap, MapMaker, composite_regions ERROR_CONQUEST_SET_MAP = "No map is currently set. See `[p]conquest set map`" +log = logging.getLogger("red.fox_v3.conquest") + class Conquest(commands.Cog): """ @@ -83,28 +86,28 @@ class Conquest(commands.Cog): """ self.asset_path = bundled_data_path(self) / "assets" for guild in self.bot.guilds: - game_name = await self.config.guild(guild).current_game() - if game_name is not None: - await self.load_guild_data(guild, game_name) + game_data = await self.config.guild(guild).current_game() + if game_data is not None: + await self.load_guild_data(guild, **game_data) # for guild_id, game_name in self.current_maps.items(): # await self.current_map_load(guild_id, game_name) - async def load_guild_data(self, guild: discord.Guild, game_name: str): - map_data = await self.config.games.get_raw(game_name) - if map_data is None: - return False - map_name = map_data["map_name"] - map_path = self._path_if_custom(map_data["is_custom"]) / map_name + async def load_guild_data(self, guild: discord.Guild, map_name: str, is_custom: bool): + # map_data = await self.config.games.get_raw(game_name) + # if map_data is None: + # return False + # map_name = map_data["map_name"] + map_path = self._path_if_custom(is_custom) / map_name if ( - not (self.current_map_folder / guild.id).exists() - or not (self.current_map_folder / guild.id / map_name).exists() + not (self.current_map_folder / str(guild.id)).exists() + or not (self.current_map_folder / str(guild.id) / map_name).exists() ): return False self.current_games[guild.id] = ConquestGame( - map_path, map_name, self.current_map_folder / guild.id / map_name + map_path, map_name, self.current_map_folder / str(guild.id) / map_name ) return True @@ -274,7 +277,7 @@ class Conquest(commands.Cog): return self.mm = MapMaker(map_path) - await self.mm.load_data() + # await self.mm.load_data() await ctx.tick() @@ -500,7 +503,7 @@ class Conquest(commands.Cog): @conquest_set.command(name="map") async def _conquest_set_map( - self, ctx: Context, mapname: str, is_custom: bool = False, reset: bool = False + self, ctx: Context, mapname: str, reset: bool = False, is_custom: bool = False ): """ Select a map from current available maps @@ -516,9 +519,10 @@ class Conquest(commands.Cog): ) return - self.current_games[ctx.guild.id] = ConquestGame( - map_dir, mapname, self.current_map_folder / ctx.guild.id / mapname - ) + guild_folder = self.current_map_folder / str(ctx.guild.id) + if not guild_folder.exists(): + guild_folder.mkdir() + self.current_games[ctx.guild.id] = ConquestGame(map_dir, mapname, guild_folder / mapname) # self.current_map = mapname # self.is_custom = is_custom @@ -529,6 +533,12 @@ class Conquest(commands.Cog): await self.current_games[ctx.guild.id].resume_game(ctx, reset) + new_game = self.default_games.copy() + new_game["map_name"] = mapname + new_game["is_custom"] = is_custom + + await self.config.guild(ctx.guild).current_game.set(new_game) + # current_map_folder = await self._get_current_map_folder() # current_map = current_map_folder / f"current.{self.ext}" # @@ -603,7 +613,7 @@ class Conquest(commands.Cog): await ctx.maybe_send_embed(f"Invalid color {color}") return - if start_region < end_region: + if start_region > end_region: start_region, end_region = end_region, start_region if end_region > current_game.region_max or start_region < 1: @@ -654,4 +664,3 @@ class Conquest(commands.Cog): map_file = await current_game.get_maybe_zoomed_map("current") await ctx.send(file=map_file) - diff --git a/conquest/conquestgame.py b/conquest/conquestgame.py index c78e9ba..ab14201 100644 --- a/conquest/conquestgame.py +++ b/conquest/conquestgame.py @@ -1,5 +1,6 @@ import asyncio import json +import logging import pathlib from shutil import copyfile from types import SimpleNamespace @@ -11,6 +12,8 @@ from redbot.core import commands from conquest.regioner import ConquestMap, composite_regions +log = logging.getLogger("red.fox_v3.conquest.conquestgame") + class ConquestGame: ext = "PNG" @@ -35,7 +38,9 @@ class ConquestGame: 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.zoomed_numbered_current_map = ( + self.current_map_folder / self.zoomed_numbered_current_filename + ) self.region_max = self.source_map.region_max @@ -64,7 +69,7 @@ class ConquestGame: async def _process_take_regions(self, color, regions): im = Image.open(self.current_map) - out: Image.Image = await composite_regions( + out: Optional[Image.Image] = await composite_regions( im, regions, color, @@ -74,7 +79,9 @@ class ConquestGame: # 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 + if ( + not self.source_map.numbers_path().exists() + ): # No numbers map, can't add numbers to current return self.source_map.numbered_path() current_map = Image.open(self.current_map) @@ -92,7 +99,13 @@ class ConquestGame: 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 + 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: diff --git a/conquest/regioner.py b/conquest/regioner.py index 083bb14..b6eb892 100644 --- a/conquest/regioner.py +++ b/conquest/regioner.py @@ -1,5 +1,6 @@ import asyncio import json +import logging import pathlib import shutil from io import BytesIO @@ -8,6 +9,8 @@ from typing import List, Union from PIL import Image, ImageChops, ImageColor, ImageDraw, ImageFont, ImageOps from PIL.ImageDraw import _color_diff +log = logging.getLogger("red.fox_v3.conquest.regioner") + async def composite_regions(im, regions, color, masks_path) -> Union[Image.Image, None]: im2 = Image.new("RGB", im.size, color) @@ -137,6 +140,8 @@ class ConquestMap: self.region_max = None self.regions = {} + self.load_data() + def masks_path(self): return self.path / "masks" @@ -152,7 +157,7 @@ class ConquestMap: def numbered_path(self): return self.path / "numbered.png" - async def load_data(self): + def load_data(self): with self.data_path().open() as dp: data = json.load(dp)