]> git.cworth.org Git - turbot/blob - turbot/round.py
Add a README file for turbot
[turbot] / turbot / round.py
1 from turbot.puzzle import puzzle_blocks
2 from turbot.blocks import section_block, text_block
3
4 def round_blocks(round, puzzles, omit_header=False):
5     """Generate Slack blocks for a round
6
7     The 'round' argument should be the name of a round as it appears
8     in the datbase. The `puzzles` argument should be a list of
9     puzzles. The results will include only puzzles which have 'round'
10     in their 'rounds' attribute.
11
12     The return value is a list of bocks that can be used in any Slack
13     command acceepting blocks. It will provide all the details of the
14     puzzles in the given round, (their state, solutions, links to
15     channels and sheets, etc.).
16     """
17
18     if omit_header:
19         blocks = []
20     else:
21         round_text = "*Round: {}*".format(round)
22
23         blocks = [
24             section_block(text_block(round_text)),
25         ]
26
27     for puzzle in puzzles:
28         if 'rounds' not in puzzle:
29             continue
30         if round not in puzzle['rounds']:
31             continue
32         blocks += puzzle_blocks(puzzle)
33
34     return blocks
35
36 def round_quoted_puzzles_titles_answers(round, puzzles):
37     answers = []
38     for puzzle in puzzles:
39         if round:
40             if 'rounds' not in puzzle:
41                 continue
42             if round not in puzzle['rounds']:
43                 continue
44         else:
45             if 'rounds' in puzzle and len(puzzle['rounds']):
46                 continue
47         answer = {}
48         answer['name'] = puzzle['name']
49         if puzzle['status'] == 'solved' and 'solution' in puzzle:
50             answer['solution'] = ", ".join(puzzle['solution'])
51         else:
52             answer['solution'] = ""
53         answers.append(answer)
54
55     if not answers:
56         return ""
57
58     longest = max(len(ans['name']) for ans in answers)
59
60     format = "%{}s: %s".format(longest)
61
62     return "```" + "\n".join(
63         [format % (ans['name'], ans['solution']) for ans in answers]
64     ) + "```"