1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Added league level check

This commit is contained in:
Steffo 2016-08-13 20:03:47 +02:00
parent 03cc06d79c
commit d67ca94c77
3 changed files with 82 additions and 16 deletions

View file

@ -27,7 +27,7 @@ roman = ['I', 'II', 'III', 'IV', 'V']
# Get player rank info # Get player rank info
async def get_player_rank(region: str, summonerid: int, **kwargs): async def get_player_rank(region: str, summonerid: int, **kwargs):
print("[League] Getting league info for: {region} {summonerid}".format(region=region, summonerid=summonerid)) print("[League] Getting league info for: {region} {summonerid}".format(region=region, summonerid=summonerid))
# GET the json unofficial API response # GET the json API response
r = await loop.run_in_executor(None, requests.get, r = await loop.run_in_executor(None, requests.get,
'https://{region}.api.pvp.net/api/lol/{region}/v2.5/league/by-summoner/{summonerid}' 'https://{region}.api.pvp.net/api/lol/{region}/v2.5/league/by-summoner/{summonerid}'
'/entry?api_key={token}'.format(region=region, summonerid=summonerid, token=token)) '/entry?api_key={token}'.format(region=region, summonerid=summonerid, token=token))
@ -40,3 +40,21 @@ async def get_player_rank(region: str, summonerid: int, **kwargs):
raise RateLimitException("You've been ratelimited by Riot. Check your developer dashboard.") raise RateLimitException("You've been ratelimited by Riot. Check your developer dashboard.")
else: else:
raise Exception("Unhandled API response.") raise Exception("Unhandled API response.")
# Get player info data
async def get_player_info(region: str, summonerid: int, **kwargs):
print("[League] Getting profile info for: {region} {summonerid}".format(region=region, summonerid=summonerid))
# GET the json API response
r = await loop.run_in_executor(None, requests.get, 'https://{region}.api.pvp.net//api/lol/{region}/v1.4/summoner/'
'{summonerid}/entry?api_key={token}'
.format(region=region, summonerid=summonerid, token=token))
# Ensure the request is successful
if r.status_code == 200:
return r.json()[str(summonerid)]
elif r.status_code == 404:
raise NoRankedGamesCompletedException("This player hasn't completed any ranked games.")
elif r.status_code == 429:
raise RateLimitException("You've been ratelimited by Riot. Check your developer dashboard.")
else:
raise Exception("Unhandled API response.")

53
main.py
View file

@ -35,8 +35,13 @@ async def overwatch_level_up(timeout):
# Update data for every player in list # Update data for every player in list
for player in db: for player in db:
if "overwatch" in db[player]: if "overwatch" in db[player]:
try:
r = await overwatch.get_player_data(**db[player]["overwatch"]) r = await overwatch.get_player_data(**db[player]["overwatch"])
if r["data"]["level"] > db[player]["overwatch"]["level"]: except overwatch.NotFoundException:
print("[Overwatch] Player not found.")
else:
if "level" not in db[player]["overwatch"] \
or r["data"]["level"] > db[player]["overwatch"]["level"]:
# Send the message # Send the message
loop.create_task(send_event(eventmsg=s.overwatch_level_up, loop.create_task(send_event(eventmsg=s.overwatch_level_up,
player=player, player=player,
@ -46,6 +51,8 @@ async def overwatch_level_up(timeout):
f = open("db.json", "w") f = open("db.json", "w")
json.dump(db, f) json.dump(db, f)
f.close() f.close()
finally:
asyncio.sleep(1)
print("[Overwatch] Check completed successfully.") print("[Overwatch] Check completed successfully.")
# Wait for the timeout # Wait for the timeout
await asyncio.sleep(timeout) await asyncio.sleep(timeout)
@ -56,7 +63,7 @@ async def overwatch_level_up(timeout):
async def league_rank_change(timeout): async def league_rank_change(timeout):
while True: while True:
if discord_is_ready: if discord_is_ready:
print("[League] Starting check...") print("[League] Starting check for rank changes...")
# Update data for every player in list # Update data for every player in list
for player in db: for player in db:
if "league" in db[player]: if "league" in db[player]:
@ -73,7 +80,8 @@ async def league_rank_change(timeout):
tier_number = league.ranklist.index(r["tier"]) tier_number = league.ranklist.index(r["tier"])
roman_number = league.roman.index(r["entries"][0]["division"]) roman_number = league.roman.index(r["entries"][0]["division"])
# Check for tier changes # Check for tier changes
if tier_number != db[player]["league"]["tier"] or roman_number != db[player]["league"]["division"]: if tier_number != db[player]["league"]["tier"] \
or roman_number != db[player]["league"]["division"]:
# Send the message # Send the message
loop.create_task(send_event(eventmsg=s.league_rank_up, loop.create_task(send_event(eventmsg=s.league_rank_up,
player=player, player=player,
@ -87,8 +95,42 @@ async def league_rank_change(timeout):
f.close() f.close()
finally: finally:
# Prevent getting ratelimited by Riot # Prevent getting ratelimited by Riot
await asyncio.sleep(2)
print("[League] Rank check completed.")
# Wait for the timeout
await asyncio.sleep(timeout)
else:
await asyncio.sleep(1) await asyncio.sleep(1)
print("[League] Check completed.")
# Every timeout seconds, update player level and check for changes
async def league_level_up(timeout):
while True:
if discord_is_ready:
print("[League] Starting check for level changes...")
# Update data for every player in list
for player in db:
if "league" in db[player]:
try:
r = await league.get_player_info(**db[player]["league"])
except league.RateLimitException:
# If you've been ratelimited, skip the player and notify the console.
print("[League] Request rejected for rate limit.")
else:
# Check for level changes
if "level" not in db[player]["league"] or r["level"] > db[player]["league"]["level"]:
# Send the message
loop.create_task(send_event(eventmsg=s.league_level_up,
player=player,
level=r["level"]))
# Update database
db[player]["league"]["level"] = r["level"]
f = open("db.json", "w")
json.dump(db, f)
f.close()
finally:
# Prevent getting ratelimited by Riot
await asyncio.sleep(2)
print("[League] Level check completed.")
# Wait for the timeout # Wait for the timeout
await asyncio.sleep(timeout) await asyncio.sleep(timeout)
else: else:
@ -123,6 +165,9 @@ print("[Overwatch] Added level up check to the queue.")
loop.create_task(league_rank_change(900)) loop.create_task(league_rank_change(900))
print("[League] Added rank change check to the queue.") print("[League] Added rank change check to the queue.")
loop.create_task(league_level_up(900))
print("[League] Added level change check to the queue.")
try: try:
loop.run_until_complete(d_client.start(token)) loop.run_until_complete(d_client.start(token))
except KeyboardInterrupt: except KeyboardInterrupt:

View file

@ -14,3 +14,6 @@ league_tier_list = ["Bronzo",
"Diamante", "Diamante",
"Master", "Master",
"Challenger"] "Challenger"]
# League of Legends: Level up!
league_level_up = "{player} è salito al livello **{level}** su _League of Legends_!"