@@ -81,15 +81,15 @@ func NewLeague() *League {
8181 }
8282}
8383
84- func (l * League ) AddMatch (date time. Time , results ... * MatchResult ) ([]MatchDiff , error ) {
84+ func (l * League ) AddMatch (results [] * MatchResult ) ([]MatchDiff , error ) {
8585 if len (l .Players ) == 0 {
8686 return nil , ErrNoPlayers
8787 }
8888
8989 populatedResults := make ([]* MatchResult , 0 , len (results ))
9090 matchDiff := make ([]MatchDiff , 0 , len (results ))
9191
92- // ensure all drivers are registered and flesh out the results with ELOs
92+ // ensure all players are registered and flesh out the results with ELOs
9393 for _ , result := range results {
9494 found := false
9595 for _ , player := range l .Players {
@@ -125,7 +125,7 @@ func (l *League) AddMatch(date time.Time, results ...*MatchResult) ([]MatchDiff,
125125
126126 // loop over every other result
127127 for opponentPlayer , opponentResult := range populatedResults {
128- // skip comparing the driver to themselves
128+ // skip comparing the player to themselves
129129 if player == opponentPlayer {
130130 continue
131131 }
@@ -136,35 +136,36 @@ func (l *League) AddMatch(date time.Time, results ...*MatchResult) ([]MatchDiff,
136136 // calculate the expected score
137137 var S float64
138138
139- // if the driver finished higher than the other driver
139+ // if the player finished higher than the other player
140140 if curPosition < opponentPosition {
141141 S = 1.0
142+ } else if curPosition == opponentPosition {
143+ S = 0.5
142144 } else {
143145 S = 0.0
144146 }
145147
146148 // calculate the expected score
147149 E := 1.0 / (1.0 + math .Pow (10 , float64 (opponentELO - curELO )/ 400 ))
148150
149- // update the driver 's ELO change
151+ // update the player 's ELO change
150152 result .Player .ELOChange += int (math .Round (float64 (kValue ) * (S - E )))
151153 }
152154
153- // update the driver 's ELO
155+ // update the player 's ELO
154156 result .Player .ELO += result .Player .ELOChange
155157 matchDiff = append (matchDiff , MatchDiff {
156158 Player : result .Player ,
157159 Diff : result .Player .ELOChange ,
158160 })
159161 }
160162
161- // update the drivers ' ELOs
163+ // update the players ' ELOs
162164 for _ , result := range populatedResults {
163165 for _ , player := range l .Players {
164166 if player .Name == result .Player .Name {
165167 player .ELO = result .Player .ELO
166-
167- player .Stats .MatchesPlayed += 1
168+ player .Stats .MatchesPlayed ++
168169 if result .Position == 1 {
169170 player .Stats .MatchesWon ++
170171 }
@@ -256,7 +257,14 @@ func (l *League) RemovePlayer(name string) error {
256257func (l * League ) ResetPlayers () {
257258 for _ , p := range l .Players {
258259 p .ELO = InitialELO
259- p .Stats = & PlayerStats {}
260+ p .ELOChange = 0
261+ p .Stats = & PlayerStats {
262+ Last5Finish : []int {},
263+ MatchesPlayed : 0 ,
264+ MatchesWon : 0 ,
265+ AllTimeAveragePlace : 0 ,
266+ PeakELO : InitialELO ,
267+ }
260268 }
261269}
262270
@@ -295,7 +303,7 @@ func (l *League) GenerateGraph() (string, error) {
295303 return "" , ErrNoPlayers
296304 }
297305
298- // sort the drivers by ELO
306+ // sort the players by ELO
299307 for i := 0 ; i < len (l .Players ); i ++ {
300308 for j := i + 1 ; j < len (l .Players ); j ++ {
301309 if l .Players [i ].ELO < l .Players [j ].ELO {
@@ -329,13 +337,13 @@ func (l *League) GenerateGraph() (string, error) {
329337
330338 var firstRaceIndex int = - 1
331339
332- // loop over every race
340+ // loop over every match
333341 for i , event := range l .Matches {
334342
335343 // loop over every result
336344 for _ , result := range event .Results {
337345
338- // if the result is for the driver we're plotting
346+ // if the result is for the player we're plotting
339347 if result .Player != nil && result .Player .Name == player .Name {
340348
341349 // and this is the first time we've seen them
@@ -344,16 +352,15 @@ func (l *League) GenerateGraph() (string, error) {
344352 xys [i ].X = float64 (i )
345353 xys [i ].Y = float64 (InitialELO )
346354 labels [i ] = strconv .Itoa (InitialELO )
347- // and remember the index
348355 firstRaceIndex = i
349356 }
350357
351- // add the ELO for the driver for the current race
358+ // add the ELO for the player for the current race
352359 xys [i + 1 ].X = float64 (i + 1 )
353360 xys [i + 1 ].Y = float64 (result .Player .ELO )
354361 break
355362 } else {
356- // if we haven't seen the driver yet, just copy the last value
363+ // if we haven't seen the player yet, just copy the last value
357364 xys [i + 1 ].X = float64 (i )
358365 xys [i + 1 ].Y = xys [i ].Y
359366 }
0 commit comments