egs_zones: Determine which set to use at process initialization.
This commit is contained in:
parent
8f069e72d8
commit
a1b8b1909e
@ -20,29 +20,27 @@
|
||||
-module(egs_quests).
|
||||
-behaviour(gen_server).
|
||||
|
||||
-export([start_link/2, stop/0]). %% API.
|
||||
-export([start_link/2, stop/1]). %% API.
|
||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). %% gen_server.
|
||||
|
||||
-record(state, {zones}).
|
||||
|
||||
-define(SERVER, ?MODULE).
|
||||
|
||||
%% API.
|
||||
|
||||
%% @spec start_link(UniID, QuestID) -> {ok,Pid::pid()}
|
||||
start_link(UniID, QuestID) ->
|
||||
gen_server:start_link(?MODULE, [UniID, QuestID], []).
|
||||
|
||||
%% @spec stop() -> stopped
|
||||
stop() ->
|
||||
gen_server:call(?SERVER, stop).
|
||||
%% @spec stop(Pid) -> stopped
|
||||
stop(Pid) ->
|
||||
gen_server:call(Pid, stop).
|
||||
|
||||
%% gen_server.
|
||||
|
||||
init([UniID, QuestID]) ->
|
||||
Zones = egs_quests_db:quest_zones(QuestID),
|
||||
ZonesPids = lists:map(fun({ZoneID, _Params}) ->
|
||||
{ok, Pid} = supervisor:start_child(egs_zones_sup, {{zone, UniID, QuestID, ZoneID}, {egs_zones, start_link, [UniID, QuestID, ZoneID]}, permanent, 5000, worker, dynamic}),
|
||||
ZonesPids = lists:map(fun({ZoneID, ZoneData}) ->
|
||||
{ok, Pid} = supervisor:start_child(egs_zones_sup, {{zone, UniID, QuestID, ZoneID}, {egs_zones, start_link, [UniID, QuestID, ZoneID, ZoneData]}, permanent, 5000, worker, dynamic}),
|
||||
{ZoneID, Pid}
|
||||
end, Zones),
|
||||
{ok, #state{zones=ZonesPids}}.
|
||||
|
@ -20,25 +20,34 @@
|
||||
-module(egs_zones).
|
||||
-behaviour(gen_server).
|
||||
|
||||
-export([start_link/3, stop/0]). %% API.
|
||||
-export([start_link/4, stop/1, setid/1]). %% API.
|
||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). %% gen_server.
|
||||
|
||||
-define(SERVER, ?MODULE).
|
||||
-record(state, {
|
||||
setid = 0 :: integer()
|
||||
}).
|
||||
|
||||
%% API.
|
||||
|
||||
%% @spec start_link(UniID, QuestID, ZoneID) -> {ok,Pid::pid()}
|
||||
start_link(UniID, QuestID, ZoneID) ->
|
||||
gen_server:start_link(?MODULE, [UniID, QuestID, ZoneID], []).
|
||||
%% @spec start_link(UniID, QuestID, ZoneID, ZoneData) -> {ok,Pid::pid()}
|
||||
start_link(UniID, QuestID, ZoneID, ZoneData) ->
|
||||
gen_server:start_link(?MODULE, [UniID, QuestID, ZoneID, ZoneData], []).
|
||||
|
||||
%% @spec stop() -> stopped
|
||||
stop() ->
|
||||
gen_server:call(?SERVER, stop).
|
||||
%% @spec stop(Pid) -> stopped
|
||||
stop(Pid) ->
|
||||
gen_server:call(Pid, stop).
|
||||
|
||||
setid(Pid) ->
|
||||
gen_server:call(Pid, setid).
|
||||
|
||||
%% gen_server.
|
||||
|
||||
init([UniID, QuestID, ZoneID]) ->
|
||||
{ok, undefined}.
|
||||
init([UniID, QuestID, ZoneID, ZoneData]) ->
|
||||
SetID = rand_setid(proplists:get_value(sets, ZoneData, [100])),
|
||||
{ok, #state{setid=SetID}}.
|
||||
|
||||
handle_call(setid, _From, State) ->
|
||||
{reply, State#state.setid, State};
|
||||
|
||||
handle_call(stop, _From, State) ->
|
||||
{stop, normal, stopped, State};
|
||||
@ -57,3 +66,14 @@ terminate(_Reason, _State) ->
|
||||
|
||||
code_change(_OldVsn, State, _Extra) ->
|
||||
{ok, State}.
|
||||
|
||||
%% Internal.
|
||||
|
||||
%% @doc Return a random setid from a list of chances per set.
|
||||
rand_setid(Sets) ->
|
||||
N = crypto:rand_uniform(1, lists:sum(Sets)),
|
||||
rand_setid(N, Sets, 0).
|
||||
rand_setid(N, [Set|_Tail], I) when N < Set ->
|
||||
I;
|
||||
rand_setid(N, [Set|Tail], I) ->
|
||||
rand_setid(N - Set, Tail, I + 1).
|
||||
|
Loading…
Reference in New Issue
Block a user