diff --git a/royalnet/packs/royal/commands/leagueoflegends.py b/royalnet/packs/royal/commands/leagueoflegends.py index 885370c2..56f593f6 100644 --- a/royalnet/packs/royal/commands/leagueoflegends.py +++ b/royalnet/packs/royal/commands/leagueoflegends.py @@ -1,6 +1,7 @@ import typing import riotwatcher import logging +import asyncio from royalnet.commands import * from royalnet.utils import * from ..tables import LeagueOfLegends @@ -23,21 +24,51 @@ class LeagueoflegendsCommand(Command): _region = "euw1" + _telegram_group_id = -1001153723135 + def __init__(self, interface: CommandInterface): super().__init__(interface) self._riotwatcher = riotwatcher.RiotWatcher(api_key=self.interface.bot.get_secret("leagueoflegends")) ... + def _notify(self, + obj: LeagueOfLegends, + attribute_name: str, + old_value: typing.Any, + new_value: typing.Any): + if self.interface.name == "telegram": + if isinstance(old_value, LeagueLeague): + # This is a rank change! + # Don't send messages for every rank change, send messages just if the TIER or RANK changes! + if old_value.tier == new_value.tier and old_value.rank == new_value.rank: + return + # Prepare the message + if new_value > old_value: + message = f"๐Ÿ“ˆ [b]{obj.user}[/b] รจ salito a {new_value} su League of Legends!" + else: + message = "๐Ÿ“‰ [b]{obj.user}[/b] รจ sceso a {new_value} su League of Legends." + # Send the message + + @staticmethod + def _change(obj: LeagueOfLegends, + attribute_name: str, + new_value: typing.Any, + callback: typing.Callable[[LeagueOfLegends, str, typing.Any, typing.Any], None]): + old_value = obj.__getattribute__(attribute_name) + if old_value != new_value: + callback(obj, attribute_name, old_value, new_value) + obj.__setattr__(attribute_name, new_value) + async def _update(self, lol: LeagueOfLegends): log.info(f"Updating: {lol}") log.debug(f"Getting summoner data: {lol}") summoner = self._riotwatcher.summoner.by_id(region=self._region, encrypted_summoner_id=lol.summoner_id) - lol.profile_icon_id = summoner["profileIconId"] - lol.summoner_name = summoner["name"] - lol.puuid = summoner["puuid"] - lol.summoner_level = summoner["summonerLevel"] - lol.summoner_id = summoner["id"] - lol.account_id = summoner["accountId"] + self._change(lol, "profile_icon_id", summoner["profileIconId"], self._notify) + self._change(lol, "summoner_name", summoner["name"], self._notify) + self._change(lol, "puuid", summoner["puuid"], self._notify) + self._change(lol, "summoner_level", summoner["summonerLevel"], self._notify) + self._change(lol, "summoner_id", summoner["id"], self._notify) + self._change(lol, "account_id", summoner["accountId"], self._notify) log.debug(f"Getting leagues data: {lol}") leagues = self._riotwatcher.league.by_summoner(region=self._region, encrypted_summoner_id=lol.summoner_id) soloq = None @@ -53,13 +84,21 @@ class LeagueoflegendsCommand(Command): twtrq = LeagueLeague.from_dict(league) if league["queueType"] == "RANKED_TFT": tftq = LeagueLeague.from_dict(league) - lol.rank_soloq = soloq - lol.rank_flexq = flexq - lol.rank_twtrq = twtrq - lol.rank_tftq = tftq + self._change(lol, "rank_soloq", soloq, self._notify) + self._change(lol, "rank_flexq", flexq, self._notify) + self._change(lol, "rank_twtrq", twtrq, self._notify) + self._change(lol, "rank_tftq", tftq, self._notify) log.debug(f"Getting mastery data: {lol}") mastery = self._riotwatcher.champion_mastery.scores_by_summoner(region=self._region, encrypted_summoner_id=lol.summoner_id) - lol.mastery_score = mastery + self._change(lol, "mastery_score", mastery, self._notify) + + async def _updater(self, period: int): + while True: + session = self.alchemy.Session() + lols = session.query(self.alchemy.LeagueOfLegends).all() + for lol in lols: + await self._update(lol) + await asyncio.sleep(period) def _display(self, lol: LeagueOfLegends): string = f"โ„น๏ธ [b]{lol.summoner_name}[/b]\n" \ diff --git a/royalnet/packs/royal/utils/leagueleague.py b/royalnet/packs/royal/utils/leagueleague.py index c20ce148..31634839 100644 --- a/royalnet/packs/royal/utils/leagueleague.py +++ b/royalnet/packs/royal/utils/leagueleague.py @@ -23,7 +23,7 @@ class LeagueLeague: self.fresh_blood: bool = fresh_blood self.veteran: bool = veteran - def __str__(self): + def __str__(self) -> str: emojis = "" if self.veteran: emojis += "๐Ÿ†" @@ -33,37 +33,56 @@ class LeagueLeague: emojis += "โญ๏ธ" return f"[b]{self.tier} {self.rank}[/b] ({self.points} LP) {emojis}" - def __repr__(self): + def __repr__(self) -> str: return f"<{self.__class__.__qualname__} {self}>" - def __eq__(self, other): - if isinstance(other, LeagueLeague): - equal = True - if other.veteran: - equal &= self.veteran == other.veteran - if other.fresh_blood: - equal &= self.fresh_blood == other.fresh_blood - if other.hot_streak: - equal &= self.hot_streak == other.hot_streak - if other.inactive: - equal &= self.inactive == other.inactive - if other.losses: - equal &= self.losses == other.losses - if other.wins: - equal &= self.wins == other.wins - if other.points: - equal &= self.points == other.points - if other.rank: - equal &= self.rank == other.rank - if other.tier: - equal &= self.tier == other.tier - return equal - else: + def __eq__(self, other) -> bool: + if not isinstance(other, LeagueLeague): raise TypeError(f"Can't compare {self.__class__.__qualname__} with {other.__class__.__qualname__}") + equal = True + if other.veteran: + equal &= self.veteran == other.veteran + if other.fresh_blood: + equal &= self.fresh_blood == other.fresh_blood + if other.hot_streak: + equal &= self.hot_streak == other.hot_streak + if other.inactive: + equal &= self.inactive == other.inactive + if other.losses: + equal &= self.losses == other.losses + if other.wins: + equal &= self.wins == other.wins + if other.points: + equal &= self.points == other.points + if other.rank: + equal &= self.rank == other.rank + if other.tier: + equal &= self.tier == other.tier + return equal - def __ne__(self, other): + def __ne__(self, other) -> bool: return not self.__eq__(other) + def __gt__(self, other) -> bool: + if not isinstance(other, LeagueLeague): + raise TypeError(f"Can't compare {self.__class__.__qualname__} with {other.__class__.__qualname__}") + if not (bool(self) and bool(other)): + raise ValueError("Can't compare partial LeagueLeagues.") + if self.tier != other.tier: + # Silver is better than Bronze + return self.tier > other.tier + elif self.rank != other.rank: + # Silver I is better than Silver IV + return self.rank < other.rank + elif self.points != other.points: + # Silver I (100 LP) is better than Silver I (0 LP) + return self.points > other.points + elif self.winrate != other.winrate: + # Silver I (100 LP with 60% winrate) is better than Silver I (100 LP with 40% winrate) + return self.winrate > other.winrate + else: + return False + def __bool__(self): result = True result &= self.veteran is not None