server/src/Game.hs

63 lines
1.7 KiB
Haskell
Raw Normal View History

{-# LANGUAGE NamedFieldPuns #-}
2018-04-11 13:25:24 +02:00
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Game (
Key
, State(..)
, T(..)
, export
, new
) where
2018-04-11 13:25:24 +02:00
import Data.Map (Map, (!), fromList, mapKeys)
2018-04-11 13:25:24 +02:00
import Data.Aeson (FromJSON(..), ToJSON(..), genericToEncoding)
import qualified JSON (singleLCField)
import qualified Data (Key)
import qualified Player (Key)
import qualified Hanafuda (Card(..), cardsOfPack)
import qualified Hanafuda.Player (Player(..), Seat(..))
import qualified Hanafuda.KoiKoi (Mode(..), Move(..), On(..), new)
2018-04-11 13:25:24 +02:00
import GHC.Generics
deriving instance Generic Hanafuda.Card
deriving instance Generic Hanafuda.KoiKoi.Move
2018-04-11 13:25:24 +02:00
instance FromJSON Hanafuda.Card
instance ToJSON Hanafuda.Card
2018-04-11 13:25:24 +02:00
instance FromJSON Hanafuda.KoiKoi.Move
instance ToJSON Hanafuda.KoiKoi.Move where
2018-04-11 13:25:24 +02:00
toEncoding = genericToEncoding JSON.singleLCField
data T = T {
seats :: Map Hanafuda.Player.Seat Player.Key
, state :: Hanafuda.KoiKoi.On
}
type Key = Data.Key T
data State = State {
river :: [Hanafuda.Card]
, yakus :: Map Player.Key [Hanafuda.Card]
} deriving (Generic)
instance ToJSON State where
toEncoding = genericToEncoding JSON.singleLCField
new :: Player.Key -> Player.Key -> IO T
new p1 p2 = do
state <- Hanafuda.KoiKoi.new Hanafuda.KoiKoi.WholeYear
return $ T {
seats = fromList [(Hanafuda.Player.Player1, p1), (Hanafuda.Player.Player2, p2)]
, state
}
export :: T -> State
export (T {seats, state}) = State {
river = Hanafuda.cardsOfPack $ Hanafuda.KoiKoi.river state
, yakus = fmap extractYakus players
}
where
extractYakus = Hanafuda.cardsOfPack . Hanafuda.Player.meld
players = mapKeys (seats !) $ Hanafuda.KoiKoi.players state