diff options
author | Jasper Van der Jeugt | 2020-07-30 21:48:22 +0200 |
---|---|---|
committer | Jasper Van der Jeugt | 2020-07-30 21:48:22 +0200 |
commit | ab1267a757bcc997f05cc9babe2d1fb9bb681ce4 (patch) | |
tree | 2d044f0a704f76c5b2adaa7a506bce406410ba48 /client | |
parent | 68588db76baaf8f2f17dd9b7f3649b09e102ea85 (diff) |
Sync cards only once
Diffstat (limited to 'client')
-rw-r--r-- | client/src/Client.elm | 39 | ||||
-rw-r--r-- | client/src/Messages.elm | 35 |
2 files changed, 55 insertions, 19 deletions
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 |