2018-05-25 10:40:35 -07:00
|
|
|
module Go.Decoders exposing (..)
|
|
|
|
import Go.Types exposing (..)
|
2018-05-25 12:06:15 -07:00
|
|
|
import Go.Util exposing (pair)
|
2018-05-25 10:40:35 -07:00
|
|
|
import Json.Decode as Decode
|
|
|
|
import Json.Decode.Pipeline exposing (decode, required)
|
|
|
|
|
|
|
|
decodeIndex : Decode.Decoder Index
|
|
|
|
decodeIndex = decode pair
|
|
|
|
|> required "x" Decode.int
|
|
|
|
|> required "y" Decode.int
|
|
|
|
|
|
|
|
decodeColor : Decode.Decoder Color
|
|
|
|
decodeColor =
|
|
|
|
let
|
|
|
|
tryDecode : String -> Decode.Decoder Color
|
|
|
|
tryDecode s = case s of
|
|
|
|
"White" -> Decode.succeed White
|
|
|
|
"Black" -> Decode.succeed Black
|
|
|
|
_ -> Decode.fail "Invalid color"
|
|
|
|
in
|
|
|
|
Decode.andThen tryDecode Decode.string
|
|
|
|
|
|
|
|
decodeCell : Decode.Decoder Cell
|
|
|
|
decodeCell = decode pair
|
|
|
|
|> required "index" decodeIndex
|
|
|
|
|> required "color" decodeColor
|
|
|
|
|
|
|
|
decodeUpdate : Decode.Decoder Update
|
|
|
|
decodeUpdate = decode pair
|
|
|
|
|> required "turn" decodeColor
|
|
|
|
|> required "board" (Decode.list decodeCell)
|
|
|
|
|
|
|
|
decodeUpdatestring : String -> Result String Update
|
|
|
|
decodeUpdatestring = Decode.decodeString decodeUpdate
|