aboutsummaryrefslogtreecommitdiff
path: root/client/src
diff options
context:
space:
mode:
authorJasper Van der Jeugt2020-07-30 17:02:43 +0200
committerJasper Van der Jeugt2020-07-30 17:02:43 +0200
commitf4b0e4ec20cd903b7a603edfb043a21bb175b246 (patch)
tree72e16e3b6851d238698c1d076c73a04e0a618786 /client/src
parentf5a7875d605010540ec7c9c8f2a3ff4ed0702597 (diff)
Refactor name changing
Diffstat (limited to 'client/src')
-rw-r--r--client/src/Client.elm57
-rw-r--r--client/src/Messages.elm14
2 files changed, 26 insertions, 45 deletions
diff --git a/client/src/Client.elm b/client/src/Client.elm
index e5afa74..aa2121c 100644
--- a/client/src/Client.elm
+++ b/client/src/Client.elm
@@ -17,9 +17,8 @@ type Msg
| Send
| WebSocketIn String
-- Name changes
- | StartChangingName
- | ChangeName String
- | SubmitNewName
+ | ChangeMyName String
+ | SubmitMyName
type Model
= Error String
@@ -28,7 +27,7 @@ type Model
}
| Game
{ view : GameView
- , changingName : Maybe String
+ , changeMyName : String
}
parseRoomId : Url -> Result String String
@@ -52,26 +51,17 @@ view model = case model of
(\p -> Html.li [] [Html.text p])
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"]
- ])
-
+ , Html.input
+ [ Html.Attributes.value game.changeMyName
+ , Html.Events.onInput ChangeMyName
+ ]
+ []
+ , Html.button
+ [ Html.Events.onClick SubmitMyName
+ , Html.Attributes.disabled <| game.view.myName == game.changeMyName
+ ]
+ [Html.text "change"]
+ ]
subscriptions : Model -> Sub Msg
subscriptions model = webSocketIn WebSocketIn
@@ -95,25 +85,16 @@ update msg model = case msg of
_ ->
( Game
{ view = gameView
- , changingName = Nothing
+ , changeMyName = gameView.myName
}
, 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)
+ ChangeMyName name -> case model of
+ Game game -> (Game {game | changeMyName = 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
- )
+ SubmitMyName -> case model of
+ Game game -> (model , send <| Messages.ChangeMyName game.changeMyName)
_ -> (model, Cmd.none)
main : Program () Model Msg
diff --git a/client/src/Messages.elm b/client/src/Messages.elm
index 730a66d..3fbacc3 100644
--- a/client/src/Messages.elm
+++ b/client/src/Messages.elm
@@ -10,20 +10,20 @@ import Set exposing (Set)
type alias GameView =
{ opponents: (List String)
- , playerName: String
+ , myName: String
}
jsonDecGameView : Json.Decode.Decoder ( GameView )
jsonDecGameView =
- Json.Decode.succeed (\popponents pplayerName -> {opponents = popponents, playerName = pplayerName})
+ Json.Decode.succeed (\popponents pmyName -> {opponents = popponents, myName = pmyName})
|> required "opponents" (Json.Decode.list (Json.Decode.string))
- |> required "playerName" (Json.Decode.string)
+ |> required "myName" (Json.Decode.string)
jsonEncGameView : GameView -> Value
jsonEncGameView val =
Json.Encode.object
[ ("opponents", (Json.Encode.list Json.Encode.string) val.opponents)
- , ("playerName", Json.Encode.string val.playerName)
+ , ("myName", Json.Encode.string val.myName)
]
@@ -53,15 +53,15 @@ jsonEncServerMessage val =
type ClientMessage =
- ChangeName String
+ ChangeMyName String
jsonDecClientMessage : Json.Decode.Decoder ( ClientMessage )
jsonDecClientMessage =
- Json.Decode.lazy (\_ -> Json.Decode.map ChangeName (Json.Decode.string))
+ Json.Decode.lazy (\_ -> Json.Decode.map ChangeMyName (Json.Decode.string))
jsonEncClientMessage : ClientMessage -> Value
-jsonEncClientMessage (ChangeName v1) =
+jsonEncClientMessage (ChangeMyName v1) =
Json.Encode.string v1