module Haverer.Player (
bust,
discardAndDraw,
eliminate,
getDiscards,
getHand,
isProtected,
makePlayer,
Player,
playCard,
protect,
swapHands,
unprotect
) where
import BasicPrelude
import Control.Lens hiding (chosen)
import Haverer.Deck (Card)
data Player = Active {
_hand :: Card,
_protected :: Bool,
_discard :: [Card]
} | Inactive { _discard :: [Card] } deriving (Show, Eq)
makeLenses ''Player
makePlayer :: Card -> Player
makePlayer card = Active {
_hand = card,
_protected = False,
_discard = []
}
protect :: Player -> Player
protect = set protected True
unprotect :: Player -> Player
unprotect = set protected False
eliminate :: Player -> Player
eliminate player@(Inactive _) = player
eliminate (Active card _ discards) = Inactive (card:discards)
swapHands :: Player -> Player -> (Player, Player)
swapHands player1 player2 =
case (preview hand player1, preview hand player2) of
(Just h1, Just h2) ->
(set hand h2 player1, set hand h1 player2)
_ -> (player1, player2)
discardAndDraw :: Player -> Maybe Card -> Player
discardAndDraw player@(Inactive _) _ = player
discardAndDraw (Active card _ discards) Nothing = Inactive (card:discards)
discardAndDraw (Active card p discards) (Just newCard) =
Active newCard p (card:discards)
playCard :: Player -> Card -> Card -> Maybe Player
playCard (Inactive _) _ _ = Nothing
playCard (Active hand' p discards) dealt chosen
| hand' == chosen = Just $ Active dealt p (hand':discards)
| dealt == chosen = Just $ Active hand' p (dealt:discards)
| otherwise = Nothing
bust :: Player -> Card -> Player
bust player@(Inactive _) _ = player
bust (Active h _ discards) dealt = Inactive (h:dealt:discards)
getDiscards :: Player -> [Card]
getDiscards = view discard
getHand :: Player -> Maybe Card
getHand = preview hand
isProtected :: Player -> Maybe Bool
isProtected = preview protected