%%-----------------------------------------------------------------------------
%% @Module  :       rec_achievement
%% @Author  :       Czc
%% @Email   :       389853407@qq.com
%% @Created :       2018-01-23
%% @Description:    成就
%%-----------------------------------------------------------------------------

%% 由于成就数据可能比较庞大同时读写比较频繁,所以保存在字典
-define(P_ACHIEVEMENT,      "P_ACHIEVEMENT").

%% 成就特权等级
-define(ACHV_WEEKLY_RECEIVE_LV, 30).    %% 每周领取奖励
-define(ACHV_EXCHANGE_LV,       37).    %% 每日红钻兑交易券
-define(ACHV_FREE_SWEEP_TASK,   43).    %% 免费扫荡日常任务

%% 章节/成就奖励领取状态
-define(RECEIVE_TYPE_NOT, 0).       %% 未达条件,不可领取
-define(RECEIVE_TYPE_CAN, 1).       %% 可领取
-define(RECEIVE_TYPE_YES, 2).       %% 已领取

%% 成就进度
-define(ACHV_UNFINISH,           0).     %% 未完成
-define(ACHV_FINISH,             1).     %% 已完成未领取奖励
-define(ACHV_HAS_RECEIVE,        2).     %% 已领取奖励

-define(ACHV_WSTATUS_SUCCESS,    1).     %% 写入成功
-define(ACHV_WSTATUS_WAIT,       2).     %% 待写入

-define(ACHV_SAVE_DB_CD,         10).        %% 写入数据库CD时间

-define(NOTIFY_TYPE_CATEGORY_FIN_COUNT, 1).       %% 成就章节进度更新
-define(NOTIFY_TYPE_SUB_CATEGORY_PROGRESS, 2).    %% 成就小类进度更新

%% 成就进度数据,存进程字典中
-record(status_achievement, {
    category_map = #{},             %% #{category => count} 章节完成数量
    category_reward_list = [],      %% [{category, status}] 章节奖励领取状态
    finish_list = [],               %% [#achievement{}] 已经领取完奖励的成就列表
    achievement_list = []           %% [#achievement{}] 保存有进度数据的成就
}).

%% 成就等级,存#player_status{}
-record(role_achievement, {
    lv   = 0,               %% 等级
    exp  = 0,               %% 经验值
    attr = [],              %% 属性列表
    lv_reward_list = []     %% 等级奖励领取状态[{lv,status}]
}).

-record(achievement, {
    id = {},                    %% {category,subcategory}-{成就章节,成就小类}
    progress = 0,               %% 进度
    status = 0,                 %% 奖励状态 0: 未达成 1: 已达成 2: 已领取
    wtime = 0,                  %% 最近一次更新写入数据库的时间
    wstatus = 0                 %% 写入状态 1: 写入成功 2: 待写入
}).

%% ---------------------------------- 后台配置表 ----------------------------------
%% 章节奖励
-record(category_reward_cfg, {
    category = 0,
    name = "",
    desc = "",
    reward = [],
    count = 0           %% 完成子类的数量
}).

%% 成就基础配置表
-record(achievement_cfg, {
	type     = 0,           %% 成就大类
	sub_type = 0,           %% 成就子类
	id       = 0,           %% 成就Id
	name     = "",          %% 名称
	desc     = "",          %% 描述
	condition = 0,          %% 达成条件
	next_id  = 0,           %% 下一级成就Id
	reward   = [],          %% 奖励
	exp      = 0,           %% 成就经验值
	client_skip = {}        %% 客户端跳转
}).

%% 成就分类排序表
-record(achievement_seq_cfg, {
    type      = 0           %% 成就大类
    ,sub_type = 0           %% 成就小类
    ,seq      = 0           %% 顺序
}).

%% 成就基础配置表
-record(achievement_base_cfg, {
    category = 0,           %% 成就章节
    sub_category = 0,       %% 成就顺序
    type = 0,               %% 成就分类(前端展示)
    name = "",
    desc = "",
    is_hide = 0,
    is_inherit = 0,
    show_progress = 0,
    condition = 0,
    reward = [],
    exp = 0,               %% 成就经验值
    client_skip = {}       %% 客户端跳转
}).

%% 成就等级配置表
-record(achievement_stage_cfg, {
    stage = 0,
    need_exp = 0,
    attr_list = []
}).

%% 成就等级特权配置表
-record(stage_privilege_cfg, {
    stage = 0,
    name = "",
    desc = "",
    content = [],
    reward = [],
    icon = 0            %% 前端展示icon
}).

%% 成就常量配置表
-record(achievement_kv_cfg, {
    key    = "",        %% 键
    value  = "",        %% 值
    desc   = ""         %% 描述
}).

%% 成就相关
-define(insert_achievement,
    <<"replace into `achievement`(`role_id`, `category`, `sub_category`, `progress`, `status`, `time`) values(~p, ~p, ~p, ~p, ~p, ~p)">>).
-define(select_achievement,
    <<"select `category`, `sub_category`, `progress`, `status` from `achievement` where `role_id` = ~p">>).
-define(update_achievement_progress,
    <<"update `achievement` set `progress` = ~p, `status` = ~p, `time` = ~p where `role_id` = ~p and `category` = ~p and `sub_category` = ~p">>).
-define(update_achievement_status,
    <<"update `achievement` set `status` = ~p, `time` = ~p where `role_id` = ~p and `category` = ~p and `sub_category` = ~p">>).

%% 章节奖励
-define(insert_achievement_category_reward,
    <<"replace into `achievement_category_reward` (`role_id`, `category`, `status`, `time`) values(~p, ~p, ~p, ~p)">>).
-define(select_achievement_category_reward,
    <<"select `category`, `status` from `achievement_category_reward` where `role_id` = ~p">>).


%% 成就等级
-define(insert_achievement_level,
    <<"replace into `achievement_lv`(`role_id`, `lv`, `exp`, `lv_reward_list`) values(~p, ~p, ~p, '~ts')">>).
-define(select_achievement_level,
    <<"select `lv`, `exp`, `lv_reward_list` from `achievement_lv` where `role_id` = ~p limit 1">>).
-define(update_achievement_level,
    <<"update `achievement_lv` set `lv` = ~p, `exp` = ~p where `role_id` = ~p">>).
-define(update_achievement_lv_reward_status,
    <<"update `achievement_lv` set `lv_reward_list` = '~ts' where `role_id` = ~p">>).