From 14ed8792d26326adb0b6c609e6745a7be0779a3d Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Tue, 10 Jul 2012 23:49:37 +0200 Subject: [PATCH] App mod and entity mod cleanup. Removed gen_server callback spec/doc until a project-wide standard is decided. Changed long function heads and long lines. --- .../src/browserquest_srv_app.erl | 8 +- .../src/browserquest_srv_entity_handler.erl | 92 +++---------------- 2 files changed, 17 insertions(+), 83 deletions(-) diff --git a/apps/browserquest_srv/src/browserquest_srv_app.erl b/apps/browserquest_srv/src/browserquest_srv_app.erl index 6d9e6f3..3743fb6 100644 --- a/apps/browserquest_srv/src/browserquest_srv_app.erl +++ b/apps/browserquest_srv/src/browserquest_srv_app.erl @@ -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 -> @@ -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. diff --git a/apps/browserquest_srv/src/browserquest_srv_entity_handler.erl b/apps/browserquest_srv/src/browserquest_srv_entity_handler.erl index 5a7e5a1..36badcb 100644 --- a/apps/browserquest_srv/src/browserquest_srv_entity_handler.erl +++ b/apps/browserquest_srv/src/browserquest_srv_entity_handler.erl @@ -2,7 +2,8 @@ %%% @author Niclas Axelsson %%% @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 %%%------------------------------------------------------------------- @@ -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}); @@ -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}.