Handle zoom properly, save it to json for each map as well

pull/113/head
bobloy 5 years ago
parent ff3b6cf2a5
commit 8532d4dc01

@ -19,10 +19,14 @@ class Conquest(commands.Cog):
Less important information about the cog Less important information about the cog
""" """
default_zoom_json = {"enabled": False, "x": -1, "y": -1, "zoom": 1.0}
def __init__(self, bot: Red): def __init__(self, bot: Red):
super().__init__() super().__init__()
self.bot = bot self.bot = bot
self.config = Config.get_conf(self, identifier=67111110113117101115116, force_registration=True) self.config = Config.get_conf(
self, identifier=67111110113117101115116, force_registration=True
)
default_guild = {} default_guild = {}
default_global = {"current_map": None} default_global = {"current_map": None}
@ -43,6 +47,10 @@ class Conquest(commands.Cog):
self.asset_path = bundled_data_path(self) / "assets" self.asset_path = bundled_data_path(self) / "assets"
self.current_map = await self.config.current_map() self.current_map = await self.config.current_map()
map_data_path = self.asset_path / self.current_map / "data.json"
with map_data_path.open() as mapdata:
self.map_data = json.load(mapdata)
@commands.group() @commands.group()
async def conquest(self, ctx: commands.Context): async def conquest(self, ctx: commands.Context):
""" """
@ -69,29 +77,87 @@ class Conquest(commands.Cog):
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
pass pass
@conquest_set.command(name="resetzoom")
async def _conquest_set_resetzoom(self, ctx: commands.Context):
"""Resets the zoom level of the current map"""
if self.current_map is None:
await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`")
return
zoom_json_path = self.data_path / self.current_map / "settings.json"
if not zoom_json_path.exists():
await ctx.maybe_send_embed(
f"No zoom data found for {self.current_map}, reset not needed"
)
return
with zoom_json_path.open("w+") as zoom_json:
json.dump({"enabled": False}, zoom_json)
await ctx.tick()
@conquest_set.command(name="zoom")
async def _conquest_set_zoom(self, ctx: commands.Context, x: int, y: int, zoom: float):
"""
Set the zoom level and position of the current map
x: positive integer
y: positive integer
zoom: float greater than or equal to 1
"""
if self.current_map is None:
await ctx.maybe_send_embed("No map is currently set. See `[p]conquest set map`")
return
if x < 0 or y < 0 or zoom < 1:
await ctx.send_help()
return
zoom_json_path = self.data_path / self.current_map / "settings.json"
zoom_data = self.default_zoom_json.copy()
zoom_data["enabled"] = True
zoom_data["x"] = x
zoom_data["y"] = y
zoom_data["zoom"] = zoom
with zoom_json_path.open("w+") as zoom_json:
json.dump(zoom_data, zoom_json)
await ctx.tick()
@conquest_set.command(name="zoomtest") @conquest_set.command(name="zoomtest")
async def _conquest_set_zoomtest(self, ctx: commands.Context, x: int, y: int, zoom: float): async def _conquest_set_zoomtest(self, ctx: commands.Context, x: int, y: int, zoom: float):
"""Test the zoom level and position of the current map""" """
Test the zoom level and position of the current map
x: positive integer
y: positive integer
zoom: float greater than or equal to 1
"""
if self.current_map is None: if self.current_map 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 = Image.open(self.data_path / self.current_map / "current.jpg") if x < 0 or y < 0 or zoom < 1:
await ctx.send_help()
return
zoomed_path = await self._create_zoomed_map(
self.data_path / self.current_map / "current.jpg", x, y, zoom
)
await ctx.send(file=discord.File(fp=zoomed_path, filename="current_zoomed.jpg",))
async def _create_zoomed_map(self, map_path, x, y, zoom, **kwargs):
current_map = Image.open(map_path)
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.data_path / self.current_map / "zoomed.jpg", "jpeg")
zoomed_map.save(self.data_path / self.current_map / "current_zoomed.jpg", "jpeg") return self.data_path / self.current_map / "zoomed.jpg"
await ctx.send(
file=discord.File(
fp=self.data_path / self.current_map / "current_zoomed.jpg",
filename="current_zoomed.jpg",
)
)
@conquest_set.command(name="save") @conquest_set.command(name="save")
async def _conquest_set_save(self, ctx: commands.Context, *, save_name): async def _conquest_set_save(self, ctx: commands.Context, *, save_name):
@ -104,7 +170,7 @@ class Conquest(commands.Cog):
current_map = current_map_folder / "current.jpg" current_map = current_map_folder / "current.jpg"
if not current_map_folder.exists() or not current_map.exists(): if not current_map_folder.exists() or not current_map.exists():
await ctx.maybe_send_embed("Current map doesn't exist! Try settin a new one") await ctx.maybe_send_embed("Current map doesn't exist! Try setting a new one")
return return
copyfile(current_map, current_map_folder / f"{save_name}.jpg") copyfile(current_map, current_map_folder / f"{save_name}.jpg")
@ -174,7 +240,22 @@ class Conquest(commands.Cog):
return return
current_jpg = self.data_path / self.current_map / "current.jpg" current_jpg = self.data_path / self.current_map / "current.jpg"
await ctx.send(file=discord.File(fp=current_jpg, filename="current_map.jpg"))
await self._send_maybe_zoomed_map(ctx, current_jpg, "current_map.jpg")
async def _send_maybe_zoomed_map(self, ctx, map_path, filename):
zoom_data = {"enabled": False}
zoom_json_path = self.data_path / self.current_map / "settings.json"
if zoom_json_path.exists():
with zoom_json_path.open() as zoom_json:
zoom_data = json.load(zoom_json)
if zoom_data["enabled"]:
map_path = await self._create_zoomed_map(map_path, **zoom_data)
await ctx.send(file=discord.File(fp=map_path, filename=filename))
@conquest.command("blank") @conquest.command("blank")
async def _conquest_blank(self, ctx: commands.Context): async def _conquest_blank(self, ctx: commands.Context):
@ -186,7 +267,9 @@ class Conquest(commands.Cog):
return return
current_blank_jpg = self.asset_path / self.current_map / "blank.jpg" current_blank_jpg = self.asset_path / self.current_map / "blank.jpg"
await ctx.send(file=discord.File(fp=current_blank_jpg, filename="blank_map.jpg"))
await self._send_maybe_zoomed_map(ctx, current_blank_jpg, "blank_map.jpg")
# await ctx.send(file=discord.File(fp=current_blank_jpg, filename="blank_map.jpg"))
@conquest.command("numbered") @conquest.command("numbered")
async def _conquest_numbered(self, ctx: commands.Context): async def _conquest_numbered(self, ctx: commands.Context):
@ -217,12 +300,15 @@ class Conquest(commands.Cog):
self.data_path / self.current_map / "current_numbered.jpg", "jpeg" self.data_path / self.current_map / "current_numbered.jpg", "jpeg"
) )
await ctx.send( await self._send_maybe_zoomed_map(
file=discord.File( ctx, self.data_path / self.current_map / "current_numbered.jpg", "current_numbered.jpg"
fp=self.data_path / self.current_map / "current_numbered.jpg",
filename="current_numbered.jpg",
)
) )
# await ctx.send(
# file=discord.File(
# fp=self.data_path / self.current_map / "current_numbered.jpg",
# filename="current_numbered.jpg",
# )
# )
@conquest.command(name="take") @conquest.command(name="take")
async def _conquest_take(self, ctx: commands.Context, regions: Greedy[int], *, color: str): async def _conquest_take(self, ctx: commands.Context, regions: Greedy[int], *, color: str):
@ -258,7 +344,8 @@ class Conquest(commands.Cog):
out.save(current_jpg_path, "jpeg") out.save(current_jpg_path, "jpeg")
await ctx.send(file=discord.File(fp=current_jpg_path, filename="map.jpg")) await self._send_maybe_zoomed_map(ctx, current_jpg_path, "map.jpg")
# await ctx.send(file=discord.File(fp=current_jpg_path, filename="map.jpg"))
async def _composite_regions(self, im, regions, color) -> Image.Image: async def _composite_regions(self, im, regions, color) -> Image.Image:

Loading…
Cancel
Save