feat: implement table distribution logic and update UI for match assignments
All checks were successful
Deploy tt-tagebuch / deploy (push) Successful in 43s

This commit is contained in:
Torsten Schulz (local)
2026-05-18 00:06:56 +02:00
parent 697e67d46e
commit 040e758044
4 changed files with 125 additions and 20 deletions

View File

@@ -2068,6 +2068,8 @@ class TournamentService {
match.result = `${win}:${lose}`;
// Wenn ein Match abgeschlossen wird, darf es nicht mehr als aktiv gelten
match.isActive = false;
// Freigebe den Tisch, damit er für neue Zuweisungen verfügbar ist
match.tableNumber = null;
await match.save();
console.log(`[finishMatch] match ${matchId} finished, result=${match.result}, isActive set to false`);
@@ -2921,13 +2923,17 @@ class TournamentService {
// Ermittle aktuell laufende Matches, damit wir nur Spiele verteilen, bei denen beide Spieler frei sind
const activeMatches = await TournamentMatch.findAll({ where: { tournamentId, isActive: true, isFinished: false } });
const activePlayerIds = new Set();
// Nur Spieler aus aktiv laufenden Matches mit einem zugewiesenen Tisch blockieren.
const occupiedPlayerIds = new Set();
activeMatches.forEach(am => {
if (am.player1Id) activePlayerIds.add(Number(am.player1Id));
if (am.player2Id) activePlayerIds.add(Number(am.player2Id));
if (am.tableNumber != null) {
if (am.player1Id) occupiedPlayerIds.add(Number(am.player1Id));
if (am.player2Id) occupiedPlayerIds.add(Number(am.player2Id));
}
});
console.log(`[distributeTables] activeMatches=${activeMatches.length} activePlayers=${[...activePlayerIds].slice(0,10).join(',')}`);
console.log(`[distributeTables] activeMatches=${activeMatches.length} occupiedPlayers=${[...occupiedPlayerIds].slice(0,10).join(',')}`);
// Lade nur noch die nicht abgeschlossenen, noch nicht zugewiesenen Matches
const matches = await TournamentMatch.findAll({
@@ -2937,33 +2943,59 @@ class TournamentService {
console.log(`[distributeTables] candidateMatches=${matches.length}`);
let idx = 0;
// Ermittle aktuell belegte Tischnummern basierend auf laufenden Matches
const occupiedTables = new Set();
activeMatches.forEach(am => {
if (am.tableNumber != null) occupiedTables.add(Number(am.tableNumber));
});
console.log(`[distributeTables] occupiedTables=${[...occupiedTables].join(',')}`);
// Erzeuge Liste verfügbarer Tische (1..numTables) minus belegte Tische
const availableTables = [];
for (let t = 1; t <= numTables; t++) {
if (!occupiedTables.has(t)) availableTables.push(t);
}
if (availableTables.length === 0) {
console.log('[distributeTables] no available tables (all occupied by active matches)');
return [];
}
const updated = [];
// Verwende `busyPlayers` (initial aus occupiedPlayerIds) und erweitere es bei Zuweisungen,
// damit ein Spieler nicht mehrere Tische in einer Verteilung bekommt.
const busyPlayers = new Set(occupiedPlayerIds);
for (const m of matches) {
// Spiele mit BYE (fehlendem Spieler) überspringen
if (!m.player1Id || !m.player2Id) {
console.log(`[distributeTables] skipping match ${m.id} (BYE or missing player) p1=${m.player1Id} p2=${m.player2Id}`);
continue;
}
// Nur vergeben, wenn beide Spieler aktuell nicht in einem aktiven Match sind
if (activePlayerIds.has(Number(m.player1Id)) || activePlayerIds.has(Number(m.player2Id))) {
console.log(`[distributeTables] skipping match ${m.id} because player active p1=${m.player1Id} p2=${m.player2Id}`);
// Nur vergeben, wenn beide Spieler aktuell keinen Tisch belegen (busyPlayers)
if (busyPlayers.has(Number(m.player1Id)) || busyPlayers.has(Number(m.player2Id))) {
console.log(`[distributeTables] skipping match ${m.id} because player occupies table p1=${m.player1Id} p2=${m.player2Id}`);
continue;
}
const assign = (idx % numTables) + 1;
// Wenn keine freien Tische mehr vorhanden, abbrechen
if (availableTables.length === 0) {
console.log('[distributeTables] no more available tables to assign');
break;
}
// Nimm den nächsten freien Tisch (FIFO) und markiere ihn als belegt
const assign = availableTables.shift();
m.tableNumber = assign;
// Markiere das Spiel als gestartet (läuft)
m.isActive = true;
m.isActive = true; // Markiere Spiel als gestartet
await m.save();
updated.push(m.toJSON ? m.toJSON() : m);
console.log(`[distributeTables] assigned match ${m.id} -> table ${assign}`);
// Spieler gelten nun als aktiv - damit kein weiteres Spiel für sie zugewiesen wird
activePlayerIds.add(Number(m.player1Id));
activePlayerIds.add(Number(m.player2Id));
idx++;
// Markiere Spieler als busy, damit sie in dieser Verteilung keinen weiteren Tisch bekommen
busyPlayers.add(Number(m.player1Id));
busyPlayers.add(Number(m.player2Id));
}
console.log(`[distributeTables] finished: assignedCount=${updated.length}`);