tutor/www/src/Card.elm

98 lines
2.7 KiB
Elm

module Card exposing (..)
import Json.Decode
import Json.Decode.Pipeline as JDP
type alias Prices =
{ usd : Maybe Float
, usd_foil : Maybe Float
, eur : Maybe Float
, eur_foil : Maybe Float
, tix : Maybe Float
}
type alias Oracle =
{ oracleId : String
, name : String
, manaCost : String
, cmc : Float
, typeLine : String
, oracleText : String
}
type alias Card =
{ scryfallId : String
, name : String
, setCode : String
, rarity : String
, manaCost : String
, cmc : Float
, typeLine : String
, oracleText : String
, prices : Prices
}
type alias Copy =
{ card : Card
, collection : String
, foil : Bool
}
decodeOracle : Json.Decode.Decoder Oracle
decodeOracle =
Json.Decode.succeed Oracle
|> JDP.required "oracle_id" Json.Decode.string
|> JDP.required "name" Json.Decode.string
|> JDP.required "mana_cost"
(Json.Decode.nullable Json.Decode.string
|> Json.Decode.map (Maybe.withDefault "")
)
|> JDP.required "cmc" Json.Decode.float
|> JDP.required "type_line" Json.Decode.string
|> JDP.required "oracle_text"
(Json.Decode.nullable Json.Decode.string
|> Json.Decode.map (Maybe.withDefault "")
)
decode : Json.Decode.Decoder Card
decode =
Json.Decode.succeed Card
|> JDP.required "scryfall_id" Json.Decode.string
|> JDP.required "name" Json.Decode.string
|> JDP.required "set_code" Json.Decode.string
|> JDP.required "rarity" Json.Decode.string
|> JDP.required "mana_cost"
(Json.Decode.nullable Json.Decode.string
|> Json.Decode.map (Maybe.withDefault "")
)
|> JDP.required "cmc" Json.Decode.float
|> JDP.required "type_line" Json.Decode.string
|> JDP.required "oracle_text"
(Json.Decode.nullable Json.Decode.string
|> Json.Decode.map (Maybe.withDefault "")
)
|> JDP.required "prices" decodePrices
decodeCopy : Json.Decode.Decoder Copy
decodeCopy =
Json.Decode.succeed Copy
|> JDP.required "card" decode
|> JDP.required "collection" Json.Decode.string
|> JDP.required "foil" Json.Decode.bool
decodePrices : Json.Decode.Decoder Prices
decodePrices =
Json.Decode.succeed Prices
|> JDP.required "usd" (Json.Decode.nullable Json.Decode.float)
|> JDP.required "usd_foil" (Json.Decode.nullable Json.Decode.float)
|> JDP.required "eur" (Json.Decode.nullable Json.Decode.float)
|> JDP.required "eur_foil" (Json.Decode.nullable Json.Decode.float)
|> JDP.required "tix" (Json.Decode.nullable Json.Decode.float)