Browse Source

remove state selectfield from create and modify game

testing
Burathar 4 years ago
parent
commit
7c6494cd5e
  1. 1
      app/main/forms.py
  2. 9
      app/main/routes.py
  3. 22
      app/models/game.py
  4. 1
      app/templates/create_game.html

1
app/main/forms.py

@ -8,7 +8,6 @@ from app import Config
class CreateGameForm(FlaskForm): class CreateGameForm(FlaskForm):
game_name = StringField('Game Name', validators=[InputRequired(), Length(min=0, max=64)]) 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_disabled = BooleanField('No start time')
start_time = DateTimeField(id='datetimepicker_start', format="%d-%m-%Y %H:%M") start_time = DateTimeField(id='datetimepicker_start', format="%d-%m-%Y %H:%M")
end_time_disabled = BooleanField('No end time') end_time_disabled = BooleanField('No end time')

9
app/main/routes.py

@ -36,13 +36,12 @@ def index():
@login_required @login_required
def create_game(): def create_game():
form = CreateGameForm() form = CreateGameForm()
form.state.choices = [(state.value, state.name) for state in GameState]
if form.validate_on_submit(): if form.validate_on_submit():
game = Game(name=form.game_name.data, game = Game(name=form.game_name.data,
start_time=form.start_time.data, start_time=form.start_time.data,
end_time=form.end_time.data, end_time=form.end_time.data)
state=GameState(form.state.data)) game.update_state()
game.players.append(GamePlayer(user=current_user, role=Role['owner'])) game.players.append(GamePlayer(user=current_user, role=Role['owner']))
db.session.add(game) db.session.add(game)
db.session.commit() db.session.commit()
@ -60,11 +59,9 @@ def change_game_settings(game_name):
abort(403) abort(403)
form = CreateGameForm() form = CreateGameForm()
form.state.choices = [(state.value, state.name) for state in GameState]
form.old_name = game.name form.old_name = game.name
if request.method == 'GET': if request.method == 'GET':
form.state.default = game.get_state().value
# pylint: disable=no-member # pylint: disable=no-member
form.process() form.process()
form.game_name.data = game.name form.game_name.data = game.name
@ -83,7 +80,7 @@ def change_game_settings(game_name):
game.name = form.game_name.data game.name = form.game_name.data
game.start_time = form.start_time.data game.start_time = form.start_time.data
game.end_time = form.end_time.data game.end_time = form.end_time.data
game.state = GameState(form.state.data) game.update_state()
db.session.commit() db.session.commit()
flash(f"'{game.name}' had been updated!") flash(f"'{game.name}' had been updated!")
return redirect(url_for('main.game_dashboard', game_name=game.name)) return redirect(url_for('main.game_dashboard', game_name=game.name))

22
app/models/game.py

@ -69,25 +69,35 @@ class Game(db.Model):
for pcp in pcps if pcp.review == Review.none] for pcp in pcps if pcp.review == Review.none]
def update_state(self): 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() 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 now < start: # Before Game
if self.state == GameState.hidden: if self.state == GameState.hidden:
return return False
self.state = GameState.published self.state = GameState.published
end = self.end_time or datetime.max end = self.end_time.replace(tzinfo=None) or datetime.max
if now > start and now < end: # During Game if start < now < end: # During Game
if self.state == GameState.interrupted: if self.state == GameState.interrupted:
return return False
self.state = GameState.active self.state = GameState.active
if now > end: # After Game if now > end: # After Game
if self.state == GameState.hidden: if self.state == GameState.hidden:
return return False
self.state = GameState.finished self.state = GameState.finished
if self.state != old_state:
return True
return False
def is_active(self): def is_active(self):
self.update_state() self.update_state()
return self.state == GameState.active return self.state == GameState.active

1
app/templates/create_game.html

@ -15,7 +15,6 @@
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ form.timezone }} {{ form.timezone }}
{{ wtf.form_field(form.game_name, class='form-control') }} {{ wtf.form_field(form.game_name, class='form-control') }}
{{ wtf.form_field(form.state, class='form-control') }}
{{ form.start_time.label }} {{ form.start_time.label }}
<div class="form-group row"> <div class="form-group row">

Loading…
Cancel
Save