Skip to content

Commit

Permalink
Combine messages since they are so similar
Browse files Browse the repository at this point in the history
  • Loading branch information
Qluxzz committed Nov 3, 2024
1 parent 59be61e commit 8bc3082
Showing 1 changed file with 22 additions and 39 deletions.
61 changes: 22 additions & 39 deletions src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ type alias Dollars =
Int


type InsuranceAction
= BuyInsurance
| DeclineInsurance


type Msg
= NoOp
| StartNewGame
Expand All @@ -95,8 +100,7 @@ type Msg
| Deal
| DealerTakesCard
-- Hit or Stand
| BuyInsurance
| DeclineInsurance
| TakeActionOnInsurance InsuranceAction
| TakeCard
| Stand
| Split
Expand Down Expand Up @@ -375,18 +379,24 @@ update msg model =
)

{- Hit or Stand -}
BuyInsurance ->
TakeActionOnInsurance action ->
let
currentBet =
model.player.hands |> Tuple.first |> .bet
dealerHasBlackjack =
Cards.hasBlackjack model.dealer

updatedPlayer =
model.player
|> Player.updateCurrentHand (\h -> { h | insurance = Insured (h.bet // 2), state = Playing })
|> Player.updatePlayer (\p -> { p | money = p.money - currentBet // 2 })
case action of
DeclineInsurance ->
model.player

dealerHasBlackjack =
Cards.hasBlackjack model.dealer
BuyInsurance ->
let
currentBet =
model.player.hands |> Tuple.first |> .bet
in
model.player
|> Player.updateCurrentHand (\h -> { h | insurance = Insured (h.bet // 2), state = Playing })
|> Player.updatePlayer (\p -> { p | money = p.money - currentBet // 2 })
in
( { model
| player = updatedPlayer
Expand All @@ -411,33 +421,6 @@ update msg model =
Nothing
)

DeclineInsurance ->
let
dealerHasBlackjack =
Cards.hasBlackjack model.dealer
in
( { model
| state =
if not dealerHasBlackjack then
HitOrStand

else
model.state
}
, if dealerHasBlackjack then
DealerFinish_

else
NoEffect
)
|> withToast
(if not dealerHasBlackjack then
Just "Dealer didn't have blackjack!"

else
Nothing
)

TakeCard ->
let
( cards, deck ) =
Expand Down Expand Up @@ -982,8 +965,8 @@ insuranceView =
[ Html.h1 [] [ Html.text "Buy insurance?" ]
, Html.p [] [ Html.text "Costs 50% of your bet, if dealer has blackjack, you win your initial bet back" ]
, Html.div [ Html.Attributes.class "button-group" ]
[ Html.button [ Html.Events.onClick BuyInsurance ] [ Html.text "Yes" ]
, Html.button [ Html.Events.onClick DeclineInsurance ] [ Html.text "No" ]
[ Html.button [ Html.Events.onClick (TakeActionOnInsurance BuyInsurance) ] [ Html.text "Yes" ]
, Html.button [ Html.Events.onClick (TakeActionOnInsurance DeclineInsurance) ] [ Html.text "No" ]
]
]
]
Expand Down

0 comments on commit 8bc3082

Please sign in to comment.