Implement a separate public type for games to cipher a part of its content and sign another

This commit is contained in:
Tissevert 2019-10-16 10:31:30 +02:00
parent 325d9dd610
commit 2f7be7df2b
2 changed files with 41 additions and 6 deletions

View file

@ -23,6 +23,7 @@ library
-- other-extensions: -- other-extensions:
build-depends: aeson build-depends: aeson
, base >=4.9 && <4.13 , base >=4.9 && <4.13
, bytestring
, containers , containers
, hanafuda >= 0.3.3 , hanafuda >= 0.3.3
, text , text

View file

@ -14,22 +14,24 @@ module Hanafuda.Message (
import Data.Char (toLower) import Data.Char (toLower)
import Data.Aeson ( import Data.Aeson (
FromJSON(..), FromJSONKey(..), FromJSONKeyFunction(..), Options(..), SumEncoding(..), ToJSON(..), ToJSONKey(..) FromJSON(..), FromJSONKey(..), FromJSONKeyFunction(..), Options(..), SumEncoding(..), ToJSON(..), ToJSONKey(..)
, Value, (.:), (.=), defaultOptions, eitherDecode', encode, genericParseJSON , Value(..), (.:), (.=), defaultOptions, eitherDecode', encode, genericParseJSON
, genericToEncoding, object, pairs, withObject , genericToEncoding, object, pairs, withObject, withText
) )
import Data.Aeson.Types (toJSONKeyText) import Data.Aeson.Types (toJSONKeyText)
import Data.ByteString (ByteString)
import Data.Map (Map) import Data.Map (Map)
import Data.Monoid ((<>)) import Data.Monoid ((<>))
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as Text (pack, unpack) import qualified Data.Text as Text (pack, unpack)
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import GHC.Generics (Generic) import GHC.Generics (Generic)
import qualified Hanafuda (Card(..), Flower(..), Pack, cardsOfPack, empty, packOfCards) import qualified Hanafuda (Card(..), Flower(..), Pack, cardsOfPack, empty, packOfCards)
import Hanafuda.ID (ID(..), getID) import Hanafuda.ID (ID(..), getID)
import Hanafuda.KoiKoi (PlayerID, GameBlueprint(..)) import Hanafuda.KoiKoi (PlayerID)
import qualified Hanafuda.KoiKoi as KoiKoi ( import qualified Hanafuda.KoiKoi as KoiKoi (
Action(..), Game(..), Mode(..), Move(..), Score, Source(..), Step(..), Yaku(..) Action(..), Game(..), Mode(..), Move(..), Score, Source(..), Step(..), Yaku(..)
) )
import Hanafuda.Player (Player(..), Players(..)) import Hanafuda.Player (Player(..), Players(..), Scores)
deriving instance Generic PlayerID deriving instance Generic PlayerID
instance FromJSON PlayerID instance FromJSON PlayerID
@ -139,9 +141,41 @@ instance FromJSON Hanafuda.Flower
instance ToJSON Hanafuda.Flower where instance ToJSON Hanafuda.Flower where
toEncoding = genericToEncoding defaultOptions toEncoding = genericToEncoding defaultOptions
type PublicGame = GameBlueprint Int data PrivateState = PrivateState {
opponent :: Player KoiKoi.Score
, deck :: [Hanafuda.Card]
} deriving Generic
data PublicState = PublicState {
mode :: KoiKoi.Mode
, scores :: Scores KoiKoi.Score
, month :: Hanafuda.Flower
, playing :: PlayerID
, winning :: PlayerID
, oyake :: PlayerID
, river :: Hanafuda.Pack
, step :: KoiKoi.Step
, trick :: [Hanafuda.Card]
, rounds :: [(PlayerID, KoiKoi.Score)]
} deriving Generic
instance FromJSON PublicState
instance ToJSON PublicState where
toEncoding = genericToEncoding defaultOptions
instance FromJSON ByteString where
parseJSON = withText "ByteString" (return . encodeUtf8)
instance ToJSON ByteString where
toJSON = String . decodeUtf8
toEncoding = toEncoding . decodeUtf8
data PublicGame = PublicGame {
player :: Player KoiKoi.Score
, privateState :: ByteString
, publicState :: PublicState
, publicSignature :: ByteString
} deriving Generic
deriving instance Generic PublicGame
instance FromJSON PublicGame instance FromJSON PublicGame
instance ToJSON PublicGame where instance ToJSON PublicGame where
toEncoding = genericToEncoding defaultOptions toEncoding = genericToEncoding defaultOptions