chhangement du comportement du discord bot sur modif des matchs
CI - Security, Lint & Tests / validate (push) Failing after 59s

This commit is contained in:
cedrick2711
2026-09-08 12:46:44 -04:00
parent 4a2e2f053f
commit b9791d1547
4 changed files with 145 additions and 55 deletions
+92 -43
View File
@@ -570,6 +570,12 @@ def edit_match(match_id):
flash_validation_errors(err)
return rerender()
# Snapshot the date/time before we overwrite them, so we can detect
# whether only these fields changed and choose the right Discord message.
old_date = match.date
old_start = match.start_time
old_end = match.end_time
# Assigned only once the whole form has been accepted. Assigning as
# each field was read meant a form rejected halfway had already
# changed the record in the session.
@@ -581,60 +587,103 @@ def edit_match(match_id):
match.location = data['location']
match.status = data['status']
time_changed = (
match.date != old_date or match.start_time != old_start or match.end_time != old_end
)
# Build the desired participant set and reconcile it against the
# existing participants, rather than deleting and recreating everyone.
# This preserves attendance_confirmed for players who stay.
desired = set()
if match.match_type == 'team_vs_team':
match.team1_id = data['team1_id']
match.team2_id = data['team2_id']
for team_id in (data['team1_id'], data['team2_id']):
if team_id:
for member in TeamMember.query.filter_by(team_id=team_id).all():
desired.add(member.player_id)
elif match.match_type == 'player_vs_player':
for pid in data['team1_player_ids']:
desired.add(pid)
for pid in data['team2_player_ids']:
desired.add(pid)
else:
for pid in data['player_ids']:
desired.add(pid)
existing = {
p.player_id: p for p in MatchParticipant.query.filter_by(match_id=match.id).all()
}
removed_ids = []
notified_player_ids = []
notified_participant_ids = []
kept_ids = set()
# Capture old participant ids before any deletion, so we can purge
# their pending Discord notifications once the change is committed.
stale_participant_ids = [
p.id for p in MatchParticipant.query.filter_by(match_id=match.id).all()
]
if match.match_type == 'team_vs_team':
teams_changed = data['team1_id'] != match.team1_id or data['team2_id'] != match.team2_id
if teams_changed:
MatchParticipant.query.filter_by(match_id=match.id).delete()
match.team1_id = data['team1_id']
match.team2_id = data['team2_id']
notified_player_ids, notified_participant_ids = create_participants(match, data)
for player_id, participant in list(existing.items()):
if player_id in desired:
kept_ids.add(player_id)
notified_player_ids.append(player_id)
notified_participant_ids.append(participant.id)
else:
# Same teams: the roster stands, but everyone is told again,
# because the date or the time may have moved.
# Reuse the existing participant ids so reference_id is always
# a real participant row, never the match id (fallback).
for team_id in (match.team1_id, match.team2_id):
if team_id:
for member in TeamMember.query.filter_by(team_id=team_id).all():
notified_player_ids.append(member.player_id)
existing = MatchParticipant.query.filter_by(
match_id=match.id, player_id=member.player_id
).first()
if existing:
notified_participant_ids.append(existing.id)
stale_participant_ids = [
pid for pid in stale_participant_ids if pid != existing.id
]
else:
MatchParticipant.query.filter_by(match_id=match.id).delete()
notified_player_ids, notified_participant_ids = create_participants(match, data)
removed_ids.append(participant.id)
db.session.delete(participant)
# New players get new participant rows.
for player_id in desired - set(existing.keys()):
participant = MatchParticipant(match_id=match.id, player_id=player_id)
db.session.add(participant)
db.session.flush()
notified_player_ids.append(player_id)
notified_participant_ids.append(participant.id)
kept_ids.add(player_id)
# When the date or time changed, reset attendance on everyone who
# stayed. They'll be asked to re-confirm via the modified message.
if time_changed:
for participant in MatchParticipant.query.filter_by(match_id=match.id).all():
if participant.player_id in kept_ids:
participant.attendance_confirmed = False
db.session.commit()
# Purge Discord pending entries for participants that no longer exist.
# This runs after the commit so the bot only sees the durable state.
if stale_participant_ids:
if removed_ids:
from app.discord_bot import purge_pending_for_participants
purge_pending_for_participants(stale_participant_ids)
purge_pending_for_participants(removed_ids)
notify_participants(
title=match.title,
date=match.date,
start_time=match.start_time,
end_time=match.end_time,
participants=zip_participants(notified_player_ids, notified_participant_ids),
fallback_id=match.id,
)
# Decide whether and what to notify.
if time_changed:
# All current participants get the "modified" message.
all_current = list(zip(notified_player_ids, notified_participant_ids))
notify_participants(
title=match.title,
date=match.date,
start_time=match.start_time,
end_time=match.end_time,
participants=all_current,
fallback_id=match.id,
change_kind='modified',
)
elif desired - set(existing.keys()):
# Only the newly added players are told about the match.
new_player_ids = desired - set(existing.keys())
new_pairs = [
(pid, nid)
for pid, nid in zip(notified_player_ids, notified_participant_ids)
if pid in new_player_ids
]
notify_participants(
title=match.title,
date=match.date,
start_time=match.start_time,
end_time=match.end_time,
participants=new_pairs,
fallback_id=match.id,
change_kind='scheduled',
)
# else: nothing changed that needs a notification.
flash(_('Match updated successfully!'), 'success')
return respond_after_change(url_for('tryouts.view_tryout', tryout_id=tryout.id))