%%%-------------------------------------------------------------------
%%% @doc     宠物副本-头文件
%%% Module  : dungeon_pet.hrl
%%% Created : 2020-07-04
%%% @Author : tyl
%%%-------------------------------------------------------------------

-define(CREATE_MON_GAP,  1000).  %% 单只怪刷出时间间隔
-define(DUN_PET_WAVE(DunId),    lists:max(data_dungeon_wave:get_dun_waves(DunId))).    %% 宠物副本波数
-define(DUN_PET_SEND_TV_RANK, data_dungeon_pet:get_dun_pet_kv_cfg(send_tv_rank)).      %% 发送传闻排名
-define(DUN_PET_DEFAULT_MAX_RANK, 1000).     %% 默认最大积分奖励
-define(DUN_PET_YEST_MAX_RANK, data_dungeon_pet:get_dun_pet_kv_cfg(last_day_rank_length)). %% 昨日榜最大长度

%% 日奖励领取情况
-define(DAILY_REWARD_NOT_RECEIVE,  0).      %% 未领取
-define(DAILY_REWARD_HAS_RECEIVED, 1).      %% 已领取

%% 排行榜数据
-record(dun_pet_rank_state, {
	rank_list      = []     %% 排名列表,格式[]   % 格式:#{key => [#dun_pet_rank{}...]} key = {low, high}积分上下限(已经废弃)
	,reward_status = 0      %% 12点榜单奖励结算 0-未结算 1-已经结算
}).

%% 排行榜个人数据
-record(dun_pet_rank, {
	rank            = 0     %% 排名
	,role_id        = 0     %% 角色Id
	,role_name      = ""    %% 角色名称(前端展示)
	,sex            = 0     %% 性别(前端展示)
	,photo_id       = 0     %% 头像Id(前端展示)
	,vip            = 0     %% vip等级(前端展示)
	,sup_vip        = 0     %% 贵族类型(前端展示)
	,score          = 0     %% 积分
	,dun_wave       = 0     %% 通关副本波数
	,reach_time     = 0     %% 达成时间
	,old_score      = 0     %% 昨天的积分
	,old_dun_wave   = 0     %% 昨日通关副本波数
	,old_rank       = 0     %% 昨天排名
	,is_received    = 0     %% 是否已经领取日奖励
}).

%% 昨日个人榜(服务器启动时临时存储数据)
-record(dun_pet_yest_rank, {
	role_id        = 0     %% 角色Id
	,old_score     = 0     %% 昨天的积分
	,old_dun_wave  = 0     %% 昨日通关副本波数
	,old_rank      = 0     %% 昨天排名
	,old_reach_time= 0     %% 达成时间
}).

%% 玩家宠物副本数据
-record(role_dun_pet, {
	reward_status  = []      %% 首通奖励情况
	,pet_choose    = #{}     %% 宠物助战选择 #{dun_id =>[{pet_id, pos, skill}...]}
	,reward_choose = #{}     %% 奖励选择 #{dun_id => index...}
	,dun_history_score = #{} %% 副本历史最高积分
}).


%% ============================== 后台配置 =================================
%% 常量配置表
-record(dun_pet_kv_cfg, {
	id            = 0       %% Id
	,key          = ""      %% 键
	,value        = ""      %% 值
	,remark       = ""      %% 描述
}).

%% buff球配置表
-record(dun_pet_buff_cfg, {
	id            = 0       %% Id
	,effect       = []      %% 效果
	,weight       = 0       %% 掉落概率
	,desc         = ""      %% 描述
	,client_show  = ""      %% 前端资源
}).

%% 结算奖励配置表
-record(dun_pet_reward_cfg, {
	dun_id        = 0       %% 副本Id
	,low          = 0       %% 积分下限
	,height       = 0       %% 积分上限
	,reward       = []      %% 可选掉落
}).

%% 排名奖励配置表
-record(dun_pet_rank_reward_cfg, {
	id            = 0       %% Id
	,rank_low     = 0       %% 排名下限
	,rank_height  = 0       %% 排名上限
	,need_score   = 0       %% 需求积分
	,need_dun     = 0       %% 需要通关副本
	,need_wave    = 0       %% 需要通关波数
	,reward       = []      %% 奖励内容
}).

%% 波数积分配置表
-record(dun_pet_wave_score_cfg, {
	dun_id        = 0        %% 副本Id
	,wave         = 0        %% 波数
	,score        = 0        %% 积分
}).

%% 副本信息配置表
-record(dun_pet_next_dun_cfg, {
	id            = 0        %% Id
	,dun_id       = 0        %% 副本Id
	,next_dun     = 0        %% 下一副本Id
	,pet_id       = 0        %% 宠物Id
	,pet_figure   = 0        %% 宠物形象资源Id
	,guard_mons   = []       %% 守卫怪参数
	,pet_mon      = []       %% 妖宠参数
	,follow_num   = 0        %% 跟随数量
	,reward       = []       %% 首通奖励
}).

%% ============================= 数据库操作 =============================
%% 里程碑奖励
-define(SQL_REPLACE_PET_DUN_REWARD,
	<<"REPLACE INTO role_pet_dun_reward (role_id, dun_id, status) VALUES (~p, ~p, ~p)">>).
-define(SQL_SELECT_PET_DUN_REWARD,
	<<"SELECT dun_id, status FROM role_pet_dun_reward WHERE role_id = ~p">>).

%% 排行榜(排行榜只取前999名,避免合服后数据量庞大)
-define(SQL_INSERT_DUN_PET_RANK, <<"REPLACE INTO dun_pet_rank (`role_id`, `score`, `dun_wave`, `reach_time`) VALUES (~p, ~p, ~p, ~p)">>).
-define(SQL_SELECT_DUN_PET_RANK, <<"SELECT `role_id`, `score`, `dun_wave`, `reach_time`, `old_score`, `old_dun_wave`, `status` FROM dun_pet_rank order by `dun_wave` desc limit ~p">>).
-define(SQL_SELECT_DUN_PET_YEST_RANK, <<"SELECT `role_id`, `old_score`, `old_dun_wave`,`old_reach_time` FROM dun_pet_rank order by `old_dun_wave` desc limit ~p">>).
-define(SQL_DELETE_DUN_PET_RANK, <<"DELETE FROM dun_pet_rank WHERE `role_id` = ~p">>).
-define(SQL_UPDATE_DUN_PET_RANK_REWARD_STATUS, <<"UPDATE dun_pet_rank set `status` = ~p WHERE `role_id` = ~p">>).
-define(SQL_UPDATE_DUN_PET_RANK_OLD_SCORE, <<"UPDATE dun_pet_rank set `old_score` = ~p, `old_dun_wave`= ~p, `old_reach_time` =~p WHERE `role_id` = ~p">>).
-define(SQL_UPDATE_DUN_PET_RANK_SCORE, <<"UPDATE dun_pet_rank set `score` = ~p, `dun_wave`= ~p, `reach_time` =~p WHERE `role_id` = ~p">>).
% -define(SQL_DELETE_DUN_PET_RANK, <<"DELTET FROM dun_pet_rank WHERE `rank` = ~p AND `role_id` = ~p">>).

-define(SQL_SELECT_DUN_PET_CHOOSE,
	<<"SELECT `dun_id`, `pet_id`, `pos_id` FROM role_dun_pet_choose WHERE `role_id` = ~p">>).
-define(SQL_REPLACE_DUN_PET_CHOOSE,
	<<"REPLACE INTO role_dun_pet_choose (`role_id`, `dun_id`, `pet_id`, `pos_id`) VALUES ~ts">>).
-define(SQL_BATCH_DUN_PET_VALUES,
	<<"(~p, ~p, ~p, ~p)">>).
-define(SQL_DELETE_DUN_PET_CHOOSE,
	<<"DELETE FROM role_dun_pet_choose WHERE `role_id` = ~p and `dun_id` = ~p">>).

-define(SQL_REPLACE_PET_DUN_REWARD_CHOOSE,
	<<"REPLACE INTO role_dun_pet_reward_choose (`role_id`, `dun_id`, `index`) VALUES (~p, ~p, ~p)">>).
-define(SQL_SELECT_PET_DUN_REWARD_CHOOSE,
	<<"SELECT `dun_id`, `index` FROM role_dun_pet_reward_choose WHERE `role_id` = ~p">>).

-define(SQL_REPLACE_PET_DUN_MAX_SCORE,
	<<"REPLACE INTO role_dun_pet_max_score (`role_id`, `dun_id`, `max_score`) VALUES (~p, ~p, ~p)">>).
-define(SQL_SELECT_PET_DUN_MAX_SCORE,
	<<"SELECT `dun_id`, `max_score` FROM role_dun_pet_max_score WHERE `role_id` = ~p">>).