Skip to content

Commit

Permalink
Format win rate
Browse files Browse the repository at this point in the history
  • Loading branch information
Qluxzz committed Oct 28, 2024
1 parent 84c7091 commit c7acdba
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ port module Main exposing
, Model
, Msg(..)
, Statistics
, defaultStatistics
, init
, initWithDeck
, main
Expand Down Expand Up @@ -977,7 +978,7 @@ statisticsView statistics =
[ { title = "Rounds played", description = Nothing, value = String.fromInt statistics.roundsPlayed }
, { title = "Hands won", description = Nothing, value = String.fromInt statistics.wins }
, { title = "Hands lost", description = Nothing, value = String.fromInt statistics.loses }
, { title = "Win rate", description = Nothing, value = String.fromFloat (toFloat statistics.wins / (toFloat <| Basics.clamp 1 (statistics.wins + statistics.loses) (statistics.wins + statistics.loses)) * 100) ++ "%" }
, { title = "Win rate", description = Nothing, value = formatWinRate (toFloat statistics.wins / (toFloat <| Basics.clamp 1 (statistics.wins + statistics.loses) (statistics.wins + statistics.loses)) * 100) ++ "%" }
, { title = "Blackjack", description = Nothing, value = String.fromInt statistics.blackjack }
, { title = "Blackjack push", description = Just "You and the dealer both got blackjack", value = String.fromInt statistics.blackjackPush }
, { title = "Push", description = Nothing, value = String.fromInt statistics.pushes }
Expand Down Expand Up @@ -1015,6 +1016,42 @@ statisticsView statistics =
-- HELPER FUNCTIONS


{-| If number has decimals, round to two decimals, otherwise just return integer part
-}
formatWinRate : Float -> String
formatWinRate value =
let
factor =
10 ^ 2

rounded =
round (value * toFloat factor) |> toFloat

adjusted =
rounded / toFloat factor

baseString =
String.fromFloat adjusted

parts =
String.split "." baseString
in
case parts of
[ integerPart ] ->
integerPart

[ integerPart, decimalPart ] ->
let
paddedDecimals =
String.padRight 2 '0' decimalPart
in
integerPart ++ "." ++ paddedDecimals

_ ->
-- Fallback for unexpected cases
""


allPlayers : ( Player.Player, List Player.Player ) -> List Player.Player
allPlayers ( currentPlayer, rest ) =
currentPlayer :: rest
Expand Down
15 changes: 15 additions & 0 deletions tests/GameTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,16 @@ suite =
|> ProgramTest.expectViewHas
[ Selector.exactText "Highest balance: $600"
]
, test "Win rate is formatted correctly (rounded to two decimals if not a whole number)" <|
\_ ->
start
(defaultSettings
|> withStatistics (Main.defaultStatistics |> (\s -> { s | wins = 1, loses = 2 }))
)
|> ProgramTest.clickButton "Statistics"
|> ProgramTest.expectViewHas
[ Selector.exactText "Win rate: 33.33%"
]
]
]

Expand Down Expand Up @@ -797,6 +807,11 @@ withDelay s =
{ s | delay = True }


withStatistics : Main.Statistics -> Settings -> Settings
withStatistics statistics s =
{ s | statistics = Just statistics }


start : Settings -> ProgramTest.ProgramTest Main.Model Main.Msg Main.Effect
start settings =
ProgramTest.createDocument
Expand Down

0 comments on commit c7acdba

Please sign in to comment.