aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorMichael Wittig <michael.wittig@fu-berlin.de>2010-10-14 14:35:37 +0200
committerMichael Wittig <michael.wittig@fu-berlin.de>2010-10-14 14:35:37 +0200
commit1f3f1443259937eb08de6c001791b28514ba67d9 (patch)
treea7acee510c81de20e8d8f5b53f4246e1e0dd1aa8 /server
parent10419216d9c2c3d71997229d605df9321a94c113 (diff)
downloaderlang-1f3f1443259937eb08de6c001791b28514ba67d9.tar.gz
erlang-1f3f1443259937eb08de6c001791b28514ba67d9.tar.xz
erlang-1f3f1443259937eb08de6c001791b28514ba67d9.zip
search_best
Diffstat (limited to 'server')
-rw-r--r--server/media.erl27
1 files changed, 18 insertions, 9 deletions
diff --git a/server/media.erl b/server/media.erl
index fb1d951..8921724 100644
--- a/server/media.erl
+++ b/server/media.erl
@@ -19,7 +19,7 @@
init() ->
mnesia:create_schema([node()]),
mnesia:start(),
- mnesia:create_table(track, [{index, [y]}, {attributes, record_info(fields, track)}]),
+ mnesia:create_table(track, [{attributes, record_info(fields, track)}]),
read_files(filelib:wildcard(?TESTPATTERN),0,0),
io:format("Initialisation of mnesia successful.\n"),
start_playing().
@@ -52,13 +52,14 @@ insert(Artist, Title, Filepath) ->
% search the track with the highest votes and return {Artist, Title}
-search_best(_,_,_) -> {"Allison Crowe", "Northern Lights"}.
-%search_best([Head|Rest], Max_Votes, Track) ->
-% if (Max_Votes < Head#track.votes) and (Head#track.locked == 0) ->
-% search_best(Rest, Head#track.votes, Head);
-% true -> search_best(Rest, Max_Votes, Track)
-% end;
-%search_best([], _, Track) -> {Track#track.Artist, Track#track.Title}.
+%search_best(_,_,_) -> {"Allison Crowe", "Northern Lights"}.
+search_best([Head|Rest], Max_Votes, Track) ->
+ if
+ ((Max_Votes =< Head#track.votes) and (Head#track.locked == false)) ->
+ search_best(Rest, Head#track.votes, Head);
+ true -> search_best(Rest, Max_Votes, Track)
+ end;
+search_best([], _, Track) -> {Track#track.artist, Track#track.title}.
% We want to query in order to simplify the next calls.
@@ -86,7 +87,15 @@ play(Artist, Title, Callback) ->
[Head|_] = ask(Artist, Title),
{_, Title, Artist, _, _, Fp} = Head,
Port = erlang:open_port({spawn_executable, "/usr/bin/mplayer"}, [{args, [Fp]}, exit_status]),
- io:format("playing: ~w, Artist: ~w~n", [Title, Artist]),
+
+ % lock the song
+ F = fun() ->
+ New = Head#track{locked = 1},
+ mnesia:write(New)
+ end,
+ mnesia:transaction(F),
+
+ io:format("playing: ~s, Artist: ~s~n", [Title, Artist]),
receive
{Port, {exit_status, 0}} -> start_playing();
{Port, {exit_status, S}} -> throw({commandfailed, S})