1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Add save_db() function

This commit is contained in:
Steffo 2017-02-09 14:52:16 +01:00
parent e06dc9a5e8
commit 5dffe99f99

37
main.py
View file

@ -32,6 +32,14 @@ file = open("discordtoken.txt", "r")
token = file.read() token = file.read()
file.close() file.close()
# Save the db
def save_db():
f = open("db.json", "w")
json.dump(db, f)
f.close()
del f
# Every timeout seconds, update player status and check for levelups # Every timeout seconds, update player status and check for levelups
async def overwatch_status_change(timeout): async def overwatch_status_change(timeout):
while True: while True:
@ -59,9 +67,6 @@ async def overwatch_status_change(timeout):
loop.create_task(send_event(eventmsg=s.overwatch_level_up, player=player, level=level)) loop.create_task(send_event(eventmsg=s.overwatch_level_up, player=player, level=level))
# Update database # Update database
db[player]["overwatch"]["level"] = level db[player]["overwatch"]["level"] = level
f = open("db.json", "w")
json.dump(db, f)
f.close()
# Check for rank changes # Check for rank changes
rank = r["data"]["competitive"]["rank"] rank = r["data"]["competitive"]["rank"]
if rank is not None: if rank is not None:
@ -77,9 +82,7 @@ async def overwatch_status_change(timeout):
rank=rank, medal=overwatch.url_to_medal(r["data"]["competitive"]["rank_img"]))) rank=rank, medal=overwatch.url_to_medal(r["data"]["competitive"]["rank_img"])))
# Update database # Update database
db[player]["overwatch"]["rank"] = rank db[player]["overwatch"]["rank"] = rank
f = open("db.json", "w") save_db()
json.dump(db, f)
f.close()
finally: finally:
asyncio.sleep(1) asyncio.sleep(1)
print("[Overwatch] Check completed successfully.") print("[Overwatch] Check completed successfully.")
@ -126,9 +129,7 @@ async def league_rank_change(timeout):
# Update database # Update database
db[player]["league"]["tier"] = tier_number db[player]["league"]["tier"] = tier_number
db[player]["league"]["division"] = roman_number db[player]["league"]["division"] = roman_number
f = open("db.json", "w") save_db()
json.dump(db, f)
f.close()
finally: finally:
# Prevent getting ratelimited by Riot # Prevent getting ratelimited by Riot
await asyncio.sleep(2) await asyncio.sleep(2)
@ -166,9 +167,7 @@ async def league_level_up(timeout):
loop.create_task(send_event(eventmsg=s.league_level_up, player=player, level=level)) loop.create_task(send_event(eventmsg=s.league_level_up, player=player, level=level))
# Update database # Update database
db[player]["league"]["level"] = level db[player]["league"]["level"] = level
f = open("db.json", "w") save_db()
json.dump(db, f)
f.close()
finally: finally:
# Prevent getting ratelimited by Riot # Prevent getting ratelimited by Riot
await asyncio.sleep(2) await asyncio.sleep(2)
@ -223,9 +222,7 @@ async def brawlhalla_update_mmr(timeout):
loop.create_task(send_event(s.brawlhalla_new_mmr, player=player, mmr=mmr, oldmmr=old_mmr)) loop.create_task(send_event(s.brawlhalla_new_mmr, player=player, mmr=mmr, oldmmr=old_mmr))
# Update database # Update database
db[player]["brawlhalla"]["mmr"] = mmr db[player]["brawlhalla"]["mmr"] = mmr
f = open("db.json", "w") save_db()
json.dump(db, f)
f.close()
break break
finally: finally:
await asyncio.sleep(1) await asyncio.sleep(1)
@ -277,9 +274,7 @@ async def opendota_last_match(timeout):
db[player]["dota"] = { db[player]["dota"] = {
"lastmatch": last "lastmatch": last
} }
f = open("db.json", "w") save_db()
json.dump(db, f)
f.close()
finally: finally:
await asyncio.sleep(2) await asyncio.sleep(2)
print("[OpenDota] Check successful.") print("[OpenDota] Check successful.")
@ -319,9 +314,7 @@ async def osu_pp(timeout):
loop.create_task(send_event(s.osu_pp_change, **f)) loop.create_task(send_event(s.osu_pp_change, **f))
else: else:
db[player]["osu"][str(mode)] = 0.0 db[player]["osu"][str(mode)] = 0.0
f = open("db.json", "w") save_db()
json.dump(db, f)
f.close()
finally: finally:
await asyncio.sleep(5) await asyncio.sleep(5)
print("[Osu!] Check successful.") print("[Osu!] Check successful.")
@ -371,10 +364,10 @@ print("[League] Added level change check to the queue.")
loop.create_task(osu_pp(1800)) loop.create_task(osu_pp(1800))
print("[Osu!] Added pp change check to the queue.") print("[Osu!] Added pp change check to the queue.")
# Run until ^C
try: try:
loop.run_until_complete(d_client.start(token)) loop.run_until_complete(d_client.start(token))
except KeyboardInterrupt: except KeyboardInterrupt:
loop.run_until_complete(d_client.logout()) loop.run_until_complete(d_client.logout())
# cancel all tasks lingering
finally: finally:
loop.close() loop.close()