Create event for the same position is not allowed
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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",)
|
||||
|
||||
19
game/migrations/0006_auto_20201107_0010.py
Normal file
19
game/migrations/0006_auto_20201107_0010.py
Normal file
@@ -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",
|
||||
),
|
||||
]
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user