From 8f692d36aca6c836a559520fea3da1cf49b68ae7 Mon Sep 17 00:00:00 2001 From: flavis Date: Sun, 29 Mar 2020 01:41:57 +0100 Subject: [PATCH] fix json instance --- src/Skat/AI/Online.hs | 1 + src/Skat/Bidding.hs | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Skat/AI/Online.hs b/src/Skat/AI/Online.hs index f7e8948..919e075 100644 --- a/src/Skat/AI/Online.hs +++ b/src/Skat/AI/Online.hs @@ -145,6 +145,7 @@ newtype ChosenResponse = ChosenResponse Card newtype BidResponse = BidResponse Int newtype YesNo = YesNo Bool newtype GameResponse = GameResponse Game + deriving Show newtype ChosenCards = ChosenCards [Card] instance ToJSON Query where diff --git a/src/Skat/Bidding.hs b/src/Skat/Bidding.hs index e9e29e9..8c613d7 100644 --- a/src/Skat/Bidding.hs +++ b/src/Skat/Bidding.hs @@ -9,6 +9,7 @@ import Data.Aeson hiding (Null) import Skat.Card import Data.List (sortOn) import Data.Ord (Down(..)) +import Control.Monad -- | different game types data Game = Colour Colour Modifier @@ -34,6 +35,7 @@ instance FromJSON Game where "nullhand" -> return NullHand "nullouvert" -> return NullOuvert "nullouverthand" -> return NullOuvertHand + _ -> mzero -- | modifiers for grand and colour games data Modifier = Einfach @@ -51,14 +53,15 @@ data Modifier = Einfach instance FromJSON Modifier where parseJSON = withObject "Modifier" $ \v -> do hnd <- v .: "hand" - if read hnd then do - schneider <- v .: "schneider" - schwarz <- v .: "schwarz" - ouvert <- v .: "ouvert" + if hnd then do + schneider <- v .:? "schneider" .!= False + schwarz <- v .:? "schwarz" .!= False + ouvert <- v .:? "ouvert" .!= False case (schneider, schwarz, ouvert) of (_, _, True) -> return Ouvert (True, False, _) -> return HandSchneiderAngesagt (_, True, _) -> return HandSchwarzAngesagt + _ -> return Hand else return Einfach -- | calculate the value of a game with given cards