From b965920ada97105bdad75902a550520e3b3bf468 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Wed, 7 Jul 2021 13:28:50 +0200 Subject: [PATCH] random name generator and stuff --- frontend/assets/sass/style.scss | 6 +- frontend/elm.json | 1 + frontend/src/Main.elm | 117 ++++++++++++++++++++++++++++++-- frontend/src/Model.elm | 3 + frontend/src/WsMessage.elm | 73 ++++++++++++++------ 5 files changed, 176 insertions(+), 24 deletions(-) diff --git a/frontend/assets/sass/style.scss b/frontend/assets/sass/style.scss index 470a4a4..97bb155 100644 --- a/frontend/assets/sass/style.scss +++ b/frontend/assets/sass/style.scss @@ -36,13 +36,17 @@ nav { background-size: contain; } - .board-search-input { + input { background-color: white; border-radius: .3em; border: 1px solid white; padding: .4em; } + button { + line-height: 1.7; + } + ul { grid-area: rest-nav; list-style-type: none; diff --git a/frontend/elm.json b/frontend/elm.json index 8282746..ea8fdc6 100644 --- a/frontend/elm.json +++ b/frontend/elm.json @@ -11,6 +11,7 @@ "elm/html": "1.0.0", "elm/http": "2.0.0", "elm/json": "1.1.2", + "elm/random": "1.0.0", "elm/url": "1.0.0" }, "indirect": { diff --git a/frontend/src/Main.elm b/frontend/src/Main.elm index 6d0bd81..ecece82 100644 --- a/frontend/src/Main.elm +++ b/frontend/src/Main.elm @@ -4,10 +4,13 @@ import Browser import Browser.Navigation as Nav import Html exposing (..) import Html.Attributes exposing (..) +import Html.Events exposing (onClick, onInput) import Model exposing (Model) import Navigation +import Random import Url import Url.Parser +import WsMessage exposing (createSession) main = @@ -59,6 +62,75 @@ type Msg | LinkClicked Browser.UrlRequest | WsIn String | WsOut String + | CreateRetro + | CreatePoker + | NameChanged String + | NameGenerated ScrambledName + | RandomizeName + + + +-- BOOT TIME + + +type alias ScrambledName = + { prefix : String + , suffix : String + } + + +randomUserName : Cmd Msg +randomUserName = + let + prefixes = + [ "Joyful" + , "Squiddly" + , "Fast" + , "Tinkering" + , "Freezy" + , "Warm-To-The-Touch" + , "Red" + , "Pale" + , "Hardy" + , "Hardened" + , "Rocky" + , "Pokey" + , "Screeching" + , "Sweet" + , "Grumpy" + , "Tempered" + , "Bendy" + ] + + suffixes = + [ "Rock" + , "Impala" + , "Liliac" + , "Bike" + , "Tinker" + , "Red" + , "Green" + , "Squid" + , "Sponge" + , "Star" + , "Fish" + , "Tandem" + , "Ground dweller" + , "Bird" + , "Ice cream" + , "Brush" + , "Highborn" + ] + in + Random.generate NameGenerated + (Random.map2 ScrambledName + (Random.uniform "Chuck" prefixes) + (Random.uniform "Norris" suffixes) + ) + + + +-- UPDATE PART update : Msg -> Model -> ( Model, Cmd Msg ) @@ -77,19 +149,39 @@ update msg model = -- TODO error reporting ( { model | route = Navigation.Home }, Cmd.none ) + -- WEBSOCKETS WsOut wsmsg -> ( model, wsout wsmsg ) WsIn strmsg -> ( model, Cmd.none ) + -- INIT + NameChanged name -> + ( { model | userName = name }, Cmd.none ) + + NameGenerated scrambled -> + ( { model | userName = scrambled.prefix ++ " " ++ scrambled.suffix }, Cmd.none ) + + RandomizeName -> + ( model, randomUserName ) + + -- RETRO + CreateRetro -> + Debug.todo "implement create retro" + + -- POKER + CreatePoker -> + Debug.log (createSession model) + ( model, wsout (createSession model) ) + routeChanged : Navigation.Route -> Model -> ( Model, Cmd Msg ) routeChanged route model = case route of Navigation.Home -> ( { model | route = route } - , Cmd.none + , randomUserName ) Navigation.Poker session -> @@ -98,6 +190,10 @@ routeChanged route model = ) + +-- HTML STUFF + + view : Model -> Browser.Document Msg view model = { title = "Open-Retro" @@ -106,11 +202,24 @@ view model = [ h1 [ class "app-title" ] [ text "Open-Retro" ] , ul [] [ li [] - [ input [ class "board-search-input", placeholder "Session ID..." ] [] ] + [ input [ onInput NameChanged, value model.userName ] + [] + ] + , li [] + [ button + [ onClick RandomizeName ] + [ text "🎲" ] + ] , li [] - [ text "🂿 New retro" ] + [ button + [ onClick CreateRetro ] + [ text "🂿 New retro" ] + ] , li [] - [ text "🃠 New poker" ] + [ button + [ onClick CreatePoker ] + [ text "🃠 New poker" ] + ] ] ] , main_ [] (appContent model) diff --git a/frontend/src/Model.elm b/frontend/src/Model.elm index 0a42d20..70ec89f 100644 --- a/frontend/src/Model.elm +++ b/frontend/src/Model.elm @@ -16,6 +16,7 @@ type alias Model = , route : Navigation.Route , httpError : Maybe Http.Error , userUuid : Uuid + , userName : String , session : Maybe String } @@ -38,6 +39,8 @@ initialModel init = Maybe.Nothing -- userUuid init.userUuid + -- userName + "" -- session Maybe.Nothing diff --git a/frontend/src/WsMessage.elm b/frontend/src/WsMessage.elm index a1a1c11..13b36bd 100644 --- a/frontend/src/WsMessage.elm +++ b/frontend/src/WsMessage.elm @@ -1,19 +1,42 @@ -module WsMessage exposing (..) +module WsMessage exposing (Action(..), Publisher, WsMessage, createSession, decode, msgDecoder, strToAction) +import Dict exposing (Dict) import Json.Decode exposing (Decoder, andThen, decodeString, fail, field, map2, map3, string, succeed) +import Json.Encode as Enc +import Model exposing (Model) type Action = Publish + | CreateSession + + +stringToAction : Dict String Action +stringToAction = + Dict.fromList + [ ( "Publish", Publish ) + , ( "CreateSession", CreateSession ) + ] + + +actionToString : Action -> String +actionToString action = + Dict.filter + (\_ v -> v == action) + stringToAction + |> Dict.keys + |> List.head + |> Maybe.withDefault "" strToAction : String -> Decoder Action strToAction str = - if str == "Publish" then - succeed Publish + case Dict.get str stringToAction of + Just val -> + succeed val - else - fail ("invalid action '" ++ str ++ "'") + Nothing -> + fail ("invalid action '" ++ str ++ "'") type alias Publisher = @@ -33,26 +56,19 @@ type alias WsMessage = -- DECODERS -publisherDecoder : Decoder Publisher -publisherDecoder = - map2 Publisher - (field "uuid" string) - (field "name" string) - - msgDecoder : Decoder WsMessage msgDecoder = map3 WsMessage - actionDecoder - (field "publisher" publisherDecoder) + (field "action" string |> andThen strToAction) + (field "publisher" + (map2 Publisher + (field "uuid" string) + (field "name" string) + ) + ) (field "payload" string) -actionDecoder : Decoder Action -actionDecoder = - field "action" string |> andThen strToAction - - decode : String -> Maybe WsMessage decode str = case decodeString msgDecoder str of @@ -61,3 +77,22 @@ decode str = Err _ -> Nothing + + + +-- ENCODERS + + +createSession : Model -> String +createSession model = + Enc.object + [ ( "action", Enc.string (actionToString CreateSession) ) + , ( "publisher" + , Enc.object + [ ( "uuid", Enc.string model.userUuid ) + , ( "name", Enc.string model.userName ) + ] + ) + , ( "payload", Enc.string "" ) + ] + |> Enc.encode 0