-module(dispatcher).
-export([start/0, init/0, handle/3]).
start() ->
server:start(dis, dispatcher).
init() ->
dict:new().
handle(_, {register, {User, Password}}, Dict) ->
case dict:find(User, Dict) of
{ok, _}->
{{error, duplicated_user}, Dict};
_ ->
{{ok, user_created}, dict:store(User, Password, Dict)}
end;
handle(From, {login, {User, Password}}, Dict) ->
case dict:find(User, Dict) of
{ok, Password} ->
{{ok, {logged_in, client:start(From)}}, Dict};
_ ->
{{error, user_or_password_invalid}, Dict}
end;
handle(_, _, Dict) ->
{{error, unknown_command}, Dict}.