Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonassimoen committed Jun 15, 2024
1 parent 6093cce commit e55e0c4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/controllers/Match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const PostRecalculateMatchPoints = async (req: any, rep: any) => {
where: {
id: matchId,
}
});
});/*
const matchPlayerStats = await prisma.statistic.findMany({
where: {
matchId,
Expand All @@ -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,
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/Statistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/Team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down

0 comments on commit e55e0c4

Please sign in to comment.