Skip to content

Commit

Permalink
Share the sound profile
Browse files Browse the repository at this point in the history
  • Loading branch information
HadrienMP committed Mar 27, 2021
1 parent 8d1d5d9 commit e16089e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/elm/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down
21 changes: 14 additions & 7 deletions src/elm/Shared.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand All @@ -22,6 +24,7 @@ init =
{ clock = Off
, turnLength = Lib.Duration.ofMinutes 8
, mobbers = []
, soundProfile = Sound.Library.ClassicWeird
}


Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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 )
( state, Cmd.none )
18 changes: 17 additions & 1 deletion src/elm/SharedEvents.elm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Event
| RotatedMobbers
| ShuffledMobbers Mobbers
| TurnLengthChanged Duration
| SelectedMusicProfile Sound.Library.Profile



Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 )
]

25 changes: 23 additions & 2 deletions src/elm/Sound/Library.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Sound.Library exposing (Profile(..), Sound, default, pick)
module Sound.Library exposing (Profile(..), Sound, default, pick, profileFromString, profileToString)

import Random

Expand All @@ -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"
Expand All @@ -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 )
Expand Down
54 changes: 34 additions & 20 deletions src/elm/Sound/Settings.elm
Original file line number Diff line number Diff line change
@@ -1,53 +1,59 @@
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 )
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" ]
Expand All @@ -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" ]
Expand Down
6 changes: 4 additions & 2 deletions src/sass/tabs/_timer.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,6 +47,10 @@ input[type="range"] {
margin-left: 0;
}

p {
padding: $padding-space;
}

img {
width: 100%;
opacity: .6;
Expand Down

0 comments on commit e16089e

Please sign in to comment.