First steps for tournament

This commit is contained in:
Torsten Schulz
2025-03-13 16:19:07 +01:00
parent df41720b50
commit 821f9d24f5
5 changed files with 111 additions and 16 deletions

View File

@@ -4,6 +4,7 @@ import Tournament from "../models/Tournament.js";
import TournamentGroup from "../models/TournamentGroup.js";
import TournamentMatch from "../models/TournamentMatch.js";
import TournamentMember from "../models/TournamentMember.js";
import TournamentResult from "../models/TournamentResult.js";
import { checkAccess } from '../utils/userUtils.js';
class TournamentService {
@@ -226,6 +227,11 @@ class TournamentService {
model: Member,
as: 'member',
}
},
{
model: TournamentResult,
as: 'tournamentResults',
order: [['set', 'ASC']]
}
],
order: [['id', 'ASC']]
@@ -233,16 +239,67 @@ class TournamentService {
return matches;
}
async addMatchResult(token, clubId, tournamentId, matchId, result) {
async addMatchResult(token, clubId, tournamentId, matchId, set, result) {
await checkAccess(token, clubId);
const match = await TournamentMatch.findByPk(matchId);
if (!match || match.tournamentId != tournamentId) {
throw new Error("Match not found or doesn't belong to this tournament");
const matches = await TournamentMatch.findAll({
where: { id: matchId, tournamentId },
});
if (matches.length > 0) {
const match = matches[0];
const tournamentResult = await TournamentResult.findOne({where: {
matchId: match.id,
set: set,
}
});
if (tournamentResult && tournamentResult.set == set) {
tournamentResult.result = result;
await tournamentResult.save();
} else {
const points = result.split(':');
await TournamentResult.create({
matchId,
set,
pointsPlayer1: points[0],
pointsPlayer2: points[1],
});
}
return;
}
match.result = result;
await match.save();
throw new Error('Match not found');
}
async finishMatch(token, clubId, tournamentId, matchId) {
await checkAccess(token, clubId);
const matches = await TournamentMatch.findAll({
where: { id: matchId, tournamentId },
include: [
{
model: TournamentResult,
as: 'tournamentResults'
}
],
order: [[{ model: TournamentResult, as: 'tournamentResults' }, 'set', 'ASC']]
});
let win = 0;
let lose = 0;
for (const results of matches[0].tournamentResults) {
if (results.pointsPlayer1 > results.pointsPlayer2) {
win++;
} else {
lose++;
}
}
const result = win.toString() + ':' + lose.toString();
if (matches.length == 1) {
const match = matches[0];
match.isFinished = true;
match.result = result;
await match.save();
return;
}
throw new Error('Match not found');
}
}