]> git.cworth.org Git - turbot/blob - turbot/puzzle.py
810c97e5e671c977bab712b79acd4ddf973e5516
[turbot] / turbot / puzzle.py
1 from turbot.blocks import section_block, text_block
2 from turbot.channel import channel_url
3 from boto3.dynamodb.conditions import Key
4
5 def find_puzzle_for_url(turb, hunt_id, url):
6     """Given a hunt_id and URL, return the puzzle with that URL
7
8     Returns None if no puzzle with the given URL exists in the database,
9     otherwise a dictionary with all fields from the puzzle's row in
10     the database.
11     """
12
13     response = turb.table.query(
14         IndexName='url_index',
15         KeyConditionExpression=(
16             Key('hunt_id').eq(hunt_id) &
17             Key('url').eq(url)
18         )
19     )
20
21     if response['Count'] == 0:
22         return None
23
24     return response['Items'][0]
25
26 def puzzle_block(puzzle):
27     """Generate Slack blocks for a puzzle
28
29     The puzzle argument should be a dictionary as returned from the
30     database. The return value can be used in a Slack command
31     expecting blocks to provide all the details of a puzzle, (its
32     state, solution, links to channel and sheet, etc.).
33     """
34
35     name = puzzle['name']
36     status = puzzle['status']
37     solution = puzzle['solution']
38     channel_id = puzzle['channel_id']
39     url = puzzle.get('url', None)
40     sheet_url = puzzle.get('sheet_url', None)
41     state = puzzle.get('state', None)
42     status_emoji = ''
43     solution_str = ''
44
45     if status == 'solved':
46         status_emoji = ":ballot_box_with_check:"
47     else:
48         status_emoji = ":white_square:"
49
50     if len(solution):
51         solution_str = "*`" + '`, `'.join(solution) + "`*"
52
53     links = []
54     if url:
55         links.append("<{}|Puzzle>".format(url))
56     if sheet_url:
57         links.append("<{}|Sheet>".format(sheet_url))
58
59     state_str = ''
60     if state:
61         state_str = "\n{}".format(state)
62
63     puzzle_text = "{}{} <{}|{}> ({}){}".format(
64         status_emoji, solution_str,
65         channel_url(channel_id), name,
66         ', '.join(links), state_str
67     )
68
69     return section_block(text_block(puzzle_text))