Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryan-senn/a40fc763a1d6016714ee47f079a689a0 to your computer and use it in GitHub Desktop.
Save ryan-senn/a40fc763a1d6016714ee47f079a689a0 to your computer and use it in GitHub Desktop.
module Decoders exposing (..)
import Json.Decode exposing (Decoder, list, dict, string, int, nullable, andThen, succeed, fail)
import Json.Decode.Pipeline exposing (decode, required)
import Json.Decode.Extra exposing (date)
import Dict exposing (Dict)
import Types exposing
( Data
, Config, FieldsConfig, FieldLevelConfig
, Tile, TileType (..)
, Resource (..), ResourceAmount, ResourceStock
, Settlement
, Field, FieldType (..), Farm (..)
)
dataDecoder : Decoder Data
dataDecoder =
decode Data
|> required "tiles" tilesDecoder
|> required "settlements" settlementsDecoder
|> required "config" configDecoder
|> required "date" date
tilesDecoder : Decoder (Dict String Tile)
tilesDecoder =
dict tileDecoder
tileDecoder : Decoder Tile
tileDecoder =
decode Tile
|> required "id" string
|> required "x" int
|> required "y" int
|> required "tileType" tileTypeDecoder
|> required "fields" fieldsDecoder
tileTypeDecoder : Decoder TileType
tileTypeDecoder =
string
|> andThen stringToTileType
stringToTileType : String -> Decoder TileType
stringToTileType tileType =
case tileType of
"Valley" ->
succeed Valley
"Mountain" ->
succeed Mountain
"Desert" ->
succeed Desert
"Lake" ->
succeed Lake
"Swamp" ->
succeed Swamp
_ ->
fail ("Value " ++ tileType ++ " is not a valid tileType")
settlementsDecoder : Decoder (Dict String Settlement)
settlementsDecoder =
dict settlementDecoder
settlementDecoder : Decoder Settlement
settlementDecoder =
decode Settlement
|> required "id" string
|> required "tile" tileDecoder
|> required "wood" resourceStockDecoder
|> required "stone" resourceStockDecoder
|> required "iron" resourceStockDecoder
|> required "clay" resourceStockDecoder
|> required "fruit" resourceStockDecoder
|> required "vegetable" resourceStockDecoder
|> required "meat" resourceStockDecoder
|> required "wheat" resourceStockDecoder
|> required "hop" resourceStockDecoder
|> required "bread" resourceStockDecoder
|> required "beer" resourceStockDecoder
fieldsDecoder : Decoder (Dict String Field)
fieldsDecoder =
dict fieldDecoder
fieldDecoder : Decoder Field
fieldDecoder =
decode Field
|> required "id" string
|> required "fieldType" fieldTypeDecoder
|> required "level" int
fieldTypeDecoder : Decoder FieldType
fieldTypeDecoder =
string
|> andThen stringToFieldType
stringToFieldType : String -> Decoder FieldType
stringToFieldType fieldType =
case fieldType of
"Forest" ->
succeed Forest
"StoneQuarry" ->
succeed StoneQuarry
"IronMine" ->
succeed IronMine
"ClayPit" ->
succeed ClayPit
"Undeveloped" ->
succeed (Farm Undeveloped)
"FruitFarm" ->
succeed (Farm FruitFarm)
"VegetableFarm" ->
succeed (Farm VegetableFarm)
"CattleFarm" ->
succeed (Farm CattleFarm)
"WheatFarm" ->
succeed (Farm WheatFarm)
"HopFarm" ->
succeed (Farm HopFarm)
_ ->
fail ("Value " ++ fieldType ++ " is not a valid fieldType")
configDecoder : Decoder Config
configDecoder =
decode Config
|> required "fields" fieldsConfigDecoder
fieldsConfigDecoder : Decoder FieldsConfig
fieldsConfigDecoder =
decode FieldsConfig
|> required "forest" fieldLevelsConfigDecoder
fieldLevelsConfigDecoder : Decoder (Dict String FieldLevelConfig)
fieldLevelsConfigDecoder =
dict fieldLevelConfigDecoder
fieldLevelConfigDecoder : Decoder FieldLevelConfig
fieldLevelConfigDecoder =
decode FieldLevelConfig
|> required "level" int
|> required "cost" costsDecoder
|> required "time" int
|> required "production" resourceAmountDecoder
costsDecoder : Decoder (List ResourceAmount)
costsDecoder =
list resourceAmountDecoder
resourceAmountDecoder : Decoder ResourceAmount
resourceAmountDecoder =
decode ResourceAmount
|> required "resource" resourceDecoder
|> required "amount" int
resourceDecoder : Decoder Resource
resourceDecoder =
string
|> andThen stringToResource
stringToResource : String -> Decoder Resource
stringToResource resource =
case resource of
"Wood" ->
succeed Wood
"Stone" ->
succeed Stone
"Iron" ->
succeed Iron
"Clay" ->
succeed Clay
"Fruit" ->
succeed Fruit
"Vegetable" ->
succeed Vegetable
"Meat" ->
succeed Meat
"Wheat" ->
succeed Wheat
"Hop" ->
succeed Hop
"Bread" ->
succeed Bread
"Beer" ->
succeed Beer
_ ->
fail ("Value " ++ resource ++ " is not a valid resource")
resourceStockDecoder : Decoder ResourceStock
resourceStockDecoder =
decode ResourceStock
|> required "amount" int
|> required "lastUpdated" (nullable date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment