Browse Source

finish create_game.html, add Role enum

feature_tests
Burathar 4 years ago
parent
commit
35455dc465
  1. 26
      app/forms.py
  2. 8
      app/models.py
  3. 8
      app/routes.py
  4. 119
      app/templates/create_game.html
  5. 4
      the_hunt.py

26
app/forms.py

@ -1,7 +1,8 @@ @@ -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): @@ -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')
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

8
app/models.py

@ -6,6 +6,12 @@ from sqlalchemy.sql import func @@ -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): @@ -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')

8
app/routes.py

@ -1,7 +1,7 @@ @@ -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(): @@ -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

119
app/templates/create_game.html

@ -2,46 +2,107 @@ @@ -2,46 +2,107 @@
{% import 'bootstrap/wtf.html' as wtf %}
{% block head %}
{{ super() }}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css">
{{ super() }}
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css">
{% endblock %}
{% block app_content %}
<h1>Create a new game</h1>
<div class="row">
<div class="col-md-4">
{{ wtf.quick_form(form) }}
<h1>Create a new game</h1>
<div class="col-md-4 col-sm-6 col-xs-8">
<hr>
<form action="" method="post" class="form" role="form">
{{ form.csrf_token() }}
{{ form.timezone }}
{{ wtf.form_field(form.game_name, class='form-control') }}
{{ form.start_time.label }}
<div class="form-group row">
<div class="col-sm-7">
{{ form.start_time }}
</div>
<div class="col-sm-5 align-self-center">
{{ wtf.form_field(form.start_time_disabled, class='form-control') }}
</div>
</div>
</div>
{{ form.end_time.label }}
<div class="form-group row">
<div class="col-sm-7">
{{ form.end_time }}
</div>
<div class="col-sm-5 align-self-center">
{{ wtf.form_field(form.end_time_disabled, class='form-control') }}
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<hr>
</div>
{% endblock %}
TODO:
- maak time picker 24 uur based
https://eonasdan.github.io/bootstrap-datetimepicker/Options/#locale+
- optie voor nu
{% block scripts %}
{{ super() }}
<!-- TODO: Scripts downloaden naar repo? -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
<script type="text/javascript">
{{ super() }}
<!-- TODO: Scripts downloaden naar repo? -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>
<script type="text/javascript" src="https://momentjs.com/downloads/moment-timezone-with-data.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker({
useCurrent: false, //Important! See issue #1075
locale: 'en-gb'
});
$('#datetimepicker2').datetimepicker({
useCurrent: false, //Important! See issue #1075
locale: 'en-gb'
});
$("#datetimepicker1").on("dp.change", function (e) {
$('#datetimepicker2').data("DateTimePicker").minDate(e.date);
});
$("#datetimepicker2").on("dp.change", function (e) {
$('#datetimepicker1').data("DateTimePicker").maxDate(e.date);
});
var date = new Date()
$('#datetimepicker_start').datetimepicker({
//useCurrent: false, //Important! See issue #1075
locale: 'en-gb',
format: 'DD-MM-YYYY HH:mm',
keepInvalid: true,
sideBySide: true,
defaultDate: date,
timeZone: moment.tz.guess()
});
date.setDate(date.getDate() + 1)
$('#datetimepicker_end').datetimepicker({
//useCurrent: false, //Important! See issue #1075
locale: 'en-gb',
format: 'DD-MM-YYYY HH:mm',
keepInvalid: true,
sideBySide: true,
defaultDate: date,
timeZone: moment.tz.guess()
});
$("#datetimepicker_start").on("dp.change", function (e) {
$('#datetimepicker_end').data("DateTimePicker").minDate(e.date);
});
$("#datetimepicker_end").on("dp.change", function (e) {
$('#datetimepicker_start').data("DateTimePicker").maxDate(e.date);
});
$("#start_time_disabled").change(function() {
updateDateTimePicker('#datetimepicker_start', '#start_time_disabled')
});
$("#end_time_disabled").change(function() {
updateDateTimePicker('#datetimepicker_end', '#end_time_disabled')
});
function updateDateTimePicker(picker, checkbox){
if ($(checkbox).prop("checked")) {
$(picker).data("DateTimePicker").disable();
}
else {
$(picker).data("DateTimePicker").enable();
}
}
updateDateTimePicker('#datetimepicker_start', '#start_time_disabled');
updateDateTimePicker('#datetimepicker_end', '#end_time_disabled');
$('#timezone')[0].value = moment.tz.guess();
});
</script>
</script>
{% endblock %}

4
the_hunt.py

@ -1,10 +1,10 @@ @@ -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}

Loading…
Cancel
Save