%%%===================================================================
%%% @author zmh
%%% @doc 材料上交/联合BUFF
%%% 注意 本活动单开

-module(custom_unite_buff).
-include("server.hrl").
-include("predefine.hrl").
-include("def_goods.hrl").
-include("scene.hrl").
-include("goods.hrl").
-include("dungeon.hrl").
-include("custom_act.hrl").
-include("custom_unite_buff.hrl").
-include("def_module.hrl").
-include("rec_event.hrl").
-include("def_event.hrl").
-include("common.hrl").
-include("errcode.hrl").
-include("figure.hrl").
-include("activitycalen.hrl").
-include("battle.hrl").
-include("drop.hrl").

-compile(export_all).

-export([
	handle_event/2,      %%事件处理
	get_unite_buff/2,
	submit_items/4,      %%提交
	fetch_awards/4,      %%领取
	check_discount/2,    %%返回折扣
	check_buff/2,        %%判断BUFF是否在
	check_times/2,       %%判断并返回可用次数
	check_times/3,
	cost_times/2,        %%扣取次数
	cost_times/3,
	cost_times/4,
	battle_buffs/1,       %%战斗设计buff
	get_buffs/1,
	sync_unite_buff/5,    %%同步buff
	notify_other_module/5,
	calc_buff_hurt/3,     %%伤害BUFF

	get_capsule_disc/2, %%获得龙神宝库折扣
	get_mult_drop/3, %%查看个人幻魔/次元幻魔掉落
	calc_drop_ratio/1, %%掉落权重校正
	get_unite_num/2  %%获得材料和宠物副本次数
]).


handle_event(Status,#event_callback{type_id=?EVENT_LOGIN_CAST})->
	#player_status{id=RoleID}=Status,
	Type = ?CUSTOM_ACT_TYPE_UNITE_BUFF,
	SubTypes = lib_custom_act_api:get_open_subtype_ids(Type),
	Now = utime:unixtime(),
	init_unite_data(RoleID,Type,SubTypes,Now),
	{ok,Status};
handle_event(Status, _) ->
    {ok, Status}.

init_unite_data(_RoleID,_Type,[],_Now)->
	ok;
init_unite_data(RoleID,Type,[H|T],Now)->
	CustomActData = lib_custom_act_data:custom_act_data(Type,H),
    #custom_act_data{act_data = ActData} = CustomActData,
    case ActData of
        #act_unite_buff{buffs=OBuffs}->
        	Buffs = load_act_buffs(H),
            NBuffs = update_buff(Buffs,OBuffs),
            NActData = ActData#act_unite_buff{buffs=NBuffs};
        _->
        	NBuffs = load_act_buffs(H),
        	NActData = #act_unite_buff{buffs=NBuffs}
    end,
    NBuffs /= [] andalso lib_custom_act_data:save_custom_data(RoleID,#custom_act_data{type=Type,subtype=H,time=Now,act_data=NActData}),
    init_unite_data(RoleID,Type,T,Now).

load_act_buffs(SubType)->
	Sql = io_lib:format(?SQL_UNITE_ACT_BUFFS_GET, [SubType]),
	case db:get_row(Sql) of
		[Buffs0]->
			Buffs1 = util:to_term(Buffs0),
			Buffs = [#unite_buff{dot=Dot,bid=Bid,stime=STime,etime=ETime}||{Dot,Bid,STime,ETime}<-Buffs1],
			Buffs;
		_->
			[]
	end.


%% ---------------------------------------------------------------------------
%% @doc 活动派发接口：定制活动开启或者关闭 默认处理
notify_other_module(?CUSTOM_ACT_STATUS_OPEN,_IsKfActType,ActInfo, _Stime, _Etime) ->
    lib_custom_act:broadcast_act_info(?CUSTOM_ACT_STATUS_OPEN, [ActInfo]);
notify_other_module(ActStatus,_IsKfActType, #act_info{key = {_ActType, SubType}} = ActInfo,_Stime, _Etime)
    when ActStatus =:= ?CUSTOM_ACT_STATUS_CLOSE
    orelse ActStatus =:= ?CUSTOM_ACT_STATUS_MANUAL_CLOSE ->
	    lib_custom_act:broadcast_act_info(ActStatus, [ActInfo]),
	    % %% 活动结束清理数据的才需要
	    Sql = io_lib:format(<<"delete from `unite_buff_mgr` where `act`=~p">>,[SubType]),
	    db:execute(Sql),
	    %% 玩家身上清除活动数据
	    mod_unite_buff_mgr:cast({close_act,SubType});
notify_other_module(_, _, _, _, _) -> ok.


%%获得材料和宠物副本次数
get_unite_num(Status,DunType)->
	case lists:member(DunType,[?DUNGEON_TYPE_MATERIAL,?DUNGEON_TYPE_PET]) of
		true->
			check_times(Status,dungeon_refresh);
		_->
			0
	end.

%%联合BUFF对伤害造成影响
%%buff只有进入次元/个人才有
calc_buff_hurt(Aer,Der,Hurt)->
	#battle_status{sign=ASign,unite_buffs=AUniteBuffs}=Aer,
	#battle_status{unite_buffs=DUniteBuffs}=Der,
	AIsMon = ASign =:= ?BATTLE_SIGN_MON,
	UniteBuffs = case AIsMon of
		true->
			DUniteBuffs;
		_->
			AUniteBuffs
	end,
	Ratio = maps:get(boss_buff,UniteBuffs,0),
	NHurt = case Ratio>=1 of
		true ->
		   case AIsMon of
				true->
					util:ceil(Hurt*(1-Ratio/100));
				_->
					util:ceil(Hurt*(1+Ratio/100))
			end;
		_->
			Hurt
	end,
	NHurt.



%%龙神是否折扣
get_capsule_disc(_Status,1)->
	0;
%%50连不享受折扣
get_capsule_disc(_Status,50)->
	0;
get_capsule_disc(Status,Times)->
	check_times(Status,capsule_egg_discount,Times).


%%查看个人幻魔/次元幻魔掉落
get_mult_drop(Status,DunId,Scene)->
	case DunId>0 of
		true->%个人幻魔检测副本
			case data_dungeon:get_dun(DunId) of
        		#dun_cfg{type = ?DUNGEON_TYPE_PER_BOSS}->
        			case check_buff(Status,extra_drop) of
						{ok,Num}->
							Num;
						_->
							0
					end;
				_->
					0
			end;
		_-> %%次元幻魔 混乱星域幻魔检测场景
			case data_scene:get(Scene) of
		        #ets_scene{type=SceneT}->
		            case lists:member(SceneT,[?SCENE_TYPE_REWARD_BOSS,?SCENE_TYPE_DESERTED_BOSS]) of
		            	true->
		            		case check_buff(Status,extra_drop) of
								{ok,Num}->
									Num;
								_->
									0
							end;
						_->
							0
		        	end;
		        _ ->
		            0
		    end
    end.

get_double_key(DunId,Scene)->
	case DunId>0 of
		true->%个人幻魔检测副本
			case data_dungeon:get_dun(DunId) of
        		#dun_cfg{type = ?DUNGEON_TYPE_PER_BOSS}->
        			1;
				_->
					0
			end;
		_-> %%次元幻魔 混乱星域检测场景
			case data_scene:get(Scene) of
		        #ets_scene{type=?SCENE_TYPE_REWARD_BOSS}->
		        	2;
		        #ets_scene{type=?SCENE_TYPE_DESERTED_BOSS}->
		            3;
		        _ ->
		            0
		    end
    end.

calc_drop_ratio(Status)->
	#player_status{scene = Scene,dungeon = #status_dungeon{dun_id = DunId}}=Status,
	BffAdd = get_mult_drop(Status,DunId,Scene),
	Key = get_double_key(DunId,Scene),
	case BffAdd>0 of
		true when Key>0->
			Num = check_times(Status,extra_drop,Key),
			case Num>0 of
				true->
					cost_times(Status,extra_drop,Key),
					BffAdd;
				_->
					0
			end;
		_->
			0
	end.

do_fix_rule([],_BffAdd,Rules)->
	Rules;
do_fix_rule([{Round,Drops}|T],BffAdd,Rules)->
	NDrops = do_fix_drop(Drops,BffAdd,[]),
	do_fix_rule(T,BffAdd,[{Round,NDrops}|Rules]).

do_fix_drop([],_BffAdd,Drops)->
	Drops;
do_fix_drop([H|T],BffAdd,Drops)->
	#drop{weight = W}=H,
	NewW = min(round(W + W*BffAdd),?RATIO_MAX),
	NewH = H#drop{weight=NewW},
	do_fix_drop(T,BffAdd,[NewH|Drops]).

%%战斗相关buff收集 次元  次元
battle_buffs(Status)->
	#player_status{scene=Scene,dungeon = #status_dungeon{dun_id = DunId}}=Status,
	case data_dungeon:get_dun(DunId) of
    	#dun_cfg{type=DunT}->
    		skip;
    	_->
    		DunT=0
    end,
    case data_scene:get(Scene) of
		#ets_scene{type=SceneT}->
			skip;
		_->
			SceneT = 0
	end,
    IsPerDun = DunT=:=?DUNGEON_TYPE_PER_BOSS andalso SceneT =:= ?SCENE_TYPE_DUNGEON,
    IsBoss = lists:member(SceneT,[?SCENE_TYPE_REWARD_BOSS,?SCENE_TYPE_DESERTED_BOSS]),
    case IsPerDun orelse IsBoss of
    	true->
			Type = ?CUSTOM_ACT_TYPE_UNITE_BUFF,
			SubTypes = lib_custom_act_api:get_open_subtype_ids(Type),
			Mons = [?MON_REWARD_BOSS,?MON_DESERTED_BOSS],
			Now  = utime:unixtime(),
			Times = [{Key,check_times(SubTypes,Type,data_custom_unite_buff:get_buff_kind(boss_refresh),Now,Key)}||Key<-Mons],
			case check_buff(SubTypes,Type,data_custom_unite_buff:get_buff_kind(boss_buff),Now) of
				{ok,Ratio}->
					skip;
				_->
					Ratio = 0
			end,
			#{boss_refresh=>Times,boss_buff=>Ratio};
		_->
			#{}
	end.


%%注意  这个活动单开
get_buffs(_Status)->
	Type = ?CUSTOM_ACT_TYPE_UNITE_BUFF,
	SubTypes = lib_custom_act_api:get_open_subtype_ids(Type),
	Now  = utime:unixtime(),
	get_buffs(SubTypes,Type,Now).

get_buffs([],_,_Now)->
	[];
get_buffs([H|_T],Type,Now)->
	RUniteBuff = get_unite_buff(Type,H),
	#act_unite_buff{buffs=Buffs}=RUniteBuff,
	UseBuffs = [Buf||Buf<-Buffs,Buf#unite_buff.etime>Now],
	UseBuffs.

%%14 21需要拿折扣
check_discount(Status,Buff)->
	case check_buff(Status,Buff) of
		{ok,Dis}->
			Dis/100;
		_->
			1
	end.

%%判断BUFF
check_buff(_Status,Buff)->
	Kind = data_custom_unite_buff:get_buff_kind(Buff),
	Type = ?CUSTOM_ACT_TYPE_UNITE_BUFF,
	SubTypes = lib_custom_act_api:get_open_subtype_ids(Type),
	Now  = utime:unixtime(),
	check_buff(SubTypes,Type,Kind,Now).

check_buff([],_Type,_Kind,_Now)->
	false;
check_buff([H|T],Type,Kind,Now)->
	RUniteBuff = get_unite_buff(Type,H),
	#act_unite_buff{buffs=Buffs}=RUniteBuff,
	UseBuffs = check_filter_buff(Buffs,Kind,Now,[]),
	case length(UseBuffs)=<0 of
		true->
			check_buff(T,Type,Kind,Now);
		_->
			{ok,lists:max(UseBuffs)}
	end.

check_filter_buff([],_Kind,_Now,Buffs)->
	Buffs;
check_filter_buff([H|T],Kind,Now,Buffs)->
	#unite_buff{bid=Bid,etime=ETime}=H,
	case data_custom_unite_buff:get_buff(Bid) of
		#base_unite_buff{kind=Kind,discount=DisC,data=Data} when Now<ETime->
			case lists:member(Kind,[5,14,21]) of
				true->
					check_filter_buff(T,Kind,Now,[DisC|Buffs]);
				_->
					check_filter_buff(T,Kind,Now,[Data|Buffs])
			end;
		_->
			check_filter_buff(T,Kind,Now,Buffs)
	end.


%%次数获取
check_times(_Status,Buff)->
	check_times(_Status,Buff,1).
check_times(_Status,Buff,Key)->
	Kind = data_custom_unite_buff:get_buff_kind(Buff),
	Type = ?CUSTOM_ACT_TYPE_UNITE_BUFF,
	Now  = utime:unixtime(),
	SubTypes = lib_custom_act_api:get_open_subtype_ids(Type),
	check_times(SubTypes,Type,Kind,Now,Key).

check_times([],_Type,_Kind,_Now,_Key)->
	0;
check_times([H|_T],Type,Kind,Now,Key)->
	RUniteBuff = get_unite_buff(Type,H),
	#act_unite_buff{buffs=Buffs}=RUniteBuff,
	Num = check_times_buff(Buffs,Kind,Now,Key,0),
	Num.

check_times_buff([],_Kind,_Now,_Key,Num)->
	Num;
check_times_buff([H|T],Kind,Now,Key,Num)->
	#unite_buff{bid=Bid,etime=ETime,used=UsedList}=H,
	case lists:keyfind(Key,1,UsedList) of
		{_,Used}->
			skip;
		_->
			Used = 0
	end,
	case data_custom_unite_buff:get_buff(Bid) of
		#base_unite_buff{kind=Kind,data=Max} when Now<ETime->
			check_times_buff(T,Kind,Now,Key,Num+Max-Used);
		_->
			check_times_buff(T,Kind,Now,Key,Num)
	end.

%%需要同步场景扣除
scene_cost_times(Player,MapKey,Key)->
	mod_scene_agent:update(Player,[{unite_buff_cost,[MapKey,Key]}]).

scene_cost(UniteBuffs,[MapKey,Key])->
	Datas = maps:get(MapKey,UniteBuffs,[]),
	NValue = case lists:keyfind(Key,1,Datas) of
		{_,Value}->
			max(Value-1,0);
		_->
			0
	end,
	NDatas = lists:keystore(Key,1,Datas,{Key,NValue}),
	maps:put(MapKey,NDatas,UniteBuffs).


%%扣次数
cost_times(Player,Buff)->
	cost_times(Player,Buff,1).
cost_times(Player,Buff,Key)->
	cost_times(Player,Buff,Key,1).
cost_times(Player,Buff,Key,Add)->
	#player_status{id=RoleID}=Player,
	Kind = data_custom_unite_buff:get_buff_kind(Buff),
	Type = ?CUSTOM_ACT_TYPE_UNITE_BUFF,
	SubTypes = lib_custom_act_api:get_open_subtype_ids(Type),
	Now = utime:unixtime(),
	cost_times(RoleID,SubTypes,Type,Kind,Now,Key,Add).

cost_times(_RoleID,[],_Type,_Kind,_Now,_Key,_Add)->
	0;
cost_times(RoleID,[H|T],Type,Kind,Now,Key,Add)->
	RUniteBuff = get_unite_buff(Type,H),
	#act_unite_buff{buffs=Buffs}=RUniteBuff,
	{Buf,NBuffs} = cost_times_buff(Buffs,Kind,Key,[],Add),
	case Buf /=0  of
		true->
			NRUniteBuff = RUniteBuff#act_unite_buff{buffs=NBuffs},
			#unite_buff{
				dot = Dot,
				bid = Bid,
				stime = STime,
				etime = ETime,
				used = Used
			}=Buf,
			{ok,Bin} = pt_141:write(14105,[H,Dot,Bid,STime,ETime,Used]),
			lib_server_send:send_to_uid(RoleID,Bin),
			lib_custom_act_data:save_custom_data(RoleID,#custom_act_data{type=Type,subtype=H,time=Now,act_data=NRUniteBuff});
		_->
			cost_times(RoleID,T,Type,Kind,Now,Key,Add)
	end.

cost_times_buff([],_Kind,_Key,Buffs,_Add)->
	{0,Buffs};
cost_times_buff([H|T],Kind,Key,Buffs,Add)->
	#unite_buff{bid=Bid,used=UsedList}=H,
	case lists:keyfind(Key,1,UsedList) of
		{_,Used}->
			skip;
		_->
			Used = 0
	end,
	case data_custom_unite_buff:get_buff(Bid) of
		#base_unite_buff{kind=Kind,data=Max} when Used<Max->
			NUsedList = lists:keystore(Key,1,UsedList,{Key,min(Used+Add,Max)}),
			NewH = H#unite_buff{used=NUsedList},
			{NewH,[NewH|Buffs]++T};
		_->
			cost_times_buff(T,Kind,Key,[H|Buffs],Add)
	end.


%%拿个人数据
get_unite_buff(Type,SubType)->
	CustomActData = lib_custom_act_data:custom_act_data(Type,SubType),
    #custom_act_data{act_data = ActData} = CustomActData,
    case ActData of
        #act_unite_buff{}->
            ActData;
        _->
            #act_unite_buff{}
    end.

%%buff同步
sync_unite_buff(Status,SubType,Now,Buffs,Bin)->
	#player_status{id=RoleID}=Status,
	Type = ?CUSTOM_ACT_TYPE_UNITE_BUFF,
	RUniteBuff = get_unite_buff(Type,SubType),
	#act_unite_buff{buffs=OBuffs}=RUniteBuff,
	NBuffs = update_buff(Buffs,OBuffs),
    NewRUniteBuff = RUniteBuff#act_unite_buff{buffs=NBuffs},
	lib_custom_act_data:save_custom_data(RoleID,#custom_act_data{type=Type,subtype=SubType,time=Now,act_data=NewRUniteBuff}),
	lib_server_send:send_to_uid(RoleID,Bin),
    Status.

%%防止更新掉使用次数
update_buff([],Buffs)->
	Buffs;
update_buff([H|T],OBuffs)->
	#unite_buff{dot=Dot,bid=Bid,stime=STime,etime=ETime}=H,
	case lists:keytake(Dot,#unite_buff.dot,OBuffs) of
		{value,Buf,Tail}->
			update_buff(T,[Buf#unite_buff{bid=Bid,stime=STime,etime=ETime}|Tail]);
		_->
			update_buff(T,[H|OBuffs])
	end.


%%提交材料
submit_items(SubType,OpTy,Num,Status)->
	#player_status{id=RoleID,figure=#figure{lv=RoleLv},role_liveness=#role_liveness{olive=Live}}=Status,
	Type = ?CUSTOM_ACT_TYPE_UNITE_BUFF,
	{_,NeedLv} = lib_custom_act_util:keyfind_act_condition(Type,SubType,role_lv),
	case OpTy of
		0->
			{_,CostI} = lib_custom_act_util:keyfind_act_condition(Type,SubType,cost_type),
			%{_,CostN} = lib_custom_act_util:keyfind_act_condition(Type,SubType,group),
			Costs = [{?TYPE_GOODS,CostI,Num}],
			NewAdd = Num;
		_->
			{_,CostI} = lib_custom_act_util:keyfind_act_condition(Type,SubType,cost),
			Costs = [{?TYPE_GOLD,?GOODS_ID_GOLD,CostI*Num}],
			NewAdd = Num
	end,

	{_,Suit}  =  lib_custom_act_util:keyfind_act_condition(Type,SubType,suit),
	CKlist = [
		{check_open,Type,SubType},
		{check_lv,NeedLv,RoleLv},
		{check_cost,Costs,Status}
	],
	case util:check_lists(?MODULE,CKlist) of
		{ok,NStatus}->
			#act_info{stime=STime} = lib_custom_act_util:get_custom_act_open_info(Type,SubType),
			Day = util:get_open_day(STime),
			#base_unite_buff_ctrl{
				awards=AwardWList
			}=data_custom_unite_buff:get_ctrl_cfg(Suit,Day),
			Awards = calc_awards(AwardWList,NewAdd,[]),
			Now = utime:unixtime(),
			RUniteBuff = get_unite_buff(Type,SubType),
			#act_unite_buff{process=ORolePro}=RUniteBuff,
			NRolePro = ORolePro+NewAdd,
			NewRUniteBuff = RUniteBuff#act_unite_buff{process=NRolePro},
			{_,{_,Min,_}}=utime:unixtime_to_localtime(Now),
			Zone = Min div ?CALC_ZONE,
			mod_unite_buff_mgr:cast({submit_items,SubType,Suit,Zone,RoleID,Live,NRolePro,NewAdd,Now}),
			lib_custom_act_data:save_custom_data(RoleID,#custom_act_data{type=Type,subtype=SubType,time=Now,act_data=NewRUniteBuff}),
			Produce = #produce{type=custom_unite_buff,reward=Awards},
            lib_goods_api:send_reward_with_mail(RoleID, Produce, ?MOD_UNITE_BUFF),
			NStatus;
		{false,Res}->
			{ok,Bin} = pt_141:write(14101,[Res,0,0,0]),
			lib_server_send:send_to_uid(RoleID,Bin),
			Status
	end.

calc_awards(_AwardWList,0,Awards0)->
	lib_goods_util:merge_same_goods(Awards0);
calc_awards(AwardWList,Num,Awards)->
	Award = urand:rand_with_weight(AwardWList),
	calc_awards(AwardWList,Num-1,[Award|Awards]).

fetch_awards(SubType,0,Dot,Status)->
	#player_status{id=RoleID}=Status,
	Type = ?CUSTOM_ACT_TYPE_UNITE_BUFF,
	RUniteBuff = get_unite_buff(Type,SubType),
	#act_unite_buff{
		process=Pro,
		role_awards=RoleDones
	}=RUniteBuff,
	ReCfg = lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, Dot),
	CKlist = [
		{check_cfg,ReCfg,custom_act_reward_cfg},
		{check_open,Type,SubType},
		{check_repeate,Dot,RoleDones},
		{check_role_pro,Pro,ReCfg}
	],
	case util:check_lists(?MODULE,CKlist) of
		true->
			Now = utime:unixtime(),
			#custom_act_reward_cfg{reward=Awards}=ReCfg,
			Produce = #produce{type=custom_unite_buff,reward=Awards},
            lib_goods_api:send_reward_with_mail(RoleID, Produce, ?MOD_UNITE_BUFF),
            NewRoleDones = [Dot|RoleDones],
            NewRUniteBuff = RUniteBuff#act_unite_buff{role_awards=NewRoleDones},
            lib_custom_act_data:save_custom_data(RoleID,#custom_act_data{type=Type,subtype=SubType,time=Now,act_data=NewRUniteBuff}),
			Res = ?SUCCESS;
		{false,Res}->
			skip
	end,
	{ok,Bin} = pt_141:write(14102,[Res,SubType,0,Dot]),
	lib_server_send:send_to_uid(RoleID,Bin),
	ok;
fetch_awards(SubType,OpTy,Dot,Status)->
	#player_status{id=RoleID}=Status,
	Type = ?CUSTOM_ACT_TYPE_UNITE_BUFF,
	RUniteBuff = get_unite_buff(Type,SubType),
	SerPro = mod_unite_buff_mgr:get_server_pro(SubType),
	{_,Suit}  =  lib_custom_act_util:keyfind_act_condition(Type,SubType,suit),
	ReCfg = data_custom_unite_buff:get_role_dot_awards(Suit,Dot),
	#act_unite_buff{
		server_awards=SerDones
	}=RUniteBuff,
	CKlist = [
		{check_cfg,ReCfg,base_unite_buff_awards},
		{check_open,Type,SubType},
		{check_repeate,Dot,SerDones},
		{check_server_pro,SerPro,ReCfg}
	],
	case util:check_lists(?MODULE,CKlist) of
		{ok,Awards}->
			Now = utime:unixtime(),
			Produce = #produce{type=custom_unite_buff,reward=Awards},
            lib_goods_api:send_reward_with_mail(RoleID, Produce, ?MOD_UNITE_BUFF),
            NewSerDones = [Dot|SerDones],
            NewRUniteBuff = RUniteBuff#act_unite_buff{server_awards=NewSerDones},
            lib_custom_act_data:save_custom_data(RoleID,#custom_act_data{type=Type,subtype=SubType,time=Now,act_data=NewRUniteBuff}),
			Res = ?SUCCESS;
		{false,Res}->
			skip
	end,
	{ok,Bin} = pt_141:write(14102,[Res,SubType,OpTy,Dot]),
	lib_server_send:send_to_uid(RoleID,Bin),
	ok.


check({check_role_pro,Pro,ReCfg})->
	#custom_act_reward_cfg{condition=Cons}=ReCfg,
	case lists:keyfind(submit,1,Cons) of
		{_,Need} when Pro>=Need->
			true;
		_->
			{false,?ERRCODE(err331_process_unreached)}
	end;
check({check_server_pro,Pro,ReCfg})->
	#base_unite_buff_awards{type=Type,need=Need,awards=AwardsCfg}=ReCfg,
	case lists:member(Type,[2,3]) of
		true->
			case Pro>=Need of
				true->
					case Type of
						2->
							{ok,AwardsCfg};
						_->
							[_,Awards]=AwardsCfg,
							{ok,Awards}
					end;
				_->
					{false,?ERRCODE(err331_process_unreached)}
			end;
		_->
			{false,?ERRCODE(err331_great_reward_type_err)}
	end;
check({check_repeate,Dot,Dones})->
	case lists:member(Dot,Dones) of
		true->
			{false,?ERRCODE(err333_already_get_reward)};
		_->
			true
	end;
check({check_cfg,Config,Atom})->
	case is_record(Config,Atom) of
		true->
			true;
		_->
			{false,?ERRCODE(missing_config)}
	end;
check({check_lv,NeedLv,RoleLv})->
	case RoleLv>=NeedLv of
		true->
			true;
		_->
			{false,?ERRCODE(err331_lv_unreach)}
	end;
check({check_cost,Costs,Status})->
	case lib_goods_api:cost_object_list_with_check(Status, Costs, custom_unite_buff, ["custom unite buff"]) of
        {true,NStatus,_}->
            {ok,NStatus};
		{false,Err,_}->
    		{false,Err}
    end;
check({check_open,Type,SubType})->
    case lib_custom_act_api:is_open_act(Type,SubType) of
        true->
            true;
        _->
            {false,?ERRCODE(err331_act_closed)}
    end.

