@@ -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,28 @@ 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 (
100+ ( log ) => log . matchId === match . id
101+ ) ;
102+ const player2MatchLog = player2SortedLogs . find (
103+ ( log ) => log . matchId === match . id
104+ ) ;
97105
98106 let eloDiff = 0 ;
99107 let player1Elo = BASE_ELO ;
100108 let player2Elo = BASE_ELO ;
101109
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 ;
110+ if ( player1MatchLog ) {
111+ const matchIndex = player1SortedLogs . indexOf ( player1MatchLog ) ;
112+ player1Elo =
113+ matchIndex === 0 ? BASE_ELO : player1SortedLogs [ matchIndex - 1 ] . elo ;
114+ eloDiff = player1MatchLog . elo - player1Elo ;
115+ }
116+
117+ if ( player2MatchLog ) {
118+ const matchIndex = player2SortedLogs . indexOf ( player2MatchLog ) ;
119+ player2Elo =
120+ matchIndex === 0 ? BASE_ELO : player2SortedLogs [ matchIndex - 1 ] . elo ;
108121 }
109122
110123 return {
0 commit comments