From e55e0c4f7ef1dea4607ea21e23dc24f3db5739b8 Mon Sep 17 00:00:00 2001 From: Jonas Simoen Date: Sat, 15 Jun 2024 17:10:48 +0200 Subject: [PATCH] minor fixes --- src/controllers/Match.ts | 12 ++++++++---- src/controllers/Statistic.ts | 6 ++++-- src/controllers/Team.ts | 4 ++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/controllers/Match.ts b/src/controllers/Match.ts index c84ea27..7ee6dcf 100644 --- a/src/controllers/Match.ts +++ b/src/controllers/Match.ts @@ -97,7 +97,7 @@ export const PostRecalculateMatchPoints = async (req: any, rep: any) => { where: { id: matchId, } - }); + });/* const matchPlayerStats = await prisma.statistic.findMany({ where: { matchId, @@ -110,10 +110,10 @@ export const PostRecalculateMatchPoints = async (req: any, rep: any) => { const matchPlayerStatsRecalc = matchPlayerStats.map((ps: any) => { const calculatedPoints = calculatePoints(ps, ps?.player?.positionId); return {...ps, points: calculatedPoints }; - }); + });*/ await prisma.$transaction( async (prisma) => { - await Promise.all(matchPlayerStatsRecalc.map((ps: Statistic) => { + /*await Promise.all(matchPlayerStatsRecalc.map((ps: Statistic) => { return prisma.statistic.update({ where: { id: ps.id, @@ -138,7 +138,11 @@ export const PostRecalculateMatchPoints = async (req: any, rep: any) => { } } }); - })), + })),*/ + // Recalculation logic + await prisma.$queryRaw`CALL "recalculateMatchPoints"(${match?.id})` + // Update player and team points + await prisma.$queryRaw`CALL "calculateTeamAndPlayerPoints"(${match?.weekId})` // Captain - Vice Captain points multipliers (Triple Captain / Vice victory) await prisma.$queryRaw`CALL "processViceCaptainAndBoosters"(${match?.weekId})`, // HiddenGem - GoalRush points multipliers diff --git a/src/controllers/Statistic.ts b/src/controllers/Statistic.ts index 90f0d1a..71d838a 100644 --- a/src/controllers/Statistic.ts +++ b/src/controllers/Statistic.ts @@ -169,13 +169,14 @@ export const PutMatchStatisticHandler = async (req: any, rep: any) => { }); const playersWithCalculatedPoints = req.body.stats.map((stat: any) => { const player = playersWithPositionIds.find((player: any) => player.id === stat.playerId); + const opponentGoals = player?.clubId === match.homeId ? req.body.goalMinutes.away : req.body.goalMinutes.home; const statGoalsAgainst = { ...stat, - goalsAgainst: stat.red ? req.body.goalMinutes.home.filter((gm: number) => stat.in <= gm).length : req.body.goalMinutes.home.filter((gm: number) => stat.in <= gm && stat.out >= gm).length + goalsAgainst: stat.red ? opponentGoals.filter((gm: number) => stat.in <= gm).length : opponentGoals.filter((gm: number) => stat.in <= gm && stat.out >= gm).length } const calculatedPoints = calculatePoints(statGoalsAgainst, player?.positionId ); return ({ - ...stat, + ...statGoalsAgainst, clubId: player!.clubId, minutesPlayed: Math.min(90, stat.out) - stat.in || 0, calculatedPoints, @@ -213,6 +214,7 @@ export const PutMatchStatisticHandler = async (req: any, rep: any) => { players: { update: homeP.map((stat: ExtendedStat) => { const reducedStat = pick(pickBy(stat, (v, k) => (v !== null && v !== undefined)), subselection); + console.log(stat.playerId, stat.goalsAgainst); return ({ where: { id: stat.playerId, diff --git a/src/controllers/Team.ts b/src/controllers/Team.ts index 9e83748..2037ade 100644 --- a/src/controllers/Team.ts +++ b/src/controllers/Team.ts @@ -325,8 +325,8 @@ export const GetPointsTeamHandler = async (req: any, rep: any) => { id: +req.params.weekId } }), - prisma.$queryRaw`SELECT "teamId", CAST(SUM(points) AS int) AS points, CAST(RANK() OVER(ORDER BY SUM(points) DESC) AS int) FROM "Selection" s WHERE "weekId" = ${+req.params.weekId} AND starting = 1 GROUP BY "teamId" ORDER BY rank ASC`, - prisma.$queryRaw`SELECT "teamId", CAST(SUM(points) AS int) AS points, CAST(RANK() OVER(ORDER BY SUM(points) DESC) AS int) FROM "Selection" s WHERE starting = 1 GROUP BY "teamId" ORDER BY rank ASC`, + prisma.$queryRaw`SELECT t.id as "teamId", CAST(SUM(s.points) AS int) AS points, CAST(RANK() OVER(ORDER BY SUM(s.points) DESC) AS int) FROM "Selection" s JOIN "Team" t on t.id = s."teamId" WHERE s."weekId" = ${+req.params.weekId} AND s.starting = 1 OR (t."superSubs" = 1 and s.starting = 0) GROUP BY t.id ORDER BY rank ASC`, + prisma.$queryRaw`SELECT t.id as "teamId", CAST(SUM(s.points) AS int) AS points, CAST(RANK() OVER(ORDER BY SUM(s.points) DESC) AS int) FROM "Selection" s JOIN "Team" t on t.id = s."teamId" WHERE s.starting = 1 OR (t."superSubs" = 1 and s.starting = 0) GROUP BY t.id ORDER BY rank ASC`, ]); const {selections, Transfer, user, ...team} = teamWithSelections!;