diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/media.erl | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/server/media.erl b/server/media.erl index 21c94a0..851cc21 100644 --- a/server/media.erl +++ b/server/media.erl @@ -1,5 +1,5 @@ -module(media). --export([init/0,insert/3, ask/2, all/0, play/3]). +-export([init/0,insert/3, ask/2, all/0, play/3, vote/2, devote/2]). -define(TESTPATTERN, "../ac/*.mp3"). % Since we are not willing to calculate and deliver all the id3 tags everytime they are requested, @@ -24,7 +24,7 @@ player(path) -> init() -> mnesia:create_schema([node()]), mnesia:start(), - mnesia:create_table(track, [{attributes, record_info(fields, track)}]), + mnesia:create_table(track, [], {index, [y]}, {type, bag}, [{attributes, record_info(fields, track)}]), read_files(filelib:wildcard(?TESTPATTERN),0,0), io:format("Initialisation of mnesia successful.\n"). @@ -64,11 +64,36 @@ all() -> {atomic, Results} = mnesia:transaction(F), Results. -% We want to play mp3s from our database. +% We want to play mp3s from our database. After we play them they will become locked. +% In practice we are going to set their locked variable to true and spawn a process which will unlock them after a certain time. +% Well this could be considered abuse. play(Artist, Title, Callback) -> [Head|_] = ask(Artist, Title), {_, _, _, _, _, Fp} = Head, Port = erlang:open_port({spawn_executable, "/usr/bin/mplayer"}, [{args, [Fp]}]). -% We want to execute commands locally +% Of course we need a query to find out whats actually the most wished for track. +% We will do it by requesting all the records from the database and then iteramte over just taking a look at the vote +% variable, so it is like list of integers. In case no tracks were voted for we just take the first track we find and play it. Of course it is locked afterwards so another will be choosen. + +vote(Artist, Title) -> + F = fun() -> + [Head|_] = ask(Artist, Title), + Votes = Head#track.votes + 1, + New = Head#track{votes = Votes}, + mnesia:write(New) + end, + mnesia:transaction(F). + +devote(Artist, Title) -> + F = fun() -> + [Head|_] = ask(Artist, Title), + Votes = Head#track.votes - 1, + New = Head#track{votes = Votes}, + mnesia:write(New) + end, + mnesia:transaction(F). + +top() -> + All = all(). |