-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict_points.py
354 lines (312 loc) · 11.7 KB
/
predict_points.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import pandas as pd
df = pd.read_csv(r'data/points.csv')
qualified = list(df["QUALIFIED"])
team = list(df["TEAM"])
gamesP = list(df["P"])
gamesW = list(df["W"])
gamesL = list(df["L"])
gamesNR = list(df["NR"])
nrr = list(df["NRR"])
pts = list(df["PTS"])
scoreF = list(df["FOR"])
scoreA = list(df["AGAINST"])
colors = {"RCB": "#D5152C", "GT": "#1C2234", "SRH": "#E34633", "CSK": "#FFD230", "DC": "#32579D",
"KKR": "#3D245D", "LSG": "#2956C9", "MI": "#285290", "RR": "#264AA5", "PBKS": "#DD212E"}
fcolors = {"RCB": "#000000", "GT": "#ffffff", "SRH": "#000000", "CSK": "#000000", "DC": "#ffffff",
"KKR": "#ffffff", "LSG": "#000000", "MI": "#ffffff", "RR": "#ffffff", "PBKS": "#000000"}
def fix_overs(no_overs):
no_overs = str(round(no_overs, 1))
new = no_overs.split(".")
if int(new[1]) >= 6:
if new[0] == "19":
return 20.0
else:
overs = float(new[0]) + 1
return overs
else:
return float(no_overs)
def add_overs(overs1, overs2):
new = overs1 + overs2
overs = str(new).split(".")
if int(overs[1]) >= 6:
overs[0] = str(int(overs[0]) + 1)
overs[1] = str(int(overs[1]) - 6)
return float(".".join(overs))
def find_scores(avg_score_win_for, avg_score_win_ag, avg_score_lose_for, avg_score_lose_ag):
win_for_runs = avg_score_win_for[0]
win_for_overs = avg_score_win_for[1]
win_ag_runs = avg_score_win_ag[0]
win_ag_overs = avg_score_win_ag[1]
lose_for_runs = avg_score_lose_for[0]
lose_for_overs = avg_score_lose_for[1]
lose_ag_runs = avg_score_lose_ag[0]
lose_ag_overs = avg_score_lose_ag[1]
win_score_runs = max(win_for_runs, lose_ag_runs)
win_score_overs = max(win_for_overs, lose_ag_overs)
lose_score_runs = min(lose_for_runs, win_ag_runs)
lose_score_overs = max(lose_for_overs, win_ag_overs)
if win_score_runs < lose_score_runs:
win_score_runs, lose_score_runs = lose_score_runs, win_score_runs
return win_score_runs, lose_score_runs, win_score_overs, lose_score_overs
def get_perf(team, scoreF, scoreA):
performance = {}
for i in range(len(team)):
a, b = scoreF[i].split('/')
c, d = scoreA[i].split('/')
a = int(a)
b = round(float(b), 1)
c = int(c)
d = round(float(d), 1)
performance[team[i]] = [a, b, c, d]
return performance
def get_nrr(avg_score_for, avg_score_ag):
nrr_for = avg_score_for[0]/avg_score_for[1]
nrr_ag = avg_score_ag[0]/avg_score_ag[1]
return (nrr_for - nrr_ag)/10
def reset_table():
df = pd.read_csv(r'data/points.csv')
team = list(df["TEAM"])
gamesP = list(df["P"])
gamesW = list(df["W"])
gamesL = list(df["L"])
gamesNR = list(df["NR"])
nrr = list(df["NRR"])
pts = list(df["PTS"])
qualified = list(df["QUALIFIED"])
table = []
for i in range(len(team)):
data = []
data.append(team[i])
data.append(gamesP[i])
data.append(gamesW[i])
data.append(gamesL[i])
data.append(gamesNR[i])
data.append(nrr[i])
data.append(pts[i])
data.append(qualified[i])
table.append(data)
return table
def make_dict(table):
table_dict = {}
for i in table:
table_dict[i[0]] = i
return table_dict
def sort_values(values):
for ind in range(len(values)):
min_index = ind
for j in range(ind + 1, len(values)):
# select the minimum element in every iteration
if values[j][6] < values[min_index][6]:
min_index = j
elif values[j][6] == values[min_index][6]:
if values[j][5] < values[min_index][5]:
min_index = j
# swapping the elements to sort the array
(values[ind], values[min_index]) = (values[min_index], values[ind])
values.reverse()
def print_table(table):
print("|-------------------------------------------------------|")
print("| POS | TEAM | P | W | L | NR | NNR | PTS | ")
print("|-------------------------------------------------------|")
count = 1
for i in table:
print(
f"|{count : ^5}|{i[0] : ^6}|{i[1] : ^5}|{i[2] : ^5}|{i[3] : ^5}|{i[4] : ^6}|{i[5] : ^9.3f}|{i[6] : ^7}|")
count += 1
print("|-------------------------------------------------------|")
row = ""
def add_to_table(table, table_count):
content = Element("copy-content")
content.element.style.display = "block"
row = """
<p class="title-para" id="scenario-table">Final Points Table - </p>
<table id="table-later">
<thead>
<tr>
<th>POS</th>
<th>TEAM</th>
<th>P</th>
<th>W</th>
<th>L</th>
<th>NR</th>
<th>NNR</th>
<th>PTS</th>
</tr>
</thead>
<tbody id="points-data">
"""
for i in range(len(table)):
row = row + f"""
<tr>
<td>{i+1}</td>
<td>{table[i][0]}</td>
<td>{table[i][1]}</td>
<td>{table[i][2]}</td>
<td>{table[i][3]}</td>
<td>{table[i][4]}</td>
<td>{round(table[i][5],3)}</td>
<td>{table[i][6]}</td>
</tr>
"""
row += """</tbody>
</table>
<br>
<hr>"""
content.element.innerHTML += row
def winners(teams1, teams2):
possible_winners = []
possible_losers = []
for i in range(2**len(teams1)):
winner = []
loser = []
for j in range(len(teams1)):
if i & (1 << j):
winner.append(teams2[j])
loser.append(teams1[j])
else:
winner.append(teams1[j])
loser.append(teams2[j])
possible_winners.append(winner)
possible_losers.append(loser)
return possible_winners, possible_losers
def assign_points(team_name, wins, loses, table):
for i in range(len(wins)):
# Team P W L NR NRR PTS
# ['GT', 11, 8, 3, 0, 0.951, 16]
# 0 1 2 3 4 5 6
table_dict = make_dict(table)
win_team = list(table_dict[wins[i]])
lose_time = list(table_dict[loses[i]])
performance = get_perf(team, scoreF, scoreA)
avg_score_win_for = [round(
performance[wins[i]][0]/win_team[1]), fix_overs(performance[wins[i]][1]/win_team[1])]
avg_score_win_ag = [round(performance[wins[i]][2]/win_team[1]),
fix_overs(performance[wins[i]][3]/win_team[1])]
avg_score_lose_for = [round(
performance[loses[i]][0]/lose_time[1]), fix_overs(performance[loses[i]][1]/lose_time[1])]
avg_score_lose_ag = [round(performance[loses[i]][2]/lose_time[1]),
fix_overs(performance[loses[i]][3]/lose_time[1])]
win_score_runs, lose_score_runs, win_score_overs, lose_score_overs = find_scores(
avg_score_win_for, avg_score_win_ag, avg_score_lose_for, avg_score_lose_ag)
performance[wins[i]][0] += win_score_runs
performance[wins[i]][1] = add_overs(
performance[wins[i]][1], win_score_overs)
performance[loses[i]][2] += win_score_runs
performance[loses[i]][3] = add_overs(
performance[loses[i]][3], win_score_overs)
performance[loses[i]][0] += lose_score_runs
performance[loses[i]][1] = add_overs(
performance[loses[i]][1], lose_score_overs)
performance[wins[i]][2] += lose_score_runs
performance[wins[i]][3] = add_overs(
performance[wins[i]][3], lose_score_overs)
win_team[1] += 1
win_team[2] += 1
win_team[6] += 2
lose_time[1] += 1
lose_time[3] += 1
score_win_for = [performance[wins[i]][0], performance[wins[i]][1]]
score_win_ag = [performance[wins[i]][2], performance[wins[i]][3]]
score_lose_for = [performance[loses[i]][0], performance[loses[i]][1]]
score_lose_ag = [performance[loses[i]][2], performance[loses[i]][3]]
win_nrr_new = get_nrr(score_win_for, score_win_ag)
lose_nrr_new = get_nrr(score_lose_for, score_lose_ag)
if win_nrr_new < 0:
win_nrr_new *= -1
if lose_nrr_new > 0:
lose_nrr_new *= -1
win_team[5] += win_nrr_new
lose_time[5] += lose_nrr_new
table_dict[wins[i]] = win_team
table_dict[loses[i]] = lose_time
table = list(table_dict.values())
return table
def team_wins(team, teams1, teams2, wins, table_count):
content = Element("copy-content")
content.element.style.display = "block"
content_text = f"""<br>
<p class="title-para" style="background-color:{colors[team]};color:{fcolors[team]}" id="scenario-number">Scenario - {table_count+1}</p>
<div id="scenarios">
<h1>{team} will qualify if -</h1>
<hr>
"""
for i in range(len(wins)):
content_text += f"<p class = 'winner_row'> {wins[i] : <4} wins - {teams1[i] : <4} VS {teams2[i] : <4} </p>"
content_text += "</div>"
content.element.innerHTML += content_text
table_count += 1
return table_count
def handle_click(event):
table_count = 0
resultText = Element("results")
resultText.element.style.display = "none"
content = Element("copy-content" )
content.element.innerHTML = ""
content.element.style.display = "none"
global ch, wins, team, qualified
team_name = Element("teams").element.value
iterations = Element("iterations").element.value
ch = 0
results_printed = False
teamInd = team.index(team_name)
# in case, IPL.COM removes the Q sign,
if qualified[teamInd] == "Yes" or team_name == "GT":
resultText = Element("results")
resultText.element.innerHTML = f"Congratulations!🎉 Your team {team_name} has already qualified" + "🥳"
resultText.element.style.display = "block"
exit(0)
for l in range(len(wins)):
table = reset_table()
if team_name in loses[l]:
continue
table = assign_points(team_name, wins[l], loses[l], table)
sort_values(table)
top4 = table[:4]
for team_q in top4:
if team_q[0] == team_name: # printing all tables where RCB qualifies
table_count = team_wins(team_q[0], teams1, teams2, wins[l], table_count)
print_table(table)
add_to_table(table, table_count)
ch = ch + 1
results_printed = True
if ch == int(iterations):
exit(0)
if results_printed == False:
resultText = Element("results")
resultText.element.innerHTML = f"Sorry🥹 but your team {team_name} cannot qualify this season. Better luck next time."
resultText.element.style.display = "block"
exit(0)
ch = 0
table = reset_table()
row = ""
for i in range(len(table)):
row = row + f"""
<tr>
<td>{i+1}</td>
<td>{table[i][0]}</td>
<td>{table[i][1]}</td>
<td>{table[i][2]}</td>
<td>{table[i][3]}</td>
<td>{table[i][4]}</td>
<td>{table[i][5]}</td>
<td>{table[i][6]}</td>
</tr>
"""
pointsbody = Element("points-table")
pointsbody.element.innerHTML = row
df = pd.read_csv(r'data/ipl_fixtures.csv')
teams1 = list(df["team1"])
teams2 = list(df["team2"])
wins, loses = winners(teams1, teams2)
html = ""
for i in range(1, len(wins)):
if i > 10 or i > 2**len(wins):
break
html = html + f"""
<option value="{i}">{i}</option>
"""
iterationsmenu = Element("iterations")
iterationsmenu.element.innerHTML = html
buttons = document.querySelectorAll("#calc-button")
for button in buttons:
button.onclick = handle_click