Skip to content

Commit

Permalink
App mod and entity mod cleanup.
Browse files Browse the repository at this point in the history
Removed gen_server callback spec/doc until a project-wide
standard is decided.
Changed long function heads and long lines.
  • Loading branch information
Gustav Simonsson committed Jul 10, 2012
1 parent 0514786 commit 14ed879
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 83 deletions.
8 changes: 3 additions & 5 deletions apps/browserquest_srv/src/browserquest_srv_app.erl
Expand Up @@ -8,15 +8,13 @@
%% ===================================================================
%% Application callbacks
%% ===================================================================

start(_StartType, _StartArgs) ->

application:start(lager),
lager:set_loglevel(lager_console_backend, debug),

%% {Host, list({Path, Handler, Opts})}
%% Dispatch the requests (whatever the host is) to
%% erws_handler, without any additional options.
%% Dispatch the requests (whatever the host is) to the server handler
%% without any additional options.
ListeningPort =
case application:get_env(listening_port) of
undefined ->
Expand All @@ -29,7 +27,7 @@ start(_StartType, _StartArgs) ->
{'_', browserquest_srv_handler, []}
]}],

%% lager:debug("Starting browserquest_srv on port ~p", [ListeningPort]),
lager:debug("Starting browserquest_srv on port ~p", [ListeningPort]),

%% Name, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts
%% Listen in 10100/tcp for http connections.
Expand Down
92 changes: 14 additions & 78 deletions apps/browserquest_srv/src/browserquest_srv_entity_handler.erl
Expand Up @@ -2,7 +2,8 @@
%%% @author Niclas Axelsson <burbas@Niclass-MacBook-Pro.local>
%%% @copyright (C) 2012, Niclas Axelsson
%%% @doc
%%%
%%% Handles entities; unregisters and registers entities such as mobs, items and
%%% players, and broadcasts events to these entities.
%%% @end
%%% Created : 8 Jul 2012 by Niclas Axelsson <burbas@Niclass-MacBook-Pro.local>
%%%-------------------------------------------------------------------
Expand Down Expand Up @@ -81,66 +82,40 @@ move_zone(OldZone, NewZone) ->
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initializes the server
%%
%% @spec init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
init([]) ->
Args = [fun add_mob/1, browserquest_srv_map:get_attribute("mobAreas")],
erlang:spawn(lists, foreach, Args),
{ok, #state{zones = dict:new(), targets = dict:new()}}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @spec handle_call(Request, From, State) ->
%% {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_call({register, Pid, Zone, Id}, _From, State = #state{targets = Targets, zones = Zones}) ->
UpdatedZones = dict:update(Zone, fun(Nodes) ->
[Pid|Nodes]
end, [Pid], Zones),

handle_call({register, Pid, Zone, Id}, _From,
State = #state{targets = Targets,zones = Zones}) ->
UpdatedZones =
dict:update(Zone, fun(Nodes) -> [Pid|Nodes] end, [Pid], Zones),
UpdatedTargets = dict:store(Id, Pid, Targets),

{reply, ok, State#state{zones = UpdatedZones, targets = UpdatedTargets}};

handle_call({unregister, Pid, Zone}, _From, State = #state{zones = Zones}) ->
UpdatedZones = dict:update(Zone, fun(Nodes) ->
[ X || X <- Nodes, X /= Pid, X /= {static, Pid} ]
end, [], Zones),
Update = fun(Nodes) -> [ X || X <- Nodes, X /= Pid, X /= {static, Pid}] end,
UpdatedZones = dict:update(Zone, Update, [], Zones),
{reply, ok, State#state{zones = UpdatedZones}};

handle_call({get_target, Target}, _From, State = #state{targets = Targets}) ->
Reply = dict:find(Target, Targets),
{reply, Reply, State};

handle_call({event, Pid, Zone, Type, Message}, _From, State = #state{zones = Zones}) when is_pid(Pid) ->
handle_call({event, Pid, Zone, Type, Message}, _From,
State = #state{zones = Zones}) when is_pid(Pid) ->
case dict:find(Zone, Zones) of
{ok, Nodes} ->
[ gen_server:cast(Node, {event, Pid, Type, Message}) || Node <- Nodes, Node /= Pid, Node /= {static, Pid} ];
[gen_server:cast(Node, {event, Pid, Type, Message})
|| Node <- Nodes, Node /= Pid, Node /= {static, Pid}];
_ ->
[]
end,
{reply, ok, State};

handle_call({event, Target, _Zone, Type, Message}, _From, State = #state{targets = Targets}) ->
handle_call({event, Target, _Zone, Type, Message}, _From,
State = #state{targets = Targets}) ->
case dict:find(Target, Targets) of
{ok, Pid} ->
gen_server:cast(Pid, {event, Pid, Type, Message});
Expand All @@ -153,54 +128,15 @@ handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
{noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling all non call/cast messages
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
{noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
%%
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Convert process state when code is changed
%%
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @end
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.

Expand Down

0 comments on commit 14ed879

Please sign in to comment.