Skip to content

Commit

Permalink
cleaned up stats.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nfearnley committed May 21, 2024
1 parent 7c172b9 commit aa47cc1
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 94 deletions.
4 changes: 2 additions & 2 deletions sizebot/cogs/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from sizebot.lib import checks, userdb, utils
from sizebot.lib.constants import colors, emojis
from sizebot.lib.menu import Menu
from sizebot.lib.stats import statmap
from sizebot.lib.stats import all_stats
from sizebot.lib.types import BotContext
from sizebot.lib.units import SV, WV

Expand All @@ -41,7 +41,7 @@

alpha_warning = f"{emojis.warning} **This command is in ALPHA.** It may break, be borked, change randomly, be removed randomly, or be deprecated at any time. Proceed with caution."
accuracy_warning = f"{emojis.warning} **This command may not be entirely accurate.** It makes assumptions and guesses that have a decent amount of wiggle room, even because the information isn't known or because the calculations are meant to be for fiction only. Don't take these results at face value!"
stats_string = utils.sentence_join([f"`{v}`" for v in statmap.keys()])
stats_string = utils.sentence_join([f"`{s.key}`" for s in all_stats])


async def post_report(report_type: str, message: discord.Message, report_text: str):
Expand Down
4 changes: 2 additions & 2 deletions sizebot/lib/fakeplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sizebot.lib.types import BotContext
from sizebot.lib.units import SV, WV
from sizebot.lib.utils import parse_scale
from sizebot.lib.stats import HOUR, statmap
from sizebot.lib.stats import HOUR, get_mapped_stat

T = TypeVar("T")

Expand Down Expand Up @@ -69,7 +69,7 @@ def parse_keyvalue(kv_str: str) -> tuple[str, Any]:
key, val_str = m.groups()
# Special exception for shoesize where we _actually_ set footlength
if key != "shoesize":
key = statmap.get(key, None)
key = get_mapped_stat(key)
if key not in fakestats:
raise InvalidStat(key)
stat = fakestats[key]
Expand Down
1 change: 0 additions & 1 deletion sizebot/lib/macrovision.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import sizebot.data
from sizebot.conf import conf
from sizebot.lib import errors
from sizebot.lib.digidecimal import Decimal
from sizebot.lib.stats import StatBox
from sizebot.lib.units import SV
Expand Down
16 changes: 4 additions & 12 deletions sizebot/lib/proportions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sizebot.lib.types import EmbedField, EmbedToSend, StrToSend
from sizebot.lib.units import SV, TV, WV
from sizebot.lib.userdb import User
from sizebot.lib.stats import Stat, calc_view_angle, statmap, StatBox
from sizebot.lib.stats import Stat, calc_view_angle, get_mapped_stat, StatBox

logger = logging.getLogger("sizebot")

Expand Down Expand Up @@ -40,7 +40,7 @@ def get_speedcompare(userdata1: User, userdata2: User, requesterID: int) -> Embe
# stat with stat.key=key
def get_speedcompare_stat(userdata1: User, userdata2: User, key: str) -> EmbedToSend | None:
small, big, _, _, _ = _get_compare_statboxes(userdata1, userdata2)
mapped_key = _get_mapped_stat(key)
mapped_key = get_mapped_stat(key)
if mapped_key is None:
return None
stat = big[mapped_key]
Expand Down Expand Up @@ -161,7 +161,7 @@ def get_compare_bytag(userdata1: User, userdata2: User, tag: str, requesterID: i
# stat with stat.key=key
def get_compare_stat(userdata1: User, userdata2: User, key: str) -> StrToSend | None:
small, big, small_viewby_big, big_viewby_small, _ = _get_compare_statboxes(userdata1, userdata2)
mapped_key = _get_mapped_stat(key)
mapped_key = get_mapped_stat(key)
if mapped_key is None:
return None
small_stat = small_viewby_big[mapped_key]
Expand Down Expand Up @@ -210,7 +210,7 @@ def get_stats_bytag(userdata: User, tag: str, requesterID: int) -> EmbedToSend:

# stat with stat.key=key
def get_stat(userdata: User, key: str) -> StrToSend | None:
mapped_key = _get_mapped_stat(key)
mapped_key = get_mapped_stat(key)
if mapped_key is None:
return None
stats = StatBox.load(userdata.stats).scale(userdata.scale)
Expand Down Expand Up @@ -325,14 +325,6 @@ def _get_compare_color(requesterID: int, small: StatBox, big: StatBox) -> str:
return colors.purple


def _get_mapped_stat(key: str) -> str | None:
try:
mapped_key = statmap[key]
except KeyError:
return None
return mapped_key


def _get_compare_statboxes(userdata1: User, userdata2: User) -> tuple[StatBox, StatBox, StatBox, StatBox, Decimal]:
stats1 = StatBox.load(userdata1.stats).scale(userdata1.scale)
stats2 = StatBox.load(userdata2.stats).scale(userdata2.scale)
Expand Down
7 changes: 4 additions & 3 deletions sizebot/lib/statproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging

from sizebot.lib.errors import InvalidStat, InvalidStatTag
from sizebot.lib.stats import statmap, taglist
from sizebot.lib.stats import get_mapped_stat, taglist
from sizebot.lib.types import BotContext

re_full_string = r"\$(\w+=[^;$]+;?)+"
Expand Down Expand Up @@ -35,9 +35,10 @@ def parse(cls, s: str) -> StatProxy:
raise InvalidStatTag(s)
return StatProxy(s, True)
else:
if s not in statmap.keys():
mapped = get_mapped_stat(s)
if mapped is None:
raise InvalidStat(s)
return StatProxy(statmap[s], False)
return StatProxy(mapped, False)

@classmethod
async def convert(cls, ctx: BotContext, argument: str) -> StatProxy:
Expand Down
Loading

0 comments on commit aa47cc1

Please sign in to comment.