From 2f7be7df2bb791fbe44bf041d27721c4b2d9a26a Mon Sep 17 00:00:00 2001 From: Tissevert Date: Wed, 16 Oct 2019 10:31:30 +0200 Subject: [PATCH] Implement a separate public type for games to cipher a part of its content and sign another --- hanafuda-APILanguage.cabal | 1 + src/Hanafuda/Message.hs | 46 +++++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/hanafuda-APILanguage.cabal b/hanafuda-APILanguage.cabal index 6a54660..9cded08 100644 --- a/hanafuda-APILanguage.cabal +++ b/hanafuda-APILanguage.cabal @@ -23,6 +23,7 @@ library -- other-extensions: build-depends: aeson , base >=4.9 && <4.13 + , bytestring , containers , hanafuda >= 0.3.3 , text diff --git a/src/Hanafuda/Message.hs b/src/Hanafuda/Message.hs index c83b451..96350d1 100644 --- a/src/Hanafuda/Message.hs +++ b/src/Hanafuda/Message.hs @@ -14,22 +14,24 @@ module Hanafuda.Message ( import Data.Char (toLower) import Data.Aeson ( FromJSON(..), FromJSONKey(..), FromJSONKeyFunction(..), Options(..), SumEncoding(..), ToJSON(..), ToJSONKey(..) - , Value, (.:), (.=), defaultOptions, eitherDecode', encode, genericParseJSON - , genericToEncoding, object, pairs, withObject + , Value(..), (.:), (.=), defaultOptions, eitherDecode', encode, genericParseJSON + , genericToEncoding, object, pairs, withObject, withText ) import Data.Aeson.Types (toJSONKeyText) +import Data.ByteString (ByteString) import Data.Map (Map) import Data.Monoid ((<>)) import Data.Text (Text) import qualified Data.Text as Text (pack, unpack) +import Data.Text.Encoding (decodeUtf8, encodeUtf8) import GHC.Generics (Generic) import qualified Hanafuda (Card(..), Flower(..), Pack, cardsOfPack, empty, packOfCards) import Hanafuda.ID (ID(..), getID) -import Hanafuda.KoiKoi (PlayerID, GameBlueprint(..)) +import Hanafuda.KoiKoi (PlayerID) import qualified Hanafuda.KoiKoi as KoiKoi ( Action(..), Game(..), Mode(..), Move(..), Score, Source(..), Step(..), Yaku(..) ) -import Hanafuda.Player (Player(..), Players(..)) +import Hanafuda.Player (Player(..), Players(..), Scores) deriving instance Generic PlayerID instance FromJSON PlayerID @@ -139,9 +141,41 @@ instance FromJSON Hanafuda.Flower instance ToJSON Hanafuda.Flower where 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 ToJSON PublicGame where toEncoding = genericToEncoding defaultOptions