Skip to content

Commit

Permalink
fix(stats): include more stats
Browse files Browse the repository at this point in the history
  • Loading branch information
jonassimoen committed Jan 17, 2024
1 parent f4fb96a commit 6cf115a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
6 changes: 3 additions & 3 deletions 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?.clubId === match?.homeId ? match.awayScore : match.homeScore) || 0, player!.positionId || 0);
const calculatedPoints = calculatePoints(stat, player!.positionId || 0);
return ({
...stat,
clubId: player!.clubId,
Expand All @@ -148,7 +148,7 @@ export const PutMatchStatisticHandler = async (req: any, rep: any) => {
update: {
players: {
update: homeP.map((stat: ExtendedStat) => {
const reducedStat = pick(stat, ["minutesPlayed", "goals", "assists", "shots", "shotsOnTarget", "saves", "keyPasses", "accuratePasses", "totalPasses", "tackles", "blocks", "interceptions", "dribblesAttempted", "dribblesSuccess", "dribblesPast", "foulsDrawn", "foulsCommited", "penaltySaved", "penaltyCommited", "penaltyWon", "penaltyScored", "penaltyMissed", "duelsWon", "duelsTotal", "red", "yellow", "motm", "starting"]);
const reducedStat = pick(stat, ["minutesPlayed", "goalsAgainst", "goals", "assists", "shots", "shotsOnTarget", "saves", "keyPasses", "accuratePasses", "totalPasses", "tackles", "blocks", "interceptions", "dribblesAttempted", "dribblesSuccess", "dribblesPast", "foulsDrawn", "foulsCommited", "penaltySaved", "penaltyCommited", "penaltyWon", "penaltyScored", "penaltyMissed", "duelsWon", "duelsTotal", "red", "yellow", "motm", "starting"]);
return ({
where: {
id: stat.playerId,
Expand Down Expand Up @@ -195,7 +195,7 @@ export const PutMatchStatisticHandler = async (req: any, rep: any) => {
update: {
players: {
update: awayP.map((stat: ExtendedStat) => {
const reducedStat = pick(stat, ["minutesPlayed", "goals", "assists", "shots", "shotsOnTarget", "saves", "keyPasses", "accuratePasses", "totalPasses", "tackles", "blocks", "interceptions", "dribblesAttempted", "dribblesSuccess", "dribblesPast", "foulsDrawn", "foulsCommited", "penaltySaved", "penaltyCommited", "penaltyWon", "penaltyScored", "penaltyMissed", "duelsWon", "duelsTotal", "red", "yellow", "motm", "starting"]);
const reducedStat = pick(stat, ["minutesPlayed", "goalsAgainst", "goals", "assists", "shots", "shotsOnTarget", "saves", "keyPasses", "accuratePasses", "totalPasses", "tackles", "blocks", "interceptions", "dribblesAttempted", "dribblesSuccess", "dribblesPast", "foulsDrawn", "foulsCommited", "penaltySaved", "penaltyCommited", "penaltyWon", "penaltyScored", "penaltyMissed", "duelsWon", "duelsTotal", "red", "yellow", "motm", "starting"]);
return ({
where: {
id: stat.playerId,
Expand Down
1 change: 1 addition & 0 deletions src/types/body-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export const MatchStatisticPutSchema = {
penaltyMissed: { type: 'number' },
duelsWon: { type: 'number' },
duelsTotal: { type: 'number' },
goalsAginst: { type: 'number' },
red: { type: 'boolean' },
yellow: { type: 'boolean' },
motm: { type: 'boolean' },
Expand Down
25 changes: 21 additions & 4 deletions src/utils/PointsCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,42 @@ const PPS = {
INTERCEPTIONS_PER_7: [0, 2, 2, 2, 2],
}

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

// Minutes played
tempPoints += playerStat.minutesPlayed > 60 ? PPS.PLAYED_MORE_THAN_60_MIN[positionId] : (playerStat.minutesPlayed > 0) ? PPS.PLAYED_LESS_THAN_60_MIN[positionId] : 0;
// Goals
tempPoints += (playerStat.goals || 0) * PPS.GOAL[positionId];
// Assists
tempPoints += (playerStat.assists || 0) * PPS.ASSIST[positionId];
// Penalty missed
tempPoints += (playerStat.penaltyMissed || 0) * PPS.PENALTY_MISS[positionId];
// Penalty saved
tempPoints += (playerStat.penaltySaved || 0) * PPS.PENALTY_SAVE[positionId];
// Man of the Match
tempPoints += playerStat.motm ? PPS.MOTM[positionId] : 0;
// Yellow card
tempPoints += playerStat.yellow ? PPS.YELLOW[positionId] : 0;
// Red card
tempPoints += playerStat.red ? PPS.RED[positionId] : 0;
// Saves (per 2)
tempPoints += (Math.floor(playerStat.saves / 2) || 0) * PPS.SAVES_PER_2[positionId];
tempPoints += (Math.floor(goalsOpponent / 2) || 0) * PPS.CONCEDED_2[positionId];
tempPoints += (goalsOpponent || 0 ) === 0 ? PPS.CLEAN_SHEET[positionId] : 0;
// Goals against
tempPoints += (Math.floor(playerStat.goalsAgainst / 2) || 0) * PPS.CONCEDED_2[positionId];
// Clean sheet
tempPoints += (playerStat.goalsAgainst || 0 ) === 0 ? PPS.CLEAN_SHEET[positionId] : 0;
// Passing accuracy above 85%
tempPoints += playerStat.totalPasses !== 0 && (playerStat.accuratePasses / playerStat.totalPasses > 0.85) ? PPS.PASS_ACCURACY_MORE_85[positionId] : 0;
// Key passes (per 2)
tempPoints += (Math.floor(playerStat.keyPasses / 2) || 0) * PPS.KEY_PASSES_PER_2[positionId];
// Succesfull dribbles (per 5)
tempPoints += (Math.floor(playerStat.dribblesSuccess / 5) || 0) * PPS.DRIBBLES_SUCCESS_PER_5[positionId];
tempPoints += (playerStat.duelsWon > playerStat.duelsTotal - playerStat.duelsWon) ? PPS.PASS_ACCURACY_MORE_85[positionId] : 0;
// More duels won than lost
tempPoints += (playerStat.duelsWon > playerStat.duelsTotal - playerStat.duelsWon) ? PPS.DUELS_WON_MORE[positionId] : 0;
// Commited fouls (per 3)
tempPoints += (Math.floor(playerStat.foulsCommited / 3) || 0) * PPS.FOULS_COMMITED_PER_3[positionId];
// Interceptions (per 7)
tempPoints += (Math.floor(playerStat.interceptions / 7) || 0) * PPS.INTERCEPTIONS_PER_7[positionId];

return tempPoints;
Expand Down

0 comments on commit 6cf115a

Please sign in to comment.