diff options
author | Jasper Van der Jeugt | 2020-07-30 16:16:52 +0200 |
---|---|---|
committer | Jasper Van der Jeugt | 2020-07-30 16:16:52 +0200 |
commit | f5a7875d605010540ec7c9c8f2a3ff4ed0702597 (patch) | |
tree | 2ea9642ff2c50ee86d861126d8a766fbd922e3d8 /client | |
parent | 4e1068c41b84f0813b82fe61816271b92ca76f48 (diff) |
Allow people to change their name
Diffstat (limited to '')
-rw-r--r-- | client/src/Client.elm | 62 | ||||
-rw-r--r-- | client/src/Messages.elm | 27 |
2 files changed, 82 insertions, 7 deletions
diff --git a/client/src/Client.elm b/client/src/Client.elm index 442f089..e5afa74 100644 --- a/client/src/Client.elm +++ b/client/src/Client.elm @@ -2,7 +2,10 @@ port module Client exposing (main) import Browser import Html exposing (Html) +import Html.Attributes +import Html.Events import Json.Decode +import Json.Encode import Messages exposing (GameView) import Url exposing (Url) @@ -13,6 +16,10 @@ type Msg = Ignore | Send | WebSocketIn String + -- Name changes + | StartChangingName + | ChangeName String + | SubmitNewName type Model = Error String @@ -21,6 +28,7 @@ type Model } | Game { view : GameView + , changingName : Maybe String } parseRoomId : Url -> Result String String @@ -39,16 +47,38 @@ view model = case model of [Html.text <| "Connecting to room " ++ state.roomId ++ "..."] ] Game game -> - [ Html.h1 [] [Html.text "Players"] + [ Html.h1 [] [Html.text "Opponents"] , Html.ul [] <| List.map (\p -> Html.li [] [Html.text p]) - game.view.players - ] + game.view.opponents + , Html.h1 [] [Html.text "You"] + ] ++ + (case game.changingName of + Nothing -> + [ Html.p [] + [Html.text game.view.playerName] + , Html.button + [Html.Events.onClick StartChangingName] + [Html.text "change"] + ] + Just name -> + [ Html.input + [ Html.Attributes.value name + , Html.Events.onInput ChangeName + ] + [] + , Html.button + [Html.Events.onClick SubmitNewName] + [Html.text "change"] + ]) subscriptions : Model -> Sub Msg subscriptions model = webSocketIn WebSocketIn +send : Messages.ClientMessage -> Cmd Msg +send = webSocketOut << Json.Encode.encode 0 << Messages.jsonEncClientMessage + update : Msg -> Model -> (Model, Cmd Msg) update msg model = case msg of Ignore -> (model, Cmd.none) @@ -60,7 +90,31 @@ update msg model = case msg of 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) + case model of + Game game -> (Game {game | view = gameView}, Cmd.none) + _ -> + ( Game + { view = gameView + , changingName = Nothing + } + , Cmd.none + ) + + StartChangingName -> case model of + Game game -> + (Game {game | changingName = Just game.view.playerName}, Cmd.none) + _ -> (model, Cmd.none) + ChangeName name -> case model of + Game game -> (Game {game | changingName = Just name}, Cmd.none) + _ -> (model, Cmd.none) + SubmitNewName -> case model of + Game game -> + ( Game {game | changingName = Nothing} + , case game.changingName of + Nothing -> Cmd.none + Just name -> send <| Messages.ChangeName name + ) + _ -> (model, Cmd.none) main : Program () Model Msg main = Browser.application diff --git a/client/src/Messages.elm b/client/src/Messages.elm index 2239a74..730a66d 100644 --- a/client/src/Messages.elm +++ b/client/src/Messages.elm @@ -9,16 +9,23 @@ import Set exposing (Set) type alias GameView = - { players: (List String) + { opponents: (List String) + , playerName: String } jsonDecGameView : Json.Decode.Decoder ( GameView ) jsonDecGameView = - Json.Decode.succeed (\pplayers -> {players = pplayers}) |> custom (Json.Decode.list (Json.Decode.string)) + Json.Decode.succeed (\popponents pplayerName -> {opponents = popponents, playerName = pplayerName}) + |> required "opponents" (Json.Decode.list (Json.Decode.string)) + |> required "playerName" (Json.Decode.string) jsonEncGameView : GameView -> Value jsonEncGameView val = - (Json.Encode.list Json.Encode.string) val.players + Json.Encode.object + [ ("opponents", (Json.Encode.list Json.Encode.string) val.opponents) + , ("playerName", Json.Encode.string val.playerName) + ] + type ServerMessage = @@ -44,3 +51,17 @@ jsonEncServerMessage val = in encodeSumObjectWithSingleField keyval val + +type ClientMessage = + ChangeName String + +jsonDecClientMessage : Json.Decode.Decoder ( ClientMessage ) +jsonDecClientMessage = + Json.Decode.lazy (\_ -> Json.Decode.map ChangeName (Json.Decode.string)) + + +jsonEncClientMessage : ClientMessage -> Value +jsonEncClientMessage (ChangeName v1) = + Json.Encode.string v1 + + |