]> git.cworth.org Git - turbot/blobdiff - turbot/hunt.py
Farm blocks-creation code out to various supporting files
[turbot] / turbot / hunt.py
index 87e47fa1324cf603423bdcbebff6c65b1f78056a..77c179771a17cd7e01eed6f5d3230bf554a225b6 100644 (file)
@@ -1,3 +1,9 @@
+from turbot.blocks import section_block, text_block, divider_block
+from turbot.round import round_blocks
+from turbot.puzzle import puzzle_block
+from turbot.channel import channel_url
+from boto3.dynamodb.conditions import Key
+
 def find_hunt_for_hunt_id(turb, hunt_id):
     """Given a hunt ID find the database item for that hunt
 
@@ -16,3 +22,54 @@ def find_hunt_for_hunt_id(turb, hunt_id):
         return response['Item']
     else:
         return None
+
+def hunt_blocks(turb, hunt):
+    """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.).
+    """
+
+    name = hunt['name']
+    hunt_id = hunt['hunt_id']
+    channel_id = hunt['channel_id']
+
+    response = turb.table.query(
+        KeyConditionExpression=(
+            Key('hunt_id').eq(hunt_id) &
+            Key('SK').begins_with('puzzle-')
+        )
+    )
+    puzzles = response['Items']
+
+    # Compute the set of rounds across all puzzles
+    rounds = set()
+    for puzzle in puzzles:
+        if 'rounds' not in puzzle:
+            continue
+        for round in puzzle['rounds']:
+            rounds.add(round)
+
+    hunt_text = "*<{}|{}>*".format(channel_url(channel_id), name)
+
+    blocks = [
+        section_block(text_block(hunt_text)),
+    ]
+
+    # Construct blocks for each round
+    for round in rounds:
+        blocks += round_blocks(round, puzzles)
+
+    # Also blocks for any puzzles not in any round
+    stray_puzzles = [puzzle for puzzle in puzzles if 'rounds' not in puzzle]
+    if len(stray_puzzles):
+        stray_text = "*Puzzles with no asigned round*"
+        blocks.append(section_block(text_block(stray_text)))
+        for puzzle in stray_puzzles:
+            blocks.append(puzzle_block(puzzle))
+
+    blocks.append(divider_block())
+
+    return blocks