2019-10-16 18:53:27 +02:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2018-05-11 12:31:53 +02:00
|
|
|
module Game (
|
2019-08-24 23:29:40 +02:00
|
|
|
export
|
2018-05-11 12:31:53 +02:00
|
|
|
, new
|
2018-05-15 18:21:07 +02:00
|
|
|
, play
|
2018-05-11 12:31:53 +02:00
|
|
|
) where
|
2018-04-11 13:25:24 +02:00
|
|
|
|
2019-08-24 23:29:40 +02:00
|
|
|
import qualified App (T, update)
|
|
|
|
import Control.Monad.Except (runExceptT, throwError)
|
|
|
|
import Control.Monad.Reader (lift)
|
|
|
|
import Control.Monad.Writer (runWriterT)
|
2019-10-16 18:53:27 +02:00
|
|
|
import Crypto.Saltine.Core.Sign (signDetached)
|
|
|
|
import Data.Aeson (encode)
|
|
|
|
import Data.Map ((!), mapWithKey)
|
2019-08-24 23:29:40 +02:00
|
|
|
import qualified Hanafuda (empty)
|
2019-10-16 18:53:27 +02:00
|
|
|
import Hanafuda.KoiKoi (Game, GameID, Mode(..), PlayerID)
|
2019-08-24 23:29:40 +02:00
|
|
|
import qualified Hanafuda.KoiKoi as KoiKoi (
|
2019-10-16 18:53:27 +02:00
|
|
|
Action, Game(..), Move(..), play, new
|
2019-08-24 23:29:40 +02:00
|
|
|
)
|
2019-10-16 18:53:27 +02:00
|
|
|
import Hanafuda.Message (PrivateState(..), PublicGame(..), PublicPlayer(..), PublicState(..))
|
2019-10-13 22:00:35 +02:00
|
|
|
import qualified Hanafuda.Player (Player(..), Players(..))
|
2019-08-24 23:29:40 +02:00
|
|
|
import qualified Server (register)
|
|
|
|
|
|
|
|
new :: (PlayerID, PlayerID) -> App.T GameID
|
|
|
|
new (for, to) =
|
|
|
|
Server.register <$> (lift $ KoiKoi.new (for, to) WholeYear) >>= App.update
|
|
|
|
|
2019-10-16 18:53:27 +02:00
|
|
|
extractPrivateState :: PlayerID -> Game -> PrivateState
|
|
|
|
extractPrivateState playerID game = undefined
|
|
|
|
|
|
|
|
extractPublicState :: Game -> PublicState
|
|
|
|
extractPublicState game = PublicState {
|
|
|
|
mode = KoiKoi.mode game
|
|
|
|
, scores = KoiKoi.scores game
|
|
|
|
, month = KoiKoi.month game
|
|
|
|
, playing = KoiKoi.playing game
|
|
|
|
, winning = KoiKoi.winning game
|
|
|
|
, oyake = KoiKoi.oyake game
|
|
|
|
, river = KoiKoi.river game
|
|
|
|
, step = KoiKoi.step game
|
|
|
|
, trick = KoiKoi.trick game
|
|
|
|
, rounds = KoiKoi.rounds game
|
2019-08-24 23:29:40 +02:00
|
|
|
}
|
2019-10-16 18:53:27 +02:00
|
|
|
|
|
|
|
export :: PlayerID -> Game -> App.T PublicGame
|
|
|
|
export playerID game = do
|
|
|
|
secretKey <- asks $ fst . keypair . mServer
|
|
|
|
return $ PublicGame {
|
|
|
|
playerHand = hand $ players ! playerID
|
|
|
|
, privateState = extractPrivateState playerID game
|
|
|
|
, publicState
|
|
|
|
, publicSignature = signDetached secretKey publicState
|
|
|
|
}
|
2018-05-11 12:31:53 +02:00
|
|
|
where
|
2019-10-16 18:53:27 +02:00
|
|
|
Hanafuda.Player.Players players = KoiKoi.players game
|
|
|
|
publicState = encode $ extractPublicState game
|
2018-05-15 18:21:07 +02:00
|
|
|
|
2019-08-24 23:29:40 +02:00
|
|
|
play :: PlayerID -> KoiKoi.Move -> Game -> App.T (Either String Game, [KoiKoi.Action])
|
|
|
|
play playerID move game = lift . runWriterT . runExceptT $
|
|
|
|
if playing game == playerID
|
|
|
|
then KoiKoi.play move game
|
|
|
|
else throwError "Not your turn"
|