diff --git a/backend/services/tournamentService.js b/backend/services/tournamentService.js index ec043f7..4109f9a 100644 --- a/backend/services/tournamentService.js +++ b/backend/services/tournamentService.js @@ -180,32 +180,52 @@ class TournamentService { // 2) Alte Matches löschen await TournamentMatch.destroy({ where: { tournamentId } }); - // 3) Shuffle + verteilen - const shuffled = members.slice(); - for (let i = shuffled.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; + // 3) Prüfe, ob Spieler bereits manuell zugeordnet wurden + const alreadyAssigned = members.filter(m => m.groupId !== null); + const unassigned = members.filter(m => m.groupId === null); + + if (alreadyAssigned.length > 0) { + // Spieler sind bereits manuell zugeordnet - nicht neu verteilen + console.log(`${alreadyAssigned.length} Spieler bereits zugeordnet, ${unassigned.length} noch nicht zugeordnet`); + } else { + // Keine manuellen Zuordnungen - zufällig verteilen + const shuffled = members.slice(); + for (let i = shuffled.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; + } + groups.forEach((g, idx) => { + shuffled + .filter((_, i) => i % groups.length === idx) + .forEach(m => m.update({ groupId: g.id })); + }); } - groups.forEach((g, idx) => { - shuffled - .filter((_, i) => i % groups.length === idx) - .forEach(m => m.update({ groupId: g.id })); - }); - // 4) Round‑Robin anlegen wie gehabt + // 4) Round‑Robin anlegen wie gehabt - NUR innerhalb jeder Gruppe for (const g of groups) { const gm = await TournamentMember.findAll({ where: { groupId: g.id } }); + if (gm.length < 2) { + console.warn(`Gruppe ${g.id} hat nur ${gm.length} Teilnehmer - keine Matches erstellt`); + continue; + } const rounds = this.generateRoundRobinSchedule(gm); for (let roundIndex = 0; roundIndex < rounds.length; roundIndex++) { for (const [p1Id, p2Id] of rounds[roundIndex]) { - await TournamentMatch.create({ - tournamentId, - groupId: g.id, - round: 'group', - player1Id: p1Id, - player2Id: p2Id, - groupRound: roundIndex + 1 - }); + // Prüfe, ob beide Spieler zur gleichen Gruppe gehören + const p1 = gm.find(p => p.id === p1Id); + const p2 = gm.find(p => p.id === p2Id); + if (p1 && p2 && p1.groupId === p2.groupId && p1.groupId === g.id) { + await TournamentMatch.create({ + tournamentId, + groupId: g.id, + round: 'group', + player1Id: p1Id, + player2Id: p2Id, + groupRound: roundIndex + 1 + }); + } else { + console.warn(`Spieler gehören nicht zur gleichen Gruppe: ${p1Id} (${p1?.groupId}) vs ${p2Id} (${p2?.groupId}) in Gruppe ${g.id}`); + } } } } diff --git a/frontend/src/views/TournamentsView.vue b/frontend/src/views/TournamentsView.vue index 0b3da5e..be980b0 100644 --- a/frontend/src/views/TournamentsView.vue +++ b/frontend/src/views/TournamentsView.vue @@ -24,34 +24,41 @@ Spielen in Gruppen
-

Teilnehmer

- - - +
+

Teilnehmer

+ +
+
+ +
+ + +
+