]> git.cworth.org Git - turbot/commitdiff
Add a /tag command to add or remove tags from puzzle
authorCarl Worth <cworth@cworth.org>
Sat, 9 Jan 2021 13:04:28 +0000 (05:04 -0800)
committerCarl Worth <cworth@cworth.org>
Sat, 9 Jan 2021 13:35:42 +0000 (05:35 -0800)
And put the tag values everywhere we have the state string already.

turbot/interaction.py
turbot/puzzle.py

index 7f93c040ce8c4a5406adecf80104ab84c8e47d27..fb11599034fc1f518a1ec7a19a7d54ade43862be 100644 (file)
@@ -82,6 +82,7 @@ def multi_static_select(turb, payload):
 actions['multi_static_select'] = {"*": multi_static_select}
 
 def edit(turb, body, args):
 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.
     """Implementation of the `/edit` command
 
     To edit the puzzle for the current channel.
@@ -893,6 +894,72 @@ def state(turb, body, args):
 
 commands["/state"] = state
 
 
 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-Z_]*$', tag):
+        return bot_reply("Sorry, tags can only contain letters "
+                         + "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 copy of the puzzle object
+    puzzle = old_puzzle.copy()
+
+    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
 
 def solved(turb, body, args):
     """Implementation of the /solved command
 
index b86a529ed47b9d5c64e6d4660104bcbfeb58a0e9..e3a4f741580f711d0c36df9f459a265fd364b266 100644 (file)
@@ -62,6 +62,7 @@ def puzzle_blocks(puzzle, include_rounds=False):
     url = puzzle.get('url', None)
     sheet_url = puzzle.get('sheet_url', None)
     state = puzzle.get('state', None)
     url = puzzle.get('url', None)
     sheet_url = puzzle.get('sheet_url', None)
     state = puzzle.get('state', None)
+    tags = puzzle.get('tags', [])
     status_emoji = ''
     solution_str = ''
 
     status_emoji = ''
     solution_str = ''
 
@@ -85,7 +86,15 @@ def puzzle_blocks(puzzle, include_rounds=False):
 
     state_str = ''
     if state:
 
     state_str = ''
     if state:
-        state_str = "\n{}".format(state)
+        state_str = " State: {}".format(state)
+
+    tags_str = ''
+    if tags:
+        tags_str = " Tags: "+" ".join(["`{}`".format(tag) for tag in tags])
+
+    extra_str = ''
+    if state_str or tags_str:
+        extra_str = "\n{}{}".format(tags_str, state_str)
 
     rounds_str = ''
     if include_rounds and 'rounds' in puzzle:
 
     rounds_str = ''
     if include_rounds and 'rounds' in puzzle:
@@ -101,7 +110,7 @@ def puzzle_blocks(puzzle, include_rounds=False):
         channel_url(channel_id), name,
         solution_str,
         ', '.join(links), rounds_str,
         channel_url(channel_id), name,
         solution_str,
         ', '.join(links), rounds_str,
-        state_str
+        extra_str
     )
 
     # Combining hunt ID and puzzle ID together here is safe because
     )
 
     # Combining hunt ID and puzzle ID together here is safe because
@@ -209,6 +218,10 @@ def puzzle_channel_topic(puzzle):
     if len(links):
         topic += "({})".format(', '.join(links))
 
     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)
     state = puzzle.get('state', None)
     if state:
         topic += " {}".format(state)