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)
|
2018-07-12 22:33:13 +02:00
|
|
|
import Data.Map (mapWithKey)
|
2019-08-24 23:29:40 +02:00
|
|
|
import qualified Hanafuda (empty)
|
2019-10-13 22:00:35 +02:00
|
|
|
import Hanafuda.KoiKoi (Game, GameBlueprint(..), GameID, Mode(..), PlayerID)
|
2019-08-24 23:29:40 +02:00
|
|
|
import qualified Hanafuda.KoiKoi as KoiKoi (
|
|
|
|
Action, Move(..), play, new
|
|
|
|
)
|
|
|
|
import Hanafuda.Message (PublicGame)
|
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
|
|
|
|
|
|
|
|
export :: PlayerID -> Game -> PublicGame
|
|
|
|
export playerID game = game {
|
|
|
|
deck = length $ deck game
|
|
|
|
, players = Hanafuda.Player.Players $ mapWithKey maskOpponentsHand unfiltered
|
|
|
|
}
|
2018-05-11 12:31:53 +02:00
|
|
|
where
|
2019-08-12 23:01:08 +02:00
|
|
|
Hanafuda.Player.Players unfiltered = Hanafuda.KoiKoi.players game
|
2018-05-15 18:21:07 +02:00
|
|
|
maskOpponentsHand k player
|
2019-08-24 23:29:40 +02:00
|
|
|
| k == playerID = player
|
2018-05-15 18:21:07 +02:00
|
|
|
| otherwise = player {Hanafuda.Player.hand = Hanafuda.empty}
|
|
|
|
|
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"
|