ajustements des calendriers, ajout de matchs jouer vs joueur
This commit is contained in:
+85
-22
@@ -1,7 +1,7 @@
|
||||
from flask import Blueprint, render_template, redirect, url_for, flash, request, jsonify
|
||||
from flask_login import login_required, current_user
|
||||
from extensions import db
|
||||
from models import User, Tryout, Match, MatchParticipant, Team, TeamMember, OrgTeam
|
||||
from models import User, Tryout, Match, MatchParticipant, Team, TeamMember, OrgTeam, TryoutRegistration
|
||||
from datetime import datetime, time
|
||||
|
||||
matches_bp = Blueprint('matches', __name__, url_prefix='/matches')
|
||||
@@ -97,30 +97,52 @@ def api_events_for_tryout(tryout_id):
|
||||
tryout = Tryout.query.get_or_404(tryout_id)
|
||||
|
||||
# Check if user can view this tryout
|
||||
if not current_user.can_manage_this_tryout(tryout):
|
||||
# For players, check if they're registered or participating in the match
|
||||
if current_user.role == 'player':
|
||||
# Check if player is registered for this tryout
|
||||
is_registered = TryoutRegistration.query.filter_by(
|
||||
tryout_id=tryout_id, player_id=current_user.id
|
||||
).first() is not None
|
||||
|
||||
# Check if player is participating in any matches for this tryout
|
||||
match_participation = MatchParticipant.query.filter(
|
||||
MatchParticipant.match_id == MatchParticipant.match_id
|
||||
).join(Match).filter(Match.tryout_id == tryout_id).all()
|
||||
|
||||
player_in_match = any(mp.player_id == current_user.id for mp in match_participation)
|
||||
|
||||
if not is_registered and not player_in_match:
|
||||
return jsonify([])
|
||||
can_view = current_user.can_manage_this_tryout(tryout)
|
||||
|
||||
# For players, check if they're registered or participating in a match
|
||||
is_registered = False
|
||||
player_in_match = False
|
||||
if current_user.role == 'player':
|
||||
is_registered = TryoutRegistration.query.filter_by(
|
||||
tryout_id=tryout_id, player_id=current_user.id
|
||||
).first() is not None
|
||||
|
||||
# Check if player is participating in any matches for this tryout
|
||||
player_matches = Match.query.join(MatchParticipant).filter(
|
||||
MatchParticipant.player_id == current_user.id,
|
||||
Match.tryout_id == tryout_id
|
||||
).all()
|
||||
player_in_match = len(player_matches) > 0
|
||||
|
||||
# Non-participating players cannot see the calendar
|
||||
if not can_view and not is_registered and not player_in_match:
|
||||
return jsonify([])
|
||||
|
||||
events = []
|
||||
|
||||
# Add tryout date as an event (read-only, for context)
|
||||
events.append({
|
||||
'id': f'tryout_{tryout.id}',
|
||||
'title': f'Tryout: {tryout.title}',
|
||||
'date': tryout.date.strftime('%Y-%m-%d'),
|
||||
'type': 'tryout',
|
||||
'color': '#3b82f6', # Blue for tryouts
|
||||
'extendedProps': {
|
||||
'location': tryout.location or 'TBD',
|
||||
'status': tryout.status,
|
||||
'description': tryout.description or '',
|
||||
'tryout_id': tryout.id
|
||||
}
|
||||
})
|
||||
|
||||
for match in tryout.matches:
|
||||
match_color = '#10b981' if match.match_type == 'team_vs_team' else '#f59e0b'
|
||||
# Determine color based on match type
|
||||
if match.match_type == 'team_vs_team' or match.match_type == 'player_vs_player':
|
||||
match_color = '#10b981' # Green for team matches
|
||||
else:
|
||||
match_color = '#f59e0b' # Orange for scrims
|
||||
|
||||
# Build participant string
|
||||
# Build participant string with proper grouping
|
||||
participants_str = ''
|
||||
if match.match_type == 'team_vs_team':
|
||||
teams = []
|
||||
@@ -129,10 +151,22 @@ def api_events_for_tryout(tryout_id):
|
||||
if match.team2:
|
||||
teams.append(match.team2.name)
|
||||
participants_str = f"{' vs '.join(teams)}"
|
||||
elif match.match_type == 'player_vs_player':
|
||||
# Get players grouped by team side
|
||||
team1_players = [p.player.full_name for p in match.participants.filter_by(team_side=1).all()]
|
||||
team2_players = [p.player.full_name for p in match.participants.filter_by(team_side=2).all()]
|
||||
if team1_players and team2_players:
|
||||
participants_str = f"{', '.join(team1_players)} vs {', '.join(team2_players)}"
|
||||
else:
|
||||
participants_str = 'TBD vs TBD'
|
||||
else:
|
||||
player_names = [p.player.full_name for p in match.participants.all()]
|
||||
participants_str = ', '.join(player_names) if player_names else 'No players'
|
||||
|
||||
# Include time for calendar display
|
||||
start_time_str = match.start_time.strftime('%H:%M') if match.start_time else None
|
||||
end_time_str = match.end_time.strftime('%H:%M') if match.end_time else None
|
||||
|
||||
events.append({
|
||||
'id': f'match_{match.id}',
|
||||
'title': match.title + ' (' + participants_str + ')',
|
||||
@@ -144,7 +178,10 @@ def api_events_for_tryout(tryout_id):
|
||||
'status': match.status,
|
||||
'match_type': match.match_type,
|
||||
'tryout_id': tryout.id,
|
||||
'match_id': match.id
|
||||
'match_id': match.id,
|
||||
'participants': participants_str,
|
||||
'start_time': start_time_str,
|
||||
'end_time': end_time_str
|
||||
}
|
||||
})
|
||||
|
||||
@@ -245,6 +282,17 @@ def create_match(tryout_id):
|
||||
match.team1_id = int(team1_id) if team1_id else None
|
||||
match.team2_id = int(team2_id) if team2_id else None
|
||||
|
||||
# Handle player vs player matches
|
||||
elif match_type == 'player_vs_player':
|
||||
team1_player_ids = request.form.getlist('team1_player_ids')
|
||||
team2_player_ids = request.form.getlist('team2_player_ids')
|
||||
for pid in team1_player_ids:
|
||||
participant = MatchParticipant(match_id=match.id, player_id=int(pid), team_side=1)
|
||||
db.session.add(participant)
|
||||
for pid in team2_player_ids:
|
||||
participant = MatchParticipant(match_id=match.id, player_id=int(pid), team_side=2)
|
||||
db.session.add(participant)
|
||||
|
||||
# Handle player scrim matches
|
||||
elif match_type == 'player_scrim':
|
||||
player_ids = request.form.getlist('player_ids')
|
||||
@@ -273,6 +321,9 @@ def edit_match(match_id):
|
||||
teams = Team.query.filter_by(tryout_id=tryout.id).all()
|
||||
all_players = User.query.filter_by(role='player').order_by(User.full_name).all()
|
||||
current_player_ids = [p.player_id for p in match.participants.all()]
|
||||
# Get players grouped by team side for player_vs_player matches
|
||||
team1_player_ids = [p.player_id for p in match.participants.filter_by(team_side=1).all()]
|
||||
team2_player_ids = [p.player_id for p in match.participants.filter_by(team_side=2).all()]
|
||||
|
||||
if request.method == 'POST':
|
||||
match.title = request.form.get('title')
|
||||
@@ -308,6 +359,18 @@ def edit_match(match_id):
|
||||
match.team1_id = int(team1_id) if team1_id else None
|
||||
match.team2_id = int(team2_id) if team2_id else None
|
||||
|
||||
# Handle player vs player matches - update participants
|
||||
elif match.match_type == 'player_vs_player':
|
||||
MatchParticipant.query.filter_by(match_id=match.id).delete()
|
||||
team1_player_ids = request.form.getlist('team1_player_ids')
|
||||
team2_player_ids = request.form.getlist('team2_player_ids')
|
||||
for pid in team1_player_ids:
|
||||
participant = MatchParticipant(match_id=match.id, player_id=int(pid), team_side=1)
|
||||
db.session.add(participant)
|
||||
for pid in team2_player_ids:
|
||||
participant = MatchParticipant(match_id=match.id, player_id=int(pid), team_side=2)
|
||||
db.session.add(participant)
|
||||
|
||||
# Handle player scrim matches - update participants
|
||||
elif match.match_type == 'player_scrim':
|
||||
MatchParticipant.query.filter_by(match_id=match.id).delete()
|
||||
@@ -320,7 +383,7 @@ def edit_match(match_id):
|
||||
flash('Match updated successfully!', 'success')
|
||||
return redirect(url_for('tryouts.view_tryout', tryout_id=tryout.id))
|
||||
|
||||
return render_template('pages/edit_match.html', match=match, tryout=tryout, teams=teams, all_players=all_players, current_player_ids=current_player_ids)
|
||||
return render_template('pages/edit_match.html', match=match, tryout=tryout, teams=teams, all_players=all_players, current_player_ids=current_player_ids, team1_player_ids=team1_player_ids, team2_player_ids=team2_player_ids)
|
||||
|
||||
|
||||
@matches_bp.route('/<int:match_id>/delete', methods=['POST'])
|
||||
|
||||
Reference in New Issue
Block a user