+ solution_str,
+ ', '.join(links), rounds_str,
+ extra_str
+ )
+
+ # Combining hunt ID and puzzle ID together here is safe because
+ # hunt_id is restricted to not contain a hyphen, (see
+ # valid_id_re in interaction.py)
+ hunt_and_sort_key = "{}-{}".format(puzzle['hunt_id'], puzzle['SK'])
+
+ return [
+ accessory_block(
+ section_block(text_block(puzzle_text)),
+ button_block("✏", "edit_puzzle", hunt_and_sort_key)
+ )
+ ]
+
+def puzzle_matches_one(puzzle, pattern):
+ """Returns True if this puzzle matches the given string (regexp)
+
+ A match will be considered on any of puzzle title, round title,
+ puzzle URL, puzzle state, puzzle type, tags, or solution
+ string. The string can include regular expression syntax. Matching
+ is case insensitive.
+ """
+
+ p = re.compile('.*'+pattern+'.*', re.IGNORECASE)
+
+ if p.match(puzzle['name']):
+ return True
+
+ if 'rounds' in puzzle:
+ for round in puzzle['rounds']:
+ if p.match(round):
+ return True
+
+ if 'url' in puzzle:
+ if p.match(puzzle['url']):
+ return True
+
+ if 'state' in puzzle:
+ if p.match(puzzle['state']):
+ return True
+
+ if 'type' in puzzle:
+ if p.match(puzzle['type']):
+ return True
+
+ if 'solution' in puzzle:
+ for solution in puzzle['solution']:
+ if p.match(solution):
+ return True
+
+ if 'tags' in puzzle:
+ for tag in puzzle['tags']:
+ if p.match(tag):
+ return True
+
+ return False
+
+def puzzle_matches_all(puzzle, patterns):
+ """Returns True if this puzzle matches all of the given list of patterns
+
+ A match will be considered on any of puzzle title, round title,
+ puzzle URL, puzzle state, puzzle types, tags, or solution
+ string. All patterns must match the puzzle somewhere, (that is,
+ there is an implicit logical AND between patterns). Patterns can
+ include regular expression syntax. Matching is case insensitive.
+
+ """
+
+ for pattern in patterns:
+ if not puzzle_matches_one(puzzle, pattern):
+ return False
+
+ return True
+
+def puzzle_id_from_name(name):
+ return re.sub(r'[^a-zA-Z0-9_]', '', name).lower()
+
+def puzzle_sort_key(puzzle):
+ """Return an appropriate sort key for a puzzle in the database
+
+ The sort key must start with "puzzle-" to distinguish puzzle items
+ in the database from all non-puzzle items. After that, though, the
+ only requirements are that each puzzle have a unique key and they
+ give us the ordering we want. And for ordering, we want meta puzzles
+ before non-meta puzzles and then alphabetical order by name within
+ each of those groups.
+
+ So puting a "-meta-" prefix in front of the puzzle ID does the trick.
+ """
+
+ return "puzzle-{}{}".format(
+ "-meta-" if puzzle['type'] == "meta" else "",
+ puzzle['puzzle_id']
+ )
+
+def puzzle_channel_topic(puzzle):
+ """Compute the channel topic for a puzzle"""
+
+ topic = ''
+
+ if puzzle['status'] == 'solved':
+ topic += "SOLVED: `{}` ".format('`, `'.join(puzzle['solution']))
+
+ links = []
+
+ url = puzzle.get('url', None)
+ if url:
+ links.append("<{}|Puzzle>".format(url))
+
+ sheet_url = puzzle.get('sheet_url', None)
+ if sheet_url:
+ links.append("<{}|Sheet>".format(sheet_url))
+
+ if len(links):
+ topic += "({}) ".format(', '.join(links))
+
+ tags = puzzle.get('tags', [])
+ if tags:
+ topic += " {}".format(" ".join(["`{}`".format(t) for t in tags]))
+
+ state = puzzle.get('state', None)
+ if state:
+ topic += " {}".format(state)
+
+ return topic
+
+def puzzle_channel_description(puzzle):
+ """Compute the channel description for a puzzle"""
+
+ url = puzzle.get('url', None)
+ sheet_url = puzzle.get('sheet_url', None)
+ tags = puzzle.get('tags', [])
+ state = puzzle.get('state', None)
+
+ description = (
+ "Discussion to solve the puzzle \"{}\".\n".format(puzzle['name'])
+ )
+
+ if url:
+ description += "See the <{}|Original puzzle>\n".format(url)
+
+ if sheet_url:
+ description += (
+ "Actual solving work takes place in the "
+ + "<{}|shared spreadsheet>\n".format(sheet_url)
+ )
+
+ if tags:
+ description += "This puzzle has the following tags: {}\n".format(
+ " ".join(["`{}`".format(t) for t in tags]))
+
+ if state:
+ description += "This puzzle has a state of: {}\n".format(state)
+
+ description += (
+ "You can see a summary of this information at any point "
+ + "by issuing the `/puzzle` command and you can edit any of "
+ + "this information by issuing the `/edit` command"