]> git.cworth.org Git - turbot/commitdiff
Use a deep copy when comparing a puzzle to detect state modifications
authorCarl Worth <cworth@cworth.org>
Sat, 9 Jan 2021 13:16:20 +0000 (05:16 -0800)
committerCarl Worth <cworth@cworth.org>
Sat, 9 Jan 2021 13:35:42 +0000 (05:35 -0800)
Since the recent tags is an array, we need a deep copy of the puzzle dict
in order to be able to reliably compare the puzzle prior to the copy with
the one after the copy where a tag has been added or removed.

With this fix, adding or removing a tag now reliably updates the channel
description.

turbot/interaction.py
turbot/puzzle.py

index fb11599034fc1f518a1ec7a19a7d54ade43862be..99e76e6d688f34266cb568d5a808dd2fc9fc0a82 100644 (file)
@@ -13,7 +13,8 @@ from turbot.puzzle import (
     puzzle_update_channel_and_sheet,
     puzzle_id_from_name,
     puzzle_blocks,
-    puzzle_sort_key
+    puzzle_sort_key,
+    puzzle_copy
 )
 from turbot.round import round_quoted_puzzles_titles_answers
 import turbot.rot
@@ -881,8 +882,8 @@ def state(turb, body, args):
         return bot_reply(
             "Sorry, the /state command only works in a puzzle channel")
 
-    # Make a copy of the puzzle object
-    puzzle = old_puzzle.copy()
+    # Make a deep copy of the puzzle object
+    puzzle = puzzle_copy(old_puzzle)
 
     # Update the puzzle in the database
     puzzle['state'] = args
@@ -941,8 +942,8 @@ def tag(turb, body, args):
 
     # OK. Error checking is done. Let's get to work
 
-    # Make a copy of the puzzle object
-    puzzle = old_puzzle.copy()
+    # Make a deep copy of the puzzle object
+    puzzle = puzzle_copy(old_puzzle)
 
     if action == 'remove':
         puzzle['tags'] = [t for t in puzzle['tags'] if t != tag]
@@ -977,8 +978,8 @@ def solved(turb, body, args):
         return bot_reply(
             "Error, no solution provided. Usage: `/solved SOLUTION HERE`")
 
-    # Make a copy of the puzzle object
-    puzzle = old_puzzle.copy()
+    # Make a deep copy of the puzzle object
+    puzzle = puzzle_copy(old_puzzle)
 
     # Set the status and solution fields in the database
     puzzle['status'] = 'solved'
index e3a4f741580f711d0c36df9f459a265fd364b266..b361b10f3d322ce5efb43abd92c3e978e3e8c9d3 100644 (file)
@@ -293,3 +293,12 @@ def puzzle_update_channel_and_sheet(turb, puzzle, old_puzzle=None):
             channel=channel_id,
             name=channel_name
         )
+
+# A copy deep enough to work for puzzle_update_channel_and_sheet above
+def puzzle_copy(old_puzzle):
+    new_puzzle = old_puzzle.copy()
+
+    if 'tags' in old_puzzle:
+        new_puzzle['tags'] = old_puzzle['tags'].copy()
+
+    return new_puzzle