-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd3thmatch.py
More file actions
executable file
·135 lines (99 loc) · 3.82 KB
/
d3thmatch.py
File metadata and controls
executable file
·135 lines (99 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
import json
import os
import time
import urllib.request
from datetime import datetime, timedelta, timezone
"""
Before the idea of a tournament bot even existed, I had the idea of a series of
tools to make TOing large tournaments via challonge more bearable. This is the
rough prototype I started before getting distracted with other things.
The idea was to have something warn, and then auto-DQ people who didn't show up
for their matches. This tool just checks for matches that haven't made progress
in TIMEOUT_IN_MINS minutes, and prints them to the screen. I think. Idk, it's
been a while.
I copied this into the repo because at some point we may want to copypaste some
of the late-checking logic into the tournament bot. If nothing else, this gives
some crappy-but-functional (I think) sample code for interacting with the
Challonge API. Enjoy(?)
-Perry
"""
CHALLONGE_API = 'https://api.challonge.com/v1'
KEY_ENV_VAR = 'API_KEY'
TOURNEY_ENV_VAR = 'TOURNAMENT_ID'
TIMEOUT_IN_MINS = 10
PDT = timezone(timedelta(hours=-7))
CHALLONGE_DATE_FORMAT = '%Y-%m-%dT%H:%M:%S.%f%z'
CHECK_INTERVAL_IN_SECS = 60
class LateMatch:
def __init__(self, p1ID, p2ID, late_by_mins):
self.p1ID = p1ID
self.p2ID = p2ID
self.late_mins = late_by_mins
def find_late_matches(matches):
# Sort matches by ID.
match_by_id = {}
for m in matches:
match_by_id[m['id']] = m
# Filter for open matches.
open_matches = [m for m in matches if m['state'] == "open"]
# Find late players.
late = []
for m in open_matches:
last_updated = datetime.strptime(m['updated_at'],
CHALLONGE_DATE_FORMAT)
elapsed_mins = (datetime.now(PDT) - last_updated).seconds / 60
if elapsed_mins > TIMEOUT_IN_MINS:
late.append(
LateMatch(m['player1_id'], m['player2_id'],
round(elapsed_mins)))
return late
def get_players_by_id(key, tourney_id):
raw = make_request(CHALLONGE_API,
f'/tournaments/{tourney_id}/participants.json',
{'api_key': key})
# Raw format is a list of dicts, all with one property "participant".
# Convert into dict of players by ID.
players = {}
for p in raw:
player = p["participant"]
players[player["id"]] = player
return players
def get_matches(key, tourney_id):
raw = make_request(CHALLONGE_API,
f'/tournaments/{tourney_id}/matches.json',
{'api_key': key})
# Raw format is a little weird - a list of dicts, where each dict has one property "match".
# Convert into list of matches.
matches = []
for m in raw:
matches.append(m["match"])
return matches
def make_request(base_url, additional_url, params={}):
"""Fetches resource at URL, converts JSON response to object."""
url = base_url + additional_url
first_item = True
for param, value in params.items():
if first_item:
url += f'?{param}={value}'
continue
url += f'&{param}={value}'
response = urllib.request.urlopen(url)
# Convert raw response to usable JSON object
response_as_string = response.read().decode('utf-8')
return json.loads(response_as_string)
def main():
key = os.environ[KEY_ENV_VAR]
tourney_id = os.environ[TOURNEY_ENV_VAR]
players = get_players_by_id(key, tourney_id)
while True:
late_players = find_late_matches(get_matches(key, tourney_id))
for m in late_players:
p1 = players[m.p1ID]
p2 = players[m.p2ID]
print(
f"Match between {p1['display_name']} and {p2['display_name']} is running {m.late_mins} minutes late."
)
time.sleep(CHECK_INTERVAL_IN_SECS)
if __name__ == '__main__':
main()