From ab1267a757bcc997f05cc9babe2d1fb9bb681ce4 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Thu, 30 Jul 2020 21:48:22 +0200 Subject: Sync cards only once --- client/src/Client.elm | 39 ++++++++++++++++++++++++++------------- client/src/Messages.elm | 35 +++++++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 19 deletions(-) (limited to 'client') diff --git a/client/src/Client.elm b/client/src/Client.elm index 41871e9..5c6d167 100644 --- a/client/src/Client.elm +++ b/client/src/Client.elm @@ -1,9 +1,10 @@ port module Client exposing (main) +import Array exposing (Array) import Browser -import Html exposing (Html) import Html.Attributes import Html.Events +import Html exposing (Html) import Json.Decode import Json.Encode import Messages exposing (GameView) @@ -20,13 +21,16 @@ type Msg | ChangeMyName String | SubmitMyName +type alias Cards = {black : Array String, white : Array String} + type Model = Error String | Connecting { roomId : String } | Game - { view : GameView + { cards : Cards + , view : GameView , changeMyName : String } @@ -70,21 +74,21 @@ view model = case model of ] ++ (case game.view.blackCard of Nothing -> [] - Just c -> [blackCard c]) ++ - (List.map whiteCard game.view.hand) + Just c -> [blackCard game.cards c]) ++ + (List.map (whiteCard game.cards) game.view.hand) -blackCard : Messages.BlackCard -> Html a -blackCard (Messages.BlackCard string) = +blackCard : Cards -> Messages.BlackCard -> Html a +blackCard cards (Messages.BlackCard idx) = let blank = Html.span [Html.Attributes.class "blank"] [] in Html.div [Html.Attributes.class "card", Html.Attributes.class "black"] <| - List.intersperse blank <| - List.map Html.text <| - String.split "\\BLANK" string + List.intersperse blank <| List.map Html.text <| + String.split "\\BLANK" <| Maybe.withDefault "" <| + Array.get idx cards.black -whiteCard : Messages.WhiteCard -> Html a -whiteCard (Messages.WhiteCard string) = Html.div +whiteCard : Cards -> Messages.WhiteCard -> Html a +whiteCard cards (Messages.WhiteCard idx) = Html.div [Html.Attributes.class "card", Html.Attributes.class "white"] - [Html.text string] + [Html.text <| Maybe.withDefault "" <| Array.get idx cards.white] subscriptions : Model -> Sub Msg subscriptions model = webSocketIn WebSocketIn @@ -107,11 +111,20 @@ update msg model = case msg of Game game -> (Game {game | view = gameView}, Cmd.none) _ -> ( Game - { view = gameView + { cards = {black = Array.empty, white = Array.empty} + , view = gameView , changeMyName = gameView.myName } , Cmd.none ) + Ok (Messages.SyncCards cards) -> + let arr = + { black = Array.fromList cards.black + , white = Array.fromList cards.white + } in + case model of + Game game -> (Game {game | cards = arr}, Cmd.none) + _ -> (model, Cmd.none) ChangeMyName name -> case model of Game game -> (Game {game | changeMyName = name}, Cmd.none) diff --git a/client/src/Messages.elm b/client/src/Messages.elm index d2c2716..15a0d1c 100644 --- a/client/src/Messages.elm +++ b/client/src/Messages.elm @@ -9,30 +9,50 @@ import Set exposing (Set) type BlackCard = - BlackCard String + BlackCard Int jsonDecBlackCard : Json.Decode.Decoder ( BlackCard ) jsonDecBlackCard = - Json.Decode.lazy (\_ -> Json.Decode.map BlackCard (Json.Decode.string)) + Json.Decode.lazy (\_ -> Json.Decode.map BlackCard (Json.Decode.int)) jsonEncBlackCard : BlackCard -> Value jsonEncBlackCard (BlackCard v1) = - Json.Encode.string v1 + Json.Encode.int v1 type WhiteCard = - WhiteCard String + WhiteCard Int jsonDecWhiteCard : Json.Decode.Decoder ( WhiteCard ) jsonDecWhiteCard = - Json.Decode.lazy (\_ -> Json.Decode.map WhiteCard (Json.Decode.string)) + Json.Decode.lazy (\_ -> Json.Decode.map WhiteCard (Json.Decode.int)) jsonEncWhiteCard : WhiteCard -> Value jsonEncWhiteCard (WhiteCard v1) = - Json.Encode.string v1 + Json.Encode.int v1 + + + +type alias Cards = + { black: (List String) + , white: (List String) + } + +jsonDecCards : Json.Decode.Decoder ( Cards ) +jsonDecCards = + Json.Decode.succeed (\pblack pwhite -> {black = pblack, white = pwhite}) + |> required "black" (Json.Decode.list (Json.Decode.string)) + |> required "white" (Json.Decode.list (Json.Decode.string)) + +jsonEncCards : Cards -> Value +jsonEncCards val = + Json.Encode.object + [ ("black", (Json.Encode.list Json.Encode.string) val.black) + , ("white", (Json.Encode.list Json.Encode.string) val.white) + ] @@ -64,6 +84,7 @@ jsonEncGameView val = type ServerMessage = Welcome Int + | SyncCards Cards | SyncGameView GameView | Bye @@ -71,6 +92,7 @@ jsonDecServerMessage : Json.Decode.Decoder ( ServerMessage ) jsonDecServerMessage = let jsonDecDictServerMessage = Dict.fromList [ ("Welcome", Json.Decode.lazy (\_ -> Json.Decode.map Welcome (Json.Decode.int))) + , ("SyncCards", Json.Decode.lazy (\_ -> Json.Decode.map SyncCards (jsonDecCards))) , ("SyncGameView", Json.Decode.lazy (\_ -> Json.Decode.map SyncGameView (jsonDecGameView))) , ("Bye", Json.Decode.lazy (\_ -> Json.Decode.succeed Bye)) ] @@ -80,6 +102,7 @@ jsonEncServerMessage : ServerMessage -> Value jsonEncServerMessage val = let keyval v = case v of Welcome v1 -> ("Welcome", encodeValue (Json.Encode.int v1)) + SyncCards v1 -> ("SyncCards", encodeValue (jsonEncCards v1)) SyncGameView v1 -> ("SyncGameView", encodeValue (jsonEncGameView v1)) Bye -> ("Bye", encodeValue (Json.Encode.list identity [])) in encodeSumObjectWithSingleField keyval val -- cgit v1.2.3