From 2e40d7032c497c181fc11fced3c8a15d8c6f4e60 Mon Sep 17 00:00:00 2001 From: Michel Wilhelm Date: Fri, 6 Nov 2020 21:12:57 -0300 Subject: [PATCH] Create event for the same position is not allowed --- api/resources/game.py | 12 ++++++++++-- game/admin.py | 2 +- game/migrations/0006_auto_20201107_0010.py | 19 +++++++++++++++++++ game/models.py | 4 ++-- game/signals.py | 9 ++++++--- 5 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 game/migrations/0006_auto_20201107_0010.py diff --git a/api/resources/game.py b/api/resources/game.py index 90a1792..ac703e6 100644 --- a/api/resources/game.py +++ b/api/resources/game.py @@ -44,14 +44,22 @@ class GameEventResource(APIView): game = Game.objects.get(pk=game_id) except Game.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) - print(game.status, game.status == GameStatuses.FINISHED) + if game.status == GameStatuses.FINISHED: - print("WTF, DEVERIA PASSAR AQUI") return Response( {"message": "Game is already finished"}, status=status.HTTP_412_PRECONDITION_FAILED, ) + row = request.data.get("row") + col = request.data.get("col") + game_event = GameEvent.objects.filter(game=game, row=row, col=col).first() + if game_event: + return Response( + {"message": "This event was already registered"}, + status=status.HTTP_409_CONFLICT, + ) + serializer = GameEventSerializer(data=request.data) if serializer.is_valid(): serializer.save() diff --git a/game/admin.py b/game/admin.py index 60e6a79..f83268e 100644 --- a/game/admin.py +++ b/game/admin.py @@ -24,6 +24,6 @@ class GameAdmin(admin.ModelAdmin): @admin.register(GameEvent) class GameEventAdmin(admin.ModelAdmin): - list_display = ("id", "created_at", "game", "type", "event_row", "event_col") + list_display = ("id", "created_at", "game", "type", "row", "col") list_filter = ("type",) diff --git a/game/migrations/0006_auto_20201107_0010.py b/game/migrations/0006_auto_20201107_0010.py new file mode 100644 index 0000000..7b2dc25 --- /dev/null +++ b/game/migrations/0006_auto_20201107_0010.py @@ -0,0 +1,19 @@ +# Generated by Django 3.1.3 on 2020-11-07 00:10 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("game", "0005_auto_20201106_2357"), + ] + + operations = [ + migrations.RenameField( + model_name="gameevent", old_name="event_col", new_name="col", + ), + migrations.RenameField( + model_name="gameevent", old_name="event_row", new_name="row", + ), + ] diff --git a/game/models.py b/game/models.py index 3a51d09..7529f71 100644 --- a/game/models.py +++ b/game/models.py @@ -94,14 +94,14 @@ class GameEvent(models.Model): help_text="The game event", ) - event_row = models.PositiveIntegerField( + row = models.PositiveIntegerField( "The row clicked", default=None, null=True, blank=True, help_text="Row on the board where the event occurred, if applicable", ) - event_col = models.PositiveIntegerField( + col = models.PositiveIntegerField( "The column clicked", default=None, null=True, diff --git a/game/signals.py b/game/signals.py index 8ca2152..1c4d972 100644 --- a/game/signals.py +++ b/game/signals.py @@ -46,6 +46,9 @@ def identify_click_event(sender, signal, instance, **kwargs): if not instance.type == EventTypes.CLICK_NAIVE: return + if instance.row is None and instance.col is None: + return + ms = Minesweeper( instance.game.rows, instance.game.cols, @@ -53,11 +56,11 @@ def identify_click_event(sender, signal, instance, **kwargs): instance.game.board, ) - if ms.is_mine(instance.metadata["row"], instance.metadata["col"]): + if ms.is_mine(instance.row, instance.col): instance.type = EventTypes.CLICK_MINE - elif ms.is_empty(instance.metadata["row"], instance.metadata["col"]): + elif ms.is_empty(instance.row, instance.col): instance.type = EventTypes.CLICK_EMPTY - elif ms.is_point(instance.metadata["row"], instance.metadata["col"]): + elif ms.is_point(instance.row, instance.col): instance.type = EventTypes.CLICK_POINT