%% -----------------------
%% desc:跨服团战头文件
%% author:hh
%% time:20/07/21
%% -----------------------

%% 分线规则
%% 1.跨服团战这边选定分线，因进入的战场固定，所以不参与自动强制分线
%% 2.3个社团一个战场,一个战场可能有多条分线(多个房间)

%% 划分战场规则
%% 1.划分战场时检测阵营内部有无服务器合服了，有的话要排除服务器
%% 2.只有满足开服天数要求的服务器才能参与划分战场

%% 跨服团战公共管理进程
-record(camp_war_state, {
    status       = 0,           %% 阶段
    start_time   = 0,           %% 阶段开始时间
    next_time    = 0,           %% 下一阶段时间
    end_time     = 0,           %% 结束时间
    zone_state   = 0,           %% 0未分区|1已分区
    extra_map    = #{},         %% 其余数据的map
    worker_pids  = #{},         %% 阵营进程
    fac_index    = 1,           %% 跨服团战内部阵营编号
    cls_type     = 0            %% 1跨服|0本服
    }).

%% 跨服团战阵营管理进程
-record(zone_fac_state, {
    zone_fac         = undefined,    %% {zone_id, fac_id}
    fac_index        = 0,            %% 阵营编号
    status           = 0,            %% 所处阶段
    max_pool         = 0,            %% 阵营最大分线ID
    res_id           = 1,            %% 生成采集资源使用
    arena_map        = #{},          %% 战场与社团列表映射 arena_id => [{guild_id, server_id}]
    guild_arena_map  = #{},          %% 社团列表与战场映射 guild_id => arena_id
    arena_room_map   = #{},          %% 战场与分线映射 arena_id => [pool_id,...]
    room_role_num    = #{},          %% 分线人数 pool_id => num
    resource         = #{},          %% 资源列表 pool => [{物资ID(怪物ID), {x, y}, Index}|_]
    role_rank        = [],           %% 战场内个人排名 arena_id => [#camp_war_role{}]
    guild_rank       = [],           %% 战场内社团排名 arena_id => [#camp_war_guild{}]
    % hurt_top         = 0,            %% 阵营内伤害第一的社团id
    buff_list        = [],           %% 社团buff增益列表 [#guild_res{}|_]
    extra_map        = #{},           %% 其余数据
    cls_type         = 0
    }).

%% 本地公会战信息
-record(local_gwar_info, {
    guild_id = 0,
    server_id = 0,
    server_num = 0,
    zone_id = 0,
    fac_id = 0,
    guild_flag = 0,
    guild_name = <<>>,
    guild_power = 0,
    chief_info = [],
    score = 0
    }).

%% 社团buff采集信息
-record(guild_res, {
    guild_id = 0,
    % server_id = 0,
    buff_lists = []         %% {物资id,数量}
    }).

%% 阵营玩家数据
-record(camp_war_role, {
    role_id = 0,
    role_name = "",
    guild_id = 0,
    server_id = 0,
    server_num = 0,
    lv = 0,
    vip = 0,
    sup_vip = 0,
    sex = 0,
    photo_profile_id = 0,
    dress_id = 0,
    zone_facs = [],
    hurt = 0,
    in_act = 0,              %% 是否在活动场景
    is_host = 0              %% 是否托管
    }).

%% 阵营社团数据
-record(camp_war_guild, {
    guild_id = 0,
    guild_name = "",
    guild_flag = 0,
    guild_power = 0,
    chief_info = [],
    server_id = 0,
    server_num = 0,
    zone_facs = [],
    nums = 1,       %% 社团参与人数
    hurt = 0        %% 社团输出伤害
    }).

%% 百团大战结果
-record(guild_war_ret, {
    guild_id = 0, 
    guild_name = 0, 
    score = 0, 
    guild_flag = 0,
    guild_power = 0,
    chief_info = [] % #rec_picture{}
    }).

%% 人物进场数据
-record(role_camp_war, {
    coordinate = []
    }).

%% 当前状态
-define(CAMP_WAR_CLOSE,     0).            
-define(CAMP_WAR_OPENING_1, 1).            %% 入场
-define(CAMP_WAR_OPENING_2, 2).            %% 采集
-define(CAMP_WAR_OPENING_3, 3).            %% 杀boss

%% #camp_war_state.extra_map key
-define(FAC_ZONE_INIT,          zone_init).        %% val:0分区数据未准备|1分区数据已准备
-define(ZONE_INIT_INTERVAL,     zone_calc_ref).    %% val:is_reference()
-define(CAMP_WAR_GM_TYPE,       gm_type).          %% val:normal|gm 
-define(FAC_ARENA_INIT,         arena_init).       %% val:0战场未分配|1战场已分配
-define(MGR_INIT_STATE,         mgr_init).         %% 是否已完成初始化
-define(FAC_GUILD_INIT,         guild_init).       %% 社团数据是否已处理
-define(IS_TODAY_ACT,           today_act).        %% 今日是否有活动
-define(WORKER_STOP_REF,        worker_stop_ref).  %% 工作进程停止定时
-define(PRE_KILL_REF,           pre_kill_ref).     %% 预清理怪物定时

%% #zeon_fac_state.extra_map key
-define(RES_UPUPDATE_REF,        res_update).       %% 资源刷新定时
-define(HURT_UPDATE_REF,         hurt_update).      %% 伤害排名刷新定时
-define(INIT_REF,                worker_init).      %% worker 初始化定时
-define(RES_START_TIME,          res_stime).        %% 物资资源开始时间
-define(OFF_HOST_PID,            off_hosting_pid).  %% 托管进程
-define(BOSS_UNIQUE_ID,          boss_unique_id).   %% boss唯一ID
-define(HOSTING_REF,             hosting_battle).   %% 通知托管玩家发动攻击定时

%% ets
-define(ETS_FAC_SRV_ZONE,               ets_camp_war_svr_zone).     %% 服务器与{分区,阵营}映射 server_id => {zone_id，fac_id}
-define(ETS_FAC_ZONE_FACS,              ets_camp_war_zone_facs).    %% 区里所有阵营列表 zone_id => facs = [_|_]
-define(ETS_FAC_LOCAL_GWAR_INFO,        ets_local_gwar_info).       %% 本地百团大战收集信息 {zone_id, fac_id} => [#local_gwar_info{}]
-define(ETS_FAC_ARENA_INFO,             ets_camp_war_arena_info).   %% 战场分配列表 {zone_id, fac_id} => {{zone_id, fac_id}, fac_index(跨服团战内部编号), [{arena_id, [{guild_id,server_id}|_]}]}
-define(ETS_FAC_LOG_RET,                ets_camp_war_log_ret).      %% 阵营战果保存

-define(PRE_FAC_ZONE_CALC_TIME,  3600).

-define(ZONE_INIT_INTERVAL_TIME, 600000).
-define(HURT_UPFATE_TIME,  15000).

-define(FAC_POOL_BASE(P), 1000*P).

%% 本服玩法默认分区&阵营
-define(LOC_ZONE, 1).
-define(LOC_FAC,  1).

%% 本服和跨服的活动日历子id
-define(CLS_ACT_MOD, 1).    %% 跨服
-define(LOC_ACT_MOD, 2).    %% 本服

%% 个人排行奖励
-record(base_camp_war_role_rank_reward, {
    rank_min = 0,
    rank_max = 0,
    rewards = []
    }).


%% 社团排行奖励
-record(base_camp_war_rank_reward, {
    rank_min = 0,
    rank_max = 0,
    arena_id = 0,
    rewards = []
    }).

-define(SQL_SELECT_LOCAL_GUILD_SCORE, 
    <<"SELECT guild_id, guild_name, guild_flag, guild_power, chief_info, score, server_id, server_num, zone_id, fac_id FROM camp_war_guild_info">>).

-define(SQL_BATCH_REPLACE_LOCAL_GUILD_SCORE, 
    <<"REPLACE INTO camp_war_guild_info (guild_id, guild_name, guild_flag, guild_power, chief_info, score, server_id, server_num, zone_id, fac_id) VALUES ~ts">>).

-define(SQL_BATCH_REPLACE_LOCAL_GUILD_SCORE_VAL, 
    <<"(~p, '~ts', ~p, ~p, '~ts', ~p, ~p, ~p, ~p, ~p)">>).

-define(SQL_REPLACE_INTO_LOCAL_GUILD_SCORE, 
    <<"REPLACE INTO camp_war_guild_info set guild_id = ~p, guild_name = ~ts, guild_flag = ~p, score = ~p, server_id = ~p, server_num = ~p, zone_id = ~p, fac_id = ~p">>).

-define(REPLACE_INTO_CAMP_WAR_RESULT, 
    <<"REPLACE INTO camp_war_guild_rank_log (`guild_id`, `zone_id`, `fac_id`, `svr_id`, `svr_num`, `guild_name`, `hurt`, `guild_power`, `chief_info`)">>).

-define(SQL_DELETE_CAMP_WAR_RESULT, 
    <<"DELETE FROM camp_war_guild_rank_log">>).

-define(SQL_SELECT_CAMP_WAR_RESULT,
    <<"SELECT `zone_id`, `fac_id`, `svr_id`, `guild_id`, `svr_num`, `guild_name`, `hurt`, `guild_power`, `chief_info` FROM camp_war_guild_rank_log">>).

-define(SQL_UPFATE_CAMP_WAR_RESULT, 
    <<"UPDATE camp_war_guild_rank_log SET `zone_id` = ~p, `fac_id` = ~p WHERE `guild_id` = ~p">>).

-define(SQL_DELETE_LOCAL_GUILD_SCORE, 
    <<"DELETE FROM camp_war_guild_info">>).

-define(SQL_DELETE_LOCAL_ONE_GUILD_SCORE, 
    <<"DELETE FROM camp_war_guild_info WHERE (zone_id = ~p AND fac_id = ~p) OR (zone_id = ~p AND fac_id = ~p)">>).