From 231b46d9fbafaff2a8cdcc7bd6c9a813b1a61742 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Wed, 29 Jul 2020 21:51:54 +0200 Subject: Collect player sinks --- client/src/Client.elm | 3 ++- client/src/Messages.elm | 18 +++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) (limited to 'client') diff --git a/client/src/Client.elm b/client/src/Client.elm index ffb2b0e..0644a12 100644 --- a/client/src/Client.elm +++ b/client/src/Client.elm @@ -46,7 +46,8 @@ update msg model = case msg of WebSocketIn json -> case Json.Decode.decodeString Messages.jsonDecServerMessage json of Err str -> (Error <| Json.Decode.errorToString str, Cmd.none) - Ok Messages.Welcome -> Debug.log "Welcome" (model, Cmd.none) + Ok (Messages.Welcome playerId) -> + Debug.log ("Welcome " ++ String.fromInt playerId) (model, Cmd.none) Ok Messages.Bye -> Debug.log "Bye" (model, Cmd.none) main : Program () Model Msg diff --git a/client/src/Messages.elm b/client/src/Messages.elm index 2510544..76b24f5 100644 --- a/client/src/Messages.elm +++ b/client/src/Messages.elm @@ -9,18 +9,22 @@ import Set exposing (Set) type ServerMessage = - Welcome + Welcome Int | Bye jsonDecServerMessage : Json.Decode.Decoder ( ServerMessage ) -jsonDecServerMessage = - let jsonDecDictServerMessage = Dict.fromList [("Welcome", Welcome), ("Bye", Bye)] - in decodeSumUnaries "ServerMessage" jsonDecDictServerMessage +jsonDecServerMessage = + let jsonDecDictServerMessage = Dict.fromList + [ ("Welcome", Json.Decode.lazy (\_ -> Json.Decode.map Welcome (Json.Decode.int))) + , ("Bye", Json.Decode.lazy (\_ -> Json.Decode.succeed Bye)) + ] + in decodeSumObjectWithSingleField "ServerMessage" jsonDecDictServerMessage jsonEncServerMessage : ServerMessage -> Value jsonEncServerMessage val = - case val of - Welcome -> Json.Encode.string "Welcome" - Bye -> Json.Encode.string "Bye" + let keyval v = case v of + Welcome v1 -> ("Welcome", encodeValue (Json.Encode.int v1)) + Bye -> ("Bye", encodeValue (Json.Encode.list identity [])) + in encodeSumObjectWithSingleField keyval val -- cgit v1.2.3