puzzle_update_channel_and_sheet,
puzzle_id_from_name,
puzzle_blocks,
- puzzle_sort_key
+ puzzle_sort_key,
+ puzzle_copy
)
from turbot.round import round_quoted_puzzles_titles_answers
import turbot.rot
actions['multi_static_select'] = {"*": multi_static_select}
+def edit(turb, body, args):
+
+ """Implementation of the `/edit` command
+
+ To edit the puzzle for the current channel.
+
+ This is simply a shortcut for `/puzzle edit`.
+ """
+
+ return edit_puzzle_command(turb, body)
+
+commands["/edit"] = edit
+
+
def edit_puzzle_command(turb, body):
"""Implementation of the `/puzzle edit` command
return edit_puzzle(turb, puzzle, trigger_id)
- return lambda_ok
-
def edit_puzzle_button(turb, payload):
"""Handler for the action of user pressing an edit_puzzle button"""
return lambda_ok
-def new_hunt(turb, payload):
+def new_hunt_command(turb, body):
+ """Implementation of the '/hunt new' command
+
+ As dispatched from the hunt() function.
+ """
+
+ trigger_id = body['trigger_id'][0]
+
+ return new_hunt(turb, trigger_id)
+
+def new_hunt_button(turb, payload):
"""Handler for the action of user pressing the new_hunt button"""
+ trigger_id = payload['trigger_id']
+
+ return new_hunt(turb, trigger_id)
+
+def new_hunt(turb, trigger_id):
+ """Common code for implementing a new hunt dialog
+
+ This implementation is common whether the operations was invoked
+ by a button (new_hunt_button) or a command (new_hunt_command).
+ """
+
view = {
"type": "modal",
"private_metadata": json.dumps({}),
],
}
- result = turb.slack_client.views_open(trigger_id=payload['trigger_id'],
+ result = turb.slack_client.views_open(trigger_id=trigger_id,
view=view)
if (result['ok']):
submission_handlers[result['view']['id']] = new_hunt_submission
# For a meta puzzle, also display the titles and solutions for all
# puzzles in the same round.
- if puzzle['type'] == 'meta':
+ if puzzle.get('type', 'plain') == 'meta':
puzzles = hunt_puzzles_for_hunt_id(turb, puzzle['hunt_id'])
# Drop this puzzle itself from the report
commands["/puzzle"] = puzzle
+def new(turb, body, args):
+ """Implementation of the `/new` command
+
+ To create a new puzzle.
+
+ This is simply a shortcut for `/puzzle new`.
+ """
+
+ return new_puzzle(turb, body)
+
+commands["/new"] = new
+
def new_puzzle(turb, body):
"""Implementation of the "/puzzle new" command
return bot_reply(
"Sorry, the /state command only works in a puzzle channel")
- # Make a copy of the puzzle object
- puzzle = old_puzzle.copy()
+ # Make a deep copy of the puzzle object
+ puzzle = puzzle_copy(old_puzzle)
# Update the puzzle in the database
puzzle['state'] = args
commands["/state"] = state
+def tag(turb, body, args):
+ """Implementation of the `/tag` command.
+
+ Arg is either a tag to add (optionally prefixed with '+'), or if
+ prefixed with '-' is a tag to remove.
+ """
+
+ if not args:
+ return bot_reply("Usage: `/tag [+]TAG_TO_ADD` "
+ + "or `/tag -TAG_TO_REMOVE`.")
+
+ channel_id = body['channel_id'][0]
+
+ old_puzzle = puzzle_for_channel(turb, channel_id)
+
+ if not old_puzzle:
+ return bot_reply(
+ "Sorry, the /tag command only works in a puzzle channel")
+
+ if args[0] == '-':
+ tag = args[1:]
+ action = 'remove'
+ else:
+ tag = args
+ if tag[0] == '+':
+ tag = tag[1:]
+ action = 'add'
+
+ # Force tag to all uppercase
+ tag = tag.upper()
+
+ # Reject a tag that is not alphabetic or underscore A-Z_
+ if not re.match(r'^[A-Z0-9_]*$', tag):
+ return bot_reply("Sorry, tags can only contain letters, numbers, "
+ + "and the underscore character.")
+
+ if action == 'remove':
+ if 'tags' not in old_puzzle or tag not in old_puzzle['tags']:
+ return bot_reply("Nothing to do. This puzzle is not tagged "
+ + "with the tag: {}".format(tag))
+ else:
+ if 'tags' in old_puzzle and tag in old_puzzle['tags']:
+ return bot_reply("Nothing to do. This puzzle is already tagged "
+ + "with the tag: {}".format(tag))
+
+ # OK. Error checking is done. Let's get to work
+
+ # Make a deep copy of the puzzle object
+ puzzle = puzzle_copy(old_puzzle)
+
+ if action == 'remove':
+ puzzle['tags'] = [t for t in puzzle['tags'] if t != tag]
+ else:
+ if 'tags' not in puzzle:
+ puzzle['tags'] = [tag]
+ else:
+ puzzle['tags'].append(tag)
+
+ turb.table.put_item(Item=puzzle)
+
+ puzzle_update_channel_and_sheet(turb, puzzle, old_puzzle=old_puzzle)
+
+ return lambda_ok
+
+commands["/tag"] = tag
+
def solved(turb, body, args):
"""Implementation of the /solved command
The args string should be a confirmed solution."""
channel_id = body['channel_id'][0]
- user_name = body['user_name'][0]
+ user_id = body['user_id'][0]
old_puzzle = puzzle_for_channel(turb, channel_id)
return bot_reply(
"Error, no solution provided. Usage: `/solved SOLUTION HERE`")
- # Make a copy of the puzzle object
- puzzle = old_puzzle.copy()
+ # Make a deep copy of the puzzle object
+ puzzle = puzzle_copy(old_puzzle)
# Set the status and solution fields in the database
puzzle['status'] = 'solved'
# Report the solution to the puzzle's channel
slack_send_message(
turb.slack_client, channel_id,
- "Puzzle mark solved by {}: `{}`".format(user_name, args))
+ "Puzzle mark solved by <@{}>: `{}`".format(user_id, args))
# Also report the solution to the hunt channel
hunt = find_hunt_for_hunt_id(turb, puzzle['hunt_id'])
character in a term). All terms must match on a puzzle in order
for that puzzle to be included. But a puzzle will be considered to
match if any of the puzzle title, round title, puzzle URL, puzzle
- state, or puzzle solution match. Matching will be performed
- without regard to case sensitivity and the search terms can
- include regular expression syntax.
+ state, puzzle type, tags, or puzzle solution match. Matching will
+ be performed without regard to case sensitivity and the search
+ terms can include regular expression syntax.
+
"""
channel_id = body['channel_id'][0]
response_url = body['response_url'][0]
+ # First, farm off "/hunt new" as a separate command
+ if args == "new":
+ return new_hunt_command(turb, body)
+
terms = None
if args:
# The first word can be a puzzle status and all remaining word