Skip to content

Commit

Permalink
feat: update new points calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonassimoen committed Jan 17, 2024
1 parent c70516a commit d2f73b4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/controllers/Statistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ 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 calculatedPoints = calculatePoints(stat, player!.positionId || 0);
const calculatedPoints = calculatePoints(stat, (player?.clubId === match?.homeId ? match.awayScore : match.homeScore) || 0, player!.positionId || 0);
return ({
...stat,
clubId: player!.clubId,
Expand Down
89 changes: 49 additions & 40 deletions src/utils/PointsCalculator.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
declare type Statistic = {
minutesPlayed: number,
goals: number,
assists: number,
shots: number,
shotsOnTarget: number,
saves: number,
keyPasses: number,
passAccuracy: number,
tackles: number,
blocks: number,
interceptions: number,
dribblesAttempted: number,
dribblesSuccess: number,
dribblesPast: number,
foulsDrawn: number,
foulsCommited: number,
penaltySaved: number,
penaltyCommited: number,
penaltyWon: number,
penaltyScored: number,
penaltyMissed: number,
duelsWon: number,
duelsTotal: number,
goalsAgainst: number,
red: boolean,
yellow: boolean,
motm: boolean,
id: number
playerId: number
matchId: number
teamPoints: number
points: number
minutesPlayed: number
goals: number
assists: number
shots: number
shotsOnTarget: number
saves: number
keyPasses: number
accuratePasses: number
totalPasses: number
tackles: number
blocks: number
interceptions: number
dribblesAttempted: number
dribblesSuccess: number
dribblesPast: number
foulsDrawn: number
foulsCommited: number
penaltySaved: number
penaltyCommited: number
penaltyWon: number
penaltyScored: number
penaltyMissed: number
duelsWon: number
duelsTotal: number
goalsAgainst: number
red: boolean
yellow: boolean
motm: boolean
starting: boolean
}

const PPS = {
Expand All @@ -42,18 +49,15 @@ const PPS = {
CONCEDED_2: [0, -2, -1, 0, 0],
SAVES_PER_2: [0, 1, 0, 0, 0],
CLEAN_SHEET: [0, 4, 4, 1, 0],

// PASS_ACCURACY_MORE_65 = 0,
// KEY_PASSES_PER_2 = 0,
// DRIBBLES_SUCCESS_MORE_PER_5 = 0,
// DUELS_WON_MORE_75 = 0,
// FOULS_COMMITED_PER_3 = 0,
// INTERCEPTIONS_PER_7 = 0,


PASS_ACCURACY_MORE_85: [0, 2, 3, 4, 3],
KEY_PASSES_PER_2: [0, 5, 3, 3, 3],
DRIBBLES_SUCCESS_PER_5: [0, 5, 3, 3, 3],
DUELS_WON_MORE: [0, 1, 1, 1, 1],
FOULS_COMMITED_PER_3: [0, -2, -2, -2, -2],
INTERCEPTIONS_PER_7: [0, 2, 2, 2, 2],
}

export const calculatePoints = (playerStat: Statistic, positionId: number): number => {
export const calculatePoints = (playerStat: Statistic, goalsOpponent: number, positionId: number): number => {
let tempPoints = 0;

tempPoints += playerStat.minutesPlayed > 60 ? PPS.PLAYED_MORE_THAN_60_MIN[positionId] : (playerStat.minutesPlayed > 0) ? PPS.PLAYED_LESS_THAN_60_MIN[positionId] : 0;
Expand All @@ -65,9 +69,14 @@ export const calculatePoints = (playerStat: Statistic, positionId: number): numb
tempPoints += playerStat.yellow ? PPS.YELLOW[positionId] : 0;
tempPoints += playerStat.red ? PPS.RED[positionId] : 0;
tempPoints += (Math.floor(playerStat.saves / 2) || 0) * PPS.SAVES_PER_2[positionId];
// tempPoints += playerStat.ownGoal * PPS.OWN_GOAL[positionId];
// tempPoints += Math.floor(playerStat.goalsAgainst / 2) * PPS.CONCEDED_2[positionId];
// tempPoints += playerStat.goalsAgainst === 0 ? PPS.CLEAN_SHEET[positionId] : 0;
tempPoints += (Math.floor(goalsOpponent / 2) || 0) * PPS.CONCEDED_2[positionId];
tempPoints += (goalsOpponent || 0 ) === 0 ? PPS.CLEAN_SHEET[positionId] : 0;
tempPoints += playerStat.totalPasses !== 0 && (playerStat.accuratePasses / playerStat.totalPasses > 0.85) ? PPS.PASS_ACCURACY_MORE_85[positionId] : 0;
tempPoints += (Math.floor(playerStat.keyPasses / 2) || 0) * PPS.KEY_PASSES_PER_2[positionId];
tempPoints += (Math.floor(playerStat.dribblesSuccess / 5) || 0) * PPS.DRIBBLES_SUCCESS_PER_5[positionId];
tempPoints += playerStat.duelsWon > playerStat.duelsTotal ? PPS.PASS_ACCURACY_MORE_85[positionId] : 0;
tempPoints += (Math.floor(playerStat.foulsCommited / 3) || 0) * PPS.FOULS_COMMITED_PER_3[positionId];
tempPoints += (Math.floor(playerStat.interceptions / 7) || 0) * PPS.INTERCEPTIONS_PER_7[positionId];

return tempPoints;
}

0 comments on commit d2f73b4

Please sign in to comment.