Skip to content

Commit c13b08d

Browse files
committed
implement timer, seeded playlists
1 parent 618c076 commit c13b08d

File tree

4 files changed

+51
-37
lines changed

4 files changed

+51
-37
lines changed

lib/pling/playlists/music_library.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Pling.Playlists.MusicLibrary do
99
alias Pling.Playlists.{Playlist, Track}
1010

1111
@notification_key :first_track_notified
12+
@default_playlist_id "4DIYG1WrBI9jRJiul9vmxj"
1213

1314
@doc """
1415
Loads all playlists from the database and returns them as a map with spotify_id keys.
@@ -177,4 +178,18 @@ defmodule Pling.Playlists.MusicLibrary do
177178

178179
defp get_first_image_url([%{"url" => url} | _]), do: url
179180
defp get_first_image_url(_), do: nil
181+
182+
@doc """
183+
Loads the default playlist along with its tracks.
184+
"""
185+
def load_default_playlist do
186+
case get_or_fetch_playlist(@default_playlist_id) do
187+
{:ok, _, playlist} ->
188+
{:ok, playlist}
189+
190+
{:error, reason} ->
191+
Logger.error("Failed to load default playlist: #{inspect(reason)}")
192+
{:error, reason}
193+
end
194+
end
180195
end

lib/pling/rooms/room/impl.ex

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,18 @@ defmodule Pling.Rooms.Room.Impl do
3131

3232
def play(state) do
3333
state
34+
|> Map.put(:timer_ref, cancel_timer(state))
3435
|> Map.put(:playing?, true)
35-
|> Map.put(:countdown, state.spotify_track_duration)
36+
|> Map.put(:countdown, 30)
3637
|> schedule_next_tick()
3738
end
3839

3940
def pause(state) do
4041
state
4142
|> Map.put(:timer_ref, cancel_timer(state))
42-
|> reset_playback()
43+
|> Map.put(:countdown, 7)
44+
|> Map.put(:playing?, false)
45+
|> schedule_next_tick()
4346
end
4447

4548
def update_track(state) do
@@ -64,10 +67,12 @@ defmodule Pling.Rooms.Room.Impl do
6467
track: new_track,
6568
queue: new_queue
6669
}}
70+
|> Map.put(:countdown, 30)
6771
end
6872

6973
def set_playlist(state, playlist_id) do
7074
playlists = MusicLibrary.load_playlists()
75+
playlist = Map.get(playlists, playlist_id)
7176

7277
tracks =
7378
playlist_id
@@ -79,7 +84,7 @@ defmodule Pling.Rooms.Room.Impl do
7984
state
8085
|> Map.put(:playlists, playlists)
8186
|> Map.put(:selection, %{
82-
playlist: playlist_id,
87+
playlist: playlist,
8388
track: initial_track,
8489
queue: initial_queue
8590
})
@@ -89,7 +94,7 @@ defmodule Pling.Rooms.Room.Impl do
8994
def process_tick(state) do
9095
new_state = tick(state)
9196

92-
if new_state.playing? do
97+
if new_state.countdown && new_state.countdown > 0 do
9398
schedule_next_tick(new_state)
9499
else
95100
%{new_state | timer_ref: nil}
@@ -124,21 +129,23 @@ defmodule Pling.Rooms.Room.Impl do
124129

125130
defp tick(state) do
126131
case state do
127-
%{playing?: false} -> state
128132
%{countdown: nil} -> state
129133
%{countdown: 0} -> handle_timeout(state)
130134
%{countdown: count} -> update_countdown(state, count - 1)
131135
end
132136
end
133137

134138
defp handle_timeout(state) do
135-
new_state =
136-
state
137-
|> update_track()
138-
|> Map.put(:countdown, state.spotify_track_duration)
139-
|> Map.put(:playing?, false)
139+
if state.playing? do
140+
new_state =
141+
state
142+
|> update_track()
143+
|> Map.put(:countdown, 30)
140144

141-
new_state
145+
schedule_next_tick(new_state)
146+
else
147+
%{state | countdown: nil}
148+
end
142149
end
143150

144151
defp update_countdown(state, new_count) do

lib/pling/rooms/room_state.ex

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,32 @@ defmodule Pling.Rooms.RoomState do
44
"""
55

66
@default_track_duration 30
7-
@default_playlist_id "6ZSeHvrhmEH4erjxudpULB"
7+
@default_playlist_id "4DIYG1WrBI9jRJiul9vmxj"
88

99
def initialize(room_code, game_mode \\ "vs", leader_id \\ nil) do
1010
if leader_id == nil do
1111
raise "leader_id must be provided when initializing a room"
1212
end
1313

14+
# Load default playlist
15+
{:ok, default_playlist} = Pling.Playlists.MusicLibrary.load_default_playlist()
16+
1417
%{
1518
scores: %{},
1619
playing?: false,
1720
countdown: nil,
1821
timer_ref: nil,
1922
timer_threshold: 10,
2023
spotify_track_duration: @default_track_duration,
21-
playlists: %{},
24+
playlists: %{@default_playlist_id => default_playlist},
2225
room_code: room_code,
2326
game_mode: game_mode,
2427
leader_id: leader_id,
2528
recent_plings: [],
2629
selection: %{
27-
playlist: @default_playlist_id,
28-
queue: []
30+
playlist: default_playlist,
31+
track: nil,
32+
queue: Pling.Playlists.MusicLibrary.get_tracks(@default_playlist_id, %{})
2933
}
3034
}
3135
end
@@ -47,7 +51,7 @@ defmodule Pling.Rooms.RoomState do
4751
}
4852
end
4953

50-
defp format_playlist(playlist), do: Map.take(playlist, [:spotify_id, :name])
54+
defp format_playlist(playlist), do: Map.take(playlist, [:spotify_id, :name, :official, :image_url])
5155

5256
defp format_playlists(playlists) when is_map(playlists) do
5357
playlists

lib/pling_web/live/room_live.ex

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,12 @@ defmodule PlingWeb.RoomLive do
188188
"""
189189
end
190190

191-
defp playlist_info(%{selection: selection, playlists: playlists} = assigns)
192-
when not is_nil(selection) and not is_nil(playlists) do
191+
defp playlist_info(%{selection: selection} = assigns) do
193192
playlist_name =
194-
cond do
195-
# Get playlist name directly from the playlists map using playlist ID
196-
selection.playlist && Map.has_key?(playlists, selection.playlist) ->
197-
Map.get(playlists, selection.playlist).name
198-
199-
# Get playlist name from track's playlist_spotify_id
200-
selection.track && Map.has_key?(playlists, selection.track.playlist_spotify_id) ->
201-
Map.get(playlists, selection.track.playlist_spotify_id).name
202-
203-
true ->
204-
# Track exists but playlist not found, nothing.
205-
nil
206-
gettext("Unknown Playlist")
193+
if selection.playlist do
194+
selection.playlist.name || gettext("Unknown Playlist")
195+
else
196+
nil
207197
end
208198

209199
assigns = assign(assigns, :playlist_name, playlist_name)
@@ -218,8 +208,6 @@ defmodule PlingWeb.RoomLive do
218208
"""
219209
end
220210

221-
defp playlist_info(assigns), do: ~H""
222-
223211
defp user_list(%{users: []} = assigns) do
224212
~H"""
225213
{gettext("Waiting for users...")}
@@ -237,8 +225,8 @@ defmodule PlingWeb.RoomLive do
237225
<span class={["text-slate-900", @is_current_user? && "font-bold"]}>
238226
{@single_user.user_id}
239227
</span>
240-
{gettext("is in the")}
241-
<span class="text-slate-900">{@room_code}</span>{gettext("room")}
228+
{gettext("is in")}
229+
<span class="text-slate-900">{@room_code}</span>
242230
"""
243231
end
244232

@@ -266,7 +254,7 @@ defmodule PlingWeb.RoomLive do
266254
</span>
267255
{if index < length(@other_users_with_current) - 1, do: ", "}
268256
<% end %>
269-
{gettext("in the")}
257+
{gettext("is in")}
270258
<span class="text-slate-900">{@room_code}</span>{gettext("room")}
271259
"""
272260
end
@@ -452,7 +440,7 @@ defmodule PlingWeb.RoomLive do
452440
active? = assigns[:active?]
453441

454442
active_class =
455-
if active?, do: "bg-indigo-900 text-indigo-50", else: "bg-indigo-50 text-indigo-900"
443+
if active?, do: "bg-brand text-red-50", else: "bg-red-50 text-red-900"
456444

457445
assigns = assign(assigns, :active_class, active_class)
458446

0 commit comments

Comments
 (0)