diff options
author | Jasper Van der Jeugt | 2020-07-30 17:02:43 +0200 |
---|---|---|
committer | Jasper Van der Jeugt | 2020-07-30 17:02:43 +0200 |
commit | f4b0e4ec20cd903b7a603edfb043a21bb175b246 (patch) | |
tree | 72e16e3b6851d238698c1d076c73a04e0a618786 | |
parent | f5a7875d605010540ec7c9c8f2a3ff4ed0702597 (diff) |
Refactor name changing
-rw-r--r-- | client/src/Client.elm | 57 | ||||
-rw-r--r-- | client/src/Messages.elm | 14 | ||||
-rw-r--r-- | server/lib/Cafp/Game.hs | 6 | ||||
-rw-r--r-- | server/lib/Cafp/Messages.hs | 6 |
4 files changed, 32 insertions, 51 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 diff --git a/server/lib/Cafp/Game.hs b/server/lib/Cafp/Game.hs index f3d500f..af958ae 100644 --- a/server/lib/Cafp/Game.hs +++ b/server/lib/Cafp/Game.hs @@ -48,7 +48,7 @@ leaveGame pid = over gamePlayers $ HMS.delete pid processClientMessage :: PlayerId -> ClientMessage -> Game -> Game processClientMessage pid msg game = case msg of - ChangeName name -> + ChangeMyName name -> game & gamePlayers . ix pid .~ name gameViewForPlayer :: PlayerId -> Game -> GameView @@ -56,6 +56,6 @@ gameViewForPlayer self game = let opponents = map snd . HMS.toList . HMS.delete self $ game ^. gamePlayers name = fromMaybe "" $ game ^. gamePlayers . at self in GameView - { gameViewOpponents = opponents - , gameViewPlayerName = name + { gameViewOpponents = opponents + , gameViewMyName = name } diff --git a/server/lib/Cafp/Messages.hs b/server/lib/Cafp/Messages.hs index d92ce80..29f5575 100644 --- a/server/lib/Cafp/Messages.hs +++ b/server/lib/Cafp/Messages.hs @@ -9,8 +9,8 @@ import Data.Text (Text) import Elm.Derive data GameView = GameView - { gameViewOpponents :: [Text] - , gameViewPlayerName :: Text + { gameViewOpponents :: [Text] + , gameViewMyName :: Text } deriving (Show) data ServerMessage @@ -20,7 +20,7 @@ data ServerMessage deriving (Show) data ClientMessage - = ChangeName Text + = ChangeMyName Text deriving (Show) deriveBoth (defaultOptionsDropLower 8) ''GameView |