Skip to content

Commit

Permalink
Allow owners to pull up guild stats from any of the bots guilds.
Browse files Browse the repository at this point in the history
Make stats display consistent for both places in adventurestats command.
Change adventureresults to accept the guild instead of a context object. We only ever used the guild ID anyway from it.
  • Loading branch information
TrustyJAID committed Mar 12, 2024
1 parent e6dec2a commit d52a5a4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
8 changes: 4 additions & 4 deletions adventure/adventure.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ async def get_challenge(self, monsters: Dict[str, Monster], rng: Random):
return choice

def _dynamic_monster_stats(self, ctx: commands.Context, choice: Monster, rng: Random) -> Monster:
stat_range = self._adv_results.get_stat_range(ctx)
stat_range = self._adv_results.get_stat_range(ctx.guild)
win_percentage = stat_range.get("win_percent", 0.5)
choice["cdef"] = choice.get("cdef", 1.0)
if win_percentage >= 0.90:
Expand Down Expand Up @@ -867,7 +867,7 @@ async def update_monster_roster(
async def _simple(
self, ctx: commands.Context, adventure_msg, challenge: Union[int, str, None] = None, attribute: str = None
):
stat_range = self._adv_results.get_stat_range(ctx)
stat_range = self._adv_results.get_stat_range(ctx.guild)
c = await Character.from_json(ctx, self.config, ctx.author, self._daily_bonus)
if stat_range.max_stat <= 0:
stat_range.max_stat = max(c.att, c.int, c.cha) * 5
Expand Down Expand Up @@ -1538,9 +1538,9 @@ async def _result(self, ctx: commands.Context, message: discord.Message):
int_dipl=humanize_number(dipl),
)
if dmg_dealt >= diplomacy:
self._adv_results.add_result(ctx, "attack", dmg_dealt, people, slain)
self._adv_results.add_result(ctx.guild, "attack", dmg_dealt, people, slain)
else:
self._adv_results.add_result(ctx, "talk", diplomacy, people, persuaded)
self._adv_results.add_result(ctx.guild, "talk", diplomacy, people, persuaded)
result_msg = result_msg + "\n" + damage_str + diplo_str

await calc_msg.delete()
Expand Down
29 changes: 17 additions & 12 deletions adventure/adventureresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dataclasses import dataclass
from typing import List, Literal, MutableMapping, TypedDict

from redbot.core import commands
import discord

log = logging.getLogger("red.cogs.adventure")

Expand All @@ -22,6 +22,11 @@ def __getitem__(self, name: str):
def get(self, name: str, default=None):
return getattr(self, name, default)

def __str__(self):
return "Main Stat: {stat_type}\nMin Stat: {min_stat:0.2f}\nMax Stat: {max_stat:0.2f}\nWin%: {win_percent:0.2f}".format(
stat_type=self.stat_type, min_stat=self.min_stat, max_stat=self.max_stat, win_percent=self.win_percent * 100
)


class Raid(TypedDict):
main_action: str
Expand All @@ -37,7 +42,7 @@ def __init__(self, num_raids: int):
self._num_raids: int = num_raids
self._last_raids: MutableMapping[int, List[Raid]] = {}

def add_result(self, ctx: commands.Context, main_action: str, amount: float, num_ppl: int, success: bool):
def add_result(self, guild: discord.Guild, main_action: str, amount: float, num_ppl: int, success: bool):
"""Add result to this object.
:main_action: Main damage action taken by the adventurers
(highest amount dealt). Should be either "attack" or
Expand All @@ -46,20 +51,20 @@ def add_result(self, ctx: commands.Context, main_action: str, amount: float, num
:num_ppl: Number of people in adventure.
:success: Whether adventure was successful or not.
"""
if ctx.guild.id not in self._last_raids:
self._last_raids[ctx.guild.id] = []
if guild.id not in self._last_raids:
self._last_raids[guild.id] = []

if len(self._last_raids.get(ctx.guild.id, [])) >= self._num_raids:
if len(self._last_raids.get(guild.id, [])) >= self._num_raids:
try:
self._last_raids[ctx.guild.id].pop(0)
self._last_raids[guild.id].pop(0)
except IndexError:
pass

self._last_raids[ctx.guild.id].append(
self._last_raids[guild.id].append(
Raid(main_action=main_action, amount=amount, num_ppl=num_ppl, success=success)
)

def get_stat_range(self, ctx: commands.Context) -> StatRange:
def get_stat_range(self, guild: discord.Guild) -> StatRange:
"""Return reasonable stat range for monster pool to have based
on last few raids' damage.
Expand All @@ -68,14 +73,14 @@ def get_stat_range(self, ctx: commands.Context) -> StatRange:
# how much % to increase damage for solo raiders so that they
# can't just solo every monster based on their own average
# damage
if ctx.guild.id not in self._last_raids:
self._last_raids[ctx.guild.id] = []
if guild.id not in self._last_raids:
self._last_raids[guild.id] = []
SOLO_RAID_SCALE: float = 0.25
min_stat: float = 0.0
max_stat: float = 0.0
stat_type: str = "hp"
win_percent: float = 0.0
if len(self._last_raids.get(ctx.guild.id, [])) == 0:
if len(self._last_raids.get(guild.id, [])) == 0:
return StatRange(stat_type=stat_type, min_stat=min_stat, max_stat=max_stat, win_percent=win_percent)

# tally up stats for raids
Expand All @@ -86,7 +91,7 @@ def get_stat_range(self, ctx: commands.Context) -> StatRange:
num_wins = 0
stat_type = "hp"
avg_amount = 0
raids = self._last_raids.get(ctx.guild.id, [])
raids = self._last_raids.get(guild.id, [])
raid_count = len(raids)
if raid_count == 0:
num_wins = self._num_raids // 2
Expand Down
29 changes: 15 additions & 14 deletions adventure/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,38 +188,39 @@ async def devreset(self, ctx: commands.Context, users: commands.Greedy[Union[dis
@commands.command(name="adventurestats")
@commands.bot_has_permissions(add_reactions=True, embed_links=True)
@commands.is_owner()
async def _adventurestats(self, ctx: commands.Context):
async def _adventurestats(self, ctx: commands.Context, guild: Optional[discord.Guild] = None):
"""[Owner] Show all current adventures."""
msg = bold(_("Active Adventures\n"))
embed_list = []

if guild is None:
guild: discord.Guild = ctx.guild
if guild is None:
return await ctx.send_help()
if len(self._sessions) > 0:
for server_id, adventure in self._sessions.items():
guild = self.bot.get_guild(server_id)
if guild is None:
server = self.bot.get_guild(server_id)
if server is None:
# should not happen but the type checker is happier
continue
stat_range = self._adv_results.get_stat_range(ctx)
stat_range = self._adv_results.get_stat_range(server)
pdef = adventure.monster_modified_stats["pdef"]
mdef = adventure.monster_modified_stats["mdef"]
cdef = adventure.monster_modified_stats.get("cdef", 1.0)
hp = adventure.monster_hp()
dipl = adventure.monster_dipl()
msg += (
f"{guild.name} - "
f"{server.name} - "
f"[{adventure.challenge}]({adventure.message.jump_url})\n"
f"[{stat_range['stat_type']}-min:{stat_range['min_stat']}-max:{stat_range['max_stat']}-winratio:{stat_range['win_percent']}] "
f"(hp:{hp}-char:{dipl}-pdef:{pdef}-mdef:{mdef}-cdef:{cdef})\n\n"
f"(hp:**{hp}**-char:**{dipl}**-pdef:**{pdef:0.2f}**-mdef:**{mdef:0.2f}**-cdef:**{cdef:0.2f}**)\n"
f"{stat_range}\n\n"
)
else:
msg += "None.\n\n"
msg += bold(_("Guild Stats\n"))
stats = self._adv_results.get_stat_range(ctx)
msg += _("Main Stat: {stat_type}\nMin Stat: {min_stat}\nMax Stat: {max_stat}\nWin%: {win_percent}").format(
stat_type=stats.stat_type, min_stat=stats.min_stat, max_stat=stats.max_stat, win_percent=stats.win_percent
)
for page in pagify(msg, delims=["\n\n"], page_length=1000):
stats = self._adv_results.get_stat_range(guild)
stats_msg = _("Stats for {guild_name}\n{stats}").format(guild_name=guild.name, stats=str(stats))
for page in pagify(msg, delims=["\n\n"], page_length=2048):
embed = discord.Embed(description=page)
embed.add_field(name=_("Guild Stats"), value=stats_msg)
embed_list.append(embed)
await BaseMenu(
source=SimpleSource(embed_list),
Expand Down

0 comments on commit d52a5a4

Please sign in to comment.