Lint fix v1
This commit is contained in:
@@ -1 +1 @@
|
||||
default_app_config = "game.apps.GameConfig"
|
||||
default_app_config = 'game.apps.GameConfig'
|
||||
|
||||
@@ -6,24 +6,24 @@ from .models import Game, GameEvent
|
||||
@admin.register(Game)
|
||||
class GameAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
"id",
|
||||
"created_at",
|
||||
"modified_at",
|
||||
"rows",
|
||||
"cols",
|
||||
"mines",
|
||||
"win",
|
||||
"status",
|
||||
'id',
|
||||
'created_at',
|
||||
'modified_at',
|
||||
'rows',
|
||||
'cols',
|
||||
'mines',
|
||||
'win',
|
||||
'status',
|
||||
)
|
||||
|
||||
list_filter = (
|
||||
"win",
|
||||
"status",
|
||||
'win',
|
||||
'status',
|
||||
)
|
||||
|
||||
|
||||
@admin.register(GameEvent)
|
||||
class GameEventAdmin(admin.ModelAdmin):
|
||||
list_display = ("id", "created_at", "game", "type", "row", "col")
|
||||
list_display = ('id', 'created_at', 'game', 'type', 'row', 'col')
|
||||
|
||||
list_filter = ("type",)
|
||||
list_filter = ('type',)
|
||||
|
||||
@@ -2,9 +2,9 @@ from django.apps import AppConfig
|
||||
|
||||
|
||||
class GameConfig(AppConfig):
|
||||
name = "game"
|
||||
verbose_name = "Game"
|
||||
verbose_name_plural = "Games"
|
||||
name = 'game'
|
||||
verbose_name = 'Game'
|
||||
verbose_name_plural = 'Games'
|
||||
|
||||
def ready(self):
|
||||
import game.signals # noqa
|
||||
|
||||
28
game/game.py
28
game/game.py
@@ -16,11 +16,9 @@ class Minesweeper:
|
||||
self.board_progress = board_progress
|
||||
|
||||
def create_board(self):
|
||||
""" Creating the board cells with 0 as default value """
|
||||
"""Creating the board cells with 0 as default value"""
|
||||
self.board = [[0 for col in range(self.cols)] for row in range(self.rows)]
|
||||
self.board_progress = [
|
||||
["-" for col in range(self.cols)] for row in range(self.rows)
|
||||
]
|
||||
self.board_progress = [['-' for col in range(self.cols)] for row in range(self.rows)]
|
||||
|
||||
def put_mine(self):
|
||||
"""Put a single mine on the board.
|
||||
@@ -36,7 +34,7 @@ class Minesweeper:
|
||||
return row, col
|
||||
|
||||
def put_mines(self):
|
||||
""" Put the desired amount of mines on the board """
|
||||
"""Put the desired amount of mines on the board"""
|
||||
for mine in range(1, self.mines + 1):
|
||||
row, col = self.put_mine()
|
||||
|
||||
@@ -71,31 +69,31 @@ class Minesweeper:
|
||||
self.increment_safe_point(row - 1, col - 1)
|
||||
|
||||
def is_mine(self, row, col):
|
||||
""" Checks whether the given location have a mine """
|
||||
"""Checks whether the given location have a mine"""
|
||||
if not self.is_point_in_board(row, col):
|
||||
return False
|
||||
return self.board[row][col] == -1
|
||||
|
||||
def is_empty(self, row, col):
|
||||
""" Checks whether the given location is empty """
|
||||
"""Checks whether the given location is empty"""
|
||||
if not self.is_point_in_board(row, col):
|
||||
return False
|
||||
return self.board[row][col] == 0
|
||||
|
||||
def is_point(self, row, col):
|
||||
""" Checks whether the given location have pontuation """
|
||||
"""Checks whether the given location have pontuation"""
|
||||
if not self.is_point_in_board(row, col):
|
||||
return False
|
||||
return self.board[row][col] > 0
|
||||
|
||||
def is_point_in_board(self, row, col):
|
||||
""" Checks whether the location is inside board """
|
||||
if row in range(0, self.rows) and col in range(0, self.cols):
|
||||
"""Checks whether the location is inside board"""
|
||||
if row in range(self.rows) and col in range(self.cols):
|
||||
return True
|
||||
return False
|
||||
|
||||
def increment_safe_point(self, row, col):
|
||||
""" Creates the mine's pontuation frame """
|
||||
"""Creates the mine's pontuation frame"""
|
||||
|
||||
# Ignores if the point whether not in the board
|
||||
if not self.is_point_in_board(row, col):
|
||||
@@ -129,7 +127,7 @@ class Minesweeper:
|
||||
while r < max_row:
|
||||
c = min_col
|
||||
while c < max_col:
|
||||
if not self.board[r][c] == -1 and self.board_progress[r][c] == "-":
|
||||
if not self.board[r][c] == -1 and self.board_progress[r][c] == '-':
|
||||
self.board_progress[r][c] = self.board[r][c]
|
||||
|
||||
if self.board[r][c] == 0:
|
||||
@@ -138,7 +136,7 @@ class Minesweeper:
|
||||
r += 1
|
||||
|
||||
def reveal(self, row, col):
|
||||
""" Reveals the cell's content and yours adjacents cells """
|
||||
"""Reveals the cell's content and yours adjacents cells"""
|
||||
self.board_progress[row][col] = self.board[row][col]
|
||||
|
||||
# We will show adjacents only if the cell clicked is zero
|
||||
@@ -146,14 +144,14 @@ class Minesweeper:
|
||||
self.reveal_adjacents(row, col)
|
||||
|
||||
def win(self):
|
||||
""" Identify if the player won the game """
|
||||
"""Identify if the player won the game"""
|
||||
unrevealed = 0
|
||||
for row in self.board_progress:
|
||||
for cell in row:
|
||||
if cell == -1:
|
||||
return False
|
||||
|
||||
if cell == "-":
|
||||
if cell == '-':
|
||||
unrevealed += 1
|
||||
if (unrevealed - self.mines) == 0:
|
||||
return True
|
||||
|
||||
@@ -1,65 +1,61 @@
|
||||
# Generated by Django 3.1.3 on 2020-11-05 03:03
|
||||
|
||||
from django.db import migrations, models
|
||||
import django_mysql.models
|
||||
from django.db import migrations, models
|
||||
|
||||
import internal.utils
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = []
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="Game",
|
||||
name='Game',
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
'id',
|
||||
models.AutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
verbose_name='ID',
|
||||
),
|
||||
),
|
||||
(
|
||||
"created_at",
|
||||
models.DateTimeField(
|
||||
auto_now_add=True, verbose_name="Creation date"
|
||||
),
|
||||
'created_at',
|
||||
models.DateTimeField(auto_now_add=True, verbose_name='Creation date'),
|
||||
),
|
||||
(
|
||||
"modified_at",
|
||||
models.DateTimeField(auto_now=True, verbose_name="Last update"),
|
||||
'modified_at',
|
||||
models.DateTimeField(auto_now=True, verbose_name='Last update'),
|
||||
),
|
||||
(
|
||||
"rows",
|
||||
models.PositiveIntegerField(default=10, verbose_name="Board rows"),
|
||||
'rows',
|
||||
models.PositiveIntegerField(default=10, verbose_name='Board rows'),
|
||||
),
|
||||
(
|
||||
"cols",
|
||||
models.PositiveIntegerField(default=10, verbose_name="Board cols"),
|
||||
'cols',
|
||||
models.PositiveIntegerField(default=10, verbose_name='Board cols'),
|
||||
),
|
||||
(
|
||||
"mines",
|
||||
models.PositiveIntegerField(
|
||||
default=5, verbose_name="Mines on board"
|
||||
),
|
||||
'mines',
|
||||
models.PositiveIntegerField(default=5, verbose_name='Mines on board'),
|
||||
),
|
||||
(
|
||||
"board",
|
||||
'board',
|
||||
django_mysql.models.JSONField(
|
||||
default=internal.utils.empty_list,
|
||||
verbose_name="Generated board",
|
||||
verbose_name='Generated board',
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"verbose_name": "Game",
|
||||
"verbose_name_plural": "Games",
|
||||
"db_table": "games",
|
||||
'verbose_name': 'Game',
|
||||
'verbose_name_plural': 'Games',
|
||||
'db_table': 'games',
|
||||
},
|
||||
),
|
||||
]
|
||||
|
||||
@@ -1,68 +1,64 @@
|
||||
# Generated by Django 3.1.3 on 2020-11-06 02:25
|
||||
|
||||
from django.db import migrations, models
|
||||
import django_mysql.models
|
||||
from django.db import migrations, models
|
||||
|
||||
import game.models
|
||||
import internal.utils
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("game", "0001_initial"),
|
||||
('game', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="game",
|
||||
name="status",
|
||||
model_name='game',
|
||||
name='status',
|
||||
field=models.IntegerField(
|
||||
choices=[(0, "NOT_PLAYED"), (1, "PLAYING"), (2, "FINISHED")],
|
||||
default=game.models.GameStatuses["NOT_PLAYED"],
|
||||
help_text="Actual game status",
|
||||
choices=[(0, 'NOT_PLAYED'), (1, 'PLAYING'), (2, 'FINISHED')],
|
||||
default=game.models.GameStatuses['NOT_PLAYED'],
|
||||
help_text='Actual game status',
|
||||
),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="game",
|
||||
name="win",
|
||||
model_name='game',
|
||||
name='win',
|
||||
field=models.BooleanField(
|
||||
blank=True,
|
||||
default=None,
|
||||
help_text="Did the user win the game?",
|
||||
help_text='Did the user win the game?',
|
||||
null=True,
|
||||
verbose_name="Win?",
|
||||
verbose_name='Win?',
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="game",
|
||||
name="board",
|
||||
model_name='game',
|
||||
name='board',
|
||||
field=django_mysql.models.JSONField(
|
||||
default=internal.utils.empty_list,
|
||||
help_text="Whe generated board game",
|
||||
verbose_name="Generated board",
|
||||
help_text='Whe generated board game',
|
||||
verbose_name='Generated board',
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="game",
|
||||
name="cols",
|
||||
field=models.PositiveIntegerField(
|
||||
default=10, help_text="Board's total columns", verbose_name="Board cols"
|
||||
),
|
||||
model_name='game',
|
||||
name='cols',
|
||||
field=models.PositiveIntegerField(default=10, help_text="Board's total columns", verbose_name='Board cols'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="game",
|
||||
name="mines",
|
||||
model_name='game',
|
||||
name='mines',
|
||||
field=models.PositiveIntegerField(
|
||||
default=5,
|
||||
help_text="Board's total placed mines",
|
||||
verbose_name="Mines on board",
|
||||
verbose_name='Mines on board',
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="game",
|
||||
name="rows",
|
||||
field=models.PositiveIntegerField(
|
||||
default=10, help_text="Board's total rows", verbose_name="Board rows"
|
||||
),
|
||||
model_name='game',
|
||||
name='rows',
|
||||
field=models.PositiveIntegerField(default=10, help_text="Board's total rows", verbose_name='Board rows'),
|
||||
),
|
||||
]
|
||||
|
||||
@@ -1,73 +1,69 @@
|
||||
# Generated by Django 3.1.3 on 2020-11-06 03:54
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import django_mysql.models
|
||||
from django.db import migrations, models
|
||||
|
||||
import game.models
|
||||
import internal.utils
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("game", "0002_auto_20201106_0225"),
|
||||
('game', '0002_auto_20201106_0225'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="GameEvent",
|
||||
name='GameEvent',
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
'id',
|
||||
models.AutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
verbose_name='ID',
|
||||
),
|
||||
),
|
||||
(
|
||||
"created_at",
|
||||
models.DateTimeField(
|
||||
auto_now_add=True, verbose_name="Creation date"
|
||||
),
|
||||
'created_at',
|
||||
models.DateTimeField(auto_now_add=True, verbose_name='Creation date'),
|
||||
),
|
||||
(
|
||||
"type",
|
||||
'type',
|
||||
models.IntegerField(
|
||||
choices=[
|
||||
(0, "START_GAME"),
|
||||
(1, "PAUSE"),
|
||||
(2, "RESUME"),
|
||||
(3, "CLICK_MINE"),
|
||||
(4, "CLICK_POINT"),
|
||||
(5, "CLICK_EMPTY"),
|
||||
(6, "CLICK_FLAG"),
|
||||
(7, "GAME_OVER"),
|
||||
(0, 'START_GAME'),
|
||||
(1, 'PAUSE'),
|
||||
(2, 'RESUME'),
|
||||
(3, 'CLICK_MINE'),
|
||||
(4, 'CLICK_POINT'),
|
||||
(5, 'CLICK_EMPTY'),
|
||||
(6, 'CLICK_FLAG'),
|
||||
(7, 'GAME_OVER'),
|
||||
],
|
||||
default=game.models.EventTypes["START_GAME"],
|
||||
help_text="The game event",
|
||||
default=game.models.EventTypes['START_GAME'],
|
||||
help_text='The game event',
|
||||
),
|
||||
),
|
||||
(
|
||||
"metadata",
|
||||
'metadata',
|
||||
django_mysql.models.JSONField(
|
||||
default=internal.utils.empty_object,
|
||||
help_text="Some usefull event metadata",
|
||||
verbose_name="Event metadata",
|
||||
help_text='Some usefull event metadata',
|
||||
verbose_name='Event metadata',
|
||||
),
|
||||
),
|
||||
(
|
||||
"game",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE, to="game.game"
|
||||
),
|
||||
'game',
|
||||
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='game.game'),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"verbose_name": "Game event",
|
||||
"verbose_name_plural": "Game events",
|
||||
"db_table": "game_events",
|
||||
'verbose_name': 'Game event',
|
||||
'verbose_name_plural': 'Game events',
|
||||
'db_table': 'game_events',
|
||||
},
|
||||
),
|
||||
]
|
||||
|
||||
@@ -1,41 +1,41 @@
|
||||
# Generated by Django 3.1.3 on 2020-11-06 04:53
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
import game.models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("game", "0003_gameevent"),
|
||||
('game', '0003_gameevent'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name="gameevent",
|
||||
name='gameevent',
|
||||
options={
|
||||
"ordering": ["created_at"],
|
||||
"verbose_name": "Game event",
|
||||
"verbose_name_plural": "Game events",
|
||||
'ordering': ['created_at'],
|
||||
'verbose_name': 'Game event',
|
||||
'verbose_name_plural': 'Game events',
|
||||
},
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="gameevent",
|
||||
name="type",
|
||||
model_name='gameevent',
|
||||
name='type',
|
||||
field=models.IntegerField(
|
||||
choices=[
|
||||
(0, "START_GAME"),
|
||||
(1, "PAUSE"),
|
||||
(2, "RESUME"),
|
||||
(3, "CLICK_MINE"),
|
||||
(4, "CLICK_POINT"),
|
||||
(5, "CLICK_EMPTY"),
|
||||
(6, "CLICK_FLAG"),
|
||||
(7, "GAME_OVER"),
|
||||
(8, "CLICK_NAIVE"),
|
||||
(0, 'START_GAME'),
|
||||
(1, 'PAUSE'),
|
||||
(2, 'RESUME'),
|
||||
(3, 'CLICK_MINE'),
|
||||
(4, 'CLICK_POINT'),
|
||||
(5, 'CLICK_EMPTY'),
|
||||
(6, 'CLICK_FLAG'),
|
||||
(7, 'GAME_OVER'),
|
||||
(8, 'CLICK_NAIVE'),
|
||||
],
|
||||
default=game.models.EventTypes["START_GAME"],
|
||||
help_text="The game event",
|
||||
default=game.models.EventTypes['START_GAME'],
|
||||
help_text='The game event',
|
||||
),
|
||||
),
|
||||
]
|
||||
|
||||
@@ -1,51 +1,54 @@
|
||||
# Generated by Django 3.1.3 on 2020-11-06 23:57
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
import game.models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("game", "0004_auto_20201106_0453"),
|
||||
('game', '0004_auto_20201106_0453'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(model_name="gameevent", name="metadata",),
|
||||
migrations.RemoveField(
|
||||
model_name='gameevent',
|
||||
name='metadata',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="gameevent",
|
||||
name="event_col",
|
||||
model_name='gameevent',
|
||||
name='event_col',
|
||||
field=models.PositiveIntegerField(
|
||||
blank=True,
|
||||
default=None,
|
||||
help_text="Column on the board where the event occurred, if applicable",
|
||||
help_text='Column on the board where the event occurred, if applicable',
|
||||
null=True,
|
||||
verbose_name="The column clicked",
|
||||
verbose_name='The column clicked',
|
||||
),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="gameevent",
|
||||
name="event_row",
|
||||
model_name='gameevent',
|
||||
name='event_row',
|
||||
field=models.PositiveIntegerField(
|
||||
blank=True,
|
||||
default=None,
|
||||
help_text="Row on the board where the event occurred, if applicable",
|
||||
help_text='Row on the board where the event occurred, if applicable',
|
||||
null=True,
|
||||
verbose_name="The row clicked",
|
||||
verbose_name='The row clicked',
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="game",
|
||||
name="status",
|
||||
model_name='game',
|
||||
name='status',
|
||||
field=models.IntegerField(
|
||||
choices=[
|
||||
(0, "NOT_PLAYED"),
|
||||
(1, "PLAYING"),
|
||||
(2, "PAUSED"),
|
||||
(3, "FINISHED"),
|
||||
(0, 'NOT_PLAYED'),
|
||||
(1, 'PLAYING'),
|
||||
(2, 'PAUSED'),
|
||||
(3, 'FINISHED'),
|
||||
],
|
||||
default=game.models.GameStatuses["NOT_PLAYED"],
|
||||
help_text="Actual game status",
|
||||
default=game.models.GameStatuses['NOT_PLAYED'],
|
||||
help_text='Actual game status',
|
||||
),
|
||||
),
|
||||
]
|
||||
|
||||
@@ -4,16 +4,19 @@ from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("game", "0005_auto_20201106_2357"),
|
||||
('game', '0005_auto_20201106_2357'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name="gameevent", old_name="event_col", new_name="col",
|
||||
model_name='gameevent',
|
||||
old_name='event_col',
|
||||
new_name='col',
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name="gameevent", old_name="event_row", new_name="row",
|
||||
model_name='gameevent',
|
||||
old_name='event_row',
|
||||
new_name='row',
|
||||
),
|
||||
]
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
# Generated by Django 3.1.3 on 2020-11-07 01:30
|
||||
|
||||
from django.db import migrations
|
||||
import django_mysql.models
|
||||
from django.db import migrations
|
||||
|
||||
import internal.utils
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("game", "0006_auto_20201107_0010"),
|
||||
('game', '0006_auto_20201107_0010'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="game",
|
||||
name="board_progress",
|
||||
model_name='game',
|
||||
name='board_progress',
|
||||
field=django_mysql.models.JSONField(
|
||||
default=internal.utils.empty_list,
|
||||
help_text="This board is updated at each GameEvent recorded",
|
||||
verbose_name="Progress board",
|
||||
help_text='This board is updated at each GameEvent recorded',
|
||||
verbose_name='Progress board',
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="game",
|
||||
name="board",
|
||||
model_name='game',
|
||||
name='board',
|
||||
field=django_mysql.models.JSONField(
|
||||
default=internal.utils.empty_list,
|
||||
help_text="The generated board game",
|
||||
verbose_name="Generated board",
|
||||
help_text='The generated board game',
|
||||
verbose_name='Generated board',
|
||||
),
|
||||
),
|
||||
]
|
||||
|
||||
@@ -1,47 +1,43 @@
|
||||
# Generated by Django 4.1 on 2022-08-30 23:20
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
import game.models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("game", "0007_auto_20201107_0130"),
|
||||
('game', '0007_auto_20201107_0130'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name="game",
|
||||
name="id",
|
||||
field=models.BigAutoField(
|
||||
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
|
||||
),
|
||||
model_name='game',
|
||||
name='id',
|
||||
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="gameevent",
|
||||
name="id",
|
||||
field=models.BigAutoField(
|
||||
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
|
||||
),
|
||||
model_name='gameevent',
|
||||
name='id',
|
||||
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="gameevent",
|
||||
name="type",
|
||||
model_name='gameevent',
|
||||
name='type',
|
||||
field=models.IntegerField(
|
||||
choices=[
|
||||
(0, "START_GAME"),
|
||||
(1, "PAUSE"),
|
||||
(2, "RESUME"),
|
||||
(3, "CLICK_MINE"),
|
||||
(4, "CLICK_POINT"),
|
||||
(5, "CLICK_EMPTY"),
|
||||
(6, "CLICK_FLAG"),
|
||||
(7, "GAME_OVER"),
|
||||
(8, "CLICK_NAIVE"),
|
||||
(0, 'START_GAME'),
|
||||
(1, 'PAUSE'),
|
||||
(2, 'RESUME'),
|
||||
(3, 'CLICK_MINE'),
|
||||
(4, 'CLICK_POINT'),
|
||||
(5, 'CLICK_EMPTY'),
|
||||
(6, 'CLICK_FLAG'),
|
||||
(7, 'GAME_OVER'),
|
||||
(8, 'CLICK_NAIVE'),
|
||||
],
|
||||
default=game.models.EventTypes["CLICK_NAIVE"],
|
||||
help_text="The game event",
|
||||
default=game.models.EventTypes['CLICK_NAIVE'],
|
||||
help_text='The game event',
|
||||
),
|
||||
),
|
||||
]
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
from enum import IntEnum
|
||||
|
||||
from django.db import models
|
||||
from django_mysql.models import JSONField
|
||||
|
||||
from internal.utils import empty_list
|
||||
|
||||
from .game import Minesweeper
|
||||
|
||||
|
||||
class EnumChoicesBase(IntEnum):
|
||||
""" Enum was used as choices of Game.status because explicit is better than implicit """
|
||||
"""Enum was used as choices of Game.status because explicit is better than implicit"""
|
||||
|
||||
@classmethod
|
||||
def choices(cls):
|
||||
@@ -15,7 +17,7 @@ class EnumChoicesBase(IntEnum):
|
||||
|
||||
|
||||
class GameStatuses(EnumChoicesBase):
|
||||
""" Statuses used by the player and system on game """
|
||||
"""Statuses used by the player and system on game"""
|
||||
|
||||
NOT_PLAYED = 0
|
||||
PLAYING = 1
|
||||
@@ -24,43 +26,35 @@ class GameStatuses(EnumChoicesBase):
|
||||
|
||||
|
||||
class Game(models.Model):
|
||||
created_at = models.DateTimeField("Creation date", auto_now_add=True)
|
||||
modified_at = models.DateTimeField("Last update", auto_now=True)
|
||||
created_at = models.DateTimeField('Creation date', auto_now_add=True)
|
||||
modified_at = models.DateTimeField('Last update', auto_now=True)
|
||||
|
||||
rows = models.PositiveIntegerField(
|
||||
"Board rows", default=10, help_text="Board's total rows"
|
||||
)
|
||||
cols = models.PositiveIntegerField(
|
||||
"Board cols", default=10, help_text="Board's total columns"
|
||||
)
|
||||
mines = models.PositiveIntegerField(
|
||||
"Mines on board", default=5, help_text="Board's total placed mines"
|
||||
)
|
||||
rows = models.PositiveIntegerField('Board rows', default=10, help_text="Board's total rows")
|
||||
cols = models.PositiveIntegerField('Board cols', default=10, help_text="Board's total columns")
|
||||
mines = models.PositiveIntegerField('Mines on board', default=5, help_text="Board's total placed mines")
|
||||
|
||||
board = JSONField(
|
||||
"Generated board", default=empty_list, help_text="The generated board game"
|
||||
)
|
||||
board = JSONField('Generated board', default=empty_list, help_text='The generated board game')
|
||||
board_progress = JSONField(
|
||||
"Progress board",
|
||||
'Progress board',
|
||||
default=empty_list,
|
||||
help_text="This board is updated at each GameEvent recorded",
|
||||
help_text='This board is updated at each GameEvent recorded',
|
||||
)
|
||||
|
||||
win = models.BooleanField(
|
||||
"Win?",
|
||||
'Win?',
|
||||
default=None,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="Did the user win the game?",
|
||||
help_text='Did the user win the game?',
|
||||
)
|
||||
status = models.IntegerField(
|
||||
choices=GameStatuses.choices(),
|
||||
default=GameStatuses.NOT_PLAYED,
|
||||
help_text="Actual game status",
|
||||
help_text='Actual game status',
|
||||
)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
""" If the board was not defined, we create a new as default """
|
||||
"""If the board was not defined, we create a new as default"""
|
||||
|
||||
if not self.board:
|
||||
ms = Minesweeper(self.rows, self.cols, self.mines)
|
||||
@@ -72,13 +66,13 @@ class Game(models.Model):
|
||||
super(Game, self).save(*args, **kwargs)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Game"
|
||||
verbose_name_plural = "Games"
|
||||
db_table = "games"
|
||||
verbose_name = 'Game'
|
||||
verbose_name_plural = 'Games'
|
||||
db_table = 'games'
|
||||
|
||||
|
||||
class EventTypes(EnumChoicesBase):
|
||||
""" Event types to generate a game timeline """
|
||||
"""Event types to generate a game timeline"""
|
||||
|
||||
START_GAME = 0
|
||||
PAUSE = 1
|
||||
@@ -92,32 +86,32 @@ class EventTypes(EnumChoicesBase):
|
||||
|
||||
|
||||
class GameEvent(models.Model):
|
||||
created_at = models.DateTimeField("Creation date", auto_now_add=True)
|
||||
game = models.ForeignKey("game.Game", on_delete=models.CASCADE)
|
||||
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.CLICK_NAIVE,
|
||||
help_text="The game event",
|
||||
help_text='The game event',
|
||||
)
|
||||
|
||||
row = models.PositiveIntegerField(
|
||||
"The row clicked",
|
||||
'The row clicked',
|
||||
default=None,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="Row on the board where the event occurred, if applicable",
|
||||
help_text='Row on the board where the event occurred, if applicable',
|
||||
)
|
||||
col = models.PositiveIntegerField(
|
||||
"The column clicked",
|
||||
'The column clicked',
|
||||
default=None,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="Column on the board where the event occurred, if applicable",
|
||||
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"
|
||||
ordering = ['created_at']
|
||||
verbose_name = 'Game event'
|
||||
verbose_name_plural = 'Game events'
|
||||
db_table = 'game_events'
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
from django.db.models.signals import post_save, pre_save
|
||||
from django.dispatch import receiver
|
||||
|
||||
from .models import Game, GameEvent, EventTypes, GameStatuses
|
||||
from .game import Minesweeper
|
||||
from .models import EventTypes, Game, GameEvent, GameStatuses
|
||||
|
||||
|
||||
@receiver(post_save, sender=Game)
|
||||
def game_start(sender, signal, instance, **kwargs):
|
||||
""" If the game was just created, insert the first event START_GAME """
|
||||
"""If the game was just created, insert the first event START_GAME"""
|
||||
if not instance.status == GameStatuses.NOT_PLAYED:
|
||||
return
|
||||
GameEvent.objects.get_or_create(game=instance, type=EventTypes.START_GAME)
|
||||
@@ -15,7 +15,7 @@ def game_start(sender, signal, instance, **kwargs):
|
||||
|
||||
@receiver(pre_save, sender=GameEvent)
|
||||
def identify_click_event(sender, signal, instance, **kwargs):
|
||||
""" Verify what is on the naive click: mine, point or empty """
|
||||
"""Verify what is on the naive click: mine, point or empty"""
|
||||
if not instance.type == EventTypes.CLICK_NAIVE:
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user