1 # -*- coding: utf-8 -*-
3 Created on Thu Jan 6 23:35:23 2022
5 @author: Avram Gottschlich
8 I copied several functions from your code;
9 if it's easier to refer to them rather than copying them that's absolutely fine
11 This rewrites the html each time it is called
12 If it's easy to call the puzzle/round functions each time they're updated,
15 Requires sorttable.js, which should be included
18 from boto3.dynamodb.conditions import Key
21 website = "https://halibut.cworth.org/"
22 #change this if we're using AWS or some other subdomain instead
24 def channel_url(channel_id):
25 """Given a channel ID, return the URL for that channel."""
27 return "https://halibutthatbass.slack.com/archives/{}".format(channel_id)
29 def find_hunt_for_hunt_id(table, hunt_id):
30 """Given a hunt ID find the database item for that hunt
32 Returns None if hunt ID is not found, otherwise a
33 dictionary with all fields from the hunt's row in the table,
34 (channel_id, active, hunt_id, name, url, sheet_url, etc.).
36 response = table.get_item(
39 'SK': 'hunt-{}'.format(hunt_id)
42 if 'Item' in response:
43 return response['Item']
47 def hunt_puzzles_for_hunt_id(table, hunt_id):
48 """Return all puzzles that belong to the given hunt_id"""
50 response = table.query(
51 KeyConditionExpression=(
52 Key('hunt_id').eq(hunt_id) &
53 Key('SK').begins_with('puzzle-')
57 return response['Items']
60 #shortcutting function for creating html links
61 #opens in a new tab for external links
62 return '<a href="{}" target="_blank" rel="noreferrer noopener">{}</a>'.format(lin, text)
65 #internal links, doesn't open new tab
66 return '<a href="{}">{}</a>'.format(lin, text)
68 def hunt_info(table, hunt_id):
70 Retrieves list of rounds, puzzles for the given hunt
73 hunt = find_hunt_for_hunt_id(table, hunt_id)
76 channel_id = hunt["channel_id"]
78 puzzles = hunt_puzzles_for_hunt_id(table, hunt_id)
81 for puzzle in puzzles:
82 if "rounds" not in puzzle:
84 for rnd in puzzle["rounds"]:
89 return puzzles, rounds
91 def round_stat(rnd, puzzles):
92 #Counts puzzles, solved, list of puzzles for a given round
99 for puzzle in puzzles:
100 if "rounds" not in puzzle:
102 if rnd in puzzle["rounds"]:
103 if puzzle['type'] == 'meta':
105 if puzzle['status'] == 'solved':
109 if puzzle['status'] == 'solved':
111 solved_puzzles.append(puzzle)
113 unsolved_puzzles.append(puzzle)
114 solved_puzzles = sorted(solved_puzzles, key = lambda i: i['name'])
115 unsolved_puzzles = sorted(unsolved_puzzles, key = lambda i: i['name'])
116 rnd_puzzles = metas + unsolved_puzzles + solved_puzzles
117 return puzzle_count, solved_count, rnd_puzzles, meta_solved, len(metas)
121 def overview(puzzles, rounds):
122 #big board, main page. saves as index.html
123 start = ['<!DOCTYPE html>\n',
126 ' <meta charset="utf-8">\n',
127 ' <meta name="viewport" content="width=device-width, initial-scale=1">\n',
129 ' <link rel="stylesheet" href="overview.css">\n',
130 ' <script type="text/javascript">\n',
131 ' // Hide all elements with class="containerTab", except for the one that matches the clickable grid column\n',
132 ' function openTab(tabName) {\n',
134 ' x = document.getElementsByClassName("containerTab");\n',
135 ' for (i = 0; i < x.length; i++) {\n',
136 ' x[i].style.display = "none";\n',
138 ' document.getElementById(tabName).style.display = "block";\n',
142 ' <title>Hunt Overview</title>\n',
143 ' <script src="sorttable.js"></script>\n'
145 ' <div class="sidenav">\n'
146 ' <a href="index.html">Hunt Overview</a>'
147 ' <a href="all.html">All Puzzles</a>\n'
148 ' <a href="unsolved.html">Unsolved</a>\n'
149 ' <a href="solved.html">Solved</a>\n'
152 columns = [' <div class="row">\n']
156 puzzle_count, solved_count, rnd_puzzles, meta_solved, metas = round_stat(rnd, puzzles)
157 if metas == meta_solved and metas > 0:
161 columns += [' <div class="column {}" onclick="openTab(\'b{}\');">\n'.format(status, i),
162 ' <p>{}</p>\n'.format(rnd),
163 ' <p>Puzzles: {}/{}</p>\n'.format(solved_count, puzzle_count),
164 ' <p>Metas: {}/{}</p>\n'.format(meta_solved, metas),
168 ' <div id="b{}" class="containerTab {}" style="display:none;">\n'.format(i, status),
169 ' <span onclick="this.parentElement.style.display=\'none\'" class="closebtn">x</span>\n',
170 ' <h2>{}</h2>\n'.format(link(website + "_".join(rnd.split()) + "_round.html", rnd)),
171 ' <table class="sortable">\n',
173 ' <th><u>Puzzle</u></th>\n',
174 ' <th><u>Answer</u></th>\n',
177 for puzzle in rnd_puzzles:
178 if puzzle['type'] == 'meta':
182 if puzzle['status'] == 'solved':
183 expanding += [' <tr class=\'solved\';>\n',
184 ' <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + ".html", puzzle['name']+meta)),
185 ' <td>{}</td>\n'.format(puzzle['solution']),
188 expanding += [' <tr class=\'unsolved\';>\n',
189 ' <td><b>{}</b></td>\n'.format(link(website + "_".join(puzzle['name'].split()) + ".html", puzzle['name']+meta)),
192 expanding.append(' </table>\n')
193 expanding.append(' </div>\n')
195 columns.append(' </div>\n')
196 end = ['</body>\n', '</html>\n']
197 html = start + expanding + columns + end
205 def round_overview(rnd, puzzles):
206 #inputs: round name, puzzles
208 #saves as (round name)_round.html, in case meta/round share names.
209 #underscores replace spaces for links.
210 rnd_puzzles, meta_solved, metas = round_stat(rnd, puzzles)[2:]
211 if meta_solved == metas and metas > 0:
217 ' <link rel="stylesheet" href="individual.css">\n',
218 ' <title>Mystery Hunt 2022</title>\n',
219 ' <script src="sorttable.js"></script>\n',
221 ' <body class="{}">\n'.format(status),
222 ' <h1><b>{}</b></h1>\n'.format(rnd),
223 ' <p>{}</p>\n'.format(link(website + "index.html", 'Hunt Overview')),
225 ' <table class="center sortable">\n',
228 ' <th>Puzzle Title/Slack</th>\n',
229 ' <th>Puzzle Link</th>\n',
231 ' <th>Overview</th>\n',
232 ' <th>Answer</th>\n',
233 #' <th>Extra Links</th>\n',
239 for puzzle in rnd_puzzles:
240 if puzzle['type'] == 'meta':
244 slack_url = channel_url(puzzle['channel_id'])
246 if puzzle['status'] == 'solved':
247 puzzle_list += [ ' <tr>\n',
248 ' <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
249 ' <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
250 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
251 ' <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
252 ' <td>{}</td>\n'.format(puzzle['solution']),
254 ' <td>{}</td>\n'.format("".join(puzzle['tags'])),
257 puzzle_list += [ ' <tr>\n',
258 ' <td><b>{}</b></td>\n'.format(elink(slack_url, puzzle['name']+meta)),
259 ' <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
260 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
261 ' <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
264 ' <td>{}</td>\n'.format(" ".join(puzzle['tags'])),
266 end = [' </tbody>\n',
271 html = start + puzzle_list + end
272 file = "{}_round.html".format('_'.join(rnd.split()))
279 def puzzle_overview(puzzle):
280 #overview page for individual puzzles. saves as (name of puzzle).html,
281 #with underscores rather than spaces
282 name = puzzle['name']
283 if puzzle['type'] == 'meta':
287 slack_url = channel_url(puzzle['channel_id'])
288 round_url = [link(website + "_".join(rnd.split()) + "_round.html", rnd) for rnd in puzzle['rounds']]
289 if puzzle['status'] == 'solved':
290 solution = puzzle['solution']
295 html = ['<!DOCTYPE html>\n',
298 ' <meta charset="utf-8">\n',
299 ' <meta name="viewport" content="width=device-width, initial-scale=1">\n',
300 ' <link rel="stylesheet" href="individual.css">\n',
301 ' <title>{}</title>\n'.format(name+meta),
302 ' <p>{}</p>'.format(link(website + 'index.html', 'Hunt Overview')),
305 '<body class="{}">\n'.format(status),
306 ' <h1>{}</h1>\n'.format(name+meta),
308 ' <table class="center">\n',
310 ' <td>{}</td>\n'.format(elink(slack_url, 'Channel')), #slack channel
311 ' <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
312 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
313 ' <td>Additional Resources</td>\n',
316 ' <table class="center">\n',
318 ' <td>Round(s): {}</td>\n'.format(" ".join(round_url)), #round page on our site
319 ' <td>Tags: {}</td>\n'.format(" ".join(puzzle['tags'])), #add tags
322 ' <td>Answer: {}</td>\n'.format(solution),
323 ' <td>State: {}</td>\n'.format(puzzle['status']),
330 # Replace all spaces and slashes in the name with underscores
331 underscored = re.sub(r'[ /]', '_', name)
332 file = "{}.html".format(underscored)
338 def puzzle_lists(puzzles, filt):
339 #filt is one of "All", "Solved", "Unsolved" and spits out the appropriate list of puzzles
340 #generates pages for all puzzles, solved puzzles, unsolved puzzles
341 #saves as all/solved/unsolved.html, has a sidebar to link to each other and to the big board
342 solved_puzzles = [puzzle for puzzle in puzzles if puzzle['status'] == 'solved']
343 unsolved_puzzles = [puzzle for puzzle in puzzles if puzzle['status'] != 'solved']
346 ' <link rel="stylesheet" href="overview.css">\n',
347 ' <title>Mystery Hunt 2022</title>\n',
348 ' <script src="sorttable.js"></script>\n',
350 ' <div class="sidenav">\n'
351 ' <a href="index.html">Hunt Overview</a>'
352 ' <a href="all.html">All Puzzles</a>\n'
353 ' <a href="unsolved.html">Unsolved</a>\n'
354 ' <a href="solved.html">Solved</a>\n'
357 ' <h1><b>{}</b></h1>\n'.format('{} Puzzles').format(filt),
358 ' <p>{}</p>\n'.format(link(website + "index.html", 'Hunt Overview')),
360 ' <table class="center sortable">\n',
363 ' <th>Puzzle Title/Slack</th>\n',
364 ' <th>Puzzle Link</th>\n',
366 ' <th>Overview</th>\n',
367 ' <th>Answer</th>\n',
368 ' <th>Round(s)</th>\n',
375 for puzzle in solved_puzzles:
376 if puzzle['type'] == 'meta':
380 slack_url = channel_url(puzzle['channel_id'])
381 round_url = link(website + "_".join(rnd.split()) + "_round.html", puzzle['rounds'][0])
382 #assuming one round per puzzle for now
384 solved_code += [' <tr>\n',
385 ' <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
386 ' <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
387 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
388 ' <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
389 ' <td>{}</td>\n'.format(puzzle['solution']),
390 ' <td>{}</td>\n'.format(round_url),
391 ' <td>{}</td>\n'.format("".join(puzzle['tags'])),
393 for puzzle in unsolved_puzzles:
394 if puzzle['type'] == 'meta':
398 slack_url = channel_url(puzzle['channel_id'])
399 round_url = link(website + "_".join(rnd.split()) + "_round.html", puzzle['rounds'][0])
400 #assuming one round per puzzle for now
402 unsolved_code += [' <tr>\n',
403 ' <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
404 ' <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
405 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
406 ' <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
408 ' <td>{}</td>\n'.format(round_url),
409 ' <td>{}</td>\n'.format("".join(puzzle['tags'])),
411 end = [' </tbody>\n',
419 for line in start + unsolved_code + solved_code + end:
422 elif filt == "Solved":
423 file2 = 'solved.html'
425 for line in start + solved_code + end:
428 elif filt == "Unsolved":
429 file3 = 'unsolved.html'
431 for line in start + unsolved_code + end:
436 # Initialize AWS resources to talk to database
437 db = boto3.resource('dynamodb')
438 table = db.Table("turbot")
439 puzzles, rounds = hunt_info(table, "mh2021")
441 overview(puzzles, rounds)
443 round_overview(rnd, puzzles)
444 for puzzle in puzzles:
445 puzzle_overview(puzzle)
446 puzzle_lists(puzzles, "All")
447 puzzle_lists(puzzles, "Solved")
448 puzzle_lists(puzzles, "Unsolved")