First steps for tournament
This commit is contained in:
@@ -121,13 +121,26 @@ export const getTournamentMatches = async (req, res) => {
|
||||
|
||||
export const addMatchResult = async (req, res) => {
|
||||
const { authcode: token } = req.headers;
|
||||
const { clubId, tournamentId, matchId, result } = req.body;
|
||||
const { clubId, tournamentId, matchId, set, result } = req.body;
|
||||
|
||||
try {
|
||||
await tournamentService.addMatchResult(token, clubId, tournamentId, matchId, result);
|
||||
await tournamentService.addMatchResult(token, clubId, tournamentId, matchId, set, result);
|
||||
res.status(200).json({ message: "Result added successfully" });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const finishMatch = async (req, res) => {
|
||||
const { authcode: token } = req.headers;
|
||||
const { clubId, tournamentId, matchId } = req.body;
|
||||
|
||||
try {
|
||||
await tournamentService.finishMatch(token, clubId, tournamentId, matchId);
|
||||
res.status(200).json({ message: "Match finished successfully" });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,15 @@ const TournamentMatch = sequelize.define('TournamentMatch', {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
isFinished: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
},
|
||||
result: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
},
|
||||
}, {
|
||||
underscored: true,
|
||||
tableName: 'tournament_match',
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
getTournament,
|
||||
getTournamentMatches,
|
||||
addMatchResult,
|
||||
finishMatch,
|
||||
} from '../controllers/tournamentController.js';
|
||||
import { authenticate } from '../middleware/authMiddleware.js';
|
||||
|
||||
@@ -23,6 +24,7 @@ router.put('/groups', authenticate, createGroups);
|
||||
router.post('/groups', authenticate, fillGroups);
|
||||
router.get('/groups', authenticate, getGroups);
|
||||
router.post('/match/result', authenticate, addMatchResult);
|
||||
router.post('/match/finish', authenticate, finishMatch);
|
||||
router.get('/matches/:clubId/:tournamentId', authenticate, getTournamentMatches);
|
||||
router.get('/:clubId/:tournamentId', authenticate, getTournament);
|
||||
router.get('/:clubId', authenticate, getTournaments);
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user