From 53e9a024a784fc572381ff75943829398acd0ac5 Mon Sep 17 00:00:00 2001 From: faucomte97 Date: Fri, 1 Nov 2024 12:32:00 +0000 Subject: [PATCH 1/6] fix: Remove invalid attempts and snake case-ify field names --- game/end_to_end_tests/test_cow_crashes.py | 4 +- game/end_to_end_tests/test_python_levels.py | 4 +- game/level_management.py | 6 +-- .../0105_delete_invalid_attempts.py | 18 +++++++++ game/migrations/0106_fields_to_snake_case.py | 38 +++++++++++++++++++ game/models.py | 10 ++--- game/random_road.py | 4 +- game/serializers.py | 12 +++--- game/static/game/js/game.js | 2 +- game/static/game/js/level_editor.js | 14 +++---- game/templates/game/game.html | 24 ++++++------ game/tests/test_level_editor.py | 36 +++++++++--------- game/tests/test_level_selection.py | 6 +-- game/tests/utils/level.py | 12 +++--- game/views/level.py | 6 +-- game/views/level_moderation.py | 5 ++- 16 files changed, 130 insertions(+), 71 deletions(-) create mode 100644 game/migrations/0105_delete_invalid_attempts.py create mode 100644 game/migrations/0106_fields_to_snake_case.py diff --git a/game/end_to_end_tests/test_cow_crashes.py b/game/end_to_end_tests/test_cow_crashes.py index d89244a0c..8b8527f5d 100644 --- a/game/end_to_end_tests/test_cow_crashes.py +++ b/game/end_to_end_tests/test_cow_crashes.py @@ -47,7 +47,7 @@ def setUpClass(cls): TestCowCrashes.cow_level = Level( name="Cow crashing", anonymous=False, - blocklyEnabled=True, + blockly_enabled=True, character=van, cows='[{"minCows":"7","maxCows":"7","potentialCoordinates":[{"x":4,"y":4},{"x":2,"y":4},{"x":3,"y":7},{"x":4,"y":6},{"x":2,"y":6},{"x":3,"y":1},{"x":4,"y":2}],"type":"WHITE"}]', default=False, @@ -57,7 +57,7 @@ def setUpClass(cls): model_solution="[1]", origin='{"coordinate":[2,5],"direction":"E"}', path='[{"coordinate":[2,5],"connectedNodes":[1]},{"coordinate":[3,5],"connectedNodes":[0,4,2,5]},{"coordinate":[4,5],"connectedNodes":[1]},{"coordinate":[3,7],"connectedNodes":[4]},{"coordinate":[3,6],"connectedNodes":[8,3,6,1]},{"coordinate":[3,4],"connectedNodes":[10,1,11,16]},{"coordinate":[4,6],"connectedNodes":[4,7]},{"coordinate":[4,7],"connectedNodes":[6]},{"coordinate":[2,6],"connectedNodes":[9,4]},{"coordinate":[2,7],"connectedNodes":[8]},{"coordinate":[2,4],"connectedNodes":[13,5,12]},{"coordinate":[4,4],"connectedNodes":[5,14,15]},{"coordinate":[2,3],"connectedNodes":[10]},{"coordinate":[1,4],"connectedNodes":[10]},{"coordinate":[5,4],"connectedNodes":[11]},{"coordinate":[4,3],"connectedNodes":[11,19]},{"coordinate":[3,3],"connectedNodes":[5,17]},{"coordinate":[3,2],"connectedNodes":[18,16,19,20]},{"coordinate":[2,2],"connectedNodes":[17]},{"coordinate":[4,2],"connectedNodes":[17,15,23,22]},{"coordinate":[3,1],"connectedNodes":[21,17,22]},{"coordinate":[2,1],"connectedNodes":[20]},{"coordinate":[4,1],"connectedNodes":[20,19]},{"coordinate":[5,2],"connectedNodes":[19]}]', - pythonEnabled=False, + python_enabled=False, theme=grass, threads=1, traffic_lights="[]", diff --git a/game/end_to_end_tests/test_python_levels.py b/game/end_to_end_tests/test_python_levels.py index a0d5b298f..24cbea68a 100644 --- a/game/end_to_end_tests/test_python_levels.py +++ b/game/end_to_end_tests/test_python_levels.py @@ -45,7 +45,7 @@ def test_animal_sound_horn(self): animal_level = Level( name="Animal commands", anonymous=False, - blocklyEnabled=False, + blockly_enabled=False, character=van, cows='[{"minCows":1,"maxCows":1,"potentialCoordinates":[{"x":3,"y":4}],"type":"WHITE"}]', default=False, @@ -55,7 +55,7 @@ def test_animal_sound_horn(self): model_solution="", origin='{"coordinate":[2,4],"direction":"E"}', path='[{"coordinate":[2,4],"connectedNodes":[1]},{"coordinate":[3,4],"connectedNodes":[0,2]},{"coordinate":[4,4],"connectedNodes":[1]}]', - pythonEnabled=True, + python_enabled=True, theme=grass, threads=1, traffic_lights="[]", diff --git a/game/level_management.py b/game/level_management.py index ebf0fb924..97804d312 100644 --- a/game/level_management.py +++ b/game/level_management.py @@ -160,9 +160,9 @@ def save_level(level, data): level.max_fuel = data["max_fuel"] level.traffic_lights = data["traffic_lights"] level.cows = data["cows"] - level.blocklyEnabled = data.get("blocklyEnabled", True) - level.pythonEnabled = data.get("pythonEnabled", False) - level.pythonViewEnabled = data.get("pythonViewEnabled", False) + level.blockly_enabled = data.get("blockly_enabled", True) + level.python_enabled = data.get("python_enabled", False) + level.python_view_enabled = data.get("python_view_enabled", False) level.theme = get_theme_by_pk(pk=data["theme"]) level.character = get_character_by_pk(pk=data["character"]) level.disable_algorithm_score = data.get("disable_algorithm_score", False) diff --git a/game/migrations/0105_delete_invalid_attempts.py b/game/migrations/0105_delete_invalid_attempts.py new file mode 100644 index 000000000..5455e078c --- /dev/null +++ b/game/migrations/0105_delete_invalid_attempts.py @@ -0,0 +1,18 @@ +from django.apps.registry import Apps +from django.db import migrations + + +def delete_invalid_attempts(apps: Apps, *args): + Attempt = apps.get_model("game", "Attempt") + + Attempt.objects.filter(start_time__isnull=True).delete() + + +class Migration(migrations.Migration): + dependencies = [("game", "0104_remove_level_direct_drive")] + operations = [ + migrations.RunPython( + delete_invalid_attempts, + reverse_code=migrations.RunPython.noop, + ) + ] diff --git a/game/migrations/0106_fields_to_snake_case.py b/game/migrations/0106_fields_to_snake_case.py new file mode 100644 index 000000000..945dcf2dc --- /dev/null +++ b/game/migrations/0106_fields_to_snake_case.py @@ -0,0 +1,38 @@ +# Generated by Django 3.2.25 on 2024-11-01 12:25 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('game', '0105_delete_invalid_attempts'), + ] + + operations = [ + migrations.RenameField( + model_name='episode', + old_name='r_blocklyEnabled', + new_name='r_blockly_enabled', + ), + migrations.RenameField( + model_name='episode', + old_name='r_pythonEnabled', + new_name='r_python_enabled', + ), + migrations.RenameField( + model_name='level', + old_name='blocklyEnabled', + new_name='blockly_enabled', + ), + migrations.RenameField( + model_name='level', + old_name='pythonEnabled', + new_name='python_enabled', + ), + migrations.RenameField( + model_name='level', + old_name='pythonViewEnabled', + new_name='python_view_enabled', + ), + ] diff --git a/game/models.py b/game/models.py index 189a10cfc..3b819a18e 100644 --- a/game/models.py +++ b/game/models.py @@ -55,8 +55,8 @@ class Episode(models.Model): r_curviness = models.FloatField(default=0, null=True) r_num_tiles = models.IntegerField(default=5, null=True) r_blocks = models.ManyToManyField(Block, related_name="episodes") - r_blocklyEnabled = models.BooleanField(default=True) - r_pythonEnabled = models.BooleanField(default=False) + r_blockly_enabled = models.BooleanField(default=True) + r_python_enabled = models.BooleanField(default=False) r_trafficLights = models.BooleanField(default=False) r_cows = models.BooleanField(default=False) @@ -175,9 +175,9 @@ class Level(models.Model): disable_route_score = models.BooleanField(default=False) disable_algorithm_score = models.BooleanField(default=False) threads = models.IntegerField(blank=False, default=1) - blocklyEnabled = models.BooleanField(default=True) - pythonEnabled = models.BooleanField(default=True) - pythonViewEnabled = models.BooleanField(default=False) + blockly_enabled = models.BooleanField(default=True) + python_enabled = models.BooleanField(default=True) + python_view_enabled = models.BooleanField(default=False) theme_name = models.CharField( max_length=10, choices=theme_choices(), diff --git a/game/random_road.py b/game/random_road.py index 66ceba5b7..13b7d8a86 100644 --- a/game/random_road.py +++ b/game/random_road.py @@ -68,8 +68,8 @@ def create(episode=None): level_data["theme"] = 1 level_data["name"] = ("Random level for " + episode.name) if episode else "Default random level" level_data["character"] = 1 - level_data["blocklyEnabled"] = episode.r_blocklyEnabled if episode else True - level_data["pythonEnabled"] = episode.r_pythonEnabled if episode else False + level_data["blockly_enabled"] = episode.r_blockly_enabled if episode else True + level_data["python_enabled"] = episode.r_python_enabled if episode else False level_data["blocks"] = [{"type": block.type} for block in blocks] level = Level(default=False, anonymous=True) diff --git a/game/serializers.py b/game/serializers.py index 7fa8830e0..0678f7fe4 100644 --- a/game/serializers.py +++ b/game/serializers.py @@ -26,8 +26,8 @@ class Meta(object): "name", "title", "default", - "blocklyEnabled", - "pythonEnabled", + "blockly_enabled", + "python_enabled", ) def get_title(self, obj): @@ -67,9 +67,9 @@ class Meta(object): "levelblock_set", "map", "mode", - "blocklyEnabled", - "pythonEnabled", - "pythonViewEnabled", + "blockly_enabled", + "python_enabled", + "python_view_enabled", ) def get_title(self, obj): @@ -101,7 +101,7 @@ def get_leveldecor_set(self, obj): class LevelModeSerializer(serializers.HyperlinkedModelSerializer): class Meta(object): model = Level - fields = ("blocklyEnabled", "pythonEnabled", "pythonViewEnabled") + fields = ("blockly_enabled", "python_enabled", "python_view_enabled") class LevelMapListSerializer(serializers.HyperlinkedModelSerializer): diff --git a/game/static/game/js/game.js b/game/static/game/js/game.js index aff0caacd..86b3a9c74 100644 --- a/game/static/game/js/game.js +++ b/game/static/game/js/game.js @@ -1152,7 +1152,7 @@ ocargo.Game.prototype._setupSaveTab = function () { python_contents: ocargo.pythonControl.getCode(), blockly_enabled: BLOCKLY_ENABLED, python_enabled: PYTHON_ENABLED, - pythonViewEnabled: PYTHON_VIEW_ENABLED + python_view_enabled: PYTHON_VIEW_ENABLED } this.saving.saveWorkspace( diff --git a/game/static/game/js/level_editor.js b/game/static/game/js/level_editor.js index 65a8bd64a..7ad26bcf2 100644 --- a/game/static/game/js/level_editor.js +++ b/game/static/game/js/level_editor.js @@ -2540,9 +2540,9 @@ ocargo.LevelEditor = function(levelId) { // Language data var language = $('#language_select').val(); - state.blocklyEnabled = language === 'blockly' || language === 'both' || language === 'blocklyWithPythonView'; - state.pythonViewEnabled = language === 'blocklyWithPythonView'; - state.pythonEnabled = language === 'python' || language === 'both'; + state.blockly_enabled = language === 'blockly' || language === 'both' || language === 'blocklyWithPythonView'; + state.python_view_enabled = language === 'blocklyWithPythonView'; + state.python_enabled = language === 'python' || language === 'both'; // Description and hint data if ($('#subtitle').val().length > 0) { @@ -2675,11 +2675,11 @@ ocargo.LevelEditor = function(levelId) { // Load in language data var languageSelect = $('#language_select'); - if (state.blocklyEnabled && state.pythonViewEnabled){ + if (state.blockly_enabled && state.python_view_enabled){ languageSelect.val('blocklyWithPythonView'); - } else if(state.blocklyEnabled && state.pythonEnabled) { + } else if(state.blockly_enabled && state.python_enabled) { languageSelect.val('both'); - } else if(state.pythonEnabled) { + } else if(state.python_enabled) { languageSelect.val('python'); } else { languageSelect.val('blockly'); @@ -2869,7 +2869,7 @@ ocargo.LevelEditor = function(levelId) { // you can copy and paste into a Django migration file var state = extractState(); - var boolFields = ["pythonEnabled", "blocklyEnabled", 'fuel_gauge']; + var boolFields = ["python_enabled", "blockly_enabled", 'fuel_gauge']; var stringFields = ['path', 'traffic_lights', 'cows', 'origin', 'destinations']; var otherFields = ['max_fuel']; diff --git a/game/templates/game/game.html b/game/templates/game/game.html index 300517f7e..bf8af5a3a 100644 --- a/game/templates/game/game.html +++ b/game/templates/game/game.html @@ -73,9 +73,9 @@ var MAX_FUEL = {{level.max_fuel}} var NEXT_EPISODE = {% if level.episode.next_episode == None %}null{% else %}"{{level.episode.next_episode.id}}"{% endif %} var WORKSPACE = {% if workspace == None %}null{% else %}"{{workspace}}"{% endif %} - var BLOCKLY_ENABLED = {{level.blocklyEnabled|booltojs}} - var PYTHON_ENABLED = {{level.pythonEnabled|booltojs}} - var PYTHON_VIEW_ENABLED = {{level.pythonViewEnabled|booltojs}} + var BLOCKLY_ENABLED = {{level.blockly_enabled|booltojs}} + var PYTHON_ENABLED = {{level.python_enabled|booltojs}} + var PYTHON_VIEW_ENABLED = {{level.python_view_enabled|booltojs}} var RETURN_URL = "{{return_url}}" var PYTHON_WORKSPACE = {% if python_workspace == None %}null{% else %}"{{python_workspace|linebreaksbr}}"{% endif %} @@ -151,7 +151,7 @@
- {% if level.pythonViewEnabled or level.pythonEnabled %} + {% if level.python_view_enabled or level.python_enabled %} {% endif %} @@ -159,7 +159,7 @@
+ class="tab selectable {% if not level.blockly_enabled and not level.python_view_enabled %}hidden{% endif %}">
-
+
- {% for language_code, language in available_language_dict.items %} {% endfor %} @@ -319,24 +319,24 @@
-
+
- {% if level.pythonViewEnabled %} + {% if level.python_view_enabled %}
{% endif %} -
-