Skip to content

Commit

Permalink
don't update both players during the play-in period
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed May 25, 2021
1 parent a7f1fd1 commit aff22eb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 22 deletions.
55 changes: 51 additions & 4 deletions src/League.elm
Expand Up @@ -264,10 +264,19 @@ finishMatch outcome league =
{ won = Player.rating won
, lost = Player.rating lost
}

newPlayers =
updateRatingsIncludingPlayInPeriod
{ playerA = newRatings.won
, playerB = newRatings.lost
}
{ playerA = won
, playerB = lost
}
in
league
|> updatePlayer (Player.incrementMatchesPlayed (Player.setRating newRatings.won won))
|> updatePlayer (Player.incrementMatchesPlayed (Player.setRating newRatings.lost lost))
|> updatePlayer newPlayers.playerA
|> updatePlayer newPlayers.playerB
|> clearMatch

Draw { playerA, playerB } ->
Expand All @@ -277,13 +286,51 @@ finishMatch outcome league =
{ playerA = Player.rating playerA
, playerB = Player.rating playerB
}

newPlayers =
updateRatingsIncludingPlayInPeriod
newRatings
{ playerA = playerA
, playerB = playerB
}
in
league
|> updatePlayer (Player.incrementMatchesPlayed (Player.setRating newRatings.playerA playerA))
|> updatePlayer (Player.incrementMatchesPlayed (Player.setRating newRatings.playerB playerB))
|> updatePlayer newPlayers.playerA
|> updatePlayer newPlayers.playerB
|> clearMatch


updateRatingsIncludingPlayInPeriod :
{ playerA : Int, playerB : Int }
-> { playerA : Player, playerB : Player }
-> { playerA : Player, playerB : Player }
updateRatingsIncludingPlayInPeriod ratings players_ =
let
playerAInPlayInPeriod =
Player.matchesPlayed players_.playerA < playInMatches

playerBInPlayInPeriod =
Player.matchesPlayed players_.playerB < playInMatches
in
{ playerA =
if not playerAInPlayInPeriod && playerBInPlayInPeriod then
players_.playerA

else
players_.playerA
|> Player.setRating ratings.playerA
|> Player.incrementMatchesPlayed
, playerB =
if not playerBInPlayInPeriod && playerAInPlayInPeriod then
players_.playerB

else
players_.playerB
|> Player.setRating ratings.playerB
|> Player.incrementMatchesPlayed
}


{-| -}
playInMatches : Int
playInMatches =
Expand Down
33 changes: 19 additions & 14 deletions tests/LeagueTest.elm
Expand Up @@ -8,7 +8,7 @@ import Json.Decode as Decode
import Json.Encode as Encode
import League exposing (League, Match(..), Outcome(..))
import Player
import PlayerTest exposing (playerFuzzer)
import PlayerTest exposing (establishedPlayerFuzzer)
import Test exposing (..)


Expand Down Expand Up @@ -55,14 +55,14 @@ decoderTests =
playersTests : Test
playersTests =
describe "functionality around players"
[ fuzz playerFuzzer "adding a player makes them show up in the players list" <|
[ fuzz establishedPlayerFuzzer "adding a player makes them show up in the players list" <|
\player ->
League.init
|> League.addPlayer player
|> League.players
|> List.map Player.name
|> Expect.equal [ Player.name player ]
, fuzz playerFuzzer "retiring a player removes them from the players list" <|
, fuzz establishedPlayerFuzzer "retiring a player removes them from the players list" <|
\player ->
League.init
|> League.addPlayer player
Expand All @@ -79,13 +79,13 @@ playersTests =
startMatchTests : Test
startMatchTests =
describe "startMatch"
[ fuzz2 playerFuzzer playerFuzzer "you can't start a match when neither player is in the league" <|
[ fuzz2 establishedPlayerFuzzer establishedPlayerFuzzer "you can't start a match when neither player is in the league" <|
\playerA playerB ->
League.init
|> League.startMatch (Match playerA playerB)
|> League.currentMatch
|> Expect.equal Nothing
, fuzz2 playerFuzzer playerFuzzer "you can't start a match when one player isn't in the league" <|
, fuzz2 establishedPlayerFuzzer establishedPlayerFuzzer "you can't start a match when one player isn't in the league" <|
\playerA playerB ->
let
uniqueA =
Expand All @@ -99,7 +99,7 @@ startMatchTests =
|> League.startMatch (Match uniqueA uniqueB)
|> League.currentMatch
|> Expect.equal Nothing
, fuzz2 playerFuzzer playerFuzzer "you can start a match between two players in the league" <|
, fuzz2 establishedPlayerFuzzer establishedPlayerFuzzer "you can start a match between two players in the league" <|
\playerA playerBMaybeSame ->
let
playerB =
Expand All @@ -112,7 +112,7 @@ startMatchTests =
|> League.currentMatch
|> Maybe.map (\(Match a b) -> ( Player.name a, Player.name b ))
|> Expect.equal (Just ( Player.name playerA, Player.name playerB ))
, fuzz playerFuzzer "you can't start a match with one player against themselves" <|
, fuzz establishedPlayerFuzzer "you can't start a match with one player against themselves" <|
\player ->
League.init
|> League.addPlayer player
Expand All @@ -127,14 +127,19 @@ finishMatchTests =
let
existingPlayer =
Player.init "A"
|> Player.incrementMatchesPlayed
|> Player.incrementMatchesPlayed
|> Player.incrementMatchesPlayed
|> Player.incrementMatchesPlayed
|> Player.incrementMatchesPlayed

league =
League.init
|> League.addPlayer existingPlayer
in
describe "finishMatch"
[ describe "a win"
[ fuzz playerFuzzer "causes both players matches played to go up" <|
[ fuzz establishedPlayerFuzzer "causes both players matches played to go up" <|
\winner ->
league
|> League.addPlayer winner
Expand All @@ -148,7 +153,7 @@ finishMatchTests =
>> Maybe.map Player.matchesPlayed
>> Expect.equal (Just (Player.matchesPlayed existingPlayer + 1))
]
, fuzz playerFuzzer "changes ratings according to Elo" <|
, fuzz establishedPlayerFuzzer "changes ratings according to Elo" <|
\winner ->
let
newRatings =
Expand All @@ -169,7 +174,7 @@ finishMatchTests =
>> Maybe.map Player.rating
>> Expect.equal (Just newRatings.lost)
]
, fuzz playerFuzzer "does not change the total points in the system" <|
, fuzz establishedPlayerFuzzer "does not change the total points in the system" <|
\winner ->
league
|> League.addPlayer winner
Expand All @@ -181,7 +186,7 @@ finishMatchTests =
|> Expect.equal (Player.rating winner + Player.rating existingPlayer)
]
, describe "a draw"
[ fuzz playerFuzzer "a draw causes both players matches played to go up" <|
[ fuzz establishedPlayerFuzzer "a draw causes both players matches played to go up" <|
\player ->
league
|> League.addPlayer player
Expand All @@ -195,7 +200,7 @@ finishMatchTests =
>> Maybe.map Player.matchesPlayed
>> Expect.equal (Just (Player.matchesPlayed existingPlayer + 1))
]
, fuzz playerFuzzer "a draw changes ratings according to Elo" <|
, fuzz establishedPlayerFuzzer "a draw changes ratings according to Elo" <|
\player ->
let
newRatings =
Expand Down Expand Up @@ -224,7 +229,7 @@ finishMatchTests =
>> Maybe.map Player.rating
>> Expect.equal (Just newRatings.playerB)
]
, fuzz playerFuzzer "a draw does not change the total points in the system" <|
, fuzz establishedPlayerFuzzer "a draw does not change the total points in the system" <|
\player ->
league
|> League.addPlayer player
Expand All @@ -244,5 +249,5 @@ finishMatchTests =

leagueFuzzer : Fuzzer League
leagueFuzzer =
Fuzz.list playerFuzzer
Fuzz.list establishedPlayerFuzzer
|> Fuzz.map (List.foldr League.addPlayer League.init)
8 changes: 4 additions & 4 deletions tests/PlayerTest.elm
Expand Up @@ -13,7 +13,7 @@ import Test exposing (..)
interopTest : Test
interopTest =
describe "interop"
[ fuzz playerFuzzer "encode and decode are symmetrical" <|
[ fuzz establishedPlayerFuzzer "encode and decode are symmetrical" <|
\player ->
Player.encode player
|> Decode.decodeValue Player.decoder
Expand Down Expand Up @@ -85,8 +85,8 @@ ratingTest =
]


playerFuzzer : Fuzzer Player
playerFuzzer =
establishedPlayerFuzzer : Fuzzer Player
establishedPlayerFuzzer =
Fuzz.map3
(\name rating matches ->
Player.init name
Expand All @@ -95,7 +95,7 @@ playerFuzzer =
)
nameFuzzer
(Fuzz.intRange 1000 3000)
(Fuzz.intRange 0 50)
(Fuzz.intRange 5 50)


nameFuzzer : Fuzzer String
Expand Down

0 comments on commit aff22eb

Please sign in to comment.