The rest converted???

conquest_develop
bobloy 3 years ago
parent e02c7ba14b
commit 2315bd9fc3

@ -18,6 +18,8 @@ from redbot.core.utils.predicates import MessagePredicate
from conquest.conquestgame import ConquestGame from conquest.conquestgame import ConquestGame
from conquest.regioner import ConquestMap, MapMaker, composite_regions 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): class Conquest(commands.Cog):
""" """
@ -414,11 +416,12 @@ class Conquest(commands.Cog):
@conquest_set.command(name="resetzoom") @conquest_set.command(name="resetzoom")
async def _conquest_set_resetzoom(self, ctx: Context): async def _conquest_set_resetzoom(self, ctx: Context):
"""Resets the zoom level of the current map""" """Resets the zoom level of the current map"""
if self.current_games[ctx.guild.id] is None: current_game = self.current_games[ctx.guild.id]
await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") if current_game is None:
await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP)
return 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.maybe_send_embed(f"No zoom data found, reset not needed")
await ctx.tick() await ctx.tick()
@ -431,15 +434,16 @@ class Conquest(commands.Cog):
y: positive integer y: positive integer
zoom: float greater than or equal to 1 zoom: float greater than or equal to 1
""" """
if self.current_games[ctx.guild.id] is None: current_game = self.current_games[ctx.guild.id]
await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") if current_game is None:
await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP)
return return
if x < 0 or y < 0 or zoom < 1: if x < 0 or y < 0 or zoom < 1:
await ctx.send_help() await ctx.send_help()
return return
await self.current_games[ctx.guild.id].set_zoom(x, y, zoom) await current_game.set_zoom(x, y, zoom)
await ctx.tick() await ctx.tick()
@ -452,17 +456,18 @@ class Conquest(commands.Cog):
y: positive integer y: positive integer
zoom: float greater than or equal to 1 zoom: float greater than or equal to 1
""" """
if self.current_games[ctx.guild.id] is None: current_game = self.current_games[ctx.guild.id]
await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") if current_game is None:
await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP)
return return
if x < 0 or y < 0 or zoom < 1: if x < 0 or y < 0 or zoom < 1:
await ctx.send_help() await ctx.send_help()
return return
# out_of_date marks zoom as oudated, since this overwrite the temp # UNUSED: out_of_date marks zoom as oudated, since this overwrite the temp
zoomed_path = await self.current_games[ctx.guild.id].create_zoomed_map( zoomed_path = await current_game.create_zoomed_map(
x, y, zoom, out_of_date=True 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}")) 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") @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_games[ctx.guild.id] is None: current_game = self.current_games[ctx.guild.id]
await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") if current_game is None:
await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP)
return return
await self.current_games[ctx.guild.id].save_as(save_name) await current_game.save_as(save_name)
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_games[ctx.guild.id] is None: current_game = self.current_games[ctx.guild.id]
await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") if current_game is None:
await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP)
return 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") await ctx.maybe_send_embed(f"Saved map not found, check your spelling")
return return
@ -513,25 +520,27 @@ class Conquest(commands.Cog):
map_dir, mapname, self.current_map_folder / ctx.guild.id / mapname map_dir, mapname, self.current_map_folder / ctx.guild.id / mapname
) )
self.current_map = mapname # self.current_map = mapname
self.is_custom = is_custom # self.is_custom = is_custom
await self.config.current_map.set(self.current_map) # Save to config too # await self.config.current_map.set(self.current_map) # Save to config too
await self.config.is_custom.set(is_custom) # await self.config.is_custom.set(is_custom)
#
await self.current_map_load() # await self.current_map_load()
current_map_folder = await self._get_current_map_folder() await self.current_games[ctx.guild.id].resume_game(ctx, reset)
current_map = current_map_folder / f"current.{self.ext}"
# current_map_folder = await self._get_current_map_folder()
if not reset and current_map.exists(): # current_map = current_map_folder / f"current.{self.ext}"
await ctx.maybe_send_embed( #
"This map is already in progress, resuming from last game\n" # if not reset and current_map.exists():
"Use `[p]conquest set map [mapname] True` to start a new game" # await ctx.maybe_send_embed(
) # "This map is already in progress, resuming from last game\n"
else: # "Use `[p]conquest set map [mapname] True` to start a new game"
if not current_map_folder.exists(): # )
current_map_folder.mkdir() # else:
copyfile(check_path / mapname / f"blank.{self.ext}", current_map) # if not current_map_folder.exists():
# current_map_folder.mkdir()
# copyfile(check_path / mapname / f"blank.{self.ext}", current_map)
await ctx.tick() await ctx.tick()
@ -540,66 +549,39 @@ class Conquest(commands.Cog):
""" """
Send the current map. Send the current map.
""" """
if self.current_map is None: current_game = self.current_games[ctx.guild.id]
await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") if current_game is None:
await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP)
return return
async with ctx.typing():
current_img = await self._get_current_map_folder() / f"current.{self.ext}" map_file = await current_game.get_maybe_zoomed_map("current")
await ctx.send(file=map_file)
await self._send_maybe_zoomed_map(ctx, current_img, f"current_map.{self.ext}")
@conquest.command("blank") @conquest.command("blank")
async def _conquest_blank(self, ctx: Context): async def _conquest_blank(self, ctx: Context):
""" """
Print the blank version of the current map, for reference. Print the blank version of the current map, for reference.
""" """
if self.current_map is None: current_game = self.current_games[ctx.guild.id]
await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") if current_game is None:
await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP)
return return
async with ctx.typing():
current_blank_img = self._path_if_custom() / self.current_map / f"blank.{self.ext}" map_file = await current_game.get_maybe_zoomed_map("blank")
await ctx.send(file=map_file)
await self._send_maybe_zoomed_map(ctx, current_blank_img, f"blank_map.{self.ext}")
@conquest.command("numbered") @conquest.command("numbered")
async def _conquest_numbered(self, ctx: Context): async def _conquest_numbered(self, ctx: Context):
""" """
Print the numbered version of the current map, for reference. Print the numbered version of the current map, for reference.
""" """
if self.current_map is None: current_game = self.current_games[ctx.guild.id]
await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") if current_game is None:
await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP)
return return
async with ctx.typing(): async with ctx.typing():
numbers_path = self._path_if_custom() / self.current_map / f"numbers.{self.ext}" map_file = await current_game.get_maybe_zoomed_map("numbered")
if not numbers_path.exists(): await ctx.send(file=map_file)
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}",
)
@conquest.command(name="multitake") @conquest.command(name="multitake")
async def _conquest_multitake( async def _conquest_multitake(
@ -610,8 +592,9 @@ class Conquest(commands.Cog):
:param start_region: :param start_region:
""" """
if self.current_map is None: current_game = self.current_games[ctx.guild.id]
await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") if current_game is None:
await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP)
return return
try: try:
@ -620,18 +603,22 @@ class Conquest(commands.Cog):
await ctx.maybe_send_embed(f"Invalid color {color}") await ctx.maybe_send_embed(f"Invalid color {color}")
return 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( 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 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)] 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") @conquest.command(name="take")
async def _conquest_take(self, ctx: Context, regions: Greedy[int], *, color: str): async def _conquest_take(self, ctx: Context, regions: Greedy[int], *, color: str):
@ -645,8 +632,9 @@ class Conquest(commands.Cog):
await ctx.send_help() await ctx.send_help()
return return
if self.current_map is None: current_game = self.current_games[ctx.guild.id]
await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`") if current_game is None:
await ctx.maybe_send_embed(ERROR_CONQUEST_SET_MAP)
return return
try: try:
@ -656,10 +644,14 @@ class Conquest(commands.Cog):
return return
for region in regions: 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( 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 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)

@ -1,14 +1,18 @@
import asyncio
import json import json
import pathlib import pathlib
from shutil import copyfile from shutil import copyfile
from types import SimpleNamespace
from typing import Optional, Union
import discord 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 from conquest.regioner import ConquestMap, composite_regions
class ConquestGame: class ConquestGame:
ext = "PNG" ext = "PNG"
ext_format = "PNG" ext_format = "PNG"
@ -20,10 +24,22 @@ class ConquestGame:
self.current_map_folder = custom_map_path self.current_map_folder = custom_map_path
self.settings_json = self.current_map_folder / "settings.json" 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): async def save_region(self, region):
if not self.custom: if not self.custom:
@ -31,7 +47,19 @@ class ConquestGame:
pass # TODO: region data saving pass # TODO: region data saving
async def start_game(self): 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): async def _process_take_regions(self, color, regions):
im = Image.open(self.current_map) im = Image.open(self.current_map)
@ -43,37 +71,69 @@ class ConquestGame:
self.source_map.masks_path(), self.source_map.masks_path(),
) )
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.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""" """Pass out_of_date when created a zoomed map based on something other than the settings json"""
if out_of_date: # if out_of_date:
self.zoom_is_out_of_date = True # self.zoom_is_out_of_date.current = True
current_map = Image.open(self.current_map_folder)
w, h = current_map.size # 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 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 = 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} zoom_data = {"enabled": False}
if self.settings_json.exists(): if self.settings_json.exists():
with self.settings_json.open() as zoom_json: with self.settings_json.open() as zoom_json:
zoom_data = json.load(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 if zoom_data["enabled"]: # Send zoomed map instead of current 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, source_map=map_path, target_map=zoomed_path)
await self.create_zoomed_map(**zoom_data) map_path = zoomed_path
self.zoom_is_out_of_date = False # 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): async def reset_zoom(self):
if not self.settings_json.exists(): if not self.settings_json.exists():
@ -82,7 +142,7 @@ class ConquestGame:
with self.settings_json.open("w+") as zoom_json: with self.settings_json.open("w+") as zoom_json:
json.dump({"enabled": False}, zoom_json, sort_keys=True, indent=4) 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 return True
@ -94,7 +154,7 @@ class ConquestGame:
zoom_data["y"] = y zoom_data["y"] = y
zoom_data["zoom"] = zoom 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: 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)

Loading…
Cancel
Save