Created
January 15, 2018 19:14
-
-
Save eulerscheZahl/512fb2cf26522c5aebfeb4916b1f3998 to your computer and use it in GitHub Desktop.
riddles.io winning statistics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
""" | |
usage: stats.py username [revision=latest] | |
output: [wins, losses, draws] | |
""" | |
import sys | |
import json | |
import requests | |
headers = { | |
'accept':'application/vnd.riddles.v1+json', | |
'accept-encoding':'gzip, deflate, br', | |
'content-type':'application/json', | |
} | |
# game_key = "13dbf9d8-8821-4afa-8984-7b9c80fd0f8c" # golad | |
game_key = "fc25c934-02ab-4d1f-a9e7-0c53f51d57ed" # ms hackman | |
def load_ids(player): | |
data = {'query': 'query SchemaBotsQuery($schemaKey: String!) {\nschemaBots(schema_key: {eq: $schemaKey}) {\nedges {\nnode {\nscore\nbot {\nkey\nname\nstatus\nisTestBot\nactiveRevision {\nkey\nrevision {\nrevisionNumber\nexecutable {\nprogrammingLanguage {\nkey\nname\n}\n}\n}\n}\nuser {\nalias\nkey\nuserInfo {\ncountry {\nname\nicon\n}\n}\nemails {\nedges {\nnode {\nhash\n}\n}\n}\norganisations {\nedges {\nnode {\nname\nslug\n}\n}\n}\n}\nteam {\nkey\nname\n}\n}\n}\n}\n}\n}', 'variables': '{"schemaKey": "' + game_key + '"}'} | |
r = requests.post('https://api.riddles.io/graphql', data=json.dumps(data), headers=headers) | |
data = json.loads(r._content.decode()) | |
bots = data['data']['schemaBots']['edges'] | |
result = ('', '', '', '') | |
for bot in bots: | |
score = bot['node']['score'] | |
if bot['node']['bot']['activeRevision'] is None: continue | |
bot_name = bot['node']['bot']['name'] | |
try: #single user | |
user = bot['node']['bot']['user']['alias'] | |
user_id = bot['node']['bot']['user']['key'] | |
except: #team | |
user = bot['node']['bot']['team']['name'] | |
user_id = bot['node']['bot']['team']['key'] | |
bot_id = bot['node']['bot']['activeRevision']['key'] | |
bot_key = bot['node']['bot']['key'] | |
if player in user: result = (user, bot_name, bot_id, bot_key) | |
#print("'%s': ['%s', '%s'], # score: %s" % (user, user_id, bot_id, score)) | |
return result | |
def load_stats(player, revision): | |
player, bot_name, bot_id, bot_key = load_ids(player) | |
if revision != 0: | |
data = { 'query': 'query QueryBotDetails($key: String!) {\nbot(key: $key) {\nkey\nstatus\nactiveRevision {\nkey\n}\nbotRevisions(orderBy: ["-createdAt"]) {\nedges {\nnode {\ncreatedAt\nkey\nrevision {\nrevisionNumber\nexecutable {\nstatus\nsource\nprogrammingLanguage {\nname\n}\n}\n}\ntestMatch {\nkey\n}\n}\n}\n}\n}\n}','variables': '{"key":"' + bot_key + '"}'} | |
r = requests.post('https://api.riddles.io/graphql', data=json.dumps(data), headers=headers) | |
data = json.loads(r._content.decode()) | |
revs = data['data']['bot']['botRevisions']['edges'] | |
for rev in revs: | |
if rev['node']['revision']['revisionNumber'] == revision: | |
bot_id = rev['node']['key'] | |
data = {'query':'query CompetitionMatchLogQueryMyMatchKeys($botRevisionKeys: [String]!, $offset: Int!, $limit: Int!) {\nmatchResults(botRevision_key: { in: $botRevisionKeys }, offset: $offset, limit: $limit, orderBy:"-createdAt") {\nedges {\nnode {\nmatch {\nkey\n}\n}\n}\n}\n}','variables': '{"botRevisionKeys":["' + bot_id + '"], "limit":200, "offset":0, "schemaKey":"' + game_key + '"}'} | |
r = requests.post('https://api.riddles.io/graphql', data=json.dumps(data), headers=headers) | |
content = json.loads(r._content.decode()) | |
matches = [n['node']['match']['key'] for n in content['data']['matchResults']['edges']] | |
data = {'query':'query CompetitionMatchLogQueryAll($matchKeys: [String]!) {\nmatches(key: { in: $matchKeys }, orderBy: "-createdAt") {\nedges {\nnode {\nkey\ncreatedAt\nmatchType\nmatchResults {\nedges {\nnode {\nkey\ndeltaScore\ngameScore\nplayer\nwinner\nbotRevision {\nrevision {\nrevisionNumber\n}\nbot {\nkey\nname\nuser {\nkey\nalias\nemails {\nedges {\nnode {\nhash\n}\n}\n}\n}\nteam {\nkey\nname\n}\n}\n}\n}\n}\n}\n}\n}\n}\n}','variables':'{"matchKeys": ['+ ', '.join('"'+m+'"' for m in matches) +']}'} | |
r = requests.post('https://api.riddles.io/graphql', data=json.dumps(data), headers=headers) | |
content = json.loads(r._content.decode()) | |
results = {} | |
total = [0, 0, 0] | |
for match in content['data']['matches']['edges']: | |
player0 = match['node']['matchResults']['edges'][0]['node']['botRevision']['bot']['name'] + '_' + str(match['node']['matchResults']['edges'][0]['node']['botRevision']['revision']['revisionNumber']) | |
player1 = match['node']['matchResults']['edges'][1]['node']['botRevision']['bot']['name'] + '_' + str(match['node']['matchResults']['edges'][1]['node']['botRevision']['revision']['revisionNumber']) | |
winner = 2 | |
if match['node']['matchResults']['edges'][0]['node']['winner']: winner = 0 | |
elif match['node']['matchResults']['edges'][1]['node']['winner']: winner = 1 | |
if bot_name in player1: | |
player0, player1 = player1, player0 | |
if winner == 0: winner = 1 | |
elif winner == 1: winner = 0 | |
if player1 == 'mshackman_test_1': continue | |
pairing = player0 + ' - ' + player1 | |
if pairing not in results: results[pairing] = [0, 0, 0] | |
results[pairing][winner] += 1 | |
total[winner] += 1 | |
print(player + ': ' + str(total)) | |
print('\n'.join((r + ': ' + str(results[r])) for r in sorted(results.keys()))) | |
player = 'eulerscheZahl' | |
revision = 0 | |
if len(sys.argv) > 1: player = sys.argv[1] | |
if len(sys.argv) > 2: revision = int(sys.argv[2]) | |
load_stats(player, revision) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had to use brotli.decompress(r.content)