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

Improved timeouts

This commit is contained in:
Steffo 2016-08-13 15:36:08 +02:00
parent 3de55d688b
commit 2b2a7051d4

116
main.py
View file

@ -8,6 +8,14 @@ import telegram
loop = asyncio.get_event_loop()
d_client = discord.Client()
discord_is_ready = False
# When Discord is ready, set discord_is_ready to True
@d_client.event
async def on_ready():
global discord_is_ready
discord_is_ready = True
# Get player database from the db.json file
file = open("db.json")
@ -22,70 +30,72 @@ file.close()
# Every timeout seconds, update player status and check for levelups
async def overwatch_level_up(timeout):
while True:
# Wait for the Discord client to be ready
await d_client.wait_until_ready()
print("Checking for Overwatch updates.")
# Update data for every player in list
for player in db:
if "overwatch" in db[player]:
r = await overwatch.get_player_data(**db[player]["overwatch"])
if r["data"]["level"] > db[player]["overwatch"]["level"]:
# Convert user ID into a mention
user = "<@" + player + ">"
# Prepare the message to send
msg = s.overwatch_level_up.format(player=user, level=r["data"]["level"])
# Send the message to the discord channel
loop.create_task(d_client.send_message(d_client.get_channel("213655027842154508"), msg))
# Send the message to the telegram group chat
loop.create_task(telegram.send_message(msg, -2141322))
# Update database
db[player]["overwatch"]["level"] = r["data"]["level"]
f = open("db.json", "w")
json.dump(db, f)
f.close()
print("Check for Overwatch completed.")
# Wait for the timeout
await asyncio.sleep(timeout)
# Every timeout seconds, update player league and check for rank changes
async def league_rank_change(timeout):
while True:
# Wait for the Discord client to be ready
await d_client.wait_until_ready()
print("Checking for League of Legends updates.")
# Update data for every player in list
for player in db:
if "league" in db[player]:
try:
r = await league.get_player_rank(**db[player]["league"])
except league.NoRankedGamesCompletedException:
# If the player has no ranked games completed, skip him
continue
else:
# Convert tier into a number
tier_number = league.ranklist.index(r["tier"])
roman_number = league.roman.index(r["entries"][0]["division"])
# Check for tier changes
if tier_number != db[player]["league"]["tier"] or roman_number != db[player]["league"]["division"]:
if discord_is_ready:
print("Checking for Overwatch updates.")
# Update data for every player in list
for player in db:
if "overwatch" in db[player]:
r = await overwatch.get_player_data(**db[player]["overwatch"])
if r["data"]["level"] > db[player]["overwatch"]["level"]:
# Convert user ID into a mention
user = "<@" + player + ">"
# Prepare the message to send
msg = s.league_rank_up.format(player=user,
tier=s.league_tier_list[tier_number],
division=r["entries"][0]["division"])
msg = s.overwatch_level_up.format(player=user, level=r["data"]["level"])
# Send the message to the discord channel
loop.create_task(d_client.send_message(d_client.get_channel("213655027842154508"), msg))
# Send the message to the telegram group chat
loop.create_task(telegram.send_message(msg, -2141322))
# Update database
db[player]["league"]["tier"] = tier_number
db[player]["league"]["division"] = roman_number
db[player]["overwatch"]["level"] = r["data"]["level"]
f = open("db.json", "w")
json.dump(db, f)
f.close()
print("Check for League of Legends completed.")
# Wait for the timeout
await asyncio.sleep(timeout)
print("Check for Overwatch completed.")
# Wait for the timeout
await asyncio.sleep(timeout)
else:
await asyncio.sleep(1)
# Every timeout seconds, update player league and check for rank changes
async def league_rank_change(timeout):
while True:
if discord_is_ready:
print("Checking for League of Legends updates.")
# Update data for every player in list
for player in db:
if "league" in db[player]:
try:
r = await league.get_player_rank(**db[player]["league"])
except league.NoRankedGamesCompletedException:
# If the player has no ranked games completed, skip him
continue
else:
# Convert tier into a number
tier_number = league.ranklist.index(r["tier"])
roman_number = league.roman.index(r["entries"][0]["division"])
# Check for tier changes
if tier_number != db[player]["league"]["tier"] or roman_number != db[player]["league"]["division"]:
# Convert user ID into a mention
user = "<@" + player + ">"
# Prepare the message to send
msg = s.league_rank_up.format(player=user,
tier=s.league_tier_list[tier_number],
division=r["entries"][0]["division"])
# Send the message to the discord channel
loop.create_task(d_client.send_message(d_client.get_channel("213655027842154508"), msg))
# Send the message to the telegram group chat
loop.create_task(telegram.send_message(msg, -2141322))
# Update database
db[player]["league"]["tier"] = tier_number
db[player]["league"]["division"] = roman_number
f = open("db.json", "w")
json.dump(db, f)
f.close()
print("Check for League of Legends completed.")
# Wait for the timeout
await asyncio.sleep(timeout)
else:
await asyncio.sleep(1)
print("Added Overwatch to the queue.")
loop.create_task(overwatch_level_up(900))