-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
209 lines (165 loc) · 6.3 KB
/
utilities.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import make_request
import model
from data import data_access
import praw
import re
import datetime
r = praw.Reddit(client_id=data_access.get_config("bot_client_id"),
client_secret=data_access.get_config("bot_client_secret"),
password=data_access.get_config("bot_password"),
user_agent=data_access.get_config("bot_user_agent"),
username='dota2-post-match-bot')
subreddit = r.subreddit('dota2')
twitch_clip_bot = r.redditor('DotaClipMatchFinder')
def resolve_hero(hero_id):
return data_access.retrieve_hero_ids()[str(hero_id)]
def resolve_item(item_id):
return data_access.retrieve_item_ids()[str(item_id)]
def resolve_team(team_id):
if team_id is None:
return None
if data_access.retrieve_team(team_id) == 'TEAM_NOT_FOUND': # Get Team and Add to List
name = make_request.get_team_name_from_id(team_id)
data_access.add_team(model.Team(
team_id=team_id,
name=name,
team_logo=f'[](/logo-dota \"{name}\")',
last_played="0"
))
resolve_team(team_id)
else:
team = data_access.retrieve_team(team_id)
return model.Team(
team_id=team['team_id'],
name=team['name'],
team_logo=team['team_logo'],
last_played=team['last_played']
)
def is_potential_final_game(number, team_one_score, team_two_score):
if number is 2 and team_one_score + team_two_score == 1:
return True
if team_one_score + team_two_score == number - 1:
return True
if team_one_score == (number / 2) - 0.5:
return True
if team_two_score == (number / 2) - 0.5:
return True
return False
def match_has_disappeared(live_matches, series):
for live_match in live_matches:
if series.match_id == live_match.match_id:
return False
return True
def get_teams_that_could_win(live_match):
if live_match.radiant_series_wins + live_match.dire_series_wins == live_match.node_type - 1:
return ['radiant', 'dire']
if live_match.radiant_series_wins == (live_match.node_type / 2) - 0.5:
return ['radiant']
if live_match.dire_series_wins == (live_match.node_type / 2) - 0.5:
return ['dire']
raise Exception('Checking for teams that could win where there are no teams that can.')
def is_series_winner(live_match):
latest_match = make_request.get_match_details(live_match.match_id)
if latest_match is not False:
teams_that_could_win = get_teams_that_could_win(live_match)
for team in teams_that_could_win:
if team == 'radiant':
if latest_match['radiant_win']:
return resolve_team(latest_match['radiant_team_id'])
if team == 'dire':
if not latest_match['radiant_win']:
return resolve_team(latest_match['dire_team_id'])
return False # Series isn't over, wait for the next game.
return False
def set_if_exists(kwargs, key):
if key in kwargs.keys():
return kwargs[key]
return None
def resolve_region(region_number):
if region_number is None:
return None
if region_number is 1:
return 'North America'
if region_number is 2:
return 'South America'
if region_number is 3:
return 'Europe'
if region_number is 4:
return 'CIS'
if region_number is 5:
return 'China'
if region_number is 6:
return 'South-East Asia'
def resolve_phase(phase_number):
if phase_number is None:
return None
if phase_number is 1:
return f'Qualifier'
if phase_number is 2:
return 'Group Stage'
if phase_number is 3:
return 'Playoffs'
return f'Unknown ({phase_number})'
def resolve_node_type(node_type_number):
if node_type_number is None:
return None
if node_type_number is 1:
return 1
if node_type_number is 2:
return 3
if node_type_number is 3:
return 5
if node_type_number is 4:
return 2
def get_league_node(league, node_id):
for node_group in league.node_groups:
correct_node = search_node_group(node_group, node_id)
if correct_node is not None:
return correct_node
def get_league_node_in_group(node_group, node_id):
correct_node = search_node_group(node_group, node_id)
if correct_node is not None:
return correct_node[0]
def search_node_group(node_group, node_id):
for deep_node_group in node_group.node_groups:
correct_node = search_node_group(deep_node_group, node_id)
if correct_node:
return correct_node
correct_node = [x for x in list(map(lambda node: node if node.node_id == node_id else None,
node_group.nodes)) if x]
if not correct_node:
return None
return correct_node
def get_node_group(node_groups, node_group_id):
for node_group in node_groups:
if node_group.node_group_id == node_group_id:
return node_group
correct_node_group = get_node_group(node_group.node_groups, node_group_id)
if correct_node_group is not None:
return correct_node_group
def get_twitch_clips(match_id):
clips = [x for x in list(map(lambda comment: f'[{comment.submission.title}]({comment.submission.url})'
if str(match_id) in comment.body else None, twitch_clip_bot.comments.new())) if x]
print(clips)
return clips
def get_node_depth(node_group, node, current_depth):
if node.incoming_node_id_2 is not None:
return get_node_depth(node_group, get_league_node_in_group(node_group, node.incoming_node_id_2), current_depth + 1)
else:
return current_depth
def get_league_nodes_in_group_by_round(node_group, round_number):
nodes = []
for node in node_group:
if int(re.findall(r'\d+', node.name)[0]) == round_number:
nodes.append(node)
return nodes
def team_has_played_today(team):
if datetime.datetime.now().day == team.last_played.day and \
datetime.datetime.now().month == team.last_played.month and \
datetime.datetime.now().year == team.last_played.year:
return True
return False
def set_team_last_played(team):
data_access.set_team_last_played(team)
class APIDownException(Exception):
pass