2019-10-16 18:53:27 +02:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2018-05-11 12:31:53 +02:00
|
|
|
module Game (
|
2019-10-22 17:52:13 +02:00
|
|
|
exportGame
|
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-11-18 17:06:02 +01:00
|
|
|
import qualified App (T, get)
|
2019-10-22 17:52:13 +02:00
|
|
|
import Control.Monad.Except (runExceptT)
|
2019-08-24 23:29:40 +02:00
|
|
|
import Control.Monad.Reader (lift)
|
|
|
|
import Control.Monad.Writer (runWriterT)
|
2019-10-17 18:58:39 +02:00
|
|
|
import qualified Crypto.Saltine.Class as Saltine (IsEncoding(..))
|
2019-10-22 17:52:13 +02:00
|
|
|
import Crypto.Saltine.Core.SecretBox (newNonce, secretbox, secretboxOpen)
|
|
|
|
import Crypto.Saltine.Core.Sign (signDetached, signVerifyDetached)
|
|
|
|
import Data.Aeson (ToJSON, eitherDecode', encode)
|
2019-10-17 18:58:39 +02:00
|
|
|
import Data.ByteString (ByteString)
|
2019-10-22 17:52:13 +02:00
|
|
|
import Data.ByteString.Lazy (fromStrict, toStrict)
|
|
|
|
import Data.Map ((!), Map, mapWithKey)
|
2019-10-17 18:58:39 +02:00
|
|
|
import qualified Hanafuda (Pack)
|
2019-10-23 15:27:09 +02:00
|
|
|
import Hanafuda.KoiKoi (Game, Mode(..), Player, PlayerID, Players)
|
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-22 17:52:13 +02:00
|
|
|
import qualified Hanafuda.Player as Player (Player(..), Players(..), get)
|
|
|
|
import Keys (T(..))
|
|
|
|
import qualified Keys (public, secret)
|
2019-10-23 15:27:09 +02:00
|
|
|
import qualified Server (T(..))
|
2019-08-24 23:29:40 +02:00
|
|
|
|
2019-10-23 15:27:09 +02:00
|
|
|
new :: (PlayerID, PlayerID) -> App.T Game
|
|
|
|
new (for, to) = lift $ KoiKoi.new (for, to) WholeYear
|
2019-08-24 23:29:40 +02:00
|
|
|
|
2019-10-17 18:58:39 +02:00
|
|
|
exportPlayers :: Game -> Map PlayerID Player
|
|
|
|
exportPlayers game =
|
|
|
|
let (Player.Players players) = KoiKoi.players game in
|
|
|
|
players
|
|
|
|
|
2019-10-22 17:52:13 +02:00
|
|
|
privateState :: Game -> PrivateState
|
|
|
|
privateState game = PrivateState {
|
|
|
|
hands = Player.hand <$> players
|
2019-10-17 18:58:39 +02:00
|
|
|
, deck = KoiKoi.deck game
|
|
|
|
}
|
|
|
|
where
|
2019-10-22 17:52:13 +02:00
|
|
|
Player.Players players = KoiKoi.players game
|
2019-10-17 18:58:39 +02:00
|
|
|
|
|
|
|
getHand :: PlayerID -> Players -> Hanafuda.Pack
|
|
|
|
getHand playerID = Player.hand . (Player.get playerID)
|
|
|
|
|
|
|
|
publicPlayer :: Player -> PublicPlayer
|
|
|
|
publicPlayer player = PublicPlayer {
|
|
|
|
meld = Player.meld player
|
|
|
|
, yakus = Player.yakus player
|
|
|
|
}
|
2019-10-16 18:53:27 +02:00
|
|
|
|
2019-10-22 17:52:13 +02:00
|
|
|
privatePlayer :: Map PlayerID PublicPlayer -> PlayerID -> Hanafuda.Pack -> Player
|
|
|
|
privatePlayer publicPlayers playerID hand = Player.Player {
|
|
|
|
Player.hand
|
|
|
|
, Player.meld = meld (publicPlayers ! playerID)
|
|
|
|
, Player.yakus = yakus (publicPlayers ! playerID)
|
|
|
|
}
|
|
|
|
|
2019-10-17 19:50:21 +02:00
|
|
|
publicState :: Game -> PublicState
|
|
|
|
publicState game = PublicState {
|
2019-10-16 18:53:27 +02:00
|
|
|
mode = KoiKoi.mode game
|
|
|
|
, scores = KoiKoi.scores game
|
|
|
|
, month = KoiKoi.month game
|
2019-10-22 17:52:13 +02:00
|
|
|
, nextPlayer = KoiKoi.nextPlayer game
|
2019-10-17 18:58:39 +02:00
|
|
|
, players = publicPlayer <$> exportPlayers game
|
2019-10-16 18:53:27 +02:00
|
|
|
, playing = KoiKoi.playing game
|
|
|
|
, winning = KoiKoi.winning game
|
|
|
|
, oyake = KoiKoi.oyake game
|
|
|
|
, river = KoiKoi.river game
|
|
|
|
, step = KoiKoi.step game
|
|
|
|
, trick = KoiKoi.trick game
|
2019-10-17 19:25:35 +02:00
|
|
|
, turns = 24 - length (KoiKoi.deck game)
|
2019-10-16 18:53:27 +02:00
|
|
|
, rounds = KoiKoi.rounds game
|
2019-08-24 23:29:40 +02:00
|
|
|
}
|
2019-10-16 18:53:27 +02:00
|
|
|
|
2019-10-22 17:52:13 +02:00
|
|
|
exportGame :: PlayerID -> Game -> App.T PublicGame
|
|
|
|
exportGame playerID game = do
|
2019-11-18 17:06:02 +01:00
|
|
|
Keys.T {encrypt, sign} <- App.get Server.keys
|
2019-10-17 18:58:39 +02:00
|
|
|
n <- lift newNonce
|
2019-10-16 18:53:27 +02:00
|
|
|
return $ PublicGame {
|
2019-10-17 18:58:39 +02:00
|
|
|
nonce = Saltine.encode n
|
|
|
|
, playerHand = getHand playerID (KoiKoi.players game)
|
2019-10-17 19:50:21 +02:00
|
|
|
, private = secretbox encrypt n $ toJSON private
|
|
|
|
, public
|
2019-10-22 17:52:13 +02:00
|
|
|
, publicSignature = signDetached (Keys.secret sign) $ toJSON public
|
2019-10-16 18:53:27 +02:00
|
|
|
}
|
2018-05-11 12:31:53 +02:00
|
|
|
where
|
2019-10-17 19:50:21 +02:00
|
|
|
public = publicState game
|
2019-10-22 17:52:13 +02:00
|
|
|
private = privateState game
|
|
|
|
|
|
|
|
toJSON :: ToJSON a => a -> ByteString
|
|
|
|
toJSON = toStrict . encode
|
|
|
|
|
|
|
|
gameOf :: PublicState -> PrivateState -> Game
|
|
|
|
gameOf public private = KoiKoi.Game {
|
|
|
|
KoiKoi.mode = mode public
|
|
|
|
, KoiKoi.scores = scores public
|
|
|
|
, KoiKoi.month = month public
|
|
|
|
, KoiKoi.nextPlayer = nextPlayer public
|
|
|
|
, KoiKoi.players = Player.Players $
|
|
|
|
mapWithKey (privatePlayer $ players public) (hands private)
|
|
|
|
, KoiKoi.playing = playing public
|
|
|
|
, KoiKoi.winning = winning public
|
|
|
|
, KoiKoi.oyake = winning public
|
|
|
|
, KoiKoi.deck = deck private
|
|
|
|
, KoiKoi.river = river public
|
|
|
|
, KoiKoi.step = step public
|
|
|
|
, KoiKoi.trick = trick public
|
|
|
|
, KoiKoi.rounds = rounds public
|
|
|
|
}
|
|
|
|
|
|
|
|
importGame :: PublicGame -> App.T (Either String Game)
|
|
|
|
importGame PublicGame {nonce, private, public, publicSignature} = do
|
2019-11-18 17:06:02 +01:00
|
|
|
Keys.T {encrypt, sign} <- App.get Server.keys
|
2019-10-22 17:52:13 +02:00
|
|
|
if signVerifyDetached (Keys.public sign) publicSignature $ toJSON public
|
|
|
|
then return $ do
|
|
|
|
n <- Saltine.decode nonce `orDie` "Could not decode nonce"
|
|
|
|
decrypted <- secretboxOpen encrypt n private
|
|
|
|
`orDie` "Could not decrypt private state"
|
|
|
|
gameOf public <$> eitherDecode' (fromStrict decrypted)
|
|
|
|
else return $ Left "The game state has been tampered with"
|
|
|
|
where
|
|
|
|
orDie :: Maybe a -> String -> Either String a
|
|
|
|
orDie m errorMessage = maybe (Left errorMessage) Right m
|
2018-05-15 18:21:07 +02:00
|
|
|
|
2019-10-22 17:52:13 +02:00
|
|
|
play :: PlayerID -> KoiKoi.Move -> PublicGame -> App.T (Either String (Game, [KoiKoi.Action]))
|
|
|
|
play playerID move publicGame
|
|
|
|
| playing (public publicGame) == playerID = do
|
|
|
|
imported <- importGame publicGame
|
|
|
|
case imported of
|
|
|
|
Left errorMessage -> return $ Left errorMessage
|
|
|
|
Right game -> lift . runExceptT . runWriterT $ KoiKoi.play move game
|
|
|
|
| otherwise = return $ Left "Not your turn"
|