Implement basic communications and messages.
This commit is contained in:
36
Go/Decoders.elm
Normal file
36
Go/Decoders.elm
Normal file
@@ -0,0 +1,36 @@
|
||||
module Go.Decoders exposing (..)
|
||||
import Go.Types exposing (..)
|
||||
import Json.Decode as Decode
|
||||
import Json.Decode.Pipeline exposing (decode, required)
|
||||
|
||||
pair : a -> b -> (a, b)
|
||||
pair a1 a2 = (a1, a2)
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user