Skip to content

Commit e16089e

Browse files
committed
Share the sound profile
1 parent 8d1d5d9 commit e16089e

File tree

6 files changed

+95
-35
lines changed

6 files changed

+95
-35
lines changed

src/elm/Main.elm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ update msg model =
146146

147147
ReceivedEvent eventResult ->
148148
eventResult
149-
|> Result.map (Shared.applyTo model.shared)
149+
|> Result.map (Shared.evolve model.shared)
150150
|> Result.withDefault ( Shared.init, Cmd.none )
151151
|> Tuple.mapFirst (\shared -> { model | shared = shared })
152152

@@ -184,7 +184,7 @@ update msg model =
184184
)
185185

186186
Start ->
187-
( model, Random.generate StartWithAlarm <| Sound.Library.pick model.soundSettings.profile )
187+
( model, Random.generate StartWithAlarm <| Sound.Library.pick model.shared.soundProfile )
188188

189189
StartWithAlarm sound ->
190190
( model
@@ -351,7 +351,7 @@ view model =
351351
|> Html.map GotMobbersSettingsMsg
352352

353353
Sound ->
354-
Sound.Settings.view model.soundSettings
354+
Sound.Settings.view model.soundSettings model.shared.soundProfile
355355
|> Html.map GotSoundSettingsMsg
356356

357357
Share ->

src/elm/Shared.elm

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import Lib.Duration exposing (Duration)
77
import Lib.ListExtras exposing (rotate, uncons)
88
import Mobbers.Model exposing (Mobbers)
99
import SharedEvents
10+
import Sound.Library
1011
import Time
1112

1213

1314
type alias State =
1415
{ clock : ClockState
15-
, turnLength: Duration
16+
, turnLength : Duration
1617
, mobbers : Mobbers
18+
, soundProfile : Sound.Library.Profile
1719
}
1820

1921

@@ -22,6 +24,7 @@ init =
2224
{ clock = Off
2325
, turnLength = Lib.Duration.ofMinutes 8
2426
, mobbers = []
27+
, soundProfile = Sound.Library.ClassicWeird
2528
}
2629

2730

@@ -35,6 +38,7 @@ timePassed now state =
3538
, command
3639
)
3740

41+
3842
evolveMany : State -> List (Result Json.Decode.Error SharedEvents.Event) -> State
3943
evolveMany model events =
4044
case uncons events of
@@ -45,17 +49,17 @@ evolveMany model events =
4549
evolveMany model tail
4650

4751
( Just (Ok head), tail ) ->
48-
evolveMany (applyTo model head |> Tuple.first) tail
52+
evolveMany (evolve model head |> Tuple.first) tail
4953

5054

51-
applyTo : State -> SharedEvents.Event -> ( State, Cmd msg )
52-
applyTo state event =
55+
evolve : State -> SharedEvents.Event -> ( State, Cmd msg )
56+
evolve state event =
5357
case ( event, state.clock ) of
5458
( SharedEvents.Started started, Off ) ->
5559
( { state
5660
| clock =
5761
On
58-
{ end = Time.posixToMillis started.time + (Lib.Duration.toMillis started.length) |> Time.millisToPosix
62+
{ end = Time.posixToMillis started.time + Lib.Duration.toMillis started.length |> Time.millisToPosix
5963
, length = started.length
6064
, ended = False
6165
}
@@ -83,8 +87,11 @@ applyTo state event =
8387
( SharedEvents.ShuffledMobbers mobbers, _ ) ->
8488
( { state | mobbers = mobbers ++ List.filter (\el -> not <| List.member el mobbers) state.mobbers }, Cmd.none )
8589

86-
(SharedEvents.TurnLengthChanged turnLength, _) ->
90+
( SharedEvents.TurnLengthChanged turnLength, _ ) ->
8791
( { state | turnLength = turnLength }, Cmd.none )
8892

93+
( SharedEvents.SelectedMusicProfile profile, _ ) ->
94+
( { state | soundProfile = profile }, Cmd.none )
95+
8996
_ ->
90-
( state, Cmd.none )
97+
( state, Cmd.none )

src/elm/SharedEvents.elm

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Event
1919
| RotatedMobbers
2020
| ShuffledMobbers Mobbers
2121
| TurnLengthChanged Duration
22+
| SelectedMusicProfile Sound.Library.Profile
2223

2324

2425

@@ -55,11 +56,20 @@ decoderFromName eventName =
5556
Json.Decode.map ShuffledMobbers (Json.Decode.field "mobbers" (Json.Decode.list Mobbers.jsonDecoder))
5657

5758
"TurnLengthChanged" ->
58-
Json.Decode.map TurnLengthChanged (Json.Decode.field "seconds" (Json.Decode.map (Lib.Duration.ofSeconds) Json.Decode.int))
59+
Json.Decode.int
60+
|> Json.Decode.map (Lib.Duration.ofSeconds)
61+
|> Json.Decode.field "seconds"
62+
|> Json.Decode.map TurnLengthChanged
5963

6064
"RotatedMobbers" ->
6165
Json.Decode.succeed RotatedMobbers
6266

67+
"SelectedMusicProfile" ->
68+
Json.Decode.string
69+
|> Json.Decode.map (Sound.Library.profileFromString)
70+
|> Json.Decode.field "profile"
71+
|> Json.Decode.map SelectedMusicProfile
72+
6373
_ ->
6474
Json.Decode.fail <| "I don't know this event " ++ eventName
6575

@@ -118,3 +128,9 @@ toJson event =
118128
[ ( "name", Json.Encode.string "TurnLengthChanged" )
119129
, ( "seconds", Json.Encode.int <| Lib.Duration.toSeconds duration )
120130
]
131+
132+
SelectedMusicProfile profile ->
133+
[ ( "name", Json.Encode.string "SelectedMusicProfile" )
134+
, ( "profile", Json.Encode.string <| Sound.Library.profileToString profile )
135+
]
136+

src/elm/Sound/Library.elm

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Sound.Library exposing (Profile(..), Sound, default, pick)
1+
module Sound.Library exposing (Profile(..), Sound, default, pick, profileFromString, profileToString)
22

33
import Random
44

@@ -12,6 +12,27 @@ type Profile
1212
| Riot
1313

1414

15+
profileToString : Profile -> String
16+
profileToString profile =
17+
case profile of
18+
ClassicWeird ->
19+
"ClassicWeird"
20+
21+
Riot ->
22+
"Riot"
23+
24+
25+
profileFromString : String -> Profile
26+
profileFromString string =
27+
case string of
28+
"Riot" ->
29+
Riot
30+
_ ->
31+
ClassicWeird
32+
33+
34+
35+
1536
default : Sound
1637
default =
1738
"classic-weird/celebration.mp3"
@@ -20,7 +41,7 @@ default =
2041
pick : Profile -> Random.Generator Sound
2142
pick profile =
2243
soundsOf profile
23-
|> (\(d, list) -> Random.uniform d list)
44+
|> (\( d, list ) -> Random.uniform d list)
2445

2546

2647
soundsOf : Profile -> ( Sound, List Sound )

src/elm/Sound/Settings.elm

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,59 @@
11
module Sound.Settings exposing (..)
22

3-
import Html exposing (Html, button, div, i, img, input, label, p, text)
3+
import Html exposing (Html, button, div, img, input, label, p, text)
44
import Html.Attributes exposing (alt, class, classList, for, id, src, step, type_, value)
55
import Html.Events exposing (onClick, onInput)
66
import Js.Commands
77
import Json.Encode
88
import Lib.Icons.Ion
9+
import SharedEvents
910
import Sound.Library as SoundLibrary
1011

11-
type alias CommandPort = (Json.Encode.Value -> Cmd Msg)
12-
type alias StorePort = (Json.Encode.Value -> Cmd Msg)
12+
13+
type alias CommandPort =
14+
Json.Encode.Value -> Cmd Msg
15+
16+
17+
type alias StorePort =
18+
Json.Encode.Value -> Cmd Msg
19+
1320

1421
type alias Model =
15-
{ profile : SoundLibrary.Profile
16-
, volume : Int
17-
}
22+
{ volume : Int }
1823

1924

2025
init : Int -> Model
2126
init volume =
22-
{ profile = SoundLibrary.ClassicWeird
23-
, volume = volume
24-
}
27+
{ volume = volume }
2528

2629

2730
type Msg
2831
= VolumeChanged String
29-
| SelectedSoundProfile SoundLibrary.Profile
32+
| ShareEvent SharedEvents.Event
3033

3134

3235
update : Msg -> Model -> ( Model, Cmd Msg )
3336
update msg model =
3437
case msg of
3538
VolumeChanged rawVolume ->
3639
let
37-
volume = String.toInt rawVolume|> Maybe.withDefault model.volume
40+
volume =
41+
String.toInt rawVolume |> Maybe.withDefault model.volume
3842
in
3943
( { model | volume = volume }
4044
, Js.Commands.send <| Js.Commands.ChangeVolume volume
4145
)
4246

43-
SelectedSoundProfile profile ->
44-
( { model | profile = profile }
45-
, Cmd.none
47+
ShareEvent event ->
48+
( model
49+
, event
50+
|> SharedEvents.toJson
51+
|> SharedEvents.sendEvent
4652
)
4753

4854

49-
view : Model -> Html Msg
50-
view model =
55+
view : Model -> SoundLibrary.Profile -> Html Msg
56+
view model profile =
5157
div [ id "sound", class "tab" ]
5258
[ div
5359
[ id "volume-field", class "form-field" ]
@@ -69,15 +75,23 @@ view model =
6975
, div
7076
[ id "sound-cards" ]
7177
[ button
72-
[ classList [ ( "active", model.profile == SoundLibrary.ClassicWeird ) ]
73-
, onClick <| SelectedSoundProfile SoundLibrary.ClassicWeird
78+
[ classList [ ( "active", profile == SoundLibrary.ClassicWeird ) ]
79+
, onClick
80+
(SoundLibrary.ClassicWeird
81+
|> SharedEvents.SelectedMusicProfile
82+
|> ShareEvent
83+
)
7484
]
7585
[ img [ src "/images/weird.jpeg", alt "Man wearing a watermelon as a hat" ] []
7686
, p [] [ text "Classic Weird" ]
7787
]
7888
, button
79-
[ classList [ ( "active", model.profile == SoundLibrary.Riot ) ]
80-
, onClick <| SelectedSoundProfile SoundLibrary.Riot
89+
[ classList [ ( "active", profile == SoundLibrary.Riot ) ]
90+
, onClick
91+
(SoundLibrary.Riot
92+
|> SharedEvents.SelectedMusicProfile
93+
|> ShareEvent
94+
)
8195
]
8296
[ img [ src "/images/commune.jpg", alt "Comic book drawing of the paris commune revolution" ] []
8397
, p [] [ text "Revolution" ]

src/sass/tabs/_timer.scss

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ input[type="range"] {
3737
button {
3838
flex: 1 0;
3939
width: 130px;
40-
background: none;
4140
text-align: center;
4241
padding: 0;
43-
color: black;
4442
vertical-align: top;
4543
display: block;
4644
margin-left: $margin-space;
@@ -49,6 +47,10 @@ input[type="range"] {
4947
margin-left: 0;
5048
}
5149

50+
p {
51+
padding: $padding-space;
52+
}
53+
5254
img {
5355
width: 100%;
5456
opacity: .6;

0 commit comments

Comments
 (0)