From e16089e4d87ffc8f3287801fbc2f04be2808addd Mon Sep 17 00:00:00 2001 From: hadrienmp Date: Sat, 27 Mar 2021 22:18:50 +0100 Subject: [PATCH] Share the sound profile --- src/elm/Main.elm | 6 ++--- src/elm/Shared.elm | 21 ++++++++++----- src/elm/SharedEvents.elm | 18 ++++++++++++- src/elm/Sound/Library.elm | 25 ++++++++++++++++-- src/elm/Sound/Settings.elm | 54 ++++++++++++++++++++++++-------------- src/sass/tabs/_timer.scss | 6 +++-- 6 files changed, 95 insertions(+), 35 deletions(-) diff --git a/src/elm/Main.elm b/src/elm/Main.elm index df1cf55..1baede0 100644 --- a/src/elm/Main.elm +++ b/src/elm/Main.elm @@ -146,7 +146,7 @@ update msg model = ReceivedEvent eventResult -> eventResult - |> Result.map (Shared.applyTo model.shared) + |> Result.map (Shared.evolve model.shared) |> Result.withDefault ( Shared.init, Cmd.none ) |> Tuple.mapFirst (\shared -> { model | shared = shared }) @@ -184,7 +184,7 @@ update msg model = ) Start -> - ( model, Random.generate StartWithAlarm <| Sound.Library.pick model.soundSettings.profile ) + ( model, Random.generate StartWithAlarm <| Sound.Library.pick model.shared.soundProfile ) StartWithAlarm sound -> ( model @@ -351,7 +351,7 @@ view model = |> Html.map GotMobbersSettingsMsg Sound -> - Sound.Settings.view model.soundSettings + Sound.Settings.view model.soundSettings model.shared.soundProfile |> Html.map GotSoundSettingsMsg Share -> diff --git a/src/elm/Shared.elm b/src/elm/Shared.elm index d328585..492f003 100644 --- a/src/elm/Shared.elm +++ b/src/elm/Shared.elm @@ -7,13 +7,15 @@ import Lib.Duration exposing (Duration) import Lib.ListExtras exposing (rotate, uncons) import Mobbers.Model exposing (Mobbers) import SharedEvents +import Sound.Library import Time type alias State = { clock : ClockState - , turnLength: Duration + , turnLength : Duration , mobbers : Mobbers + , soundProfile : Sound.Library.Profile } @@ -22,6 +24,7 @@ init = { clock = Off , turnLength = Lib.Duration.ofMinutes 8 , mobbers = [] + , soundProfile = Sound.Library.ClassicWeird } @@ -35,6 +38,7 @@ timePassed now state = , command ) + evolveMany : State -> List (Result Json.Decode.Error SharedEvents.Event) -> State evolveMany model events = case uncons events of @@ -45,17 +49,17 @@ evolveMany model events = evolveMany model tail ( Just (Ok head), tail ) -> - evolveMany (applyTo model head |> Tuple.first) tail + evolveMany (evolve model head |> Tuple.first) tail -applyTo : State -> SharedEvents.Event -> ( State, Cmd msg ) -applyTo state event = +evolve : State -> SharedEvents.Event -> ( State, Cmd msg ) +evolve state event = case ( event, state.clock ) of ( SharedEvents.Started started, Off ) -> ( { state | clock = On - { end = Time.posixToMillis started.time + (Lib.Duration.toMillis started.length) |> Time.millisToPosix + { end = Time.posixToMillis started.time + Lib.Duration.toMillis started.length |> Time.millisToPosix , length = started.length , ended = False } @@ -83,8 +87,11 @@ applyTo state event = ( SharedEvents.ShuffledMobbers mobbers, _ ) -> ( { state | mobbers = mobbers ++ List.filter (\el -> not <| List.member el mobbers) state.mobbers }, Cmd.none ) - (SharedEvents.TurnLengthChanged turnLength, _) -> + ( SharedEvents.TurnLengthChanged turnLength, _ ) -> ( { state | turnLength = turnLength }, Cmd.none ) + ( SharedEvents.SelectedMusicProfile profile, _ ) -> + ( { state | soundProfile = profile }, Cmd.none ) + _ -> - ( state, Cmd.none ) \ No newline at end of file + ( state, Cmd.none ) diff --git a/src/elm/SharedEvents.elm b/src/elm/SharedEvents.elm index 60218cf..0635e80 100644 --- a/src/elm/SharedEvents.elm +++ b/src/elm/SharedEvents.elm @@ -19,6 +19,7 @@ type Event | RotatedMobbers | ShuffledMobbers Mobbers | TurnLengthChanged Duration + | SelectedMusicProfile Sound.Library.Profile @@ -55,11 +56,20 @@ decoderFromName eventName = Json.Decode.map ShuffledMobbers (Json.Decode.field "mobbers" (Json.Decode.list Mobbers.jsonDecoder)) "TurnLengthChanged" -> - Json.Decode.map TurnLengthChanged (Json.Decode.field "seconds" (Json.Decode.map (Lib.Duration.ofSeconds) Json.Decode.int)) + Json.Decode.int + |> Json.Decode.map (Lib.Duration.ofSeconds) + |> Json.Decode.field "seconds" + |> Json.Decode.map TurnLengthChanged "RotatedMobbers" -> Json.Decode.succeed RotatedMobbers + "SelectedMusicProfile" -> + Json.Decode.string + |> Json.Decode.map (Sound.Library.profileFromString) + |> Json.Decode.field "profile" + |> Json.Decode.map SelectedMusicProfile + _ -> Json.Decode.fail <| "I don't know this event " ++ eventName @@ -118,3 +128,9 @@ toJson event = [ ( "name", Json.Encode.string "TurnLengthChanged" ) , ( "seconds", Json.Encode.int <| Lib.Duration.toSeconds duration ) ] + + SelectedMusicProfile profile -> + [ ( "name", Json.Encode.string "SelectedMusicProfile" ) + , ( "profile", Json.Encode.string <| Sound.Library.profileToString profile ) + ] + diff --git a/src/elm/Sound/Library.elm b/src/elm/Sound/Library.elm index 10655c2..76fdd75 100644 --- a/src/elm/Sound/Library.elm +++ b/src/elm/Sound/Library.elm @@ -1,4 +1,4 @@ -module Sound.Library exposing (Profile(..), Sound, default, pick) +module Sound.Library exposing (Profile(..), Sound, default, pick, profileFromString, profileToString) import Random @@ -12,6 +12,27 @@ type Profile | Riot +profileToString : Profile -> String +profileToString profile = + case profile of + ClassicWeird -> + "ClassicWeird" + + Riot -> + "Riot" + + +profileFromString : String -> Profile +profileFromString string = + case string of + "Riot" -> + Riot + _ -> + ClassicWeird + + + + default : Sound default = "classic-weird/celebration.mp3" @@ -20,7 +41,7 @@ default = pick : Profile -> Random.Generator Sound pick profile = soundsOf profile - |> (\(d, list) -> Random.uniform d list) + |> (\( d, list ) -> Random.uniform d list) soundsOf : Profile -> ( Sound, List Sound ) diff --git a/src/elm/Sound/Settings.elm b/src/elm/Sound/Settings.elm index f9a0acc..b5b408c 100644 --- a/src/elm/Sound/Settings.elm +++ b/src/elm/Sound/Settings.elm @@ -1,32 +1,35 @@ module Sound.Settings exposing (..) -import Html exposing (Html, button, div, i, img, input, label, p, text) +import Html exposing (Html, button, div, img, input, label, p, text) import Html.Attributes exposing (alt, class, classList, for, id, src, step, type_, value) import Html.Events exposing (onClick, onInput) import Js.Commands import Json.Encode import Lib.Icons.Ion +import SharedEvents import Sound.Library as SoundLibrary -type alias CommandPort = (Json.Encode.Value -> Cmd Msg) -type alias StorePort = (Json.Encode.Value -> Cmd Msg) + +type alias CommandPort = + Json.Encode.Value -> Cmd Msg + + +type alias StorePort = + Json.Encode.Value -> Cmd Msg + type alias Model = - { profile : SoundLibrary.Profile - , volume : Int - } + { volume : Int } init : Int -> Model init volume = - { profile = SoundLibrary.ClassicWeird - , volume = volume - } + { volume = volume } type Msg = VolumeChanged String - | SelectedSoundProfile SoundLibrary.Profile + | ShareEvent SharedEvents.Event update : Msg -> Model -> ( Model, Cmd Msg ) @@ -34,20 +37,23 @@ update msg model = case msg of VolumeChanged rawVolume -> let - volume = String.toInt rawVolume|> Maybe.withDefault model.volume + volume = + String.toInt rawVolume |> Maybe.withDefault model.volume in ( { model | volume = volume } , Js.Commands.send <| Js.Commands.ChangeVolume volume ) - SelectedSoundProfile profile -> - ( { model | profile = profile } - , Cmd.none + ShareEvent event -> + ( model + , event + |> SharedEvents.toJson + |> SharedEvents.sendEvent ) -view : Model -> Html Msg -view model = +view : Model -> SoundLibrary.Profile -> Html Msg +view model profile = div [ id "sound", class "tab" ] [ div [ id "volume-field", class "form-field" ] @@ -69,15 +75,23 @@ view model = , div [ id "sound-cards" ] [ button - [ classList [ ( "active", model.profile == SoundLibrary.ClassicWeird ) ] - , onClick <| SelectedSoundProfile SoundLibrary.ClassicWeird + [ classList [ ( "active", profile == SoundLibrary.ClassicWeird ) ] + , onClick + (SoundLibrary.ClassicWeird + |> SharedEvents.SelectedMusicProfile + |> ShareEvent + ) ] [ img [ src "/images/weird.jpeg", alt "Man wearing a watermelon as a hat" ] [] , p [] [ text "Classic Weird" ] ] , button - [ classList [ ( "active", model.profile == SoundLibrary.Riot ) ] - , onClick <| SelectedSoundProfile SoundLibrary.Riot + [ classList [ ( "active", profile == SoundLibrary.Riot ) ] + , onClick + (SoundLibrary.Riot + |> SharedEvents.SelectedMusicProfile + |> ShareEvent + ) ] [ img [ src "/images/commune.jpg", alt "Comic book drawing of the paris commune revolution" ] [] , p [] [ text "Revolution" ] diff --git a/src/sass/tabs/_timer.scss b/src/sass/tabs/_timer.scss index 92def9f..88b3c01 100644 --- a/src/sass/tabs/_timer.scss +++ b/src/sass/tabs/_timer.scss @@ -37,10 +37,8 @@ input[type="range"] { button { flex: 1 0; width: 130px; - background: none; text-align: center; padding: 0; - color: black; vertical-align: top; display: block; margin-left: $margin-space; @@ -49,6 +47,10 @@ input[type="range"] { margin-left: 0; } + p { + padding: $padding-space; + } + img { width: 100%; opacity: .6;