from slack.errors import SlackApiError
-from turbot.blocks import input_block, section_block, text_block
+from turbot.blocks import (
+ input_block, section_block, text_block, multi_select_block
+)
from turbot.hunt import find_hunt_for_hunt_id
import turbot.rot
import turbot.sheets
# we expect this to be a puzzle channel instead
return find_hunt_for_hunt_id(turb, entry['hunt_id'])
+# python3.9 has a built-in removeprefix but AWS only has python3.8
+def remove_prefix(text, prefix):
+ if text.startswith(prefix):
+ return text[len(prefix):]
+ return text
+
+def hunt_rounds(turb, hunt_id):
+ """Returns array of strings giving rounds that exist in the given hunt"""
+
+ response = turb.table.query(
+ KeyConditionExpression=(
+ Key('hunt_id').eq(hunt_id) &
+ Key('SK').begins_with('round-')
+ )
+ )
+
+ if response['Count'] == 0:
+ return []
+
+ return [remove_prefix(option['SK'], 'round-')
+ for option in response['Items']]
+
def puzzle(turb, body, args):
"""Implementation of the /puzzle command
return bot_reply("Sorry, this channel doesn't appear to "
+ "be a hunt or puzzle channel")
+ round_options = hunt_rounds(turb, hunt['hunt_id'])
+
+ if len(round_options):
+ round_options_block = [
+ multi_select_block("Round(s)", "rounds",
+ "Existing round(s) this puzzle belongs to",
+ round_options)
+ ]
+ else:
+ round_options_block = []
+
view = {
"type": "modal",
"private_metadata": json.dumps({
section_block(text_block("*For {}*".format(hunt['name']))),
input_block("Puzzle name", "name", "Name of the puzzle"),
input_block("Puzzle URL", "url", "External URL of puzzle",
+ optional=True),
+ * round_options_block,
+ input_block("New round(s)", "new_rounds",
+ "New round(s) this puzzle belongs to " +
+ "(comma separated)",
optional=True)
]
}
state = payload['view']['state']['values']
name = state['name']['name']['value']
url = state['url']['url']['value']
+ if 'rounds' in state:
+ rounds = [option['value'] for option in
+ state['rounds']['rounds']['selected_options']]
+ else:
+ rounds = []
+ new_rounds = state['new_rounds']['new_rounds']['value']
# Create a Slack-channel-safe puzzle_id
puzzle_id = re.sub(r'[^a-zA-Z0-9_]', '', name).lower()
channel_id = response['channel']['id']
+ # Add any new rounds to the database
+ if new_rounds:
+ for round in new_rounds.split(','):
+ rounds += round
+ turb.table.put_item(
+ Item={
+ 'hunt_id': hunt_id,
+ 'SK': 'round-' + round
+ }
+ )
+
# Insert the newly-created puzzle into the database
item={
"hunt_id": hunt_id,
}
if url:
item['url'] = url
+ if rounds:
+ item['rounds'] = rounds
turb.table.put_item(Item=item)
return lambda_ok