From 6e8544979943a0dc67011ce86573385dfdff7876 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 7 Nov 2016 22:29:37 +0100 Subject: [PATCH] =?UTF-8?q?wow=20finalmente=20il=20codice=20di=20overwatch?= =?UTF-8?q?=20=C3=A8=20quasi=20leggibile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 38 ++++++++++++++++++-------------------- strings.py | 2 +- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/main.py b/main.py index 1dd86ba3..e74d7ece 100644 --- a/main.py +++ b/main.py @@ -46,34 +46,32 @@ async def overwatch_status_change(timeout): 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"]: + level = r["data"]["level"] + try: + oldlevel = db[player]["overwatch"]["level"] + except KeyError: + oldlevel = 0 + if level > oldlevel: # Send the message - loop.create_task(send_event(eventmsg=s.overwatch_level_up, - player=player, - level=r["data"]["level"])) + loop.create_task(send_event(eventmsg=s.overwatch_level_up, player=player, level=level)) # Update database - db[player]["overwatch"]["level"] = r["data"]["level"] + db[player]["overwatch"]["level"] = level 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"]: - if "rank" not in db[player]["overwatch"]: - db[player]["overwatch"]["rank"] = 0 + rank = r["data"]["competitive"]["rank"] + if rank is not None: + rank = int(rank) + try: + oldrank = int(db[player]["overwatch"]["rank"]) + except KeyError: + oldrank = 0 + if rank != oldrank: # 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"])))) + loop.create_task(send_event(eventmsg=s.overwatch_rank_change, player=player, change=oldrank-rank, rank=rank, medal=overwatch.rank_to_medal(rank))) # Update database - db[player]["overwatch"]["rank"] = int(r["data"]["competitive"]["rank"]) + db[player]["overwatch"]["rank"] = rank f = open("db.json", "w") json.dump(db, f) f.close() diff --git a/strings.py b/strings.py index 55ac6517..39d7bd7b 100644 --- a/strings.py +++ b/strings.py @@ -4,7 +4,7 @@ overwatch_level_up = "{player} è salito al livello **{level}** su _Overwatch_!" # Overwatch: Rank change! -overwatch_rank_change = "{player} è passato da {oldmedal} {oldrank} a **{medal} {rank}** su _Overwatch_!" +overwatch_rank_change = "{player} è ora **{medal} {rank}** ({change}) su _Overwatch_!" # Overwatch: Medal list overwatch_medal_list = ["Nulla",