2018-05-11 12:31:53 +02:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2018-04-11 13:25:24 +02:00
|
|
|
{-# LANGUAGE StandaloneDeriving #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2018-05-12 11:21:59 +02:00
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
2018-05-11 12:31:53 +02:00
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
|
|
module Game (
|
|
|
|
Key
|
2018-05-12 11:21:59 +02:00
|
|
|
, View(..)
|
2018-05-11 12:31:53 +02:00
|
|
|
, T(..)
|
|
|
|
, export
|
|
|
|
, new
|
|
|
|
) where
|
2018-04-11 13:25:24 +02:00
|
|
|
|
2018-05-12 11:21:59 +02:00
|
|
|
import Data.Text (pack)
|
|
|
|
import Data.Map (Map, (!), fromList, mapKeys, mapWithKey)
|
|
|
|
import Data.Aeson (FromJSON(..), ToJSON(..), ToJSON1(..), ToJSONKey(..), genericToEncoding, genericLiftToEncoding, toEncoding1, toJSON1)
|
|
|
|
import Data.Aeson.Types (toJSONKeyText)
|
|
|
|
import qualified JSON (defaultOptions, singleLCField)
|
2018-05-11 12:31:53 +02:00
|
|
|
import qualified Data (Key)
|
|
|
|
import qualified Player (Key)
|
2018-05-12 11:21:59 +02:00
|
|
|
import qualified Hanafuda (Flower(..), Card(..), Pack, cardsOfPack, empty)
|
|
|
|
import qualified Hanafuda.Player (Player(..), Seat(..), Points)
|
|
|
|
import qualified Hanafuda.KoiKoi (Mode(..), Move(..), On(..), Score, Step(..), Yaku(..), new)
|
2018-04-11 13:25:24 +02:00
|
|
|
import GHC.Generics
|
|
|
|
|
2018-05-11 12:31:53 +02:00
|
|
|
deriving instance Generic Hanafuda.Card
|
2018-05-12 11:21:59 +02:00
|
|
|
deriving instance Generic Hanafuda.Flower
|
|
|
|
deriving instance Generic Hanafuda.KoiKoi.Mode
|
2018-05-11 12:31:53 +02:00
|
|
|
deriving instance Generic Hanafuda.KoiKoi.Move
|
2018-05-12 11:21:59 +02:00
|
|
|
deriving instance Generic Hanafuda.KoiKoi.Yaku
|
|
|
|
deriving instance Generic Hanafuda.KoiKoi.Step
|
|
|
|
deriving instance Generic1 Hanafuda.Player.Player
|
2018-04-11 13:25:24 +02:00
|
|
|
|
2018-05-11 12:31:53 +02:00
|
|
|
instance FromJSON Hanafuda.Card
|
|
|
|
instance ToJSON Hanafuda.Card
|
2018-04-11 13:25:24 +02:00
|
|
|
|
2018-05-12 11:21:59 +02:00
|
|
|
instance ToJSON Hanafuda.Flower
|
|
|
|
|
|
|
|
instance ToJSON Hanafuda.Pack where
|
|
|
|
toJSON = toJSON . Hanafuda.cardsOfPack
|
|
|
|
toEncoding = toEncoding . Hanafuda.cardsOfPack
|
|
|
|
|
|
|
|
instance ToJSON Hanafuda.KoiKoi.Mode
|
|
|
|
|
2018-05-11 12:31:53 +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
|
2018-05-11 12:31:53 +02:00
|
|
|
|
2018-05-12 11:21:59 +02:00
|
|
|
instance ToJSON1 Hanafuda.Player.Player where
|
|
|
|
liftToEncoding = genericLiftToEncoding JSON.defaultOptions
|
|
|
|
|
|
|
|
instance ToJSON (Hanafuda.Player.Player Hanafuda.KoiKoi.Score) where
|
|
|
|
toJSON = toJSON1
|
|
|
|
toEncoding = toEncoding1
|
|
|
|
|
|
|
|
instance ToJSON Hanafuda.KoiKoi.Yaku where
|
|
|
|
toEncoding = genericToEncoding JSON.defaultOptions
|
|
|
|
instance ToJSONKey Hanafuda.KoiKoi.Yaku where
|
|
|
|
toJSONKey = toJSONKeyText (pack . show)
|
|
|
|
|
|
|
|
instance ToJSON Hanafuda.KoiKoi.Step where
|
|
|
|
toEncoding = genericToEncoding JSON.defaultOptions
|
|
|
|
|
2018-05-11 12:31:53 +02:00
|
|
|
data T = T {
|
2018-05-12 11:21:59 +02:00
|
|
|
keys :: Map Hanafuda.Player.Seat Player.Key
|
|
|
|
, seats :: Map Player.Key Hanafuda.Player.Seat
|
2018-05-11 12:31:53 +02:00
|
|
|
, state :: Hanafuda.KoiKoi.On
|
|
|
|
}
|
2018-05-12 11:21:59 +02:00
|
|
|
|
2018-05-11 12:31:53 +02:00
|
|
|
type Key = Data.Key T
|
2018-05-12 11:21:59 +02:00
|
|
|
type Players a = Map Player.Key a
|
2018-05-11 12:31:53 +02:00
|
|
|
|
2018-05-12 11:21:59 +02:00
|
|
|
data View = View {
|
|
|
|
mode :: Hanafuda.KoiKoi.Mode
|
|
|
|
, scores :: Players Hanafuda.Player.Points
|
|
|
|
, month :: Hanafuda.Flower
|
|
|
|
, players :: Players (Hanafuda.Player.Player Hanafuda.KoiKoi.Score)
|
|
|
|
, playing :: Player.Key
|
|
|
|
, winning :: Player.Key
|
|
|
|
, oyake :: Player.Key
|
|
|
|
, river :: [Hanafuda.Card]
|
|
|
|
, step :: Hanafuda.KoiKoi.Step
|
|
|
|
, trick :: [Hanafuda.Card]
|
2018-05-11 12:31:53 +02:00
|
|
|
} deriving (Generic)
|
|
|
|
|
2018-05-12 11:21:59 +02:00
|
|
|
instance ToJSON View where
|
2018-05-11 12:31:53 +02:00
|
|
|
toEncoding = genericToEncoding JSON.singleLCField
|
|
|
|
|
|
|
|
new :: Player.Key -> Player.Key -> IO T
|
|
|
|
new p1 p2 = do
|
|
|
|
state <- Hanafuda.KoiKoi.new Hanafuda.KoiKoi.WholeYear
|
|
|
|
return $ T {
|
2018-05-12 11:21:59 +02:00
|
|
|
keys = fromList [(Hanafuda.Player.Player1, p1), (Hanafuda.Player.Player2, p2)]
|
|
|
|
, seats = fromList [(p1, Hanafuda.Player.Player1), (p2, Hanafuda.Player.Player2)]
|
2018-05-11 12:31:53 +02:00
|
|
|
, state
|
|
|
|
}
|
|
|
|
|
2018-05-12 11:21:59 +02:00
|
|
|
export :: Player.Key -> T -> View
|
|
|
|
export key (T {keys, state}) = View {
|
|
|
|
mode = Hanafuda.KoiKoi.mode state
|
|
|
|
, scores = reindex $ Hanafuda.KoiKoi.scores state
|
|
|
|
, month = Hanafuda.KoiKoi.month state
|
|
|
|
, players = mapWithKey (\k -> if k == key then id else maskHand) players
|
|
|
|
, playing = keys ! Hanafuda.KoiKoi.playing state
|
|
|
|
, winning = keys ! Hanafuda.KoiKoi.winning state
|
|
|
|
, oyake = keys ! Hanafuda.KoiKoi.oyake state
|
|
|
|
, river = Hanafuda.cardsOfPack $ Hanafuda.KoiKoi.river state
|
|
|
|
, step = Hanafuda.KoiKoi.step state
|
|
|
|
, trick = Hanafuda.KoiKoi.trick state
|
2018-05-11 12:31:53 +02:00
|
|
|
}
|
|
|
|
where
|
2018-05-12 11:21:59 +02:00
|
|
|
reindex = mapKeys (keys !)
|
|
|
|
players = reindex $ Hanafuda.KoiKoi.players state
|
|
|
|
maskHand player = player {Hanafuda.Player.hand = Hanafuda.empty}
|