From 4e1068c41b84f0813b82fe61816271b92ca76f48 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Thu, 30 Jul 2020 14:43:25 +0200 Subject: Basic syncing --- client/src/Client.elm | 14 +++++++++++++- client/src/Messages.elm | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) (limited to 'client/src') diff --git a/client/src/Client.elm b/client/src/Client.elm index 0644a12..442f089 100644 --- a/client/src/Client.elm +++ b/client/src/Client.elm @@ -3,7 +3,7 @@ port module Client exposing (main) import Browser import Html exposing (Html) import Json.Decode -import Messages +import Messages exposing (GameView) import Url exposing (Url) port webSocketIn : (String -> msg) -> Sub msg @@ -19,6 +19,9 @@ type Model | Connecting { roomId : String } + | Game + { view : GameView + } parseRoomId : Url -> Result String String parseRoomId url = case String.split "/" url.path of @@ -35,6 +38,13 @@ view model = case model of [ Html.h1 [] [Html.text <| "Connecting to room " ++ state.roomId ++ "..."] ] + Game game -> + [ Html.h1 [] [Html.text "Players"] + , Html.ul [] <| List.map + (\p -> Html.li [] [Html.text p]) + game.view.players + ] + subscriptions : Model -> Sub Msg subscriptions model = webSocketIn WebSocketIn @@ -49,6 +59,8 @@ update msg model = case msg of Ok (Messages.Welcome playerId) -> Debug.log ("Welcome " ++ String.fromInt playerId) (model, Cmd.none) Ok Messages.Bye -> Debug.log "Bye" (model, Cmd.none) + Ok (Messages.SyncGameView gameView) -> + (Game {view = gameView}, Cmd.none) main : Program () Model Msg main = Browser.application diff --git a/client/src/Messages.elm b/client/src/Messages.elm index 76b24f5..2239a74 100644 --- a/client/src/Messages.elm +++ b/client/src/Messages.elm @@ -8,14 +8,29 @@ import Dict exposing (Dict) import Set exposing (Set) +type alias GameView = + { players: (List String) + } + +jsonDecGameView : Json.Decode.Decoder ( GameView ) +jsonDecGameView = + Json.Decode.succeed (\pplayers -> {players = pplayers}) |> custom (Json.Decode.list (Json.Decode.string)) + +jsonEncGameView : GameView -> Value +jsonEncGameView val = + (Json.Encode.list Json.Encode.string) val.players + + type ServerMessage = Welcome Int + | SyncGameView GameView | Bye jsonDecServerMessage : Json.Decode.Decoder ( ServerMessage ) jsonDecServerMessage = let jsonDecDictServerMessage = Dict.fromList [ ("Welcome", Json.Decode.lazy (\_ -> Json.Decode.map Welcome (Json.Decode.int))) + , ("SyncGameView", Json.Decode.lazy (\_ -> Json.Decode.map SyncGameView (jsonDecGameView))) , ("Bye", Json.Decode.lazy (\_ -> Json.Decode.succeed Bye)) ] in decodeSumObjectWithSingleField "ServerMessage" jsonDecDictServerMessage @@ -24,6 +39,7 @@ jsonEncServerMessage : ServerMessage -> Value jsonEncServerMessage val = let keyval v = case v of Welcome v1 -> ("Welcome", encodeValue (Json.Encode.int v1)) + SyncGameView v1 -> ("SyncGameView", encodeValue (jsonEncGameView v1)) Bye -> ("Bye", encodeValue (Json.Encode.list identity [])) in encodeSumObjectWithSingleField keyval val -- cgit v1.2.3