diff options
author | Michael Wittig <michael.wittig@fu-berlin.de> | 2010-10-14 19:14:46 +0200 |
---|---|---|
committer | Michael Wittig <michael.wittig@fu-berlin.de> | 2010-10-14 19:14:46 +0200 |
commit | 0a56de20bf1b461df480cf9f1319488c839bc6a4 (patch) | |
tree | 5e68c79d3632aba84ffdfdfa0b2a2f40aa168d15 | |
parent | f2164544210f511664ed7186107789ec55ec3edf (diff) | |
download | erlang-0a56de20bf1b461df480cf9f1319488c839bc6a4.tar.gz erlang-0a56de20bf1b461df480cf9f1319488c839bc6a4.tar.xz erlang-0a56de20bf1b461df480cf9f1319488c839bc6a4.zip |
give_votes needs to be fixed, still
-rw-r--r-- | server/cldb.erl | 32 | ||||
-rw-r--r-- | server/client.erl | 5 | ||||
-rw-r--r-- | 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. |