@@ -81,7 +81,10 @@ const findMatchesBetweenPlayers = (
8181 player1 : PlayerWithStats ,
8282 player2 : PlayerWithStats
8383) => {
84- const sortedLogs = [ ...player1 . eloLogs ] . sort (
84+ const player1SortedLogs = [ ...player1 . eloLogs ] . sort (
85+ ( a , b ) => new Date ( a . date ) . getTime ( ) - new Date ( b . date ) . getTime ( )
86+ ) ;
87+ const player2SortedLogs = [ ...player2 . eloLogs ] . sort (
8588 ( a , b ) => new Date ( a . date ) . getTime ( ) - new Date ( b . date ) . getTime ( )
8689 ) ;
8790
@@ -93,18 +96,22 @@ const findMatchesBetweenPlayers = (
9396 )
9497 . sort ( ( a , b ) => new Date ( b . date ) . getTime ( ) - new Date ( a . date ) . getTime ( ) )
9598 . map ( ( match ) => {
96- const matchLog = sortedLogs . find ( ( log ) => log . matchId === match . id ) ;
99+ const player1MatchLog = player1SortedLogs . find ( ( log ) => log . matchId === match . id ) ;
100+ const player2MatchLog = player2SortedLogs . find ( ( log ) => log . matchId === match . id ) ;
97101
98102 let eloDiff = 0 ;
99103 let player1Elo = BASE_ELO ;
100104 let player2Elo = BASE_ELO ;
101105
102- if ( matchLog ) {
103- const matchIndex = sortedLogs . indexOf ( matchLog ) ;
104- const previousElo =
105- matchIndex === 0 ? BASE_ELO : sortedLogs [ matchIndex - 1 ] . elo ;
106- eloDiff = matchLog . elo - previousElo ;
107- player1Elo = matchLog . elo ;
106+ if ( player1MatchLog ) {
107+ const matchIndex = player1SortedLogs . indexOf ( player1MatchLog ) ;
108+ player1Elo = matchIndex === 0 ? BASE_ELO : player1SortedLogs [ matchIndex - 1 ] . elo ;
109+ eloDiff = player1MatchLog . elo - player1Elo ;
110+ }
111+
112+ if ( player2MatchLog ) {
113+ const matchIndex = player2SortedLogs . indexOf ( player2MatchLog ) ;
114+ player2Elo = matchIndex === 0 ? BASE_ELO : player2SortedLogs [ matchIndex - 1 ] . elo ;
108115 }
109116
110117 return {
0 commit comments