From ca30340aaacc7cc3378f7b82b614896809ab2b37 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Sat, 18 Jan 2020 09:35:19 +0100 Subject: [PATCH] =?UTF-8?q?Follow=20=C2=ABCoordinates=C2=BB=20change=20in?= =?UTF-8?q?=20protocol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Game.hs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Game.hs b/src/Game.hs index 794d177..099d4e8 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -22,7 +22,7 @@ import qualified Hanafuda.KoiKoi as KoiKoi ( Action, Game(..), Move(..), play, new ) import Hanafuda.Message ( - GameState(..), PrivateState(..), PublicGame(..), PublicPlayer(..) + Coordinates(..), PrivateState(..), PublicGame(..), PublicPlayer(..) , PublicState(..) ) import qualified Hanafuda.Player as Player (Player(..), Players(..), get) @@ -38,13 +38,14 @@ exportPlayers game = let (Player.Players players) = KoiKoi.players game in players -getGameState :: Game -> GameState -getGameState game = GameState { +getCoordinates :: Game -> Coordinates +getCoordinates game = Coordinates { gameID = KoiKoi.gameID game - , turns = 24 - length (KoiKoi.deck game) + , month = KoiKoi.month game + , turn = 24 - length (KoiKoi.deck game) } -privateState :: GameState -> Game -> PrivateState +privateState :: Coordinates -> Game -> PrivateState privateState link game = PrivateState { link , hands = Player.hand <$> players @@ -69,12 +70,11 @@ privatePlayer publicPlayers playerID hand = Player.Player { , Player.yakus = yakus (publicPlayers ! playerID) } -publicState :: GameState -> Game -> PublicState -publicState gameState game = PublicState { - gameState +publicState :: Coordinates -> Game -> PublicState +publicState coordinates game = PublicState { + coordinates , mode = KoiKoi.mode game , scores = KoiKoi.scores game - , month = KoiKoi.month game , nextPlayer = KoiKoi.nextPlayer game , players = publicPlayer <$> exportPlayers game , playing = KoiKoi.playing game @@ -98,19 +98,19 @@ exportGame playerID game = do , publicSignature = signDetached (Keys.secret sign) $ toJSON public } where - sharedState = getGameState game - public = publicState sharedState game - private = privateState sharedState game + shared = getCoordinates game + public = publicState shared game + private = privateState shared game toJSON :: ToJSON a => a -> ByteString toJSON = toStrict . encode merge :: PublicState -> PrivateState -> Game merge public private = KoiKoi.Game { - KoiKoi.gameID = gameID $ gameState public + KoiKoi.gameID = gameID $ coordinates public , KoiKoi.mode = mode public , KoiKoi.scores = scores public - , KoiKoi.month = month public + , KoiKoi.month = month $ coordinates public , KoiKoi.nextPlayer = nextPlayer public , KoiKoi.players = Player.Players $ mapWithKey (privatePlayer $ players public) (hands private) @@ -133,7 +133,7 @@ importGame PublicGame {nonce, private, public, publicSignature} = decrypted <- secretboxOpen encrypt n private `orDie` "Could not decrypt private state" decoded <- eitherDecode' (fromStrict decrypted) - check (link decoded == gameState public) + check (link decoded == coordinates public) `orDie` "Private and public parts do not match" return $ merge public decoded where