Skip to content

Commit b506a3d

Browse files
committed
Remove timestamp arg from AddMatch
1 parent cfd151c commit b506a3d

File tree

2 files changed

+63
-36
lines changed

2 files changed

+63
-36
lines changed

main.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
256257
func (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
}

main_test.go

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package multielo_test
22

33
import (
4+
"fmt"
45
"testing"
5-
"time"
66

77
"github.com/distrobyte/multielo"
88
"github.com/stretchr/testify/assert"
@@ -236,7 +236,7 @@ func TestMatch_AddMatch(t *testing.T) {
236236
{Player: player4, Position: 4},
237237
}
238238

239-
matchDiff, err := l.AddMatch(time.Now(), results...)
239+
matchDiff, err := l.AddMatch(results)
240240
if err != nil {
241241
t.Error(err)
242242
}
@@ -312,7 +312,7 @@ func TestMatch_AddMatch(t *testing.T) {
312312
{Player: nil, Position: 3},
313313
}
314314

315-
_, err = l.AddMatch(time.Now(), results...)
315+
_, err = l.AddMatch(results)
316316
if err.Error() != "nil player found. not recording match" {
317317
t.Error(err)
318318
}
@@ -321,7 +321,7 @@ func TestMatch_AddMatch(t *testing.T) {
321321
t.Run("AddMatchLeagueNil", func(t *testing.T) {
322322
l := multielo.NewLeague()
323323

324-
_, err := l.AddMatch(time.Now())
324+
_, err := l.AddMatch([]*multielo.MatchResult{})
325325
if err != multielo.ErrNoPlayers {
326326
t.Error(err)
327327
}
@@ -386,7 +386,7 @@ func TestLeague_GetMatches(t *testing.T) {
386386
{Player: player2, Position: 2},
387387
}
388388

389-
_, err = l.AddMatch(time.Now(), results...)
389+
_, err = l.AddMatch(results)
390390
assert.NoError(t, err)
391391

392392
matches := l.GetMatches()
@@ -426,39 +426,59 @@ func TestLeague_GenerateGraph(t *testing.T) {
426426
err = l.AddPlayer("player2")
427427
assert.NoError(t, err)
428428

429-
player1, err := l.GetPlayer("player1")
429+
_, err = l.GetPlayer("player1")
430430
assert.NoError(t, err)
431-
player2, err := l.GetPlayer("player2")
431+
_, err = l.GetPlayer("player2")
432432
assert.NoError(t, err)
433433

434-
results := []*multielo.MatchResult{
435-
{Player: player1, Position: 1},
436-
{Player: player2, Position: 2},
434+
var results1 []*multielo.MatchResult
435+
for i := 0; i < 2; i++ {
436+
results1 = append(results1, &multielo.MatchResult{
437+
Player: &multielo.Player{Name: fmt.Sprintf("player%v", i+1)},
438+
Position: i + 1,
439+
})
437440
}
438441

439-
_, err = l.AddMatch(time.Now(), results...)
442+
_, err = l.AddMatch(results1)
440443
assert.NoError(t, err)
441444

442-
graphPath, err := l.GenerateGraph()
443445
assert.NoError(t, err)
444-
assert.Equal(t, "elo.png", graphPath)
445446

446447
err = l.AddPlayer("player3")
447448
assert.NoError(t, err)
448449

449-
player3, err := l.GetPlayer("player3")
450+
_, err = l.GetPlayer("player3")
450451
assert.NoError(t, err)
451452

452-
results = []*multielo.MatchResult{
453-
{Player: player1, Position: 1},
454-
{Player: player2, Position: 2},
455-
{Player: player3, Position: 3},
453+
var results2 []*multielo.MatchResult
454+
for i := 0; i < 3; i++ {
455+
results2 = append(results2, &multielo.MatchResult{
456+
Player: &multielo.Player{Name: fmt.Sprintf("player%v", i+1)},
457+
Position: i + 1,
458+
})
456459
}
457460

458-
_, err = l.AddMatch(time.Now(), results...)
461+
_, err = l.AddMatch(results2)
459462
assert.NoError(t, err)
460463

461-
graphPath, err = l.GenerateGraph()
464+
err = l.AddPlayer("player4")
465+
assert.NoError(t, err)
466+
467+
_, err = l.GetPlayer("player4")
468+
assert.NoError(t, err)
469+
470+
var results3 []*multielo.MatchResult
471+
for i := 0; i < 3; i++ {
472+
results3 = append(results3, &multielo.MatchResult{
473+
Player: &multielo.Player{Name: fmt.Sprintf("player%v", i+1)},
474+
Position: i + 1,
475+
})
476+
}
477+
478+
_, err = l.AddMatch(results3)
479+
assert.NoError(t, err)
480+
481+
graphPath, err := l.GenerateGraph()
462482
assert.NoError(t, err)
463483
assert.Equal(t, "elo.png", graphPath)
464484
}

0 commit comments

Comments
 (0)