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
22 WEBROOT = "/srv/halibut.cworth.org/www"
24 website = "https://halibut.cworth.org/"
25 #change this if we're using AWS or some other subdomain instead
27 def hunt_file(hunt, name):
28 """Return a path file 'name' within the given hunt.
30 This will be withing WEBROOT and in a hunt-specific path."""
32 return "{}/{}/{}".format(WEBROOT, hunt['channel_id'], name)
34 def filename_from_name(name):
35 """Returns a string derived from name, but with all spaces and slashes
36 replaced with underscores, (for making a clean filename)"""
37 return re.sub(r'[ /]', '_', name)
39 def channel_url(channel_id):
40 """Given a channel ID, return the URL for that channel."""
42 return "https://halibutthatbass.slack.com/archives/{}".format(channel_id)
44 def find_hunt_for_hunt_id(table, hunt_id):
45 """Given a hunt ID find the database item for that hunt
47 Returns None if hunt ID is not found, otherwise a
48 dictionary with all fields from the hunt's row in the table,
49 (channel_id, active, hunt_id, name, url, sheet_url, etc.).
51 response = table.get_item(
54 'SK': 'hunt-{}'.format(hunt_id)
57 if 'Item' in response:
58 return response['Item']
62 def hunt_puzzles_for_hunt_id(table, hunt_id):
63 """Return all puzzles that belong to the given hunt_id"""
65 response = table.query(
66 KeyConditionExpression=(
67 Key('hunt_id').eq(hunt_id) &
68 Key('SK').begins_with('puzzle-')
72 return response['Items']
75 #shortcutting function for creating html links
76 #opens in a new tab for external links
77 return '<a href="{}" target="_blank" rel="noreferrer noopener">{}</a>'.format(lin, text)
80 #internal links, doesn't open new tab
81 return '<a href="{}">{}</a>'.format(lin, text)
83 def hunt_info(table, hunt_id):
85 Retrieves list of rounds, puzzles for the given hunt
88 hunt = find_hunt_for_hunt_id(table, hunt_id)
91 channel_id = hunt["channel_id"]
93 puzzles = hunt_puzzles_for_hunt_id(table, hunt_id)
96 for puzzle in puzzles:
97 if "rounds" not in puzzle:
99 for rnd in puzzle["rounds"]:
101 rounds = list(rounds)
104 return hunt, puzzles, rounds
106 def round_stat(rnd, puzzles):
107 #Counts puzzles, solved, list of puzzles for a given round
111 unsolved_puzzles = []
114 for puzzle in puzzles:
115 if "rounds" not in puzzle:
117 if rnd in puzzle["rounds"]:
118 if puzzle['type'] == 'meta':
120 if puzzle['status'] == 'solved':
124 if puzzle['status'] == 'solved':
126 solved_puzzles.append(puzzle)
128 unsolved_puzzles.append(puzzle)
129 solved_puzzles = sorted(solved_puzzles, key = lambda i: i['name'])
130 unsolved_puzzles = sorted(unsolved_puzzles, key = lambda i: i['name'])
131 rnd_puzzles = metas + unsolved_puzzles + solved_puzzles
132 return puzzle_count, solved_count, rnd_puzzles, meta_solved, len(metas)
136 def overview(hunt, puzzles, rounds):
137 #big board, main page. saves as index.html
138 start = ['<!DOCTYPE html>\n',
141 ' <meta charset="utf-8">\n',
142 ' <meta name="viewport" content="width=device-width, initial-scale=1">\n',
144 ' <link rel="stylesheet" href="/overview.css">\n',
145 ' <script type="text/javascript">\n',
146 ' // Hide all elements with class="containerTab", except for the one that matches the clickable grid column\n',
147 ' function openTab(tabName) {\n',
149 ' x = document.getElementsByClassName("containerTab");\n',
150 ' for (i = 0; i < x.length; i++) {\n',
151 ' x[i].style.display = "none";\n',
153 ' document.getElementById(tabName).style.display = "block";\n',
157 ' <title>Hunt Overview</title>\n',
158 ' <script src="/sorttable.js"></script>\n'
160 ' <div class="sidenav">\n'
161 ' <a href="index.html">Hunt Overview</a>'
162 ' <a href="all.html">All Puzzles</a>\n'
163 ' <a href="unsolved.html">Unsolved</a>\n'
164 ' <a href="solved.html">Solved</a>\n'
167 columns = [' <div class="row">\n']
171 puzzle_count, solved_count, rnd_puzzles, meta_solved, metas = round_stat(rnd, puzzles)
172 if metas == meta_solved and metas > 0:
176 columns += [' <div class="column {}" onclick="openTab(\'b{}\');">\n'.format(status, i),
177 ' <p>{}</p>\n'.format(rnd),
178 ' <p>Puzzles: {}/{}</p>\n'.format(solved_count, puzzle_count),
179 ' <p>Metas: {}/{}</p>\n'.format(meta_solved, metas),
183 ' <div id="b{}" class="containerTab {}" style="display:none;">\n'.format(i, status),
184 ' <span onclick="this.parentElement.style.display=\'none\'" class="closebtn">x</span>\n',
185 ' <h2>{}</h2>\n'.format(link(website + filename_from_name(rnd) + "_round.html", rnd)),
186 ' <table class="sortable">\n',
188 ' <th><u>Puzzle</u></th>\n',
189 ' <th><u>Answer</u></th>\n',
192 for puzzle in rnd_puzzles:
193 if puzzle['type'] == 'meta':
197 if puzzle['status'] == 'solved':
198 expanding += [' <tr class=\'solved\';>\n',
199 ' <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + ".html", puzzle['name']+meta)),
200 ' <td>{}</td>\n'.format(puzzle['solution']),
203 expanding += [' <tr class=\'unsolved\';>\n',
204 ' <td><b>{}</b></td>\n'.format(link(website + filename_from_name(puzzle['name']) + ".html", puzzle['name']+meta)),
207 expanding.append(' </table>\n')
208 expanding.append(' </div>\n')
210 columns.append(' </div>\n')
211 end = ['</body>\n', '</html>\n']
212 html = start + expanding + columns + end
213 file = hunt_file(hunt, "index.html")
220 def round_overview(hunt, rnd, puzzles):
221 #inputs: round name, puzzles
223 #saves as (round name)_round.html, in case meta/round share names.
224 #underscores replace spaces for links.
225 rnd_puzzles, meta_solved, metas = round_stat(rnd, puzzles)[2:]
226 if meta_solved == metas and metas > 0:
232 ' <link rel="stylesheet" href="/individual.css">\n',
233 ' <title>Mystery Hunt 2022</title>\n',
234 ' <script src="/sorttable.js"></script>\n',
236 ' <body class="{}">\n'.format(status),
237 ' <h1><b>{}</b></h1>\n'.format(rnd),
238 ' <p>{}</p>\n'.format(link(website + "index.html", 'Hunt Overview')),
240 ' <table class="center sortable">\n',
243 ' <th>Puzzle Title/Slack</th>\n',
244 ' <th>Puzzle Link</th>\n',
246 ' <th>Overview</th>\n',
247 ' <th>Answer</th>\n',
248 #' <th>Extra Links</th>\n',
254 for puzzle in rnd_puzzles:
255 if puzzle['type'] == 'meta':
259 slack_url = channel_url(puzzle['channel_id'])
261 if puzzle['status'] == 'solved':
262 puzzle_list += [ ' <tr>\n',
263 ' <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
264 ' <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
265 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
266 ' <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + '.html', 'Overview')),
267 ' <td>{}</td>\n'.format(puzzle['solution']),
269 ' <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
272 puzzle_list += [ ' <tr>\n',
273 ' <td><b>{}</b></td>\n'.format(elink(slack_url, puzzle['name']+meta)),
274 ' <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
275 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
276 ' <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + '.html', 'Overview')),
279 ' <td>{}</td>\n'.format(" ".join(puzzle.get('tags',[]))),
281 end = [' </tbody>\n',
286 html = start + puzzle_list + end
287 file = hunt_file(hunt, "{}_round.html".format(filename_from_name(rnd)))
294 def puzzle_overview(hunt, puzzle):
295 #overview page for individual puzzles. saves as (name of puzzle).html,
296 #with underscores rather than spaces
297 name = puzzle['name']
298 if puzzle['type'] == 'meta':
302 slack_url = channel_url(puzzle['channel_id'])
303 if 'rounds' in puzzle:
304 round_url = [link(website + filename_from_name(rnd) + "_round.html", rnd) for rnd in puzzle['rounds']]
307 if puzzle['status'] == 'solved':
308 solution = puzzle['solution']
313 html = ['<!DOCTYPE html>\n',
316 ' <meta charset="utf-8">\n',
317 ' <meta name="viewport" content="width=device-width, initial-scale=1">\n',
318 ' <link rel="stylesheet" href="/individual.css">\n',
319 ' <title>{}</title>\n'.format(name+meta),
320 ' <p>{}</p>'.format(link(website + 'index.html', 'Hunt Overview')),
323 '<body class="{}">\n'.format(status),
324 ' <h1>{}</h1>\n'.format(name+meta),
326 ' <table class="center">\n',
328 ' <td>{}</td>\n'.format(elink(slack_url, 'Channel')), #slack channel
329 ' <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
330 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
331 ' <td>Additional Resources</td>\n',
334 ' <table class="center">\n',
336 ' <td>Round(s): {}</td>\n'.format(" ".join(round_url)), #round page on our site
337 ' <td>Tags: {}</td>\n'.format(" ".join(puzzle.get('tags',[]))), #add tags
340 ' <td>Answer: {}</td>\n'.format(solution),
341 ' <td>State: {}</td>\n'.format(puzzle['status']),
348 file = hunt_file(hunt, "{}.html".format(filename_from_name(name)))
354 def puzzle_lists(hunt, puzzles, filt):
355 #filt is one of "All", "Solved", "Unsolved" and spits out the appropriate list of puzzles
356 #generates pages for all puzzles, solved puzzles, unsolved puzzles
357 #saves as all/solved/unsolved.html, has a sidebar to link to each other and to the big board
358 solved_puzzles = [puzzle for puzzle in puzzles if puzzle['status'] == 'solved']
359 unsolved_puzzles = [puzzle for puzzle in puzzles if puzzle['status'] != 'solved']
362 ' <link rel="stylesheet" href="/overview.css">\n',
363 ' <title>Mystery Hunt 2022</title>\n',
364 ' <script src="/sorttable.js"></script>\n',
366 ' <div class="sidenav">\n'
367 ' <a href="index.html">Hunt Overview</a>'
368 ' <a href="all.html">All Puzzles</a>\n'
369 ' <a href="unsolved.html">Unsolved</a>\n'
370 ' <a href="solved.html">Solved</a>\n'
373 ' <h1><b>{}</b></h1>\n'.format('{} Puzzles').format(filt),
374 ' <p>{}</p>\n'.format(link(website + "index.html", 'Hunt Overview')),
376 ' <table class="center sortable">\n',
379 ' <th>Puzzle Title/Slack</th>\n',
380 ' <th>Puzzle Link</th>\n',
382 ' <th>Overview</th>\n',
383 ' <th>Answer</th>\n',
384 ' <th>Round(s)</th>\n',
391 for puzzle in solved_puzzles:
392 if puzzle['type'] == 'meta':
396 slack_url = channel_url(puzzle['channel_id'])
397 if 'rounds' in puzzle:
398 round_url = link(website + filename_from_name(rnd) + "_round.html", puzzle['rounds'][0])
401 #assuming one round per puzzle for now
403 solved_code += [' <tr>\n',
404 ' <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
405 ' <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
406 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
407 ' <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + '.html', 'Overview')),
408 ' <td>{}</td>\n'.format(puzzle['solution']),
409 ' <td>{}</td>\n'.format(round_url),
410 ' <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
412 for puzzle in unsolved_puzzles:
413 if puzzle['type'] == 'meta':
417 slack_url = channel_url(puzzle['channel_id'])
418 if 'rounds' in puzzle:
419 round_url = link(website + filename_from_name(rnd) + "_round.html", puzzle['rounds'][0])
422 #assuming one round per puzzle for now
424 unsolved_code += [' <tr>\n',
425 ' <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
426 ' <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
427 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
428 ' <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + '.html', 'Overview')),
430 ' <td>{}</td>\n'.format(round_url),
431 ' <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
433 end = [' </tbody>\n',
439 file1 = hunt_file(hunt, 'all.html')
441 for line in start + unsolved_code + solved_code + end:
444 elif filt == "Solved":
445 file2 = hunt_file(hunt, 'solved.html')
447 for line in start + solved_code + end:
450 elif filt == "Unsolved":
451 file3 = hunt_file(hunt, 'unsolved.html')
453 for line in start + unsolved_code + end:
458 # Initialize AWS resources to talk to database
459 db = boto3.resource('dynamodb')
460 table = db.Table("turbot")
461 hunt, puzzles, rounds = hunt_info(table, "mh2021")
463 # Create a directory for the hunt in the WEBROOT
464 root = hunt_file(hunt, "")
467 except FileExistsError:
468 # We're happy as a clam if the directory already exists
471 overview(hunt, puzzles, rounds)
473 round_overview(hunt, rnd, puzzles)
474 for puzzle in puzzles:
475 puzzle_overview(hunt, puzzle)
476 puzzle_lists(hunt, puzzles, "All")
477 puzzle_lists(hunt, puzzles, "Solved")
478 puzzle_lists(hunt, puzzles, "Unsolved")