From 7c6494cd5eebd8df0fdfd89737a44e272f95fe9e Mon Sep 17 00:00:00 2001 From: Burathar Date: Tue, 28 Jul 2020 15:28:29 +0200 Subject: [PATCH] remove state selectfield from create and modify game --- app/main/forms.py | 1 - app/main/routes.py | 9 +++------ app/models/game.py | 22 ++++++++++++++++------ app/templates/create_game.html | 1 - 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/app/main/forms.py b/app/main/forms.py index 88b091d..75cacb5 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -8,7 +8,6 @@ from app import Config class CreateGameForm(FlaskForm): game_name = StringField('Game Name', validators=[InputRequired(), Length(min=0, max=64)]) - state = SelectField('Game State', coerce=int, validators=[InputRequired()]) start_time_disabled = BooleanField('No start time') start_time = DateTimeField(id='datetimepicker_start', format="%d-%m-%Y %H:%M") end_time_disabled = BooleanField('No end time') diff --git a/app/main/routes.py b/app/main/routes.py index 5ca0715..a831ce1 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -36,13 +36,12 @@ def index(): @login_required def create_game(): form = CreateGameForm() - form.state.choices = [(state.value, state.name) for state in GameState] if form.validate_on_submit(): game = Game(name=form.game_name.data, start_time=form.start_time.data, - end_time=form.end_time.data, - state=GameState(form.state.data)) + end_time=form.end_time.data) + game.update_state() game.players.append(GamePlayer(user=current_user, role=Role['owner'])) db.session.add(game) db.session.commit() @@ -60,11 +59,9 @@ def change_game_settings(game_name): abort(403) form = CreateGameForm() - form.state.choices = [(state.value, state.name) for state in GameState] form.old_name = game.name if request.method == 'GET': - form.state.default = game.get_state().value # pylint: disable=no-member form.process() form.game_name.data = game.name @@ -83,7 +80,7 @@ def change_game_settings(game_name): game.name = form.game_name.data game.start_time = form.start_time.data game.end_time = form.end_time.data - game.state = GameState(form.state.data) + game.update_state() db.session.commit() flash(f"'{game.name}' had been updated!") return redirect(url_for('main.game_dashboard', game_name=game.name)) diff --git a/app/models/game.py b/app/models/game.py index 23c7449..7491ba8 100644 --- a/app/models/game.py +++ b/app/models/game.py @@ -69,25 +69,35 @@ class Game(db.Model): for pcp in pcps if pcp.review == Review.none] def update_state(self): + ''' + Updates state according to game.start_time and game.end_time. + Returns True if state is changed. + ''' + #pylint: disable=no-member + old_state = self.state now = datetime.utcnow() - start = self.start_time or datetime.min + start = self.start_time.replace(tzinfo=None) or datetime.min if now < start: # Before Game if self.state == GameState.hidden: - return + return False self.state = GameState.published - end = self.end_time or datetime.max - if now > start and now < end: # During Game + end = self.end_time.replace(tzinfo=None) or datetime.max + if start < now < end: # During Game if self.state == GameState.interrupted: - return + return False self.state = GameState.active if now > end: # After Game if self.state == GameState.hidden: - return + return False self.state = GameState.finished + if self.state != old_state: + return True + return False + def is_active(self): self.update_state() return self.state == GameState.active diff --git a/app/templates/create_game.html b/app/templates/create_game.html index 247c931..c3edbc2 100644 --- a/app/templates/create_game.html +++ b/app/templates/create_game.html @@ -15,7 +15,6 @@ {{ form.hidden_tag() }} {{ form.timezone }} {{ wtf.form_field(form.game_name, class='form-control') }} - {{ wtf.form_field(form.state, class='form-control') }} {{ form.start_time.label }}