from turbot.blocks import (
input_block, section_block, text_block, multi_select_block
)
-from turbot.hunt import find_hunt_for_hunt_id
+from turbot.hunt import find_hunt_for_hunt_id, hunt_blocks
+from turbot.puzzle import find_puzzle_for_url
import turbot.rot
import turbot.sheets
import turbot.slack
TableName='turbot',
KeySchema=[
{'AttributeName': 'hunt_id', 'KeyType': 'HASH'},
- {'AttributeName': 'SK', 'KeyType': 'RANGE'},
+ {'AttributeName': 'SK', 'KeyType': 'RANGE'}
],
AttributeDefinitions=[
{'AttributeName': 'hunt_id', 'AttributeType': 'S'},
{'AttributeName': 'SK', 'AttributeType': 'S'},
{'AttributeName': 'channel_id', 'AttributeType': 'S'},
{'AttributeName': 'is_hunt', 'AttributeType': 'S'},
+ {'AttributeName': 'url', 'AttributeType': 'S'}
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
}
+ ],
+ LocalSecondaryIndexes = [
+ {
+ 'IndexName': 'url_index',
+ 'KeySchema': [
+ {'AttributeName': 'hunt_id', 'KeyType': 'HASH'},
+ {'AttributeName': 'url', 'KeyType': 'RANGE'},
+ ],
+ 'Projection': {
+ 'ProjectionType': 'ALL'
+ }
+ }
]
)
return submission_error(
This is the modal view presented to the user by the puzzle function
above."""
+ # First, read all the various data from the request
meta = json.loads(metadata)
hunt_id = meta['hunt_id']
rounds = []
new_rounds = state['new_rounds']['new_rounds']['value']
+ # Before doing anything, reject this puzzle if a puzzle already
+ # exists with the same URL.
+ if url:
+ existing = find_puzzle_for_url(turb, hunt_id, url)
+ if existing:
+ return submission_error(
+ "url",
+ "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()
# Add any new rounds to the database
if new_rounds:
for round in new_rounds.split(','):
- rounds += round
+ # Drop any leading/trailing spaces from the round name
+ round = round.strip()
+ # Ignore any empty string
+ if not len(round):
+ continue
+ rounds.append(round)
turb.table.put_item(
Item={
'hunt_id': hunt_id,
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)
if not puzzle:
return bot_reply("Sorry, this is not a puzzle channel.")
+ if not args:
+ return bot_reply(
+ "Error, no solution provided. Usage: `/solved SOLUTION HERE`")
+
# Set the status and solution fields in the database
puzzle['status'] = 'solved'
puzzle['solution'].append(args)
+ if 'state' in puzzle:
+ del puzzle['state']
turb.table.put_item(Item=puzzle)
# Report the solution to the puzzle's channel
return lambda_ok
commands["/solved"] = solved
+
+
+def hunt(turb, body, args):
+ """Implementation of the /hunt command
+
+ The (optional) args string should be one of 'all', 'solved', or
+ 'unsolved' to indicate which set of puzzles should be
+ displayed. If omitted, this command will default to showing only
+ unsolved puzzles.
+ """
+
+ channel_id = body['channel_id'][0]
+ response_url = body['response_url'][0]
+
+ hunt = hunt_for_channel(turb, channel_id)
+
+ if not hunt:
+ return bot_reply("Sorry, this channel doesn't appear to "
+ + "be a hunt or puzzle channel")
+
+ blocks = hunt_blocks(turb, hunt)
+
+ requests.post(response_url,
+ json = { 'blocks': blocks },
+ headers = {'Content-type': 'application/json'}
+ )
+
+ return lambda_ok
+
+commands["/hunt"] = hunt