-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_raffle.py
204 lines (163 loc) · 4.98 KB
/
generate_raffle.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
#!/usr/bin/python3
from bs4 import BeautifulSoup
import json
import os
import random
import requests
import sys
# Get the config
config_path = input('Config file path: ')
with open(config_path, encoding='utf-8') as config_file:
config = json.load(config_file)
# Get teams to exclude
to_exclude_str = input('Team IDs to exclude, separated by commas: ').strip()
if len(to_exclude_str) > 0:
to_exclude = set(map(lambda s: int(s.strip()), to_exclude_str.split(',')))
else:
to_exclude = set()
# Validate data
to_exclude_clone = set(to_exclude)
if 'teams' not in config:
sys.exit('Config must contain teams')
teams = config['teams']
for team in teams:
if 'name' not in team or 'members' not in team or 'id' not in team:
sys.exit('Teams must have name, members, id')
if team['id'] in to_exclude:
print('excluding ' + str(team['id']))
to_exclude_clone.remove(team['id'])
if len(to_exclude_clone) > 0:
sys.exit('Excluded IDs do not exist: ' + ', '.join(to_exclude_clone))
# Get the DOMJudge host & session
dj_url = input('DOMJudge base URL (no trailing slash): ')
dj_session = input('DOMJudge session cookie: ')
# Create cookies
dj_cookies = { 'domjudge_session': dj_session }
# Get scoreboard
score_req = requests.get(dj_url + '/api/scoreboard', cookies=dj_cookies)
score_req.raise_for_status()
score = json.loads(score_req.text)
print('Downloaded scoreboard')
# Load the number of problems solved per team
for score_team in score:
team = next((t for t in teams if t['id'] == score_team['team']), None)
if team is not None:
team['solved'] = score_team['score']['num_solved']
# Check whether any teams weren't in the scoreboard
for team in teams:
if 'solved' not in team:
print('WARNING: Team without score: ' + team['name'])
# Generate the array of winners, with weighted probability
# To do so, clone people as many times as they have solved problems,
# then shuffle that array, and finally remove duplicates.
# Not very efficient, but len(teams) is <100...
winners = []
for team in teams:
if team['id'] not in to_exclude:
for member in team['members']:
winners += [(member, team['name']) for i in range(max(1, team['solved']))]
random.shuffle(winners)
result = []
for winner in winners:
if winner not in result:
result.append(winner)
# Create the out directory now (there are 2 outputs)
os.makedirs('out', exist_ok=True)
# Print the winners to a file, for logging purposes
with open('out/raffle.log', 'w') as result_log:
print('\n'.join([p[0] + ' | ' + p[1] for p in result]), file=result_log)
# Generate the HTML
result_array = '[\n' + ',\n'.join(['[' + json.dumps(p[0]) + ', ' + json.dumps(p[1]) + ']' for p in result]) + '\n]'
html = """<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Raffle</title>
<style>
#container {
background-color: #fff;
}
h1 {
font-size: 10em;
font-weight: 700;
text-align: center;
}
h2 {
font-size: 5em;
font-weight: 400;
text-align: center;
}
#next {
font-size: 2em;
color: #B0B0B0;
position: absolute;
left: 2em;
bottom: 2em;
}
#fullscreen {
font-size: 2em;
color: #F0F0F0;
position: absolute;
right: 2em;
bottom: 2em;
}
</style>
<script>
RESULTS = """ + result_array + """;
var index = 0;
var isLoading = false;
function showNext() {
if(isLoading) {
return;
}
isLoading = true;
var title = document.getElementById('title');
var subtitle = document.getElementById('subtitle');
title.textContent = '';
subtitle.textContent = '';
if(index < RESULTS.length) {
title.textContent = '.';
setTimeout(function() {
title.textContent = '. .';
setTimeout(function() {
title.textContent = '. . .';
setTimeout(function() {
title.textContent = RESULTS[index][0];
subtitle.textContent = RESULTS[index][1];
index++;
isLoading = false;
}, 1000);
}, 1000);
}, 1000);
} else {
subtitle.textContent = "there's nobody left!";
}
}
function showFullscreen() {
var container = document.getElementById('container');
if('requestFullscreen' in container) {
container.requestFullscreen();
} else if ('webkitRequestFullscreen' in container) {
container.webkitRequestFullscreen();
} else if ('mozRequestFullScreen' in container) {
container.mozRequestFullScreen();
} else if ('msRequestFullscreen' in container) {
container.msRequestFullscreen();
}
}
</script>
</head>
<body>
<div id="container">
<h1 id="title">Raffle</h1>
<h2 id="subtitle"></h2>
<a id="next" href="#" onclick="showNext(); return false;">next</a>
<a id="fullscreen" href="#" onclick="showFullscreen(); return false;">fullscreen</a>
</div>
</body>
</html>
"""
# Print the HTML to a file
with open('out/raffle.html', 'w') as html_file:
print(html, file=html_file)
print('Done!')