aboutsummaryrefslogtreecommitdiffstats
path: root/server/cldb.erl
blob: c29447141a12f3c635d279334e8cc01c091ef0cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
% The clientdatabase

-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}).

%% The initialisations 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) ->
    find(User#user.name, '_').

find(User, Pwd) ->
    F = fun() ->
		mnesia:match_object({user, User, Pwd, '_', '_', '_', '_'})
        end,
    mnesia:transaction(F).

find_logged_in_user() ->
    F = fun() ->
		mnesia:match_object({user, '_', '_', '_', true, '_', '_'})
        end,
    case mnesia:transaction(F) of
	{atomic, List} -> {ok, List};
	_ -> {error}
    end.

find_user_by_pid(Pid) ->
    F = fun() ->
		mnesia:match_object({user, '_', '_', '_', '_', Pid, '_'})
        end,
    case mnesia:transaction(F) of
	{atomic, List} -> {ok, List};
	_ -> {error}
    end.

%% Retrieve all the entrys

all() ->
    F = fun() ->
		mnesia:match_object({user, '_', '_', '_', '_', '_', '_'})
        end,
    case mnesia:transaction(F) of
	{atomic, List} -> {ok, List};
	_ -> {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|_]} ->
	    NewUserRow = UserRow#user{logged_in = true},
	    F = fun() ->
			mnesia:write(NewUserRow)
		end,
	    case mnesia:transaction(F) of
		{atomic, ok} ->
		    {ok, NewUserRow};
		{atomic, Why} ->
		    {error, Why}
	    end;

	_-> {error}
    end;

login(_, _) ->
    {error}.

%% Inorder to log out the client after his process died.
set_client_pid(User, Pid) ->
    case find(User) of
	{atomic, [UserRow|_]} ->
	    NewUserRow = UserRow#user{pid = Pid},
	    F = fun() ->
			mnesia:write(NewUserRow)
		end,
	    case mnesia:transaction(F) of
		{atomic, ok} ->
		    {ok, pid_updated};
		{atomic, Why} ->
		    {error, Why}
	    end;
	_ ->
	    {error, user_not_found}
    end.

%% Changing the database to log out a user by PID
logout(Pid) ->
    case find_user_by_pid(Pid) of
	{atomic, [UserRow|_]} ->
	    F = fun() ->
			New = UserRow#user{logged_in = false},
			mnesia:write(New)
		end,
	    case mnesia:transaction(F) of
		{atomic, ok} ->
		    {ok, logged_out};
		{atomic, Why} ->
		    {error, Why}
	    end;

	_ ->
	    {error, invalid_user}
    end.
%% Makes an entry into the database for this user.
%% If he is already registerd an error will be returned.
%% In order to vote it is needed to login afterwards.
register(User, Pwd, Root) when is_list(User) and is_list(Pwd) ->
    case find(User) of
	{atomic, []} ->
	    F = fun() ->
			mnesia:write(#user{name = User, passwd = Pwd, votes = 5, logged_in = false, pid = undef, rights = Root})
		end,
	    case mnesia:transaction(F) of
		{atomic, ok} ->
		    io:format("User created: ~s~n", [User]),
		    {ok, user_created};
		{atomic, Why} ->
		    {error, Why}
	    end;
	_ ->
	    {error, duplicated_user}
    end;
register(_,_,_) ->
    {error, invalid_username}.

%% functions to de- and increment the amount of votes a user got left.
dec_vote(User) when is_list(User) ->
    {atomic, [Head|_]} = find(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;
dec_vote(User) ->
    dec_vote(User#user.name).

inc_vote(User) ->
    F = fun() ->
                {atomic, [Head|_]} = find(User),
                Votes = Head#user.votes + 1,
                New = Head#user{votes = Votes},
                mnesia:write(New)
        end,
    mnesia:transaction(F).

%% Return rights of 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
    case find_logged_in_user() of
	{ok, List} ->
	    give_votes(List);
	_ -> error
    end.

%% Increment the votes of every user. 
%% It will be 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 from User.
get_votes(User) ->
    case find(User) of
	{atomic, [UserRow|_]} ->
	    {ok, UserRow#user.votes};
	_ ->
	    {error, user_not_found}
    end.