Skip to content

Instantly share code, notes, and snippets.

@fernandojunior
Forked from Scoppio/jinx.py
Last active November 29, 2016 04:10
Show Gist options
  • Save fernandojunior/03d1b30462ece6e3d1f78b55ee653181 to your computer and use it in GitHub Desktop.
Save fernandojunior/03d1b30462ece6e3d1f78b55ee653181 to your computer and use it in GitHub Desktop.
A simple gradient descent devised to capture the match ID of a game in a specific day, month and year.
'''
Script to find a match at a specific date time by using gradient descendent.
Adaptaded from Lucas Coppio:
https://gist.github.com/Scoppio/2d5cf12311239ca9807f643ef21b88d1
'''
import math
import time
import random
from datetime import datetime
from riotwatcher.riotwatcher import RiotWatcher, BRAZIL
import config
riot_client = RiotWatcher(key=config.API_KEY, default_region=BRAZIL)
def sign(x):
''' Get a sign of a given number x '''
return math.copysign(1, x)
def timestamp_to_datetime(t):
''' Convert a timestamp (seconds) to datetime format '''
return datetime.fromtimestamp(t)
def timestamp(*args, **kwargs):
''' Convert a datetime to timestamp (seconds) '''
return time.mktime(datetime(*args, **kwargs).timetuple())
def get_match(match_id):
return riot_client.get_match(match_id=match_id)
def find_match(year=datetime.now().year, month=1, day=1, hour=12, minute=0,
seconds=0, tolerance=43200, starting_match_id=774365872):
'''
Find a match at a specific date time with a given tolerance in seconds.
'''
converged = False
alpha = 0.000004 # learning rate
disered_timestamp = timestamp(year, month, day, hour, minute, seconds)
iter_count = 0
match_id = starting_match_id
match = None
while not converged:
try:
match = get_match(match_id=match_id)
print('OK,', match_id)
match_timestamp = match['matchCreation'] / 1e3
loss = disered_timestamp - match_timestamp
if abs(loss) < tolerance:
converged = True
else:
cost = 1/2 * (loss) ** 2
match_id = int(match_id + (alpha * cost * sign(loss)))
except Exception as e:
print('%s, %s' % (str(e), match_id))
if str(e) == 'Game data not found':
match_id += random.randrange(100)
if str(e) == 'Too many requests':
time.sleep(2) # connection time-out for get_match
finally:
iter_count += 1
match['matchCreation'] = timestamp_to_datetime(match_timestamp)
response = {k: match[k] for k in ['matchId', 'matchCreation', 'matchVersion']}
response['iterCount'] = iter_count
return response
if __name__ == '__main__':
responses = []
try:
match = riot_client.get_match(match_id=686810871)
for month in range(1, 12):
match = find_match(month=month, starting_match_id=match['matchId'])
responses.append(match)
except (KeyboardInterrupt, SystemExit):
print ('Closing process')
for response in responses:
print(response)
# {'matchVersion': '5.24.0.256', 'iterCount': 1, 'matchId': 686810871, 'matchCreation': datetime.datetime(2016, 1, 1, 11, 49, 31, 72000)}
# {'matchVersion': '6.2.0.238', 'iterCount': 254, 'matchId': 710229426, 'matchCreation': datetime.datetime(2016, 2, 1, 0, 23, 26, 328000)}
# {'matchVersion': '6.4.0.257', 'iterCount': 288, 'matchId': 728819142, 'matchCreation': datetime.datetime(2016, 3, 1, 13, 21, 28, 187000)}
# {'matchVersion': '6.6.138.2102', 'iterCount': 194, 'matchId': 752209814, 'matchCreation': datetime.datetime(2016, 4, 1, 12, 59, 1, 950000)}
# {'matchVersion': '6.8.141.4819', 'iterCount': 397, 'matchId': 776639147, 'matchCreation': datetime.datetime(2016, 5, 1, 0, 29, 2, 717000)}
# {'matchVersion': '6.10.144.9736', 'iterCount': 155, 'matchId': 799398550, 'matchCreation': datetime.datetime(2016, 6, 1, 14, 25, 9, 809999)}
# {'matchVersion': '6.13.148.7588', 'iterCount': 429, 'matchId': 821638963, 'matchCreation': datetime.datetime(2016, 7, 1, 0, 36, 54, 153000)}
# {'matchVersion': '6.15.152.1763', 'iterCount': 172, 'matchId': 847908906, 'matchCreation': datetime.datetime(2016, 8, 1, 7, 41, 53, 388999)}
# {'matchVersion': '6.17.155.1085', 'iterCount': 244, 'matchId': 870445645, 'matchCreation': datetime.datetime(2016, 9, 1, 0, 0, 19, 33999)}
# {'matchVersion': '6.19.159.7092', 'iterCount': 207, 'matchId': 894079351, 'matchCreation': datetime.datetime(2016, 10, 1, 8, 50, 59, 237999)}
# {'matchVersion': '6.21.163.5030', 'iterCount': 215, 'matchId': 920389599, 'matchCreation': datetime.datetime(2016, 11, 1, 12, 47, 16, 881000)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment