Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linter failing #22

Merged
merged 3 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@ linters:
- gomnd
- wsl
- nlreturn
- nosnakecase
- interfacer
- ifshort
- varcheck
- exhaustivestruct
- scopelint
- structcheck
- maligned
- deadcode
- golint
- structcheck
- varnamelen
- exhaustruct
- revive
18 changes: 10 additions & 8 deletions calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,29 @@ func NewCalc(k float64, opts ...CalcOpt) *Calculator {
}

// Win calculates rating for winner and loser.
func (c *Calculator) Win(winner, loser Ratinger) (newWinnerRating, newLoserRating float64) {
// The order of players in returned values is the same.
func (c *Calculator) Win(winner, loser Ratinger) (float64, float64) {
if winner == nil || loser == nil {
return 0, 0
}

newWinnerRating = winner.Rating() + c.getK(winner)*(1-probability(winner, loser))
newLoserRating = loser.Rating() + c.getK(loser)*(-probability(loser, winner))
winnerNew := winner.Rating() + c.getK(winner)*(1-probability(winner, loser))
loserNew := loser.Rating() + c.getK(loser)*(-probability(loser, winner))

return newWinnerRating, newLoserRating
return winnerNew, loserNew
}

// Draw calculates rating for both players using a 0.5 co-efficient.
func (c *Calculator) Draw(p1, p2 Ratinger) (newP1Rating, newP2Rating float64) {
// The order of players in returned values is the same.
func (c *Calculator) Draw(p1, p2 Ratinger) (float64, float64) {
if p1 == nil || p2 == nil {
return 0, 0
}

newP1Rating = p1.Rating() + c.getK(p1)*(0.5-probability(p1, p2))
newP2Rating = p2.Rating() + c.getK(p2)*(0.5-probability(p2, p1))
p1New := p1.Rating() + c.getK(p1)*(0.5-probability(p1, p2))
p2New := p2.Rating() + c.getK(p2)*(0.5-probability(p2, p1))

return newP1Rating, newP2Rating
return p1New, p2New
}

func (c *Calculator) getK(r Ratinger) float64 {
Expand Down
42 changes: 31 additions & 11 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func TestPool(t *testing.T) {

for _, tc := range testCases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()
pool := elgo.NewPool(
elgo.WithPlayerRetryInterval(100*time.Millisecond),
elgo.WithGlobalRetryInterval(100*time.Millisecond),
Expand All @@ -47,14 +49,18 @@ func TestPool(t *testing.T) {
wg.Add(tc.expectedMatches)

for i := 0; i < tc.poolSize; i++ {
go pool.AddPlayer(CreatePlayerMock(fmt.Sprint(i), rand.Float64()))
err := pool.AddPlayer(CreatePlayerMock(fmt.Sprint(i), rand.Float64())) //nolint
if err != nil {
t.Errorf("pool add player: %v", err)
}
}

for i := 0; i < tc.expectedMatches; i++ {
go func(wg *sync.WaitGroup, p *elgo.Pool, tt *testing.T) {
acceptMatch(p, tt)
go func(t *testing.T, wg *sync.WaitGroup, pool *elgo.Pool) {
t.Helper()
acceptMatch(t, pool)
wg.Done()
}(&wg, pool, t)
}(t, &wg, pool)
}

wg.Wait()
Expand Down Expand Up @@ -91,6 +97,8 @@ func TestPoolPrematureClose(t *testing.T) {
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

pool := elgo.NewPool(
elgo.WithPlayerRetryInterval(100*time.Millisecond),
elgo.WithGlobalRetryInterval(100*time.Millisecond),
Expand All @@ -100,11 +108,11 @@ func TestPoolPrematureClose(t *testing.T) {

go pool.Run()
for i := 0; i < testCase.poolSize; i++ {
go pool.AddPlayer(CreatePlayerMock(fmt.Sprint(i), rand.Float64()))
go pool.AddPlayer(CreatePlayerMock(fmt.Sprint(i), rand.Float64())) //nolint
}

for i := 0; i < testCase.closeAfterMatches; i++ {
acceptMatch(pool, t)
acceptMatch(t, pool)
}

got := len(pool.Close())
Expand Down Expand Up @@ -157,8 +165,13 @@ func TestPlayerRetryInterval(t *testing.T) {

t.Cleanup(func() { pool.Close() })

pool.AddPlayer(CreatePlayerMock("1", 100))
pool.AddPlayer(CreatePlayerMock("2", 1000))
if err := pool.AddPlayer(CreatePlayerMock("1", 100)); err != nil {
t.Errorf("pool add player: %s", err)
}

if err := pool.AddPlayer(CreatePlayerMock("2", 1000)); err != nil {
t.Errorf("pool add player: %s", err)
}

go pool.Run()

Expand Down Expand Up @@ -188,8 +201,13 @@ func TestGlobalRetryInterval(t *testing.T) {

t.Cleanup(func() { pool.Close() })

pool.AddPlayer(CreatePlayerMock("1", 100))
pool.AddPlayer(CreatePlayerMock("2", 1000))
if err := pool.AddPlayer(CreatePlayerMock("1", 100)); err != nil {
t.Errorf("pool add player: %s", err)
}

if err := pool.AddPlayer(CreatePlayerMock("2", 1000)); err != nil {
t.Errorf("pool add player: %s", err)
}

go pool.Run()

Expand All @@ -209,7 +227,9 @@ func TestGlobalRetryInterval(t *testing.T) {
}

// acceptMatch tries to read from match channel, throws error otherwise.
func acceptMatch(pool *elgo.Pool, t *testing.T) {
func acceptMatch(t *testing.T, pool *elgo.Pool) {
t.Helper()

if _, ok := <-pool.Matches(); !ok {
t.Error("channel is closed, but it shouldn't be")
}
Expand Down