%%%------------------------------------
%%% module  : mod_center_init
%%% @author  : xyao
%%% email   : jiexiaowen@gmail.com
%%% Created : 2012.10.25
%%% description: 集群数据初始化
%%%------------------------------------
-module(center_init).
-behaviour(gen_server).
-export([
        start_link/0
    ]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-include("common.hrl").
-include("server_env.hrl").
-include("server_kv.hrl").
-include("scene.hrl").
-include("team.hrl").
-include("custom_act.hrl").

start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

init([]) ->
    %% 初始ets表
    ok = init_ets(),
    %% 使用了本服的表格
    ok = lib_custom_act_dynamic_compile:dynamic_compile_cfg(),
    %% 初始化加载封测回归账号数据
    ok = comeback:init_load_data(),
    %% 跨服小分区管理进程信息
    ok = cross_zone_api:init(),
    {ok, ?MODULE}.

handle_cast(_R , Status) ->
    {noreply, Status}.

handle_call(_R , _FROM, Status) ->
    {reply, ok, Status}.

handle_info(_Reason, Status) ->
    {noreply, Status}.

terminate(_Reason, _Status) ->
    cross_zone_api:terminate(),
    ok.

code_change(_OldVsn, Status, _Extra)->
    {ok, Status}.

%% ================== 私有函数 =================
%% 初始ETS表
init_ets() ->
    %% 用户场景信息
    ets:new(?ETS_SCENE, [{keypos, #ets_scene.id}, named_table, public, set]),
    %% 服务器信息
    ets:new(?SERVER_ENV, [{keypos,#server_env.name}, named_table, public, set]),
    ets:new(?SERVER_KV, [{keypos, #server_kv.key}, named_table, public, set]),                       % 服务器键值kv
    %% 组队
    ets:new(?ETS_TEAM, [{keypos, #ets_team.team_id}, named_table, public, set]),

    ets:new(?ETS_CUSTOM_ACT, [{keypos, #act_info.key}, named_table, public, set]),                   % 当前开启的定制活动

    ok.
