@@ -5,26 +5,64 @@ export const calculateWinRate = (player: Player): number => {
5
5
return ( player . wins / player . matches ) * 100 ;
6
6
} ;
7
7
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
+ */
8
26
export const calculateScore = ( player : Player ) : number => {
9
27
const winRate = calculateWinRate ( player ) ;
28
+
10
29
const goalsPerGame = player . matches > 0 ? player . goals / player . matches : 0 ;
11
30
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 ;
14
37
} ;
15
38
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
+ */
16
45
export const generateBalancedTeams = ( players : Player [ ] ) : { teamA : Player [ ] ; teamB : Player [ ] } => {
17
46
const sortedPlayers = [ ...players ] . sort ( ( a , b ) => calculateScore ( b ) - calculateScore ( a ) ) ;
47
+
18
48
const teamA : Player [ ] = [ ] ;
19
49
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
+ ) {
23
59
teamA . push ( player ) ;
60
+ scoreA += playerScore ;
24
61
} else {
25
62
teamB . push ( player ) ;
63
+ scoreB += playerScore ;
26
64
}
27
65
} ) ;
28
-
66
+
29
67
return { teamA, teamB } ;
30
- } ;
68
+ } ;
0 commit comments