From 0a56de20bf1b461df480cf9f1319488c839bc6a4 Mon Sep 17 00:00:00 2001 From: Michael Wittig Date: Thu, 14 Oct 2010 19:14:46 +0200 Subject: give_votes needs to be fixed, still --- server/cldb.erl | 32 +++++++++++++++++++++++--------- server/client.erl | 5 ++++- server/media.erl | 4 ++-- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/server/cldb.erl b/server/cldb.erl index d514ff9..f6c7821 100644 --- a/server/cldb.erl +++ b/server/cldb.erl @@ -1,7 +1,7 @@ % The clientdatabase -module(cldb). --export([init/0, ask/2, login/2, decVote/1, incVote/1, register/3, check_rights/1]). +-export([init/0,all/0, ask/2, login/2, decVote/1, incVote/1, register/3, check_rights/1, give_votes/1]). -record(user, {name, passwd, votes, rights}). init() -> @@ -12,10 +12,18 @@ init() -> ask(User, Pwd) -> F = fun() -> - mnesia:match_object({user, User, Pwd, '_', '_'}) + mnesia:match_object({user, User, Pwd, '_', '_'}) end, mnesia:transaction(F). +all() -> + F = fun() -> + mnesia:match_object({user, '_', '_', '_', '_'}) + end, + {atomic, List} = mnesia:transaction(F), + List. + + login(User, Pwd) -> case ask(User, Pwd) of {atomic, [UserRow|_]} -> {ok, UserRow}; @@ -38,14 +46,20 @@ register(User, Pwd, Root) -> {error, duplicated_user} end. +decVote(User) when is_list(User) -> + {atomic, [Head|_]} = ask(User, '_'), + if Head#user.votes > 0 -> + F = fun() -> + Votes = Head#user.votes - 1, + New = Head#user{votes = Votes}, + mnesia:write(New) + end, + mnesia:transaction(F), + {ok}; + true -> {error} + end; decVote(User) -> - F = fun() -> - [Head|_] = ask(User, '_'), - Votes = Head#user.votes - 1, - New = Head#user{votes = Votes}, - mnesia:write(New) - end, - mnesia:transaction(F). + decVote(User#user.name). incVote(User) -> F = fun() -> diff --git a/server/client.erl b/server/client.erl index d12c891..7cee336 100644 --- a/server/client.erl +++ b/server/client.erl @@ -42,7 +42,10 @@ loop(Client, User) -> execute(Client, fun() -> media:getVotes(Client) end); {vote, Artist, Title} -> - execute(Client, fun() -> media:vote(Artist, Title) end); + case cldb:decVote(User) of + {ok} -> execute(Client, fun() -> media:vote(Artist, Title) end); + _ -> Client ! {error, no_votes_available} + end; {devote, Artist, Title} -> execute(Client, fun() -> media:devote(Artist, Title) end); diff --git a/server/media.erl b/server/media.erl index 80a3fea..5148edc 100644 --- a/server/media.erl +++ b/server/media.erl @@ -112,9 +112,9 @@ play(Artist, Title) -> io:format("playing: ~s, Artist: ~s~n", [Title, Artist]), receive - {Port, {exit_status, 0}} -> cldb:give_votes(cldb:ask("_","_")), start_playing(); + {Port, {exit_status, 0}} -> cldb:give_votes(cldb:all()), start_playing(); {Port, {exit_status, S}} -> throw({commandfailed, S}) - end. +end. % Of course we need a query to find out what the highest voted track is. -- cgit v1.2.3