]> git.cworth.org Git - turbot/blobdiff - turbot/puzzle.py
Add state string to list of puzzle attributes matched in searching
[turbot] / turbot / puzzle.py
index 810c97e5e671c977bab712b79acd4ddf973e5516..7db006b9b237702289a41125e81a4e169e0674d3 100644 (file)
@@ -1,6 +1,7 @@
 from turbot.blocks import section_block, text_block
 from turbot.channel import channel_url
 from boto3.dynamodb.conditions import Key
+import re
 
 def find_puzzle_for_url(turb, hunt_id, url):
     """Given a hunt_id and URL, return the puzzle with that URL
@@ -67,3 +68,52 @@ def puzzle_block(puzzle):
     )
 
     return section_block(text_block(puzzle_text))
+
+def puzzle_matches_one(puzzle, pattern):
+    """Returns True if this puzzle matches the given string (regexp)
+
+    A match will be considered on any of puzzle title, round title,
+    puzzle URL, puzzle state, or solution string. The string can
+    include regular expression syntax. Matching is case insensitive.
+    """
+
+    p = re.compile('.*'+pattern+'.*', re.IGNORECASE)
+
+    if p.match(puzzle['name']):
+        return True
+
+    if 'rounds' in puzzle:
+        for round in puzzle['rounds']:
+            if p.match(round):
+                return True
+
+    if 'url' in puzzle:
+        if p.match(puzzle['url']):
+            return True
+
+    if 'state' in puzzle:
+        if p.match(puzzle['state']):
+            return True
+
+    if 'solution' in puzzle:
+        for solution in puzzle['solution']:
+            if p.match(solution):
+                return True
+
+    return False
+
+def puzzle_matches_all(puzzle, patterns):
+    """Returns True if this puzzle matches all of the given list of patterns
+
+    A match will be considered on any of puzzle title, round title,
+    puzzle URL, puzzle state, or solution string. All patterns must
+    match the puzzle somewhere, (that is, there is an implicit logical
+    AND between patterns). Patterns can include regular expression
+    syntax. Matching is case insensitive.
+    """
+
+    for pattern in patterns:
+        if not puzzle_matches_one(puzzle, pattern):
+            return False
+
+    return True