diff --git a/app/models/game.py b/app/models/game.py index b7cd1d7..23c7449 100644 --- a/app/models/game.py +++ b/app/models/game.py @@ -68,8 +68,30 @@ class Game(db.Model): [player.player_caught_players for player in self.players] for pcp in pcps if pcp.review == Review.none] - def is_active(self): + def update_state(self): now = datetime.utcnow() - return now > (self.start_time or datetime.min) and \ - now < (self.end_time or datetime.max) and \ - self.state == GameState.active \ No newline at end of file + start = self.start_time or datetime.min + + 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 diff --git a/app/models/game_state.py b/app/models/game_state.py index 49fc767..8b5b22f 100644 --- a/app/models/game_state.py +++ b/app/models/game_state.py @@ -1,7 +1,7 @@ from enum import Enum class GameState(Enum): - initiated = 1 + hidden = 1 published = 2 active = 3 interrupted = 4 diff --git a/database_dump.txt b/database_dump.txt index ffe3c9b..b78e4de 100644 --- a/database_dump.txt +++ b/database_dump.txt @@ -8,14 +8,14 @@ INSERT INTO alembic_version VALUES('3ef4c34115fd'); CREATE TABLE game ( id INTEGER NOT NULL, name VARCHAR(64) NOT NULL, - state VARCHAR(11) DEFAULT 'initiated' NOT NULL, + state VARCHAR(11) DEFAULT 'hidden' NOT NULL, start_time DATETIME, end_time DATETIME, 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(2,'Spelletje','initiated',NULL,NULL); +INSERT INTO game VALUES(1,'Marijns Game','hidden','2020-07-09 12:56:00.000000',NULL); +INSERT INTO game VALUES(2,'Spelletje','hidden',NULL,NULL); CREATE TABLE player ( id INTEGER NOT NULL, name VARCHAR(64) NOT NULL, diff --git a/documentation/gamestate.txt b/documentation/gamestate.txt index e02f54e..a8e1a9b 100644 --- a/documentation/gamestate.txt +++ b/documentation/gamestate.txt @@ -1,7 +1,7 @@ Gamestate 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 active = 3 interrupted = 4 diff --git a/migrations/versions/d87b14d6a90c_add_pcp_review.py b/migrations/versions/d87b14d6a90c_add_pcp_review.py index be5ed93..03985d1 100644 --- a/migrations/versions/d87b14d6a90c_add_pcp_review.py +++ b/migrations/versions/d87b14d6a90c_add_pcp_review.py @@ -21,7 +21,7 @@ def upgrade(): op.create_table('game', sa.Column('id', sa.Integer(), 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('end_time', sa.DateTime(), nullable=True), sa.PrimaryKeyConstraint('id')