%%%-----------------------------------
%%% @Module  : clusters_zone_base
%%% @Author  : hek
%%% @Since   : 2018-07-24
%%% @Description: 分区启动服务
%%%-----------------------------------
-module(clusters_zone_base).
-export([start/2, stop/2]).
-compile(export_all).
-include("common.hrl").
-include("clusters.hrl").
-include("def_module.hrl").
-include("custom_act.hrl").

-define(CENTER_SUP, game_center_sup).

%% 启动分区
start(ZoneType, Zone) ->
    ok = start_normal_processes(init_modules_func(ZoneType, Zone)),
    ok.

%% 关闭分区时需停止
stop(_ZoneType, _Zone) ->
    ok.

start_normal_processes([{Mod, _, _}=H|T]) ->
    {ok, _} = supervisor:start_child(?CENTER_SUP, {H, H, permanent, 10000, worker, [Mod]}),
    start_normal_processes(T);
start_normal_processes([_H|T]) ->
    ?PRINT("[Error]:init_modules_func bad format: ~p~n", [_H]),
    start_normal_processes(T);
start_normal_processes([]) -> ok.

init_modules_func(ZoneType, _Zone) ->
    List = [
        %% {ModuleId::功能Id, SubModuleId::子功能Id, M::模块名, F::启动函数, A::参数列表}
    ],
    %% 筛选与当前分区类型相匹的模块列表
    F = fun({ModuleId, SubModuleId, M, F, A}, AccList) ->
        case data_clusters_zone:get_zone_type(ModuleId, SubModuleId) of
            ZoneType -> [{M, F, A}|AccList];
            _ -> AccList
        end
    end,
    lists:reverse(lists:foldl(F, [], List)).

