]> git.cworth.org Git - turbot/commitdiff
Add support for an argument to the /hunt command
authorCarl Worth <cworth@cworth.org>
Tue, 5 Jan 2021 03:49:35 +0000 (19:49 -0800)
committerCarl Worth <cworth@cworth.org>
Tue, 5 Jan 2021 03:49:35 +0000 (19:49 -0800)
This will work with three options:

/hunt all: Show all puzzles
        /hunt unsolved: Show unsolved puzzles
        /hunt solved: Show solved puzzles

turbot/events.py
turbot/hunt.py
turbot/interaction.py

index bf4154ffafce8124388c80442ec0fbe630821e06..b8fd429cbd6009e2aca5a605127b22c66486c863 100644 (file)
@@ -45,7 +45,7 @@ def home(turb, user_id):
             continue
         if user_id in slack_channel_members(turb.slack_client,
                                             hunt['channel_id']):
-            my_hunt_blocks += hunt_blocks(turb, hunt)
+            my_hunt_blocks += hunt_blocks(turb, hunt, puzzle_status='all')
         else:
             available_hunt_blocks.append(hunt_link_block(turb, hunt))
 
index 77c179771a17cd7e01eed6f5d3230bf554a225b6..6d44662b3d44f53e8ca5c654d4c5ca6177e09673 100644 (file)
@@ -23,13 +23,21 @@ def find_hunt_for_hunt_id(turb, hunt_id):
     else:
         return None
 
-def hunt_blocks(turb, hunt):
+def hunt_blocks(turb, hunt, puzzle_status='unsolved'):
     """Generate Slack blocks for a hunt
 
     The hunt argument should be a dictionary as returned from the
-    database. The return value can be used in a Slack command
-    expecting blocks to provide all the details of a hunt, (puzzles,
-    their state, solution, links to channels and sheets, etc.).
+    database.
+
+    Option 'puzzle_status' indicates which puzzles to include. If
+    either 'solved' or 'unsolved' only puzzles with that status from
+    the hunt will be included in the result. If any other value, all
+    puzzles in the hunt will be included.
+
+    The return value can be used in a Slack command expecting blocks to
+    provide all the details of a hunt, (puzzles, their state,
+    solution, links to channels and sheets, etc.).
+
     """
 
     name = hunt['name']
@@ -44,6 +52,10 @@ def hunt_blocks(turb, hunt):
     )
     puzzles = response['Items']
 
+    # Filter the set of puzzles according the the requested puzzle_status
+    if puzzle_status in ('solved', 'unsolved'):
+        puzzles = [p for p in puzzles if p['status'] == puzzle_status]
+
     # Compute the set of rounds across all puzzles
     rounds = set()
     for puzzle in puzzles:
index bb09b6854ad6a2fadb88b2923b34c9cc7a3234f5..9045778c232878e9b9898b5090bafbfbfd496aa5 100644 (file)
@@ -625,6 +625,13 @@ def hunt(turb, body, args):
 
     channel_id = body['channel_id'][0]
     response_url = body['response_url'][0]
+    status = args
+
+    if len(status):
+        if status not in ('unsolved', 'solved', 'all'):
+            return bot_reply("Usage: /hunt [all|solved|unsolved]")
+    else:
+        status = 'unsolved'
 
     hunt = hunt_for_channel(turb, channel_id)
 
@@ -632,7 +639,7 @@ def hunt(turb, body, args):
         return bot_reply("Sorry, this channel doesn't appear to "
                          + "be a hunt or puzzle channel")
 
-    blocks = hunt_blocks(turb, hunt)
+    blocks = hunt_blocks(turb, hunt, puzzle_status=status)
 
     requests.post(response_url,
                   json = { 'blocks': blocks },