Skip to content

Commit 62f272f

Browse files
authored
Merge pull request #1 from Jose26398/adjusted-player-score
Adjusted player score and added more balanced teams
2 parents 54e2fd1 + e20b787 commit 62f272f

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

src/utils/playerStats.ts

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,64 @@ export const calculateWinRate = (player: Player): number => {
55
return (player.wins / player.matches) * 100;
66
};
77

8+
/**
9+
* Adjusts a stat based on the number of matches played.
10+
*
11+
* @param stat The stat to adjust.
12+
* @param matches The number of matches played.
13+
* @param scale The scale of the adjustment. Defaults to 10.
14+
* @returns The adjusted stat.
15+
*/
16+
const adjustForMatches = (stat: number, matches: number, scale: number = 10): number => {
17+
return stat * (1 - Math.exp(-matches / scale));
18+
};
19+
20+
/**
21+
* Calculates the score of a player based on their win rate, goals per game, and assists per game.
22+
*
23+
* @param player The player to calculate the score for.
24+
* @returns The score of the player.
25+
*/
826
export const calculateScore = (player: Player): number => {
927
const winRate = calculateWinRate(player);
28+
1029
const goalsPerGame = player.matches > 0 ? player.goals / player.matches : 0;
1130
const assistsPerGame = player.matches > 0 ? player.assists / player.matches : 0;
12-
13-
return winRate * 0.5 + goalsPerGame * 30 + assistsPerGame * 20;
31+
32+
const adjustedWinRate = adjustForMatches(winRate, player.matches);
33+
const adjustedGoals = adjustForMatches(goalsPerGame, player.matches);
34+
const adjustedAssists = adjustForMatches(assistsPerGame, player.matches);
35+
36+
return adjustedWinRate * 0.7 + adjustedGoals * 10 + adjustedAssists * 5;
1437
};
1538

39+
/**
40+
* Generates a balanced team based on the player's score.
41+
*
42+
* @param players The players to generate the team from.
43+
* @returns The balanced team.
44+
*/
1645
export const generateBalancedTeams = (players: Player[]): { teamA: Player[]; teamB: Player[] } => {
1746
const sortedPlayers = [...players].sort((a, b) => calculateScore(b) - calculateScore(a));
47+
1848
const teamA: Player[] = [];
1949
const teamB: Player[] = [];
20-
21-
sortedPlayers.forEach((player, index) => {
22-
if (index % 2 === 0) {
50+
let scoreA = 0;
51+
let scoreB = 0;
52+
53+
sortedPlayers.forEach((player) => {
54+
const playerScore = calculateScore(player);
55+
if (
56+
(teamA.length < teamB.length) ||
57+
(teamA.length === teamB.length && scoreA <= scoreB)
58+
) {
2359
teamA.push(player);
60+
scoreA += playerScore;
2461
} else {
2562
teamB.push(player);
63+
scoreB += playerScore;
2664
}
2765
});
28-
66+
2967
return { teamA, teamB };
30-
};
68+
};

0 commit comments

Comments
 (0)