Creating structure for the game events (#3)
* Adding the GameVent model and migration * Initializing the GameEvent endpoint * GameEvent listing endpoint * Adding CLICK_NAIVE event * Added signal for identify what is on the click location * Using row and col integer is better than metadata with jsonfield * Create event for the same position is not allowed * Better signals control * Adding a board progress to Game model * Identifying the win status * Hide generated board from client
This commit is contained in:
@@ -6,18 +6,23 @@ from internal.utils import empty_list
|
||||
from .game import Minesweeper
|
||||
|
||||
|
||||
class GameStatuses(IntEnum):
|
||||
class EnumChoicesBase(IntEnum):
|
||||
""" Enum was used as choices of Game.status because explicit is better than implicit """
|
||||
|
||||
NOT_PLAYED = 0
|
||||
PLAYING = 1
|
||||
FINISHED = 2
|
||||
|
||||
@classmethod
|
||||
def choices(cls):
|
||||
return [(key.value, key.name) for key in cls]
|
||||
|
||||
|
||||
class GameStatuses(EnumChoicesBase):
|
||||
""" Statuses used by the player and system on game """
|
||||
|
||||
NOT_PLAYED = 0
|
||||
PLAYING = 1
|
||||
PAUSED = 2
|
||||
FINISHED = 3
|
||||
|
||||
|
||||
class Game(models.Model):
|
||||
created_at = models.DateTimeField("Creation date", auto_now_add=True)
|
||||
modified_at = models.DateTimeField("Last update", auto_now=True)
|
||||
@@ -33,8 +38,14 @@ class Game(models.Model):
|
||||
)
|
||||
|
||||
board = JSONField(
|
||||
"Generated board", default=empty_list, help_text="Whe generated board game"
|
||||
"Generated board", default=empty_list, help_text="The generated board game"
|
||||
)
|
||||
board_progress = JSONField(
|
||||
"Progress board",
|
||||
default=empty_list,
|
||||
help_text="This board is updated at each GameEvent recorded",
|
||||
)
|
||||
|
||||
win = models.BooleanField(
|
||||
"Win?",
|
||||
default=None,
|
||||
@@ -56,6 +67,7 @@ class Game(models.Model):
|
||||
ms.create_board()
|
||||
ms.put_mines()
|
||||
self.board = ms.board
|
||||
self.board_progress = ms.board_progress
|
||||
|
||||
super(Game, self).save(*args, **kwargs)
|
||||
|
||||
@@ -63,3 +75,49 @@ class Game(models.Model):
|
||||
verbose_name = "Game"
|
||||
verbose_name_plural = "Games"
|
||||
db_table = "games"
|
||||
|
||||
|
||||
class EventTypes(EnumChoicesBase):
|
||||
""" Event types to generate a game timeline """
|
||||
|
||||
START_GAME = 0
|
||||
PAUSE = 1
|
||||
RESUME = 2
|
||||
CLICK_MINE = 3
|
||||
CLICK_POINT = 4
|
||||
CLICK_EMPTY = 5
|
||||
CLICK_FLAG = 6
|
||||
GAME_OVER = 7
|
||||
CLICK_NAIVE = 8
|
||||
|
||||
|
||||
class GameEvent(models.Model):
|
||||
created_at = models.DateTimeField("Creation date", auto_now_add=True)
|
||||
game = models.ForeignKey("game.Game", on_delete=models.CASCADE)
|
||||
|
||||
type = models.IntegerField(
|
||||
choices=EventTypes.choices(),
|
||||
default=EventTypes.START_GAME,
|
||||
help_text="The game event",
|
||||
)
|
||||
|
||||
row = models.PositiveIntegerField(
|
||||
"The row clicked",
|
||||
default=None,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="Row on the board where the event occurred, if applicable",
|
||||
)
|
||||
col = models.PositiveIntegerField(
|
||||
"The column clicked",
|
||||
default=None,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="Column on the board where the event occurred, if applicable",
|
||||
)
|
||||
|
||||
class Meta:
|
||||
ordering = ["created_at"]
|
||||
verbose_name = "Game event"
|
||||
verbose_name_plural = "Game events"
|
||||
db_table = "game_events"
|
||||
|
||||
Reference in New Issue
Block a user