input_block, section_block, text_block, multi_select_block, checkbox_block
)
from turbot.hunt import find_hunt_for_hunt_id, hunt_blocks
-from turbot.puzzle import find_puzzle_for_url, find_puzzle_for_puzzle_id
+from turbot.puzzle import (
+ find_puzzle_for_url,
+ find_puzzle_for_puzzle_id,
+ puzzle_update_channel_and_sheet,
+ puzzle_id_from_name
+)
import turbot.rot
import turbot.sheets
import turbot.slack
"Puzzle status", "Solved", "solved",
checked=(puzzle.get('status', 'unsolved') == 'solved')),
input_block("Solution", "solution",
- "Solutions (comma-separated if multiple",
+ "Solution(s) (comma-separated if multiple)",
initial_value=solution_str,
optional=True),
]
function above.
"""
+ puzzle={}
+
+ # First, read all the various data from the request
+ meta = json.loads(metadata)
+ puzzle['hunt_id'] = meta['hunt_id']
+ puzzle['SK'] = meta['SK']
+ puzzle['puzzle_id'] = meta['puzzle_id']
+ puzzle['channel_id'] = meta['channel_id']
+ puzzle['channel_url'] = meta['channel_url']
+ puzzle['sheet_url'] = meta['sheet_url']
+
+ state = payload['view']['state']['values']
+
+ puzzle['name'] = state['name']['name']['value']
+ url = state['url']['url']['value']
+ if url:
+ puzzle['url'] = url
+ rounds = [option['value'] for option in
+ state['rounds']['rounds']['selected_options']]
+ if rounds:
+ puzzle['rounds'] = rounds
+ new_rounds = state['new_rounds']['new_rounds']['value']
+ puzzle_state = state['state']['state']['value']
+ if puzzle_state:
+ puzzle['state'] = puzzle_state
+ if state['solved']['solved']['selected_options']:
+ puzzle['status'] = 'solved'
+ else:
+ puzzle['status'] = 'unsolved'
+ puzzle['solution'] = []
+ solution = state['solution']['solution']['value']
+ if solution:
+ puzzle['solution'] = [
+ sol.strip() for sol in solution.split(',')
+ ]
+
+ # Verify that there's a solution if the puzzle is mark solved
+ if puzzle['status'] == 'solved' and not puzzle['solution']:
+ return submission_error("solution",
+ "A solved puzzle requires a solution.")
+
+ if puzzle['status'] == 'unsolved' and puzzle['solution']:
+ return submission_error("solution",
+ "An unsolved puzzle should have no solution.")
+
+ # Add any new rounds to the database
+ if new_rounds:
+ if 'rounds' not in puzzle:
+ puzzle['rounds'] = []
+ for round in new_rounds.split(','):
+ # Drop any leading/trailing spaces from the round name
+ round = round.strip()
+ # Ignore any empty string
+ if not len(round):
+ continue
+ puzzle['rounds'].append(round)
+ turb.table.put_item(
+ Item={
+ 'hunt_id': puzzle['hunt_id'],
+ 'SK': 'round-' + round
+ }
+ )
+
+ # Update the puzzle in the database
+ turb.table.put_item(Item=puzzle)
+
+ # We need to set the channel topic if any of puzzle name, url,
+ # state, status, or solution, has changed. Let's just do that
+ # unconditionally here.
+ puzzle_update_channel_and_sheet(turb, puzzle)
+
return lambda_ok
def new_hunt(turb, payload):
"Error: A puzzle with this URL already exists.")
# Create a Slack-channel-safe puzzle_id
- puzzle_id = re.sub(r'[^a-zA-Z0-9_]', '', name).lower()
+ puzzle_id = puzzle_id_from_name(name)
# Create a channel for the puzzle
hunt_dash_channel = "{}-{}".format(hunt_id, puzzle_id)
return lambda_ok
-# XXX: This duplicates functionality eith events.py:set_channel_description
-def set_channel_topic(turb, puzzle):
- channel_id = puzzle['channel_id']
- name = puzzle['name']
- url = puzzle.get('url', None)
- sheet_url = puzzle.get('sheet_url', None)
- state = puzzle.get('state', None)
- status = puzzle['status']
-
- description = ''
-
- if status == 'solved':
- description += "SOLVED: `{}` ".format('`, `'.join(puzzle['solution']))
-
- description += name
-
- links = []
- if url:
- links.append("<{}|Puzzle>".format(url))
- if sheet_url:
- links.append("<{}|Sheet>".format(sheet_url))
-
- if len(links):
- description += "({})".format(', '.join(links))
-
- if state:
- description += " {}".format(state)
-
- # Slack only allows 250 characters for a topic
- if len(description) > 250:
- description = description[:247] + "..."
-
- turb.slack_client.conversations_setTopic(channel=channel_id,
- topic=description)
-
def state(turb, body, args):
"""Implementation of the /state command
puzzle['state'] = args
turb.table.put_item(Item=puzzle)
- set_channel_topic(turb, puzzle)
+ puzzle_update_channel_and_sheet(turb, puzzle)
return lambda_ok
)
# And update the puzzle's description
- set_channel_topic(turb, puzzle)
-
- # And rename the sheet to suffix with "-SOLVED"
- turbot.sheets.renameSheet(turb, puzzle['sheet_url'],
- puzzle['name'] + "-SOLVED")
-
- # Finally, rename the Slack channel to add the suffix '-solved'
- channel_name = "{}-{}-solved".format(
- puzzle['hunt_id'],
- puzzle['puzzle_id'])
- turb.slack_client.conversations_rename(
- channel=puzzle['channel_id'],
- name=channel_name)
+ puzzle_update_channel_and_sheet(turb, puzzle)
return lambda_ok