From 0f38b7b45cf035ae35317f8f812d666f28c1b47c Mon Sep 17 00:00:00 2001 From: un1xx Date: Fri, 18 Apr 2025 17:54:12 +0530 Subject: [PATCH] update --- exts/war_monitor.py | 101 ++++++++++++++------------------------------ utils/funcs.py | 6 +++ 2 files changed, 38 insertions(+), 69 deletions(-) diff --git a/exts/war_monitor.py b/exts/war_monitor.py index d11415a..fb76528 100644 --- a/exts/war_monitor.py +++ b/exts/war_monitor.py @@ -17,6 +17,7 @@ from botclass import Brahmos, CustomCtx from utils.base import BasicView from utils.emojis import Emojis from utils.paginator import Paginator +from utils.funcs import format_count ALLIANCE_ID = 10177 GUILD = 943540698492334120 @@ -88,12 +89,6 @@ class WarMonitor(commands.Cog): self.war_update_task.cancel() async def war_logger(self, war: pnwkit.War): - - def format_count(num): - if num >= 1000: - return f"{num/1000:,.1f}K" - else: - return num async def build_embed(nation_id: int, color: discord.Color): nation = await self.get_nation(nation_id) @@ -145,20 +140,16 @@ class WarMonitor(commands.Cog): channel = discord.utils.get(category.text_channels, name=f"{attacker.nation_name.replace(' ', '-')}-{war.id}") return channel - async def build_nation_card(self, nation: pnwkit.Nation) -> discord.Embed: - def format_count(num): - if num >= 1000: - return f"{num/1000:,.1f}K" - else: - return num + async def build_nation_card(self, nation: pnwkit.Nation, color: discord.Color) -> discord.Embed: em = discord.Embed( title=f"{nation.nation_name}", url=f"https://politicsandwar.com/nation/id={nation.id}", - color=discord.Color.blurple(), + color=color, timestamp=datetime.datetime.now() ) em.set_thumbnail(url=nation.flag) - em.set_author(name=nation.alliance.name, icon_url=nation.alliance.flag, url=f"https://politicsandwar.com/alliance/id={nation.alliance.id}") + if nation.alliance: + em.set_author(name=nation.alliance.name, icon_url=nation.alliance.flag, url=f"https://politicsandwar.com/alliance/id={nation.alliance.id}") em.add_field(name="Score", value=f"{nation.score}") em.add_field(name="Cities", value=f"{nation.num_cities}") em.add_field( @@ -187,7 +178,7 @@ class WarMonitor(commands.Cog): em.add_field(name="Defender MAPs", value=war.def_points) em.add_field(name="Attacker Resistance", value=war.att_resistance) em.add_field(name="Defender Resistance", value=war.def_resistance) - em.add_field(name="War Type", value=war.war_type) + em.add_field(name="War Type", value=war.war_type.name) if not war.ground_control: value = attacker.nation_name if war.ground_control == war.att_id else defender.nation_name em.add_field(name="Ground Control", value=value, inline=False) @@ -200,69 +191,41 @@ class WarMonitor(commands.Cog): return em + async def calc_inrange(self, war: pnwkit.War, channel: discord.TextChannel): + alliances = self.kit.query( + "alliances", + {"id": MONITORED_ALLIANCES}, + "id, name, flag, nations {id, name, score, soldiers, tanks, aircraft, ships, missiles, nukes, num_cities}" + ) + nations = alliances.alliances[0].nations + alliances.alliances[1].nations + attacker = await self.get_nation(war.att_id) + score = attacker.score + in_range = [] + for nation in nations: + min_score = nation.score*0.75 + max_score = nation.score*2.50 + if min_score <= score <= max_score: + in_range.append(nation) + if not in_range: + await channel.send("> No allied nations in range") + else: + for n in range(0, len(in_range), 10): + message = "\n".join([f"[{n.nation_name}](https://politicsandwar.com/nation/id={n.id}) {Emojis.soldiers} {format_count(n.soldiers)} {Emojis.tanks} {format_count(n.tanks)} {Emojis.planes} {format_count(n.aircraft)} {Emojis.ships} {format_count(n.ships)} {Emojis.missiles} {format_count(n.missiles)} {Emojis.nukes} {format_count(n.nukes)}" for n in in_range[n:n+10]]) + await channel.send(message) + async def defense_handler(self, war: pnwkit.War): category = await self.bot.fetch_channel(CATEGORY) attacker = await self.get_nation(war.att_id) defender = await self.get_nation(war.def_id) channel = await category.create_text_channel(name=f"{attacker.nation_name}-{war.id}") - atk_card = await self.build_nation_card(attacker) - def_card = await self.build_nation_card(defender) + atk_card = await self.build_nation_card(attacker, discord.Color.red()) + def_card = await self.build_nation_card(defender, discord.Color.green()) war_card = await self.build_war_card(war) await channel.send(embeds=[atk_card, def_card]) button = self.timeline_button(war) await channel.send(embeds=[war_card], view=button) - - - # @tasks.loop(minutes=2) - # async def war_check(self): - # wars = await self.kit.query( - # "wars", - # {"alliance_id": ALLIANCE_ID}, - # ( - # "id," - # "date," - # "att_points," - # "def_points," - # "att_resistance," - # "def_resistance," - # "reason," - # "ground_control," - # "war_type," - # "air_superiority," - # "naval_blockade," - # "turns_left," - # "att_alliance_id," - # "def_alliance_id," - # "attacker {" - # "nation_name," - # "soldiers," - # "tanks," - # "aircraft," - # "ships," - # "missiles," - # "score," - # "flag," - # "nukes }," - # "defender {" - # "nation_name," - # "soldiers," - # "tanks," - # "aircraft," - # "ships," - # "missiles," - # "flag," - # "score," - # "nukes }" - # ) - # ) - - # wars = wars.wars - # war1 = wars[0] - # guild = await self.bot.fetch_guild(GUILD) - # # category = discord.utils.get(guild.categories, id=CATEGORY) - # # if war1.attacker.nation_name in [c.name for c in category.channels]: - # # return - # # ch = await guild.create_text_channel(name=war1.attacker.nation_name, category=category) + await self.calc_inrange(war, channel) + diff --git a/utils/funcs.py b/utils/funcs.py index 6d588b4..94a56c7 100644 --- a/utils/funcs.py +++ b/utils/funcs.py @@ -23,3 +23,9 @@ async def get_webhook( def discord_bg_embed(desc: str = "\u200b") -> discord.Embed: return discord.Embed(color=0x36393E, description=desc) + +def format_count(num): + if num >= 1000: + return f"{num/1000:,.1f}K" + else: + return num \ No newline at end of file