]> git.cworth.org Git - turbot/blobdiff - turbot/puzzle.py
Unify code to rename channel, set channel description, and rename sheet
[turbot] / turbot / puzzle.py
index b6e78833098f9ed2c4d5251a82400d01941c9b07..1a36bfb9be433460d850672733c4bfed92d6728e 100644 (file)
@@ -3,6 +3,7 @@ from turbot.blocks import (
 )
 from turbot.channel import channel_url
 from boto3.dynamodb.conditions import Key
+import turbot.sheets
 import re
 
 def find_puzzle_for_puzzle_id(turb, hunt_id, puzzle_id):
@@ -148,3 +149,66 @@ def puzzle_matches_all(puzzle, patterns):
             return False
 
     return True
+
+def puzzle_id_from_name(name):
+    return re.sub(r'[^a-zA-Z0-9_]', '', name).lower()
+
+def puzzle_update_channel_and_sheet(turb, puzzle):
+
+    channel_id = puzzle['channel_id']
+    name = puzzle['name']
+    url = puzzle.get('url', None)
+    sheet_url = puzzle.get('sheet_url', None)
+    state = puzzle.get('state', None)
+    status = puzzle['status']
+
+    topic = ''
+
+    if status == 'solved':
+        topic += "SOLVED: `{}` ".format('`, `'.join(puzzle['solution']))
+
+    topic += name
+
+    links = []
+    if url:
+        links.append("<{}|Puzzle>".format(url))
+    if sheet_url:
+        links.append("<{}|Sheet>".format(sheet_url))
+
+    if len(links):
+        topic += "({})".format(', '.join(links))
+
+    if state:
+        topic += " {}".format(state)
+
+    # Slack only allows 250 characters for a topic
+    if len(topic) > 250:
+        topic = topic[:247] + "..."
+
+    turb.slack_client.conversations_setTopic(channel=channel_id,
+                                             topic=topic)
+
+    # Rename the sheet to include indication of solved/solution status
+    sheet_name = puzzle['name']
+    if puzzle['status'] == 'solved':
+        sheet_name += " - Solved {}".format(", ".join(puzzle['solution']))
+
+    turbot.sheets.renameSheet(turb, puzzle['sheet_url'], sheet_name)
+
+    # Finally, rename the Slack channel to reflect the latest name and
+    # the solved status
+    #
+    # Note: We don't use puzzle['hunt_id'] here because we're keeping
+    # that as a persistent identifier in the database. Instead we
+    # create a new ID-like identifier from the current name.
+    channel_name = "{}-{}".format(
+        puzzle['hunt_id'],
+        puzzle_id_from_name(puzzle['name'])
+    )
+    if puzzle['status'] == 'solved':
+        channel_name += "-solved"
+
+    turb.slack_client.conversations_rename(
+        channel=puzzle['channel_id'],
+        name=channel_name
+    )