|
1 | | -import time |
| 1 | +from datetime import datetime |
2 | 2 | from flask import Blueprint, redirect, render_template, flash, request, jsonify, session |
3 | 3 | from flask_app import admin_required, db, set_route, lang_url_for as url_for, socketio |
4 | 4 | from flask_app.admin.parcours import calc_points_dist |
5 | 5 | from flask_login import login_required, current_user |
6 | 6 | from flask_app.models import Event, Edition, PassageKey, Stand, Parcours, Passage, User, Inscription, Trace |
7 | | -from datetime import datetime |
8 | | -from flask_app.admin.editions.passages.form import NewKeyForm, ChronoLoginForm, ChronoLoginForm, SetPassageForm |
9 | | -import secrets |
10 | 7 | from flask_socketio import join_room, leave_room, emit |
11 | 8 | from flask_app.admin.editions.passages import get_passage_data |
12 | 9 |
|
@@ -35,19 +32,40 @@ def parcours_disconnect(): |
35 | 32 | def get_parcours_passages(parcours): |
36 | 33 | parcours = Parcours.query.get(parcours) |
37 | 34 | edition = Edition.query.get(session['room'].split('-')[3]) |
38 | | - inscription:list[Inscription] = Inscription.query.filter_by(edition=edition, parcours=parcours).all() |
| 35 | + inscriptions:list[Inscription] = Inscription.query.filter_by(edition=edition, parcours=parcours).all() |
39 | 36 | data = [] |
40 | | - for coureur in inscription: |
| 37 | + for coureur in inscriptions: |
41 | 38 | passage = coureur.passages.order_by(Passage.time_stamp.desc()).first() |
| 39 | + first_passage:Passage = coureur.passages.order_by(Passage.time_stamp.asc()).first() |
42 | 40 | if coureur.has_started(): |
43 | | - ic(coureur.has_started(), coureur.has_finish(), coureur.has_all_right()) |
44 | 41 | pass_data = get_passage_data(passage, json=True) |
45 | | - pass_data.update({'started':True, 'finish':coureur.has_finish(), 'all_right':coureur.has_all_right()}) |
| 42 | + pass_data.update({'started':True, 'start_time':first_passage.time_stamp.timestamp(), 'id':coureur.id, 'finish':coureur.has_finish(), 'all_right':coureur.has_all_right(), 'end':coureur.end}) |
46 | 43 | data.append(pass_data) |
47 | 44 | else: |
48 | | - data.append({'started':False, 'dossard':coureur.dossard, 'name':coureur.inscrit.name}) |
| 45 | + data.append({'started':False, 'id':coureur.id, 'dossard':coureur.dossard, 'name':coureur.inscrit.name}) |
49 | 46 | return data |
50 | 47 |
|
| 48 | +@socketio.on('launch_parcours', namespace='/edition/parcours') |
| 49 | +def launch_parcours(data): |
| 50 | + parcours = Parcours.query.get(data['parcours_id']) |
| 51 | + edition = Edition.query.get(session['room'].split('-')[3]) |
| 52 | + if parcours is not None and data.get('start_time'): |
| 53 | + start_time =datetime.fromtimestamp(data['start_time']/1000) |
| 54 | + for inscription in edition.inscriptions.filter(Inscription.parcours==parcours).all(): |
| 55 | + if inscription.passages.count()==0: |
| 56 | + passage = Passage(time_stamp=start_time, inscription_id = inscription.id) |
| 57 | + db.session.add(passage) |
| 58 | + db.session.commit() |
| 59 | + db.session.refresh(passage) |
| 60 | + emit('new_passage', {'time':str(start_time), 'user':inscription.inscrit.username, 'dossard':inscription.dossard,'key':'', 'stand':passage.get_stand().name}, namespace='/dashboard', to=f'{passage.key.event.id}-{passage.key.edition.id}') |
| 61 | + |
| 62 | + first_passage:Passage = inscription.passages.order_by(Passage.time_stamp.asc()).first() |
| 63 | + pass_data = get_passage_data(passage, json=True) |
| 64 | + pass_data.update({'started':True, 'parcours_id':inscription.parcours.id, 'start_time':first_passage.time_stamp.timestamp() , 'id':inscription.id, 'finish':inscription.has_finish(), 'all_right':inscription.has_all_right(), 'end':inscription.end}) |
| 65 | + emit('new_passage', pass_data, namespace='/edition/parcours', to=f'edition-parcours-{inscription.event.id}-{inscription.edition.id}') |
| 66 | + |
| 67 | + |
| 68 | + |
51 | 69 | @set_route(parcours, '/event/<event_name>/editions/<edition_name>/parcours') |
52 | 70 | @login_required |
53 | 71 | @admin_required |
|
0 commit comments