1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Add leagueoflegends command (without autoupdate)

This commit is contained in:
Steffo 2019-10-28 16:38:32 +01:00
parent a01f6868dd
commit eb50454d07
6 changed files with 89 additions and 12 deletions

View file

@ -21,6 +21,9 @@ def run():
sentry = click.prompt("Sentry DSN", default="")
if sentry:
keyring.set_password(f"Royalnet/{secrets_name}", "sentry", sentry)
leagueoflegends = click.prompt("League of Legends API Token", default="")
if leagueoflegends:
keyring.set_password(f"Royalnet/{secrets_name}", "leagueoflegends", leagueoflegends)
if __name__ == "__main__":

View file

@ -20,6 +20,7 @@ from .youtube import YoutubeCommand
from .soundcloud import SoundcloudCommand
from .zawarudo import ZawarudoCommand
from .emojify import EmojifyCommand
from .leagueoflegends import LeagueoflegendsCommand
# Enter the commands of your Pack here!
available_commands = [
@ -44,6 +45,7 @@ available_commands = [
SoundcloudCommand,
ZawarudoCommand,
EmojifyCommand,
LeagueoflegendsCommand,
]
# Don't change this, it should automatically generate __all__

View file

@ -1,11 +1,15 @@
import typing
import riotwatcher
import logging
from royalnet.commands import *
from royalnet.utils import *
from ..tables import LeagueOfLegends
from ..utils import LeagueLeague
log = logging.getLogger(__name__)
class LeagueoflegendsCommand(Command):
name: str = "leagueoflegends"
@ -19,7 +23,56 @@ class LeagueoflegendsCommand(Command):
def __init__(self, interface: CommandInterface):
super().__init__(interface)
self._riotwatcher = riotwatcher.RiotWatcher(self.interface.bot.get_secret("riotgames"))
self._riotwatcher = riotwatcher.RiotWatcher(api_key=self.interface.bot.get_secret("leagueoflegends"))
...
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=lol.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"]
log.debug(f"Getting leagues data: {lol}")
leagues = self._riotwatcher.league.by_summoner(region=lol.region, encrypted_summoner_id=lol.summoner_id)
soloq = None
flexq = None
twtrq = None
tftq = None
for league in leagues:
if league["queueType"] == "RANKED_SOLO_5x5":
soloq = LeagueLeague.from_dict(league)
if league["queueType"] == "RANKED_FLEX_SR":
flexq = LeagueLeague.from_dict(league)
if league["queueType"] == "RANKED_FLEX_TT":
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
log.debug(f"Getting mastery data: {lol}")
mastery = self._riotwatcher.champion_mastery.scores_by_summoner(region=lol.region, encrypted_summoner_id=lol.summoner_id)
lol.mastery_score = mastery
def _display(self, lol: LeagueOfLegends):
string = f" [b]{lol.summoner_name}[/b]\n" \
f"Lv. {lol.summoner_level}\n" \
f"Mastery score: {lol.mastery_score}\n" \
f"\n"
if lol.rank_soloq:
string += f"Solo: {lol.rank_soloq}\n"
if lol.rank_flexq:
string += f"Flex: {lol.rank_flexq}\n"
if lol.rank_twtrq:
string += f"3v3: {lol.rank_twtrq}\n"
if lol.rank_tftq:
string += f"TFT: {lol.rank_tftq}\n"
return string
async def run(self, args: CommandArgs, data: CommandData) -> None:
author = await data.get_author(error_if_none=True)
@ -38,19 +91,25 @@ class LeagueoflegendsCommand(Command):
for account in author.leagueoflegends:
if account.summoner_name == name:
leagueoflegends = account
break
else:
raise CommandError("Nessun account con il nome specificato trovato.")
else:
leagueoflegends = author.leagueoflegends[0]
await self._update(leagueoflegends)
await data.session_commit()
await data.reply(self._display(leagueoflegends))
else:
region = args[0]
# Connect a new League of Legends account to Royalnet
log.debug(f"Searching for: {name}")
summoner = self._riotwatcher.summoner.by_name(region=region, summoner_name=name)
# Ensure the account isn't already connected to something else
leagueoflegends = await asyncify(data.session.query(self.alchemy.LeagueOfLegends).filter_by(summoner_id=summoner["id"]).one_or_none())
leagueoflegends = await asyncify(data.session.query(self.alchemy.LeagueOfLegends).filter_by(summoner_id=summoner["id"]).one_or_none)
if leagueoflegends:
raise CommandError(f"L'account {leagueoflegends} è già registrato su Royalnet.")
# Get rank information
log.debug(f"Getting leagues data: {name}")
leagues = self._riotwatcher.league.by_summoner(region=region, encrypted_summoner_id=summoner["id"])
soloq = None
flexq = None
@ -58,15 +117,16 @@ class LeagueoflegendsCommand(Command):
tftq = None
for league in leagues:
if league["queueType"] == "RANKED_SOLO_5x5":
soloq = league
soloq = LeagueLeague.from_dict(league)
if league["queueType"] == "RANKED_FLEX_SR":
flexq = league
flexq = LeagueLeague.from_dict(league)
if league["queueType"] == "RANKED_FLEX_TT":
twtrq = league
twtrq = LeagueLeague.from_dict(league)
if league["queueType"] == "RANKED_TFT":
tftq = league
tftq = LeagueLeague.from_dict(league)
# Get mastery score
mastery = self._riotwatcher.champion_mastery.by_summoner(region=region, encrypted_summoner_id=summoner["id"])
log.debug(f"Getting mastery data: {name}")
mastery = self._riotwatcher.champion_mastery.scores_by_summoner(region=region, encrypted_summoner_id=summoner["id"])
# Create database row
leagueoflegends = self.alchemy.LeagueOfLegends(
region=region,
@ -83,6 +143,7 @@ class LeagueoflegendsCommand(Command):
rank_tftq=tftq,
mastery_score=mastery
)
log.debug(f"Saving to the DB: {name}")
data.session.add(leagueoflegends)
await data.session_commit()
await data.reply(f"↔️ Account {leagueoflegends} connesso a {author}!")

View file

@ -1,8 +1,6 @@
from sqlalchemy import *
from sqlalchemy.orm import relationship, composite
from sqlalchemy.ext.declarative import declared_attr
# noinspection PyUnresolvedReferences
from .users import User
from ..utils import LeagueRank, LeagueTier, LeagueLeague
@ -44,7 +42,7 @@ class LeagueOfLegends:
@declared_attr
def summoner_id(self):
# aEsHyfXA2q8bK-g7GlT4kFK_0uLL3w-jBPyfMAy8kOXTJXo
return Column(String, nullable=False)
return Column(String, nullable=False, primary_key=True)
@declared_attr
def account_id(self):

View file

@ -24,7 +24,7 @@ class LeagueLeague:
self.veteran: bool = veteran
def __str__(self):
return f"{self.tier} {self.rank} ({self.points} LP)"
return f"[b]{self.tier} {self.rank}[/b] ({self.points} LP)"
def __repr__(self):
return f"<{self.__class__.__qualname__} {self}>"
@ -57,6 +57,19 @@ class LeagueLeague:
def __ne__(self, other):
return not self.__eq__(other)
def __bool__(self):
result = True
result &= self.veteran is not None
result &= self.fresh_blood is not None
result &= self.hot_streak is not None
result &= self.inactive is not None
result &= self.losses is not None
result &= self.wins is not None
result &= self.points is not None
result &= self.rank is not None
result &= self.tier is not None
return result
def __composite_values__(self):
return self.tier, \
self.rank, \

View file

@ -13,7 +13,7 @@ class LeagueTier(enum.Enum):
CHALLENGER = 8
def __str__(self):
return self.name
return self.name.capitalize()
def __repr__(self):
return f"{self.__class__.__qualname__}.{self.name}"