From f5fa2692a0cd92884c50e782e4964be24225ac31 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 29 Apr 2017 16:59:02 +0200 Subject: [PATCH] Made some improvements --- database.py | 91 ++++++++++++++++++++++------------------------------- royalbot.py | 7 ++--- 2 files changed, 40 insertions(+), 58 deletions(-) diff --git a/database.py b/database.py index a2b52ab0..23bd16c8 100644 --- a/database.py +++ b/database.py @@ -61,6 +61,42 @@ class LoL(Base): return f"" + async def update_data(self): + try: + soloq, flexq, ttq = await lol.get_rank_data("euw", self.id) + except lol.LoLAPIError as e: + # LoL returns 404 if the account is unranked + if e.status_code == 404: + self.soloq_tier = None + self.soloq_division = None + self.flexq_tier = None + self.flexq_division = None + self.ttq_tier = None + self.ttq_division = None + else: + # Update the user data + if soloq is not None: + self.soloq_tier = lol.tiers.index(soloq["tier"]) + self.soloq_division = lol.divisions.index(soloq["entries"][0]["division"]) + else: + self.soloq_tier = None + self.soloq_division = None + if flexq is not None: + self.flexq_tier = lol.tiers.index(flexq["tier"]) + self.flexq_division = lol.divisions.index(flexq["entries"][0]["division"]) + else: + self.flexq_tier = None + self.flexq_division = None + if ttq is not None: + self.ttq_tier = lol.tiers.index(ttq["tier"]) + self.ttq_division = lol.divisions.index(ttq["entries"][0]["division"]) + else: + self.ttq_tier = None + self.ttq_division = None + # Mark the user as updated + self.last_updated = datetime.datetime.now() + + def generate_discord_embed(self): embed = discord.Embed(type="rich") # TODO: change the icon @@ -77,6 +113,7 @@ class LoL(Base): embed.colour = discord.Colour(0x09AEBB) return embed + Base.metadata.create_all(engine) @@ -104,57 +141,3 @@ def new_diario_entry(dt, text): session.add(entry) # Commit the change session.commit() - - -# TODO: this can be moved to a method of the LoL class -async def update_lol(discord_id): - # Create a new database session - session = Session() - # Find the user - user = session.query(Account).filter_by(id=discord_id).join(LoL).first() - # TODO: ewww - for account in user.lol: - # Find the League of Legends ID - lid = account.id - # Poll the League API for more information - data = await lol.get_summoner_data("euw", summoner_id=lid) - # Change tracker: if anything meaningful changes, set this to True - changes = False - # Update the user data - account.summoner_name = data["name"] - account.level = data["summonerLevel"] - # Poll the League API for ranked data - try: - soloq, flexq, ttq = await lol.get_rank_data("euw", lid) - except lol.LoLAPIError as e: - if e.status_code == 404: - account.soloq_tier = None - account.soloq_division = None - account.flexq_tier = None - account.flexq_division = None - account.ttq_tier = None - account.ttq_division = None - else: - # Update the user data - if soloq is not None: - account.soloq_tier = lol.tiers.index(soloq["tier"]) - account.soloq_division = lol.divisions.index(soloq["entries"][0]["division"]) - else: - account.soloq_tier = None - account.soloq_division = None - if flexq is not None: - account.flexq_tier = lol.tiers.index(flexq["tier"]) - account.flexq_division = lol.divisions.index(flexq["entries"][0]["division"]) - else: - account.flexq_tier = None - account.flexq_division = None - if ttq is not None: - account.ttq_tier = lol.tiers.index(ttq["tier"]) - account.ttq_division = lol.divisions.index(ttq["entries"][0]["division"]) - else: - account.ttq_tier = None - account.ttq_division = None - # Mark the user as updated - account.last_updated = datetime.datetime.now() - # Commit the changes - session.commit() \ No newline at end of file diff --git a/royalbot.py b/royalbot.py index 635538db..594c2652 100644 --- a/royalbot.py +++ b/royalbot.py @@ -285,9 +285,9 @@ Sintassi: `{symbol}synclol `""" # Commit the changes to the database session.commit() # Update the newly added user - await database.update_lol(thing.author.id) + lolaccount.update_data() # Send some info to Discord - await d.client.send_message(thing.channel, "Successfully connected: ", embed=lolaccount.generate_discord_embed()) + await d.client.send_message(thing.channel, "Connessione riuscita!", embed=lolaccount.generate_discord_embed()) async def job_updatelol(singletimeout=1, alltimeout=300): @@ -299,12 +299,11 @@ async def job_updatelol(singletimeout=1, alltimeout=300): users = session.query(database.LoL).all() # Update all the users' stats for user in users: - await database.update_lol(user.parent_id) + await user.update_data() await asyncio.sleep(singletimeout) await asyncio.sleep(alltimeout) - if __name__ == "__main__": # Init universal bot commands b.commands["diario"] = diario