From 509a5c453fc7abe3d8c8e983eafd5f35bf442951 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Mon, 13 Jan 2020 08:29:40 +0100 Subject: [PATCH] Take game ID with turn into a GameState sub-structure shared between private and public parts to prevent replay-attack --- src/Hanafuda/Message.hs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Hanafuda/Message.hs b/src/Hanafuda/Message.hs index 2f13b60..dc01dcd 100644 --- a/src/Hanafuda/Message.hs +++ b/src/Hanafuda/Message.hs @@ -7,6 +7,7 @@ module Hanafuda.Message ( T(..) , FromClient(..) + , GameState(..) , PrivateState(..) , PublicGame(..) , PublicPlayer(..) @@ -139,8 +140,18 @@ instance FromJSON Hanafuda.Flower instance ToJSON Hanafuda.Flower where toEncoding = genericToEncoding defaultOptions +data GameState = GameState { + gameID :: GameID + , turns :: Int + } deriving (Eq, Generic, Show) + +instance FromJSON GameState +instance ToJSON GameState where + toEncoding = genericToEncoding defaultOptions + data PrivateState = PrivateState { - hands :: Map PlayerID Hanafuda.Pack + link :: GameState + , hands :: Map PlayerID Hanafuda.Pack , deck :: [Hanafuda.Card] } deriving Generic @@ -158,7 +169,8 @@ instance ToJSON PublicPlayer where toEncoding = genericToEncoding defaultOptions data PublicState = PublicState { - mode :: KoiKoi.Mode + gameState :: GameState + , mode :: KoiKoi.Mode , scores :: KoiKoi.Scores , month :: Hanafuda.Flower , nextPlayer :: KoiKoi.PlayerTurn @@ -169,7 +181,6 @@ data PublicState = PublicState { , river :: Hanafuda.Pack , step :: KoiKoi.Step , trick :: [Hanafuda.Card] - , turns :: Int , rounds :: [(PlayerID, KoiKoi.Score)] } deriving (Generic, Show) @@ -184,8 +195,7 @@ instance ToJSON ByteString where toEncoding = toEncoding . decodeUtf8 . B64.encode data PublicGame = PublicGame { - gameID :: GameID - , nonce :: ByteString + nonce :: ByteString , playerHand :: Hanafuda.Pack , private :: ByteString , public :: PublicState