-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.py
228 lines (184 loc) · 6.74 KB
/
execute.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import urllib2
import requests
import challonge
import json
from trueskill import Rating, quality_1vs1, rate_1vs1
import cPickle as pickle
class Player:
def __init__(self, name):
self.name = name;
self.wins = 0;
self.losses = 0;
self.elo = 0;
self.matches = [];
self.rating = Rating();
self.rank = -1;
def addMatch(match):
matches.append[match];
def updateRating(self, rating):
self.rating = rating;
def showInfo(self):
print (self.name),
print ("%dW - %dL" %(self.wins, self.losses));
print ("Rating:"),
print self.rating,
print ("Rank: %d" %(self.rank));
class Match:
def __init__(self):
self.winner = ""
self.loser = ""
self.score = ""
self.tourney = "";
self.Round = 0;
self.date = "";
def __eq__(self, other):
return self.__dict__ == other.__dict__;
def setTourney(self, tourney):
self.tourney = tourney;
def setRound(self, Round):
self.Round = Round;
def setWinner(self, winner):
self.winner = winner;
def setLoser(self, loser):
self.loser = loser;
def setScore(self, score):
self.score = score;
def setDate(self, date):
self.date = date;
def showInfo(self):
print (self.tourney + " Round"),
print (self.Round),
print (": " + self.winner + " " + self.score + " " + self.loser);
def main():
listPlayers = pickle.load(open("info/players.pickle", "rb"));
listMatches = pickle.load(open("info/matches.pickle", "rb"));
ranking = [];
user = raw_input("Enter your challonge username: ");
user = "uiblis";
api_key = raw_input("Enter your api_key: ");
api_key = "GqEWndsdFiH7JhkIc3l6Ezf8eg3VyxHkACjQN5Pa"
challonge.set_credentials(user, api_key)
print("Welcome to BetterPR!")
while True:
print("");
print("Choose an option: ");
print("1) Input a tournament URL ");
print("2) Add a match ");
print("3) View players ");
print("4) Exit ");
while True:
choice = raw_input();
if (choice == "1"):
url = getURL();
tournament = loadURL(url);
print "";
print "Tournament: " + tournament["name"];
print "Date:",
print tournament["started-at"];
while True:
decision = raw_input("Compile data from this tournament? (y/n) ");
if (decision == "n"):
break;
elif (decision == "y"):
tourneyPlayers = [];
tourneyMatches = [];
compileParticipants(url, tourneyPlayers, listPlayers);
compileMatches(url, tourneyMatches, tourneyPlayers, tournament, listPlayers, listMatches);
listPlayers.sort(key = lambda x: x.rating.mu, reverse = True)
break;
else:
print ("Please enter 'y' or 'n'. ");
break;
if (choice == "2"):
break;
if (choice == "3"):
index = 1;
for player in listPlayers:
player.rank = index;
index += 1;
player.showInfo();
break;
if (choice == "4"):
pickle.dump(listPlayers, open("info/players.pickle", "wb"))
pickle.dump(listMatches, open("info/matches.pickle", "wb"))
exit();
else:
print "Please choose a valid option."
def getURL():
while True:
url = raw_input("Enter a challonge url (eg. challonge.com/ladulite1): ");
if (url[:4] == "www."):
url = url[4:];
if (url[:13] != "challonge.com"):
try:
x, y, z = url.split(".");
a, b = z.split("/");
url = x + "-" + b;
return url;
except ValueError:
print ("Invalid url.");
else:
try:
a, b = url.split("/")
url = b;
return url;
except (ValueError):
print ("Invalid url.");
def loadURL(url):
while True:
try:
tournament = challonge.tournaments.show(url);
return tournament;
except (urllib2.HTTPError, urllib2.URLError):
print "Invalid url."
url = getURL();
def compileParticipants(url, tourneyPlayers, listPlayers):
participants = challonge.participants.index(url);
for participant in participants:
flag = 0;
tourneyPlayers.append((participant["id"], participant["name"]));
for person in listPlayers:
if participant["name"] == person.name:
flag = 1;
break;
if (flag == 1):
continue;
else:
print "adding new player";
listPlayers.append(Player(participant["name"]));
print "all players added";
def compileMatches(url, tourneyMatches, tourneyPlayers, tournament, listPlayers, listMatches):
matches = challonge.matches.index(url);
counter = 0;
for match in matches:
tourneyMatches.append(Match());
tourneyMatches[counter].setTourney(tournament["name"]);
for player in tourneyPlayers:
if match["winner-id"] == player[0]:
tourneyMatches[counter].setWinner(player[1]);
for person in listPlayers:
if person.name == player[1]:
winner = person;
if match["loser-id"] == player[0]:
tourneyMatches[counter].setLoser(player[1]);
for person in listPlayers:
if person.name == player[1]:
loser = person;
tourneyMatches[counter].setScore(match["scores-csv"]);
tourneyMatches[counter].setRound(match["round"]);
tourneyMatches[counter].setDate(match["updated-at"])
if tourneyMatches[counter] in listMatches:
print ("Match already in database, skipping: ");
continue;
else:
listMatches.append(tourneyMatches[counter]);
new_r1, new_r2 = rate_1vs1(winner.rating, loser.rating)
winner.updateRating(new_r1);
loser.updateRating(new_r2);
winner.wins = winner.wins + 1;
loser.losses = loser.losses + 1;
winner.matches.append(tourneyMatches[counter]); #change these eventually
loser.matches.append(tourneyMatches[counter]);
counter = counter + 1;
if __name__ == "__main__":
main();