Browse Source

work on player page and qrcode

feature_tests
Burathar 4 years ago
parent
commit
595a0f385e
  1. 15
      app/models.py
  2. 21
      app/routes.py
  3. 84
      app/templates/player.html

15
app/models.py

@ -144,16 +144,23 @@ class Player(UserMixin, db.Model): @@ -144,16 +144,23 @@ class Player(UserMixin, db.Model):
def check_password(self, password):
return check_password_hash(self.password_hash, password)
def last_location(self, game=None):
def locations_game(self, game):
# pylint: disable=not-an-iterable
if not self.locations:
return None
if game is None:
return max((location for location in self.locations), key=lambda location: location.timestamp)
return self.locations
game_start = game.start_time or datetime.min
game_end = game.end_time or datetime.max
return max((location for location in self.locations if location.timestamp > game_start and location.timestamp < game_end),
key=lambda location: location.timestamp)
return (location for location in self.locations if location.timestamp > game_start and location.timestamp < game_end)
def last_location(self, game=None):
# pylint: disable=not-an-iterable
if not self.locations:
return None
if game is None:
return max(self.locations, key=lambda location: location.timestamp)
return max(self.locations_game(game), key=lambda location: location.timestamp)
@staticmethod
def delete_orphans():

21
app/routes.py

@ -115,11 +115,22 @@ def game_player(game_name, player_name): @@ -115,11 +115,22 @@ def game_player(game_name, player_name):
gameplayer.role = Role[form.role.data]
db.session.commit()
return redirect(url_for('game_dashboard', game_name=game.name))
return render_template('player.html', title=f'{player.name} in {game_name}', game=game, player=player, form=form)
return render_template('player.html', title=f'{player.name} in {game_name}', game=game, player=player, form=form, json=json, location_encoder=LocationEncoder)
@app.route('/player/<auth_hash>/qrcode.png')
@login_required
def player_qrcode(auth_hash):
player = Player.query.filter_by(auth_hash = auth_hash).first_or_404()
if not is_player_game_owner(player): abort(403)
img = generate_qr_code(url_for('objective', objective_hash=objective.hash, _external=True))
return serve_pil_image(img)
def is_game_owner(game):
return current_user in [gameplayer.player for gameplayer in game.game_players if gameplayer.role == Role.owner]
def is_player_game_owner(player):
return current_user in [gameplayer.player for gameplayer in [game for game in player.games ].game_players if game_player.role == Role.owner]
def is_objective_owner(objective):
return current_user in [gameplayer.player for gameplayer in objective.game.game_players if gameplayer.role == Role.owner]
@ -149,14 +160,14 @@ def delete_objective(objective_hash): @@ -149,14 +160,14 @@ def delete_objective(objective_hash):
db.session.commit()
return redirect(url_for('game_dashboard', game_name=objective.game.name))
def generate_objective_qr_code(objective):
def generate_qr_code(url):
qr = qrcode.QRCode(
version=None,
error_correction=qrcode.constants.ERROR_CORRECT_M,
box_size=30,
border=4,
)
qr.add_data(url_for('objective', objective_hash=objective.hash, _external=True))
qr.add_data(url)
qr.make(fit=True)
return qr.make_image(fill_color='black', back_color='white')
@ -172,7 +183,7 @@ def serve_pil_image(pil_img): @@ -172,7 +183,7 @@ def serve_pil_image(pil_img):
def objective_qrcode(objective_hash):
objective = Objective.query.filter_by(hash = objective_hash).first_or_404()
if not is_objective_owner(objective): abort(403)
img = generate_objective_qr_code(objective)
img = generate_qr_code(url_for('objective', objective_hash=objective.hash, _external=True))
return serve_pil_image(img)
@app.route('/objective/<objective_hash>', methods=['GET', 'POST'])
@ -180,7 +191,7 @@ def objective_qrcode(objective_hash): @@ -180,7 +191,7 @@ def objective_qrcode(objective_hash):
def objective(objective_hash):
objective = Objective.query.filter_by(hash = objective_hash).first_or_404()
owner = is_objective_owner(objective)
qrcode = generate_objective_qr_code(objective) if owner else None
qrcode = generate_qr_code(objective) if owner else None
form = ObjectiveForm()
if form.submit.data and form.validate() and owner:
objective.name = form.objective_name.data

84
app/templates/player.html

@ -1,18 +1,88 @@ @@ -1,18 +1,88 @@
{% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block head %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='assets/leaflet/leaflet.css') }}" />
<script src="{{ url_for('static', filename='assets/leaflet/leaflet.js') }}"></script>
{% endblock %}
{% block app_content %}
<h1>Player: {{ player.name }}</h1>
<hr>
<div class="row">
<div class="col-md-4 col-sm-6 col-xs-8">
<form action="" method="post" class="form" role="form">
{{ form.hidden_tag() }}
{% for gameplayer in player.player_games if gameplayer.game == game %}
{{ wtf.form_field(form.role, class='form-control') }}
{% endfor %}
{{ wtf.form_field(form.submit, class='btn btn-primary', value='Update') }}
</form>
<div class="row">
<form action="" method="post" class="form" role="form">
{{ form.hidden_tag() }}
{% for gameplayer in player.player_games if gameplayer.game == game %}
{{ wtf.form_field(form.role, class='form-control') }}
{% endfor %}
{{ wtf.form_field(form.submit, class='btn btn-primary', value='Update') }}
</form>
</div>
{% if player.auth_hash %}
<div class="row">
<img src="{{ url_for('player_qrcode', auth_hash=player.auth_hash) }}" alt="qr_code_failed", width="100%">
</div>
{% endif %}
</div>
<div id="map" style=" height: 600px; border-radius: 10px; " class="col-md-6 col-xs-12"></div>
</div>
{% endblock %}
{% block scripts %}
{{ super() }}
{{ moment.include_moment() }}
<script type="text/javascript", crossorigin="anonymous">
// Leaflet Map
'{% set last_location = player.last_location(game) %}'
var map = L.map( 'map', {
center: ['{{ last_location.latitude or 52.2 }}', '{{ last_location.longitude or 5.3 }}'],
minZoom: 6,
maxZoom: 19,
bounds: [[50.5, 3.25], [54, 7.6]],
zoom: 9
});
L.control.scale().addTo(map);
L.tileLayer( 'https://geodata.nationaalgeoregister.nl/tiles/service/wmts/brtachtergrondkaartpastel/EPSG:3857/{z}/{x}/{y}.png', {
attribution: 'Kaartgegevens &copy; <a href="kadaster.nl">Kadaster</a>'
}).addTo( map );
var greenIcon = new L.Icon({
iconUrl: "{{ url_for('static', filename='assets/leaflet/images/marker-icon-2x-green.png') }}",
shadowUrl: "{{ url_for('static', filename='assets/leaflet/images/marker-shadow.png') }}",
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41]
});
var locations = JSON.parse('{{ json.dumps(player.locations, cls=location_encoder)|safe }}')
for (var i = 0; i < locations.length; i++){
var playerMarker = L.marker([
locations[i]['latitude'],
locations[i]['longitude']
], {icon: greenIcon}).addTo(map);
var timestamp_utc = moment.utc(locations[i]['timestamp_utc']).toDate()
var timestamp_local = moment(timestamp_utc).local().format('YYYY-MM-DD HH:mm');
playerMarker.bindTooltip(`<b>${locations[i]['player_name']}</b><br>
${timestamp_local}`).openPopup();
}
var latlngs = [
[[45.51, -122.68],
[37.77, -122.43],
[34.04, -118.2]],
[[40.78, -73.91],
[41.83, -87.62],
[32.76, -96.72]]
];
var polyline = L.polyline(locations.map(l => [l.latitude, l.longitude]), {color: 'red'}).addTo(map);
// zoom the map to the polyline
map.fitBounds(polyline.getBounds());
</script>
{% endblock %}
Loading…
Cancel
Save