Add map list, move set to under conquest, and WIP saving map

pull/113/head
bobloy 5 years ago
parent 43520893b3
commit 020d4645e8

@ -38,15 +38,55 @@ class Conquest(commands.Cog):
return return
async def load_data(self): async def load_data(self):
self.asset_path = bundled_data_path(self) / "asset_path" self.asset_path = bundled_data_path(self) / "assets"
@commands.group(name="conquestset") @commands.group()
async def conquest(self, ctx: commands.Context):
"""
Base command for conquest cog. Start with `[p]conquest set map` to select a map.
"""
if ctx.invoked_subcommand is None:
pass # TODO: Print current map probably
@conquest.command(name="list")
async def _conquest_list(self, ctx: commands.Context):
"""
List currently available maps
"""
maps_json = self.asset_path / "maps.json"
async with maps_json.open() as maps:
map_list = "\n".join(map_name for map_name in maps["maps"])
await ctx.maybe_send_embed(f"Current maps:\n{map_list}")
@conquest.command(name="current")
async def _conquest_current(self, ctx: commands.Context):
"""
Send the current map.
"""
if self.current_map:
await ctx.maybe_send_embed(
"No map is currently set. See `[p]conquestset map`"
)
return
current_jpg = self.data_path / self.current_map / "current.jpg"
with current_jpg.open() as map_file:
await ctx.send(file=discord.File(fp=map_file, filename="current_map.jpg"))
@conquest.group(name="set")
async def conquest_set(self, ctx: commands.Context): async def conquest_set(self, ctx: commands.Context):
"""Base command for admin actions like selecting a map"""
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
pass pass
@conquest_set.command(name="map") @conquest_set.command(name="map")
async def _conquest_set_map(self, ctx: commands.Context, mapname: str): async def _conquest_set_map(self, ctx: commands.Context, mapname: str):
"""
Select a map from current available maps
To add more maps, see the guide (WIP)
"""
map_dir = self.asset_path / mapname map_dir = self.asset_path / mapname
if not map_dir.exists() or not map_dir.is_dir(): if not map_dir.exists() or not map_dir.is_dir():
await ctx.maybe_send_embed( await ctx.maybe_send_embed(
@ -68,25 +108,12 @@ class Conquest(commands.Cog):
await ctx.tick() await ctx.tick()
@commands.group()
async def conquest(self, ctx: commands.Context):
if ctx.invoked_subcommand is None:
pass # TODO: Print current map probably
@conquest.command("current")
async def _conquest_current(self, ctx: commands.Context):
if self.current_map:
await ctx.maybe_send_embed(
"No map is currently set. See `[p]conquestset map`"
)
return
with open(self.data_path / self.current_map / "current.jpg") as map_file:
await ctx.send(file=discord.File(fp=map_file, filename="current_map.jpg"))
@conquest.command("blank") @conquest.command("blank")
async def _conquest_blank(self, ctx: commands.Context): async def _conquest_blank(self, ctx: commands.Context):
if self.current_map: """
Print the blank version of the current map, for reference.
"""
if self.current_map is None:
await ctx.maybe_send_embed( await ctx.maybe_send_embed(
"No map is currently set. See `[p]conquest set map`" "No map is currently set. See `[p]conquest set map`"
) )
@ -97,7 +124,10 @@ class Conquest(commands.Cog):
@conquest.command("numbered") @conquest.command("numbered")
async def _conquest_numbered(self, ctx: commands.Context): async def _conquest_numbered(self, ctx: commands.Context):
if self.current_map: """
Print the numbered version of the current map, for reference.
"""
if self.current_map is None:
await ctx.maybe_send_embed( await ctx.maybe_send_embed(
"No map is currently set. See `[p]conquest set map`" "No map is currently set. See `[p]conquest set map`"
) )
@ -110,10 +140,22 @@ class Conquest(commands.Cog):
async def _conquest_take( async def _conquest_take(
self, ctx: commands.Context, regions: Greedy[int], color: str self, ctx: commands.Context, regions: Greedy[int], color: str
): ):
"""
Claim a territory or list of territories for a specified color
:param regions: List of integer regions
:param color: Color to claim regions
"""
if not regions: if not regions:
await ctx.send_help() await ctx.send_help()
return return
if self.current_map is None:
await ctx.maybe_send_embed(
"No map is currently set. See `[p]conquest set map`"
)
return
for region in regions: for region in regions:
if region > self.map_data["region_max"] or region < 1: if region > self.map_data["region_max"] or region < 1:
await ctx.maybe_send_embed( await ctx.maybe_send_embed(
@ -121,6 +163,17 @@ class Conquest(commands.Cog):
) )
im = Image.open(self.data_path / self.current_map / "current.jpg") im = Image.open(self.data_path / self.current_map / "current.jpg")
out: Image.Image = await self._composite_image(im, regions, color)
out.save(self.data_path / self.current_map / "current.jpg", "jpeg")
output_buffer = BytesIO()
out.save(output_buffer, "jpeg")
output_buffer.seek(0)
await ctx.send(file=discord.File(fp=output_buffer, filename="map.jpg"))
async def _composite_image(self, im, regions, color) -> Image.Image:
im2 = Image.new("RGB", im.size, color) im2 = Image.new("RGB", im.size, color)
out = None out = None
for region in regions: for region in regions:
@ -131,11 +184,4 @@ class Conquest(commands.Cog):
out = Image.composite(im, im2, mask) out = Image.composite(im, im2, mask)
else: else:
out = Image.composite(out, im2, mask) out = Image.composite(out, im2, mask)
return out
output_buffer = BytesIO()
out.save(output_buffer, "jpeg")
output_buffer.seek(0)
# TODO: Save the map in between
await ctx.send(file=discord.File(fp=output_buffer, filename="map.jpg"))

@ -0,0 +1,5 @@
{
"maps": [
"simple_blank_map"
]
}
Loading…
Cancel
Save