Browse Source

implement update_state and rename GameState.initiated to GameState.hidden

testing
Burathar 4 years ago
parent
commit
9f86805787
  1. 30
      app/models/game.py
  2. 2
      app/models/game_state.py
  3. 8
      database_dump.txt
  4. 2
      documentation/gamestate.txt
  5. 2
      migrations/versions/d87b14d6a90c_add_pcp_review.py

30
app/models/game.py

@ -68,8 +68,30 @@ class Game(db.Model):
[player.player_caught_players for player in self.players] [player.player_caught_players for player in self.players]
for pcp in pcps if pcp.review == Review.none] for pcp in pcps if pcp.review == Review.none]
def is_active(self): def update_state(self):
now = datetime.utcnow() now = datetime.utcnow()
return now > (self.start_time or datetime.min) and \ start = self.start_time or datetime.min
now < (self.end_time or datetime.max) and \
self.state == GameState.active if now < start: # Before Game
if self.state == GameState.hidden:
return
self.state = GameState.published
end = self.end_time or datetime.max
if now > start and now < end: # During Game
if self.state == GameState.interrupted:
return
self.state = GameState.active
if now > end: # After Game
if self.state == GameState.hidden:
return
self.state = GameState.finished
def is_active(self):
self.update_state()
return self.state == GameState.active
def get_state(self):
self.update_state()
return self.state

2
app/models/game_state.py

@ -1,7 +1,7 @@
from enum import Enum from enum import Enum
class GameState(Enum): class GameState(Enum):
initiated = 1 hidden = 1
published = 2 published = 2
active = 3 active = 3
interrupted = 4 interrupted = 4

8
database_dump.txt

@ -8,14 +8,14 @@ INSERT INTO alembic_version VALUES('3ef4c34115fd');
CREATE TABLE game ( CREATE TABLE game (
id INTEGER NOT NULL, id INTEGER NOT NULL,
name VARCHAR(64) NOT NULL, name VARCHAR(64) NOT NULL,
state VARCHAR(11) DEFAULT 'initiated' NOT NULL, state VARCHAR(11) DEFAULT 'hidden' NOT NULL,
start_time DATETIME, start_time DATETIME,
end_time DATETIME, end_time DATETIME,
PRIMARY KEY (id), PRIMARY KEY (id),
CONSTRAINT gamestate CHECK (state IN ('initiated', 'published', 'active', 'interrupted', 'finished')) CONSTRAINT gamestate CHECK (state IN ('hidden', 'published', 'active', 'interrupted', 'finished'))
); );
INSERT INTO game VALUES(1,'Marijns Game','initiated','2020-07-09 12:56:00.000000',NULL); INSERT INTO game VALUES(1,'Marijns Game','hidden','2020-07-09 12:56:00.000000',NULL);
INSERT INTO game VALUES(2,'Spelletje','initiated',NULL,NULL); INSERT INTO game VALUES(2,'Spelletje','hidden',NULL,NULL);
CREATE TABLE player ( CREATE TABLE player (
id INTEGER NOT NULL, id INTEGER NOT NULL,
name VARCHAR(64) NOT NULL, name VARCHAR(64) NOT NULL,

2
documentation/gamestate.txt

@ -1,7 +1,7 @@
Gamestate Gamestate
This is an attribute of the Game class. It will be implemented as an enum containing the following values: This is an attribute of the Game class. It will be implemented as an enum containing the following values:
initiated = 1 hidden = 1
published = 2 published = 2
active = 3 active = 3
interrupted = 4 interrupted = 4

2
migrations/versions/d87b14d6a90c_add_pcp_review.py

@ -21,7 +21,7 @@ def upgrade():
op.create_table('game', op.create_table('game',
sa.Column('id', sa.Integer(), nullable=False), sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=64), nullable=False), sa.Column('name', sa.String(length=64), nullable=False),
sa.Column('state', sa.Enum('initiated', 'published', 'active', 'interrupted', 'finished', name='gamestate'), server_default='initiated', nullable=False), sa.Column('state', sa.Enum('hidden', 'published', 'active', 'interrupted', 'finished', name='gamestate'), server_default='hidden', nullable=False),
sa.Column('start_time', sa.DateTime(), nullable=True), sa.Column('start_time', sa.DateTime(), nullable=True),
sa.Column('end_time', sa.DateTime(), nullable=True), sa.Column('end_time', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id') sa.PrimaryKeyConstraint('id')

Loading…
Cancel
Save