From da9da98db8f80ce58383b1f9976da3ae5e813c5b Mon Sep 17 00:00:00 2001 From: Marco Ziener Date: Thu, 14 Oct 2010 10:43:15 +0200 Subject: Something --- server/media.erl | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'server/media.erl') diff --git a/server/media.erl b/server/media.erl index 3897a6a..1c41c89 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]). % Since we are not willing to calculate and deliver all the id3 tags everytime they are requested, % we try to get something persistent with mnesia. @@ -23,7 +23,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)}]), io:format("Initialisation of mnesia successful.\n"). % Basic insertion of entrys into the database. Some entries are left out because they are 0 or false. @@ -46,17 +46,41 @@ ask(Artist, Title) -> all() -> F = fun() -> - mnesia:match({track, '_','_','_','_','_'}) + mnesia:match_object({track, '_','_','_','_','_'}) end, {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, Cmd}, [exit_status]). 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(). -- cgit v1.2.3