-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDraft Runner.py
198 lines (168 loc) · 7.26 KB
/
Draft Runner.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
import random
import os
import json
import math
import draft
from discourse import DiscourseClient as DC
import dr_secrets
import re
import bs4
import templates
client = DC('https://www.chiefdelphi.com/', api_username=dr_secrets.DISCOURSE_USERNAME, api_key=dr_secrets.DISCOURSE_KEY)
# SETTINGS
ROUND_TIMING = [2, 2, 2]
START_TIME = [8, 0]
SAVE_DIR = r"E:\_Python Projects\Draft Runner Data"
OUTPUT_MODE = "CD"
RANDOM_ORDER = True
TIERS = True
YEAR = 2019
# TODO Add a rookie random function
def list_from_cd_pm(pm_id, event):
d = client.read_pm(pm_id)[0]
raw_html = d['cooked']
cleanr = re.compile('<.*?>|&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});')
cleantext = re.sub(cleanr, '', raw_html)
clean_data = {"cd_username": d['username'],
"cd_user_id": d['id'],
"content": cleantext,
"event_name": d['topic_slug']}
with open("{}/{}/{}.txt".format(SAVE_DIR, event, clean_data['cd_username']), 'w') as outfile:
outfile.write(clean_data['content'])
#TODO For some reason it misses some lists randomly
#Looks like some rate limiting issue. Investigate response errors/codes
def check_for_lists():
private_messages = client.get_pms("fantasy_first_bot")
pm_ids = {}
for pm in private_messages['topic_list']['topics']:
print(pm)
if os.path.exists("{}/{}".format(SAVE_DIR, pm['title'])):
if pm['last_poster_username'] != "fantasy_first_bot" and pm['last_poster_username'] != "discobot":
print("Sending response to {}".format(pm['last_poster_username']))
client.send_pm(id=pm['id'], content="Received your list successfully!", username=pm['last_poster_username'])
pm_ids.update({pm['title']: pm['id']})
list_from_cd_pm(pm['id'], pm['title'])
else:
if pm['last_poster_username'] != "fantasy_first_bot" and pm['last_poster_username'] != "discobot":
client.send_pm(id=pm['id'], content="Sorry, *{}* doesn't seem to exist in my database of available events to draft.".format(pm['title']),
username=pm['last_poster_username'])
return pm_ids
def get_tier_sizes(num_players, num_teams, num_picks=3):
tiers = math.ceil(num_players / math.floor(num_teams / num_picks))
current_size = math.floor(num_players / tiers)
tier_sizes = []
while num_players > current_size:
tier_sizes.append(current_size)
num_players -= current_size
tiers -= 1
current_size = math.floor(num_players / tiers)
tier_sizes.append(num_players)
return tier_sizes
def get_signups(post_id):
likes = client.get_likes(post_id)
players = {}
for like in likes['post_action_users']:
players.update({like['username']: like['id']})
return players
print("Found {} lists for events".format(len(check_for_lists())))
# CREATE DIRECTORY
start_draft = True
while start_draft is True:
event_name = input("What is the shortname for your Event?: ")
if "'" in event_name:
print("The character ' is not permitted in event names")
else:
if os.path.isdir("{}\{}".format(SAVE_DIR, event_name)):
correct_prompt = input("Looks like this event exists already! Would you like to load the data? [Y/N]: ")
if correct_prompt.lower() == "y":
start_draft = False
loading = True
else:
print("Please try again!")
else:
os.mkdir("{}\{}".format(SAVE_DIR, event_name))
start_draft = False
loading = False
base_path = "{}\{}".format(SAVE_DIR, event_name)
players_input = "{}\Players.txt".format(base_path)
teams_input = "{}\Teams.txt".format(base_path)
players_data_location = "{}\Players.json".format(base_path)
teams_data_location = "{}\Teams.json".format(base_path)
random_list_location = "{}\Randoms.json".format(base_path)
if loading is True:
print("Loading data")
bad_response = True
while bad_response is True:
correct_prompt = input("Re-update players? [Y/N]: ")
if correct_prompt.lower() == "y":
draft_signups = get_signups(2236561)
players_clean = []
for player in draft_signups.keys():
players_clean.append(player)
if RANDOM_ORDER: # If random order setting is activated.
random.shuffle(players_clean)
with open(players_data_location, 'w') as outfile:
json.dump(players_clean, outfile)
bad_response = False
elif correct_prompt.lower() == "n":
with open(players_data_location) as json_file:
players_clean = json.load(json_file)
bad_response = False
else:
print("Invalid response!")
with open(teams_data_location) as json_file:
teams_clean = json.load(json_file)
with open(random_list_location) as json_file:
random_teams = json.load(json_file)
else:
# INITIALIZE DRAFT SETTINGS
event_full_name = input("What is the full name for your Event?: ")
start_date = input("What is the start date for your event? (YYYY-MM-DD): ")
start_time = input("What is the start time for your event? (24 hr time HH:MM:SS): ")
close_date = input("What is the close date for your event? (YYYY-MM-DD): ")
close_time = input("What is the close time for your event? (24 hr time HH:MM:SS): ")
# Input Players
f = open(players_input, "w")
f.close()
fd = os.system("notepad.exe {}".format(players_input))
f = open(players_input, "r")
players = f.readlines()
players_clean = [x.replace('\n', '') for x in players]
players_clean = list(filter(None, players_clean))
if RANDOM_ORDER: # If random order setting is activated.
random.shuffle(players_clean)
with open(players_data_location, 'w') as outfile:
json.dump(players_clean, outfile)
# Input Teams
f = open(teams_input, "w")
f.close()
df = os.system("notepad.exe {}".format(teams_input))
f = open(teams_input, "r")
teams = f.readlines()
team_list = "".join(teams)
teams_clean = [x.replace('\n', '') for x in teams]
body_text = templates.new_draft(start_date, start_time, close_date, close_time, team_list, event_name)
client.new_post(title="[OFF] {}".format(event_full_name), content=body_text)
random_teams = [x.replace('\n', '') for x in teams]
random.shuffle(random_teams)
with open(teams_data_location, 'w') as outfile:
json.dump(teams_clean, outfile)
with open(random_list_location, 'w') as outfile:
json.dump(random_teams, outfile)
tier_ratio = math.ceil((len(players_clean) + 1) / (len(teams_clean) / len(ROUND_TIMING)))
base_team_list = teams_clean.copy()
available_team_list = []
mini_available_team_list = []
if TIERS:
for team in base_team_list:
available_team_list.append([team, 1])
else:
for team in base_team_list:
available_team_list.append([team, tier_ratio])
if TIERS:
tier_data = get_tier_sizes(len(players_clean), len(teams_clean), len(ROUND_TIMING))
else:
tier_data = [len(players_clean)]
print(tier_data)
draft.run_draft(START_TIME, tier_data, base_path, tier_ratio, ROUND_TIMING, RANDOM_ORDER, OUTPUT_MODE, players_clean,
available_team_list, random_teams, teams_clean, event_name)