random name generator and stuff

master
Josha von Gizycki 3 years ago
parent 0a75805e1a
commit b965920ada

@ -36,13 +36,17 @@ nav {
background-size: contain; background-size: contain;
} }
.board-search-input { input {
background-color: white; background-color: white;
border-radius: .3em; border-radius: .3em;
border: 1px solid white; border: 1px solid white;
padding: .4em; padding: .4em;
} }
button {
line-height: 1.7;
}
ul { ul {
grid-area: rest-nav; grid-area: rest-nav;
list-style-type: none; list-style-type: none;

@ -11,6 +11,7 @@
"elm/html": "1.0.0", "elm/html": "1.0.0",
"elm/http": "2.0.0", "elm/http": "2.0.0",
"elm/json": "1.1.2", "elm/json": "1.1.2",
"elm/random": "1.0.0",
"elm/url": "1.0.0" "elm/url": "1.0.0"
}, },
"indirect": { "indirect": {

@ -4,10 +4,13 @@ import Browser
import Browser.Navigation as Nav import Browser.Navigation as Nav
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (..) import Html.Attributes exposing (..)
import Html.Events exposing (onClick, onInput)
import Model exposing (Model) import Model exposing (Model)
import Navigation import Navigation
import Random
import Url import Url
import Url.Parser import Url.Parser
import WsMessage exposing (createSession)
main = main =
@ -59,6 +62,75 @@ type Msg
| LinkClicked Browser.UrlRequest | LinkClicked Browser.UrlRequest
| WsIn String | WsIn String
| WsOut 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 ) update : Msg -> Model -> ( Model, Cmd Msg )
@ -77,19 +149,39 @@ update msg model =
-- TODO error reporting -- TODO error reporting
( { model | route = Navigation.Home }, Cmd.none ) ( { model | route = Navigation.Home }, Cmd.none )
-- WEBSOCKETS
WsOut wsmsg -> WsOut wsmsg ->
( model, wsout wsmsg ) ( model, wsout wsmsg )
WsIn strmsg -> WsIn strmsg ->
( model, Cmd.none ) ( 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 : Navigation.Route -> Model -> ( Model, Cmd Msg )
routeChanged route model = routeChanged route model =
case route of case route of
Navigation.Home -> Navigation.Home ->
( { model | route = route } ( { model | route = route }
, Cmd.none , randomUserName
) )
Navigation.Poker session -> Navigation.Poker session ->
@ -98,6 +190,10 @@ routeChanged route model =
) )
-- HTML STUFF
view : Model -> Browser.Document Msg view : Model -> Browser.Document Msg
view model = view model =
{ title = "Open-Retro" { title = "Open-Retro"
@ -106,11 +202,24 @@ view model =
[ h1 [ class "app-title" ] [ text "Open-Retro" ] [ h1 [ class "app-title" ] [ text "Open-Retro" ]
, ul [] , ul []
[ li [] [ li []
[ input [ class "board-search-input", placeholder "Session ID..." ] [] ] [ input [ onInput NameChanged, value model.userName ]
[]
]
, li []
[ button
[ onClick RandomizeName ]
[ text "🎲" ]
]
, li [] , li []
[ text "🂿 New retro" ] [ button
[ onClick CreateRetro ]
[ text "🂿 New retro" ]
]
, li [] , li []
[ text "🃠 New poker" ] [ button
[ onClick CreatePoker ]
[ text "🃠 New poker" ]
]
] ]
] ]
, main_ [] (appContent model) , main_ [] (appContent model)

@ -16,6 +16,7 @@ type alias Model =
, route : Navigation.Route , route : Navigation.Route
, httpError : Maybe Http.Error , httpError : Maybe Http.Error
, userUuid : Uuid , userUuid : Uuid
, userName : String
, session : Maybe String , session : Maybe String
} }
@ -38,6 +39,8 @@ initialModel init =
Maybe.Nothing Maybe.Nothing
-- userUuid -- userUuid
init.userUuid init.userUuid
-- userName
""
-- session -- session
Maybe.Nothing Maybe.Nothing

@ -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.Decode exposing (Decoder, andThen, decodeString, fail, field, map2, map3, string, succeed)
import Json.Encode as Enc
import Model exposing (Model)
type Action type Action
= Publish = 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 : String -> Decoder Action
strToAction str = strToAction str =
if str == "Publish" then case Dict.get str stringToAction of
succeed Publish Just val ->
succeed val
else Nothing ->
fail ("invalid action '" ++ str ++ "'") fail ("invalid action '" ++ str ++ "'")
type alias Publisher = type alias Publisher =
@ -33,26 +56,19 @@ type alias WsMessage =
-- DECODERS -- DECODERS
publisherDecoder : Decoder Publisher
publisherDecoder =
map2 Publisher
(field "uuid" string)
(field "name" string)
msgDecoder : Decoder WsMessage msgDecoder : Decoder WsMessage
msgDecoder = msgDecoder =
map3 WsMessage map3 WsMessage
actionDecoder (field "action" string |> andThen strToAction)
(field "publisher" publisherDecoder) (field "publisher"
(map2 Publisher
(field "uuid" string)
(field "name" string)
)
)
(field "payload" string) (field "payload" string)
actionDecoder : Decoder Action
actionDecoder =
field "action" string |> andThen strToAction
decode : String -> Maybe WsMessage decode : String -> Maybe WsMessage
decode str = decode str =
case decodeString msgDecoder str of case decodeString msgDecoder str of
@ -61,3 +77,22 @@ decode str =
Err _ -> Err _ ->
Nothing 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

Loading…
Cancel
Save