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
17 from boto3.dynamodb.conditions import Key
19 website = "https://halibut.cworth.org/"
20 #change this if we're using AWS or some other subdomain instead
22 def channel_url(channel_id):
23 """Given a channel ID, return the URL for that channel."""
25 return "https://halibutthatbass.slack.com/archives/{}".format(channel_id)
27 def find_hunt_for_hunt_id(turb, hunt_id):
28 """Given a hunt ID find the database item for that hunt
30 Returns None if hunt ID is not found, otherwise a
31 dictionary with all fields from the hunt's row in the table,
32 (channel_id, active, hunt_id, name, url, sheet_url, etc.).
34 response = turb.table.get_item(
37 'SK': 'hunt-{}'.format(hunt_id)
40 if 'Item' in response:
41 return response['Item']
45 def hunt_puzzles_for_hunt_id(turb, hunt_id):
46 """Return all puzzles that belong to the given hunt_id"""
48 response = turb.table.query(
49 KeyConditionExpression=(
50 Key('hunt_id').eq(hunt_id) &
51 Key('SK').begins_with('puzzle-')
55 return response['Items']
58 #shortcutting function for creating html links
59 #opens in a new tab for external links
60 return '<a href="{}" target="_blank" rel="noreferrer noopener">{}</a>'.format(lin, text)
63 #internal links, doesn't open new tab
64 return '<a href="{}">{}</a>'.format(lin, text)
66 def hunt_info(turb, hunt):
68 Retrieves list of rounds, puzzles for the given hunt
71 hunt_id = hunt["hunt_id"]
72 channel_id = hunt["channel_id"]
74 puzzles = hunt_puzzles_for_hunt_id(turb, hunt_id)
77 for puzzle in puzzles:
78 if "rounds" not in puzzle:
80 for rnd in puzzle["rounds"]:
85 return puzzles, rounds
87 def round_stat(rnd, puzzles):
88 #Counts puzzles, solved, list of puzzles for a given round
95 for puzzle in puzzles:
96 if "rounds" not in puzzle:
98 if rnd in puzzle["rounds"]:
99 if puzzle['type'] == 'meta':
101 if puzzle['status'] == 'solved':
105 if puzzle['status'] == 'solved':
107 solved_puzzles.append(puzzle)
109 unsolved_puzzles.append(puzzle)
110 solved_puzzles = sorted(solved_puzzles, key = lambda i: i['name'])
111 unsolved_puzzles = sorted(unsolved_puzzles, key = lambda i: i['name'])
112 rnd_puzzles = metas + unsolved_puzzles + solved_puzzles
113 return puzzle_count, solved_count, rnd_puzzles, meta_solved, len(metas)
117 def overview(puzzles, rounds):
118 #big board, main page. saves as index.html
119 start = ['<!DOCTYPE html>\n',
122 ' <meta charset="utf-8">\n',
123 ' <meta name="viewport" content="width=device-width, initial-scale=1">\n',
125 ' <link rel="stylesheet" href="overview.css">\n',
126 ' <script type="text/javascript">\n',
127 ' // Hide all elements with class="containerTab", except for the one that matches the clickable grid column\n',
128 ' function openTab(tabName) {\n',
130 ' x = document.getElementsByClassName("containerTab");\n',
131 ' for (i = 0; i < x.length; i++) {\n',
132 ' x[i].style.display = "none";\n',
134 ' document.getElementById(tabName).style.display = "block";\n',
138 ' <title>Hunt Overview</title>\n',
139 ' <script src="sorttable.js"></script>\n'
141 ' <div class="sidenav">\n'
142 ' <a href="index.html">Hunt Overview</a>'
143 ' <a href="all.html">All Puzzles</a>\n'
144 ' <a href="unsolved.html">Unsolved</a>\n'
145 ' <a href="solved.html">Solved</a>\n'
148 columns = [' <div class="row">\n']
152 puzzle_count, solved_count, rnd_puzzles, meta_solved, metas = round_stat(rnd, puzzles)
153 if metas == meta_solved and metas > 0:
157 columns += [' <div class="column {}" onclick="openTab(\'b{}\');">\n'.format(status, i),
158 ' <p>{}</p>\n'.format(rnd),
159 ' <p>Puzzles: {}/{}</p>\n'.format(solved_count, puzzle_count),
160 ' <p>Metas: {}/{}</p>\n'.format(meta_solved, metas),
164 ' <div id="b{}" class="containerTab {}" style="display:none;">\n'.format(i, status),
165 ' <span onclick="this.parentElement.style.display=\'none\'" class="closebtn">x</span>\n',
166 ' <h2>{}</h2>\n'.format(link(website + "_".join(rnd.split()) + "_round.html", rnd)),
167 ' <table class="sortable">\n',
169 ' <th><u>Puzzle</u></th>\n',
170 ' <th><u>Answer</u></th>\n',
173 for puzzle in rnd_puzzles:
174 if puzzle['type'] == 'meta':
178 if puzzle['status'] == 'solved':
179 expanding += [' <tr class=\'solved\';>\n',
180 ' <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + ".html", puzzle['name']+meta)),
181 ' <td>{}</td>\n'.format(puzzle['solution']),
184 expanding += [' <tr class=\'unsolved\';>\n',
185 ' <td><b>{}</b></td>\n'.format(link(website + "_".join(puzzle['name'].split()) + ".html", puzzle['name']+meta)),
188 expanding.append(' </table>\n')
189 expanding.append(' </div>\n')
191 columns.append(' </div>\n')
192 end = ['</body>\n', '</html>\n']
193 html = start + expanding + columns + end
201 def round_overview(rnd, puzzles):
202 #inputs: round name, puzzles
204 #saves as (round name)_round.html, in case meta/round share names.
205 #underscores replace spaces for links.
206 rnd_puzzles, meta_solved, metas = round_stat(rnd, puzzles)[2:]
207 if meta_solved == metas and metas > 0:
213 ' <link rel="stylesheet" href="individual.css">\n',
214 ' <title>Mystery Hunt 2022</title>\n',
215 ' <script src="sorttable.js"></script>\n',
217 ' <body class="{}">\n'.format(status),
218 ' <h1><b>{}</b></h1>\n'.format(rnd),
219 ' <p>{}</p>\n'.format(link(website + "index.html", 'Hunt Overview')),
221 ' <table class="center sortable">\n',
224 ' <th>Puzzle Title/Slack</th>\n',
225 ' <th>Puzzle Link</th>\n',
227 ' <th>Overview</th>\n',
228 ' <th>Answer</th>\n',
229 #' <th>Extra Links</th>\n',
235 for puzzle in rnd_puzzles:
236 if puzzle['type'] == 'meta':
240 slack_url = channel_url(puzzle['channel_id'])
242 if puzzle['status'] == 'solved':
243 puzzle_list += [ ' <tr>\n',
244 ' <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
245 ' <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
246 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
247 ' <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
248 ' <td>{}</td>\n'.format(puzzle['solution']),
250 ' <td>{}</td>\n'.format("".join(puzzle['tags'])),
253 puzzle_list += [ ' <tr>\n',
254 ' <td><b>{}</b></td>\n'.format(elink(slack_url, puzzle['name']+meta)),
255 ' <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
256 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
257 ' <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
260 ' <td>{}</td>\n'.format(" ".join(puzzle['tags'])),
262 end = [' </tbody>\n',
267 html = start + puzzle_list + end
268 file = "{}_round.html".format('_'.join(rnd.split()))
275 def puzzle_overview(puzzle):
276 #overview page for individual puzzles. saves as (name of puzzle).html,
277 #with underscores rather than spaces
278 name = puzzle['name']
279 if puzzle['type'] == 'meta':
283 slack_url = channel_url(puzzle['channel_id'])
284 round_url = [link(website + "_".join(rnd.split()) + "_round.html", rnd) for rnd in puzzle['rounds']]
285 if puzzle['status'] == 'solved':
286 solution = puzzle['solution']
291 html = ['<!DOCTYPE html>\n',
294 ' <meta charset="utf-8">\n',
295 ' <meta name="viewport" content="width=device-width, initial-scale=1">\n',
296 ' <link rel="stylesheet" href="individual.css">\n',
297 ' <title>{}</title>\n'.format(name+meta),
298 ' <p>{}</p>'.format(link(website + 'index.html', 'Hunt Overview')),
301 '<body class="{}">\n'.format(status),
302 ' <h1>{}</h1>\n'.format(name+meta),
304 ' <table class="center">\n',
306 ' <td>{}</td>\n'.format(elink(slack_url, 'Channel')), #slack channel
307 ' <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
308 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
309 ' <td>Additional Resources</td>\n',
312 ' <table class="center">\n',
314 ' <td>Round(s): {}</td>\n'.format(" ".join(round_url)), #round page on our site
315 ' <td>Tags: {}</td>\n'.format(" ".join(puzzle['tags'])), #add tags
318 ' <td>Answer: {}</td>\n'.format(solution),
319 ' <td>State: {}</td>\n'.format(puzzle['status']),
326 underscored = "_".join(name.split())
327 file = "{}.html".format(underscored)
333 def puzzle_lists(puzzles, filt):
334 #filt is one of "All", "Solved", "Unsolved" and spits out the appropriate list of puzzles
335 #generates pages for all puzzles, solved puzzles, unsolved puzzles
336 #saves as all/solved/unsolved.html, has a sidebar to link to each other and to the big board
337 solved_puzzles = [puzzle for puzzle in puzzles if puzzle['status'] == 'solved']
338 unsolved_puzzles = [puzzle for puzzle in puzzles if puzzle['status'] != 'solved']
341 ' <link rel="stylesheet" href="overview.css">\n',
342 ' <title>Mystery Hunt 2022</title>\n',
343 ' <script src="sorttable.js"></script>\n',
345 ' <div class="sidenav">\n'
346 ' <a href="index.html">Hunt Overview</a>'
347 ' <a href="all.html">All Puzzles</a>\n'
348 ' <a href="unsolved.html">Unsolved</a>\n'
349 ' <a href="solved.html">Solved</a>\n'
352 ' <h1><b>{}</b></h1>\n'.format('{} Puzzles').format(filt),
353 ' <p>{}</p>\n'.format(link(website + "index.html", 'Hunt Overview')),
355 ' <table class="center sortable">\n',
358 ' <th>Puzzle Title/Slack</th>\n',
359 ' <th>Puzzle Link</th>\n',
361 ' <th>Overview</th>\n',
362 ' <th>Answer</th>\n',
363 ' <th>Round(s)</th>\n',
370 for puzzle in solved_puzzles:
371 if puzzle['type'] == 'meta':
375 slack_url = channel_url(puzzle['channel_id'])
376 round_url = link(website + "_".join(rnd.split()) + "_round.html", puzzle['rounds'][0])
377 #assuming one round per puzzle for now
379 solved_code += [' <tr>\n',
380 ' <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
381 ' <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
382 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
383 ' <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
384 ' <td>{}</td>\n'.format(puzzle['solution']),
385 ' <td>{}</td>\n'.format(round_url),
386 ' <td>{}</td>\n'.format("".join(puzzle['tags'])),
388 for puzzle in unsolved_puzzles:
389 if puzzle['type'] == 'meta':
393 slack_url = channel_url(puzzle['channel_id'])
394 round_url = link(website + "_".join(rnd.split()) + "_round.html", puzzle['rounds'][0])
395 #assuming one round per puzzle for now
397 unsolved_code += [' <tr>\n',
398 ' <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
399 ' <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
400 ' <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
401 ' <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
403 ' <td>{}</td>\n'.format(round_url),
404 ' <td>{}</td>\n'.format("".join(puzzle['tags'])),
406 end = [' </tbody>\n',
414 for line in start + unsolved_code + solved_code + end:
417 elif filt == "Solved":
418 file2 = 'solved.html'
420 for line in start + solved_code + end:
423 elif filt == "Unsolved":
424 file3 = 'unsolved.html'
426 for line in start + unsolved_code + end:
433 puzzles, rounds = hunt_info(turb, hunt)
434 #I am not sure where these come from
435 overview(puzzles, rounds)
437 round_overview(rnd, puzzles)
438 for puzzle in puzzles:
439 puzzle_overview(puzzle)
440 puzzle_lists(puzzles, "All")
441 puzzle_lists(puzzles, "Solved")
442 puzzle_lists(puzzles, "Unsolved")