random name generator and stuff

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

@ -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;

@ -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": {

@ -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)

@ -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

@ -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

Loading…
Cancel
Save