1
0
Fork 0

[haskell] Implemented contract

This commit is contained in:
Julien Dessaux 2023-07-03 21:29:15 +02:00
parent a775330b4f
commit b3d57cb6ad
Signed by: adyxax
GPG key ID: F92E51B86E07177E
6 changed files with 82 additions and 5 deletions

View file

@ -0,0 +1,60 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module SpaceTraders.Model.Contract
( Contract(..)
, Delivery(..)
, Payment(..)
, Terms(..)
) where
import Control.Monad
import Data.Aeson
import Data.Time
import GHC.Generics
import qualified Data.Text as T
data Contract = Contract { accepted :: Bool
, contractId :: T.Text
, contractType :: T.Text
, expiration :: UTCTime
, deadlineToAccept :: UTCTime
, factionSymbol :: T.Text
, fulfilled :: Bool
, terms :: Terms
} deriving (Generic, Show)
instance FromJSON Contract where
parseJSON (Object o) = Contract <$> o .: "accepted"
<*> o .: "id"
<*> o .: "type"
<*> o .: "expiration"
<*> o .: "deadlineToAccept"
<*> o .: "factionSymbol"
<*> o .: "fulfilled"
<*> o .: "terms"
parseJSON _ = mzero
instance ToJSON Contract where
toEncoding (Contract a i ty e d fa fu te) = pairs ( "accepted" .= a
<> "id" .= i
<> "type" .= ty
<> "expiration" .= e
<> "deadlineToAccept" .= d
<> "factionSymbol" .= fa
<> "fulfilled" .= fu
<> "terms" .= te )
data Delivery = Delivery { destinationSymbol :: T.Text
, tradeSymbol :: T.Text
, unitsFulfilled :: Int
, unitsRequired :: Int
} deriving (FromJSON, Generic, Show, ToJSON)
data Payment = Payment { onAccepted :: Int
, onFulfilled :: Int
} deriving (FromJSON, Generic, Show, ToJSON)
data Terms = Terms { deadline :: UTCTime
, deliver :: [Delivery]
, payment :: Payment
} deriving (FromJSON, Generic, Show, ToJSON)