From f777f008c56dbcf47547da1d741df64654c7b5b0 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 21 Jul 2020 23:12:14 +0200 Subject: [PATCH] Cambia la signature di linker.create --- royalpack/commands/abstract/linker.py | 12 +++++++++--- royalpack/commands/brawlhalla.py | 24 ++++++++++++++---------- royalpack/commands/dota.py | 2 +- royalpack/commands/leagueoflegends.py | 10 +++++++++- royalpack/commands/steampowered.py | 15 +++++++++++++-- 5 files changed, 46 insertions(+), 17 deletions(-) diff --git a/royalpack/commands/abstract/linker.py b/royalpack/commands/abstract/linker.py index bdebb354..ce3537ae 100644 --- a/royalpack/commands/abstract/linker.py +++ b/royalpack/commands/abstract/linker.py @@ -41,7 +41,7 @@ class LinkerCommand(rc.Command, metaclass=abc.ABCMeta): await data.reply("\n".join(message)) else: message = ["🔗 Account collegato!\n"] - created = await self.create(session=data.session, user=author, args=args) + created = await self.create(session=data.session, user=author, args=args, data=data) message.append(self.describe(created)) await data.session_commit() await data.reply("\n".join(message)) @@ -61,8 +61,14 @@ class LinkerCommand(rc.Command, metaclass=abc.ABCMeta): ... @abc.abstractmethod - async def create(self, session, user: rbt.User, args) -> Updatable: - """Create a new updatable object for a user.""" + async def create(self, + session, + user: rbt.User, + args: rc.CommandArgs, + data: Optional[rc.CommandData] = None) -> Updatable: + """Create a new updatable object for a user. + + This function is responsible for adding the object to the session.""" ... @abc.abstractmethod diff --git a/royalpack/commands/brawlhalla.py b/royalpack/commands/brawlhalla.py index e2a7e142..f7dfcb95 100644 --- a/royalpack/commands/brawlhalla.py +++ b/royalpack/commands/brawlhalla.py @@ -5,8 +5,8 @@ import logging import aiohttp from royalnet.backpack import tables as rbt -from royalnet.commands import * -from royalnet.utils import * +import royalnet.commands as rc +import royalnet.utils as ru from sqlalchemy import or_, and_ from .abstract.linker import LinkerCommand @@ -32,10 +32,14 @@ class BrawlhallaCommand(LinkerCommand): return user.steam async def get_updatables(self, session) -> List[Brawlhalla]: - return await asyncify(session.query(self.alchemy.get(Steam)).all) + return await ru.asyncify(session.query(self.alchemy.get(Steam)).all) - async def create(self, session, user: rbt.User, args) -> Updatable: - raise InvalidInputError("Brawlhalla accounts are automatically linked from Steam.") + async def create(self, + session, + user: rbt.User, + args: rc.CommandArgs, + data: Optional[rc.CommandData] = None) -> Brawlhalla: + raise rc.InvalidInputError("Brawlhalla accounts are automatically linked from Steam.") async def update(self, session, obj, change: Callable[[str, Any], Awaitable[None]]): BrawlhallaT = self.alchemy.get(Brawlhalla) @@ -47,7 +51,7 @@ class BrawlhallaCommand(LinkerCommand): log.debug(f"Checking if player has an account...") async with hcs.get(f"https://api.brawlhalla.com/search?steamid={obj.steamid.as_64}&api_key={self.token()}") as response: if response.status != 200: - raise ExternalError(f"Brawlhalla API /search returned {response.status}!") + raise rc.ExternalError(f"Brawlhalla API /search returned {response.status}!") j = await response.json() if j == {} or j == []: log.debug("No account found.") @@ -62,7 +66,7 @@ class BrawlhallaCommand(LinkerCommand): async with hcs.get(f"https://api.brawlhalla.com/player/{bh.brawlhalla_id}/ranked?api_key={self.token()}") as response: if response.status != 200: - raise ExternalError(f"Brawlhalla API /ranked returned {response.status}!") + raise rc.ExternalError(f"Brawlhalla API /ranked returned {response.status}!") j = await response.json() if j == {} or j == []: log.debug("No ranked info found.") @@ -75,7 +79,7 @@ class BrawlhallaCommand(LinkerCommand): await self._change(session=session, obj=bh, attribute="rank_1v1", new=rank) for jduo in j.get("2v2", []): - bhduo: Optional[BrawlhallaDuo] = await asyncify( + bhduo: Optional[BrawlhallaDuo] = await ru.asyncify( session.query(DuoT) .filter( or_( @@ -93,11 +97,11 @@ class BrawlhallaCommand(LinkerCommand): ) if bhduo is None: if bh.brawlhalla_id == jduo["brawlhalla_id_one"]: - otherbh: Optional[Brawlhalla] = await asyncify( + otherbh: Optional[Brawlhalla] = await ru.asyncify( session.query(BrawlhallaT).get, jduo["brawlhalla_id_two"] ) else: - otherbh: Optional[Brawlhalla] = await asyncify( + otherbh: Optional[Brawlhalla] = await ru.asyncify( session.query(BrawlhallaT).get, jduo["brawlhalla_id_one"] ) if otherbh is None: diff --git a/royalpack/commands/dota.py b/royalpack/commands/dota.py index 14b7850f..e394ecbb 100644 --- a/royalpack/commands/dota.py +++ b/royalpack/commands/dota.py @@ -37,7 +37,7 @@ class DotaCommand(LinkerCommand): async def get_updatables(self, session) -> List[Dota]: return await ru.asyncify(session.query(self.alchemy.get(Steam)).all) - async def create(self, session, user: rbt.User, args): + async def create(self, session, user: rbt.User, args: rc.CommandArgs, data: Optional[rc.CommandData] = None): raise rc.InvalidInputError("Dota accounts are automatically linked from Steam.") async def update(self, session, obj: Steam, change: Callable[[str, Any], Awaitable[None]]): diff --git a/royalpack/commands/leagueoflegends.py b/royalpack/commands/leagueoflegends.py index cfb9aa7c..40844eff 100644 --- a/royalpack/commands/leagueoflegends.py +++ b/royalpack/commands/leagueoflegends.py @@ -60,7 +60,7 @@ class LeagueoflegendsCommand(LinkerCommand): async def get_updatables(self, session) -> List[LeagueOfLegends]: return await ru.asyncify(session.query(self.alchemy.get(LeagueOfLegends)).all) - async def create(self, session, user: rbt.User, args) -> LeagueOfLegends: + async def create(self, session, user: rbt.User, args: rc.CommandArgs, data: Optional[rc.CommandData] = None) -> LeagueOfLegends: name = args.joined() # Connect a new League of Legends account to Royalnet @@ -108,6 +108,14 @@ class LeagueoflegendsCommand(LinkerCommand): rank_tftq=tftq, mastery_score=mastery ) + + await FiorygiTransaction.spawn_fiorygi( + data=data, + user=user, + qty=1, + reason="aver collegato a Royalnet il proprio account di League of Legends" + ) + session.add(leagueoflegends) return leagueoflegends diff --git a/royalpack/commands/steampowered.py b/royalpack/commands/steampowered.py index 049aea32..eb44b7c0 100644 --- a/royalpack/commands/steampowered.py +++ b/royalpack/commands/steampowered.py @@ -35,7 +35,11 @@ class SteampoweredCommand(LinkerCommand): async def get_updatables(self, session) -> List[Steam]: return await ru.asyncify(session.query(self.alchemy.get(Steam)).all) - async def create(self, session, user: rbt.User, args) -> Steam: + async def create(self, + session, + user: rbt.User, + args: rc.CommandArgs, + data: Optional[rc.CommandData] = None) -> Steam: url = args.joined() steamid64 = await self._call(steam.steamid.steam64_from_url, url) if steamid64 is None: @@ -51,8 +55,15 @@ class SteampoweredCommand(LinkerCommand): primary_clan_id=r["primaryclanid"], account_creation_date=datetime.datetime.fromtimestamp(r["timecreated"]) ) + + await FiorygiTransaction.spawn_fiorygi( + data=data, + user=user, + qty=1, + reason="aver collegato a Royalnet il proprio account di League of Legends" + ) + session.add(steam_account) - await ru.asyncify(session.commit) return steam_account async def update(self, session, obj: Steam, change: Callable[[str, Any], Awaitable[None]]):