diff options
author | Jasper Van der Jeugt | 2020-07-30 14:43:25 +0200 |
---|---|---|
committer | Jasper Van der Jeugt | 2020-07-30 14:43:25 +0200 |
commit | 4e1068c41b84f0813b82fe61816271b92ca76f48 (patch) | |
tree | a0d0c0c612157a93b5c6548d61cd8103544facf3 /client | |
parent | 724d731227294f0b2975d66ed727aca3f89c30ab (diff) |
Basic syncing
Diffstat (limited to 'client')
-rw-r--r-- | client/src/Client.elm | 14 | ||||
-rw-r--r-- | client/src/Messages.elm | 16 |
2 files changed, 29 insertions, 1 deletions
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 |