From cf584f29d2af13402f8c64fe89996460df13db41 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 2 Nov 2016 14:45:41 +0100 Subject: [PATCH] should work --- main.py | 23 +++++++++++++++++++++-- overwatch.py | 18 +++++++++++++++++- strings.py | 14 +++++++++++++- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 8fff8774..223ea886 100644 --- a/main.py +++ b/main.py @@ -30,7 +30,7 @@ token = file.read() file.close() # Every timeout seconds, update player status and check for levelups -async def overwatch_level_up(timeout): +async def overwatch_status_change(timeout): while True: if discord_is_ready: print("[Overwatch] Starting check...") @@ -45,6 +45,7 @@ async def overwatch_level_up(timeout): # If some other error occours, skip the player print("[Overwatch] Request returned an unhandled exception.") else: + # Check for levelups if "level" not in db[player]["overwatch"] \ or r["data"]["level"] > db[player]["overwatch"]["level"]: # Send the message @@ -56,6 +57,24 @@ async def overwatch_level_up(timeout): f = open("db.json", "w") json.dump(db, f) f.close() + # Check for rank changes + if r["data"]["competitive"]["rank"] is not None: + if "rank" not in db[player]["overwatch"] \ + or int(r["data"]["competitive"]["rank"]) != db[player]["overwatch"]["rank"]: + # Send the message + loop.create_task(send_event(eventmsg=s.overwatch_rank_change, + player=player, + oldmedal=overwatch.rank_to_medal( + db[player]["overwatch"]["rank"]), + oldrank=db[player]["overwatch"]["rank"], + rank=int(r["data"]["competitive"]["rank"]), + medal=overwatch.rank_to_medal( + int(r["data"]["competitive"]["rank"])))) + # Update database + db[player]["overwatch"]["rank"] = r["data"]["competitive"]["rank"] + f = open("db.json", "w") + json.dump(db, f) + f.close() finally: asyncio.sleep(1) print("[Overwatch] Check completed successfully.") @@ -226,7 +245,7 @@ async def send_event(eventmsg: str, player: str, **kwargs): loop.create_task(telegram.send_message(msg, -2141322)) -loop.create_task(overwatch_level_up(600)) +loop.create_task(overwatch_status_change(600)) print("[Overwatch] Added level up check to the queue.") loop.create_task(league_rank_change(900)) diff --git a/overwatch.py b/overwatch.py index c960541e..41133f0b 100644 --- a/overwatch.py +++ b/overwatch.py @@ -1,5 +1,6 @@ import asyncio import requests +import strings as s loop = asyncio.get_event_loop() @@ -30,4 +31,19 @@ async def get_player_data(platform: str, region: str, battletag: str, **kwargs): return pj else: raise Exception("Unhandled API response.") - \ No newline at end of file + + +# Convert rank to a medal +def rank_to_medal(rank): + if int(rank) < 1500: + return s.overwatch_medal_list[0] + elif int(rank) < 2000: + return s.overwatch_medal_list[1] + elif int(rank) < 2500: + return s.overwatch_medal_list[2] + elif int(rank) < 3000: + return s.overwatch_medal_list[3] + elif int(rank) < 3500: + return s.overwatch_medal_list[4] + elif int(rank) < 4000: + return s.overwatch_medal_list[5] diff --git a/strings.py b/strings.py index a75356c9..15375cba 100644 --- a/strings.py +++ b/strings.py @@ -3,6 +3,18 @@ # {level} = new level overwatch_level_up = "{player} è salito al livello **{level}** su _Overwatch_!" +# Overwatch: Rank change! +overwatch_rank_change = "{player} è passato da {oldmedal} {oldrank} a **{medal} {division}** su _Overwatch_!" + +# Overwatch: Medal list +overwatch_medal_list = ["Bronzo", + "Argento", + "Oro", + "Platino", + "Diamante", + "Master", + "Grandmaster"] + # League of Legends: Rank up! league_rank_up = "{player} è passato da {oldtier} {olddivision} a **{tier} {division}** su _League of Legends_!" @@ -26,4 +38,4 @@ league_roman_list = ["I", league_level_up = "{player} è salito al livello **{level}** su _League of Legends_!" # Brawlhalla: new MMR -brawlhalla_new_mmr = "{player} è passato da {oldmmr} MMR a **{mmr}** MMR su _Brawlhalla_!" +brawlhalla_new_mmr = "{player} è passato da {oldmmr} MMR a **{mmr}** MMR su _Brawlhalla_!" \ No newline at end of file