diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2010-10-15 10:51:15 +0200 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2010-10-15 10:51:15 +0200 |
commit | 32abe1a28b74263e12afd4475fd7896d9c4e6a9d (patch) | |
tree | 596d84005dbaa7df801f477b58bb39b16f0b8e5b /server/cldb.erl | |
parent | 8ce78ba045474b606267e351e945ef6be1b969c0 (diff) | |
parent | ebbf2038b2e131cc43eae1d4c98fbd6efb548f0a (diff) | |
download | erlang-32abe1a28b74263e12afd4475fd7896d9c4e6a9d.tar.gz erlang-32abe1a28b74263e12afd4475fd7896d9c4e6a9d.tar.xz erlang-32abe1a28b74263e12afd4475fd7896d9c4e6a9d.zip |
Merge branch 'master' of ssh://git.animux.de/erlang
Diffstat (limited to '')
-rw-r--r-- | server/cldb.erl | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/server/cldb.erl b/server/cldb.erl index e6f414e..8d58fb4 100644 --- a/server/cldb.erl +++ b/server/cldb.erl @@ -1,15 +1,17 @@ -% The clientdatabase +%% The client database -module(cldb). -export([init/0, all/0, find/1, login/2, dec_vote/1, inc_vote/1, register/3, check_rights/1, update_votes/0, logout/1, set_client_pid/2, get_votes/1]). -record(user, {name, passwd, votes, logged_in, pid, rights}). +%% Initialisation of the mnesia database init() -> mnesia:create_schema([node()]), mnesia:start(), mnesia:create_table(user, [{attributes, record_info(fields, user)}]), io:format("Userdb up and running \n"). +%% The basic search operations on the database find(User) when is_list(User) -> find(User, '_'); find(User) -> @@ -39,6 +41,8 @@ find_user_by_pid(Pid) -> _ -> {error} end. +%% Retrieve all entries + all() -> F = fun() -> mnesia:match_object({user, '_', '_', '_', '_', '_', '_'}) @@ -48,6 +52,7 @@ all() -> _ -> {error} end. +%% Perform the login operation of a user into the database. login(User, Pwd) when is_list(User) and is_list(Pwd) -> case find(User, Pwd) of {atomic, [UserRow|_]} -> @@ -68,6 +73,7 @@ login(User, Pwd) when is_list(User) and is_list(Pwd) -> login(_, _) -> {error}. +%% In order to log out the client after its process has died. set_client_pid(User, Pid) -> case find(User) of {atomic, [UserRow|_]} -> @@ -85,6 +91,7 @@ set_client_pid(User, Pid) -> {error, user_not_found} end. +%% Change the database to log out a user by PID logout(Pid) -> case find_user_by_pid(Pid) of {atomic, [UserRow|_]} -> @@ -103,6 +110,9 @@ logout(Pid) -> {error, invalid_user} end. +%% Adds an entry into the database for this user. +%% If he is already registered, this will return an error. +%% In order to vote, users arer required to log in after registering. register(User, Pwd, Root) when is_list(User) and is_list(Pwd) -> case find(User) of {atomic, []} -> @@ -119,10 +129,10 @@ register(User, Pwd, Root) when is_list(User) and is_list(Pwd) -> _ -> {error, duplicated_user} end; - register(_,_,_) -> {error, invalid_username}. +%% functions to de- and increment the amount of votes a user has left. dec_vote(User) when is_list(User) -> {atomic, [Head|_]} = find(User), if Head#user.votes > 0 -> @@ -147,23 +157,27 @@ inc_vote(User) -> end, mnesia:transaction(F). +%% Return rights of a user (admin or nan). check_rights(User) -> {atomic, [UserRow|_]} = find(User), UserRow#user.rights. update_votes() -> - %% after a song every logged_in client gets on more vote + %% after a song every logged in client gets one more vote to spend case find_logged_in_user() of {ok, List} -> give_votes(List); _ -> error end. +%% Increment the votes of every user. +%% This is called when a song has ended. give_votes([User|Rest]) -> inc_vote(User#user.name), give_votes(Rest); give_votes([]) -> ok. +%% Returns the amount of votes a specific user has. get_votes(User) -> case find(User) of {atomic, [UserRow|_]} -> |