From 35455dc46520909110049e913d146bb1ca2697f6 Mon Sep 17 00:00:00 2001 From: Burathar Date: Mon, 6 Jul 2020 19:18:08 +0200 Subject: [PATCH] finish create_game.html, add Role enum --- app/forms.py | 26 +++++-- app/models.py | 8 ++- app/routes.py | 8 ++- app/templates/create_game.html | 119 +++++++++++++++++++++++++-------- the_hunt.py | 4 +- 5 files changed, 126 insertions(+), 39 deletions(-) diff --git a/app/forms.py b/app/forms.py index 40a889e..8f56215 100644 --- a/app/forms.py +++ b/app/forms.py @@ -1,7 +1,8 @@ from flask_wtf import FlaskForm -from wtforms import StringField, PasswordField, BooleanField, SubmitField, TextAreaField, DateTimeField +from wtforms import StringField, PasswordField, BooleanField, SubmitField, TextAreaField, DateTimeField, BooleanField, HiddenField from wtforms.validators import DataRequired, EqualTo, ValidationError, Length from app.models import Player +from pytz import timezone class LoginForm(FlaskForm): username = StringField('Username', validators=[DataRequired()]) @@ -23,6 +24,23 @@ class RegistrationForm(FlaskForm): class CreateGameForm(FlaskForm): game_name = StringField('Game Name', validators=[DataRequired(), Length(min=0, max=64)]) - start_time = DateTimeField(id='datetimepicker1') - end_time = DateTimeField(id='datetimepicker2') - submit = SubmitField('Register') \ No newline at end of file + 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') + end_time = DateTimeField(id='datetimepicker_end', format="%d-%m-%Y %H:%M") + timezone = HiddenField(validators=[DataRequired()]) + submit = SubmitField('Register') + + def validate_start_time(self, start_time): + self.date_time_validator(self.start_time_disabled, start_time) + + def validate_end_time(self, end_time): + self.date_time_validator(self.end_time_disabled, end_time) + + def date_time_validator(self, disabled, date_time): + if disabled.data: + date_time.data = None + return + clientzone = timezone(self.timezone.data) + date_time_utc = clientzone.localize(date_time.data).astimezone(timezone('UTC')) + date_time.data = date_time_utc diff --git a/app/models.py b/app/models.py index ce130d8..ed04440 100644 --- a/app/models.py +++ b/app/models.py @@ -6,6 +6,12 @@ from sqlalchemy.sql import func from secrets import token_hex from flask_login import UserMixin +class Role(Enum): + none = 0 + owner = 1 + hunter = 2 + bunny = 3 + class GameState(Enum): initiated = 1 published = 2 @@ -17,7 +23,7 @@ class GamePlayer(db.Model): __tablename__ = 'game_player' game_id = db.Column(db.Integer, db.ForeignKey('game.id'), primary_key=True, nullable=False) player_id = db.Column(db.Integer, db.ForeignKey('player.id'), primary_key=True, nullable=False) - role = db.Column(db.String(16)) + role = db.Column(db.Enum(Role), server_default=Role(0).name) game = db.relationship('Game', back_populates='game_players') player = db.relationship('Player', back_populates='player_games') diff --git a/app/routes.py b/app/routes.py index e78f47f..6357a14 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,7 +1,7 @@ from flask import render_template, flash, redirect, url_for, request from flask_login import login_user, logout_user, current_user, login_required from app import app, db -from app.models import Player, Game +from app.models import Player, Game, Role from app.forms import LoginForm, RegistrationForm, CreateGameForm @app.route('/', methods=['GET']) @@ -48,8 +48,10 @@ def register(): def create_game(): form = CreateGameForm() if form.validate_on_submit(): - game = Game(name=form.game_name.data) - return render_template('create_game.html', title='Create Game!', form=form) + game = Game(name=form.game_name.data, start_time=form.start_time.data, end_time=form.end_time.data) + game.gameplayers.append(GamePlayer(player=current_user, role=Role['owner'])) #check if this works, otherwise use 'owner' + print(game.start_time) + return render_template('create_game.html', title='Create Game', form=form) return render_template('create_game.html', title='Create Game', form=form) @login_required diff --git a/app/templates/create_game.html b/app/templates/create_game.html index e9fd41d..1fb6f7f 100644 --- a/app/templates/create_game.html +++ b/app/templates/create_game.html @@ -2,46 +2,107 @@ {% import 'bootstrap/wtf.html' as wtf %} {% block head %} - {{ super() }} - +{{ super() }} + {% endblock %} {% block app_content %} -

Create a new game

-
-
- {{ wtf.quick_form(form) }} +

Create a new game

+ +
+
+
+ {{ form.csrf_token() }} + {{ form.timezone }} + {{ wtf.form_field(form.game_name, class='form-control') }} + + {{ form.start_time.label }} +
+
+ {{ form.start_time }} +
+
+ {{ wtf.form_field(form.start_time_disabled, class='form-control') }} +
-
+ + {{ form.end_time.label }} +
+
+ {{ form.end_time }} +
+
+ {{ wtf.form_field(form.end_time_disabled, class='form-control') }} +
+
+ + + +
+
+ + + + + {% endblock %} TODO: -- maak time picker 24 uur based https://eonasdan.github.io/bootstrap-datetimepicker/Options/#locale+ - optie voor nu {% block scripts %} - {{ super() }} - - - - - + + + + {% endblock %} \ No newline at end of file diff --git a/the_hunt.py b/the_hunt.py index ec10957..7fd9fdc 100644 --- a/the_hunt.py +++ b/the_hunt.py @@ -1,10 +1,10 @@ from app import app, db from app.models import Game, Player, Objective, Location, Notification, GamePlayer, \ - PlayerFoundObjective, NotificationPlayer, PlayerCaughtPlayer, GameState + PlayerFoundObjective, NotificationPlayer, PlayerCaughtPlayer, Role, GameState @app.shell_context_processor def make_shell_context(): return {'db': db, 'Game' : Game, 'Player' : Player, 'Objective' : Objective, 'Location' : Location, 'Notification' : Notification, 'GamePlayer' : GamePlayer, 'PlayerFoundObjective' : PlayerFoundObjective, 'NotificationPlayer' : NotificationPlayer, - 'PlayerCaughtPlayer' : PlayerCaughtPlayer, 'GameState' : GameState} + 'PlayerCaughtPlayer' : PlayerCaughtPlayer, 'Role' : Role, 'GameState' : GameState}