Take game ID with turn into a GameState sub-structure shared between private and public parts to prevent replay-attack

This commit is contained in:
Tissevert 2020-01-13 08:29:40 +01:00
parent 413d6cfc84
commit 509a5c453f

View file

@ -7,6 +7,7 @@
module Hanafuda.Message ( module Hanafuda.Message (
T(..) T(..)
, FromClient(..) , FromClient(..)
, GameState(..)
, PrivateState(..) , PrivateState(..)
, PublicGame(..) , PublicGame(..)
, PublicPlayer(..) , PublicPlayer(..)
@ -139,8 +140,18 @@ instance FromJSON Hanafuda.Flower
instance ToJSON Hanafuda.Flower where instance ToJSON Hanafuda.Flower where
toEncoding = genericToEncoding defaultOptions 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 { data PrivateState = PrivateState {
hands :: Map PlayerID Hanafuda.Pack link :: GameState
, hands :: Map PlayerID Hanafuda.Pack
, deck :: [Hanafuda.Card] , deck :: [Hanafuda.Card]
} deriving Generic } deriving Generic
@ -158,7 +169,8 @@ instance ToJSON PublicPlayer where
toEncoding = genericToEncoding defaultOptions toEncoding = genericToEncoding defaultOptions
data PublicState = PublicState { data PublicState = PublicState {
mode :: KoiKoi.Mode gameState :: GameState
, mode :: KoiKoi.Mode
, scores :: KoiKoi.Scores , scores :: KoiKoi.Scores
, month :: Hanafuda.Flower , month :: Hanafuda.Flower
, nextPlayer :: KoiKoi.PlayerTurn , nextPlayer :: KoiKoi.PlayerTurn
@ -169,7 +181,6 @@ data PublicState = PublicState {
, river :: Hanafuda.Pack , river :: Hanafuda.Pack
, step :: KoiKoi.Step , step :: KoiKoi.Step
, trick :: [Hanafuda.Card] , trick :: [Hanafuda.Card]
, turns :: Int
, rounds :: [(PlayerID, KoiKoi.Score)] , rounds :: [(PlayerID, KoiKoi.Score)]
} deriving (Generic, Show) } deriving (Generic, Show)
@ -184,8 +195,7 @@ instance ToJSON ByteString where
toEncoding = toEncoding . decodeUtf8 . B64.encode toEncoding = toEncoding . decodeUtf8 . B64.encode
data PublicGame = PublicGame { data PublicGame = PublicGame {
gameID :: GameID nonce :: ByteString
, nonce :: ByteString
, playerHand :: Hanafuda.Pack , playerHand :: Hanafuda.Pack
, private :: ByteString , private :: ByteString
, public :: PublicState , public :: PublicState