%% ---------------------------------------------------------------------------
%% @doc 循环进阶
%% @author zmh
%% @since  20200722
%% ---------------------------------------------------------------------------
-module(custom_act_circle_foster).

-export([
     ask_reward_status/3            %% 拿33104
     ,receive_reward/4              %% 领取
     ,auto_receive_reward/3
     ,init_circle_foster_data/2
     ,notify_other_module/5
     ,handle_event/2
     ,clear_circle_foster_data/3
     ,combat_change_event/2
     ,gm_test/3
]).

-compile(export_all).

-include("custom_act.hrl").
-include("common.hrl").
-include("server.hrl").
-include("figure.hrl").
-include("predefine.hrl").
-include("errcode.hrl").
-include("def_module.hrl").
-include("def_event.hrl").
-include("rec_event.hrl").
-include("role_foster.hrl").
-include("warsoul.hrl").
-include("rush_rank.hrl").
-include("def_fun.hrl").


%% 奖励状态
-define(AWARD_ACTIVATED,           0).         %% 已激活任务
-define(AWARD_CAN_GET,             1).         %% 可以领取
-define(AWARD_HAS_GET,             2).         %% 已经领取
-define(AWARD_UNACTIVATED,         3).         %% 未激活

%% ---------------------------------------------------------------------------
%% @doc 获取角色循环进阶数据
%% ---------------------------------------------------------------------------
circle_foster_data(_Player, Type, SubType) ->
    #custom_act_data{
        act_data = Data
    } = lib_custom_act_data:custom_act_data(Type, SubType),
    Data.


ask_reward_status(Player,Type,SubType)->
    #player_status{sid=Sid}=Player,
    AwardInfos0 = circle_foster_data(Player, Type, SubType),
    All = lib_custom_act_util:get_act_reward_grade_list(Type, SubType),
    AwardInfos = to_client_award(All,Type,SubType,AwardInfos0,[]),
    {ok,Bin} = pt_331:write(33104,[Type,SubType,AwardInfos]),
    lib_server_send:send_to_sid(Sid,Bin),
    {ok,Player}.

to_client_award([], _Type, _SubType,_,NAwardInfos)->
    NAwardInfos;
to_client_award([H|T],Type, SubType,AwardInfos0,NAwardInfos)->
    case lib_custom_act_util:get_act_reward_cfg_info(Type,SubType,H) of
        #custom_act_reward_cfg{
            name = Name,
            desc = Desc,
            condition = Condition,
            format = Format,
            reward = Rewards
        }->
            case lists:keyfind(H,#custom_cell_foster.award_id,AwardInfos0) of
                #custom_cell_foster{process=CurV,status=CurS}->
                    skip;
                _->
                    CurV = 0,CurS=0
            end,
            ConditionStr = util:term_to_string(Condition),
            RewardStr = util:term_to_string(Rewards),
            Data = {H,Format,CurV,CurS,0,Name,Desc,ConditionStr,RewardStr},
            to_client_award(T,Type,SubType,AwardInfos0,[Data|NAwardInfos]);
        _->
            to_client_award(T,Type,SubType,AwardInfos0,NAwardInfos)
    end.


%% ---------------------------------------------------------------------------
%% @doc 奖励领取状态判断
award_status(Player = #player_status{}, AwardCfg) ->
    #custom_act_reward_cfg{type = Type, subtype = SubType} = AwardCfg,
    AwardList = circle_foster_data(Player, Type, SubType),
    award_status(AwardList, AwardCfg);
award_status(AwardList, AwardCfg) ->
    #custom_act_reward_cfg{grade = AwardId} = AwardCfg,
    case lists:keyfind(AwardId, #custom_cell_foster.award_id, AwardList) of
        #custom_cell_foster{status = Status} ->
            Status;
        _ ->
            ?AWARD_UNACTIVATED
    end.


%% ---------------------------------------------------------------------------
%% @doc 领取活动奖励
receive_reward(Player, Type, SubType, AwardId) ->
    Result =
        case check_receive_common(Player, Type, SubType) of
            true  ->
                AwardList = circle_foster_data(Player, Type, SubType),
                case check_receive_grade(Player, Type, SubType, AwardList, AwardId) of
                    {true, RewardListThen, #custom_act_reward_cfg{reward = Reward} = CfgThen} ->
                        case lib_goods_api:can_give_goods(Player, Reward) of
                            true  ->
                                {ok, RewardListThen, CfgThen};
                            _ ->
                                {false, ?ERRCODE(err150_no_cell)}
                        end;
                    Other ->
                        Other
                end;
            Other ->
                Other
        end,
    case Result of
        {false, Reason} ->
            {ok, Bin} = pt_331:write(33100, [Reason]),
            lib_server_send:send_to_sid(Player#player_status.sid, Bin),
            {ok, Player};
        {ok, RewardList, RewardCfg} ->
            ActData = #custom_act_data{
                type = Type, subtype = SubType, act_data = RewardList
            },
            lib_custom_act_data:save_custom_data(Player#player_status.id, ActData),
            Role2 = lib_custom_act:custom_act_send_reward(Player, RewardCfg),
            log_foster_loop(Role2,Type,SubType,AwardId,RewardList,RewardCfg),
            {ok, Bin} = pt_331:write(33105, [?SUCCESS, Type, SubType, AwardId]),
            lib_server_send:send_to_sid(Player#player_status.sid, Bin),
            {ok, Role2}
    end.

%% @doc 一键领取
auto_receive_reward(Player, Type, SubType) ->
    case check_receive_common(Player, Type, SubType) of
        true  ->
            AllGrades = lib_custom_act_util:get_act_reward_grade_list(Type, SubType),
            AwardList = circle_foster_data(Player, Type, SubType),
            {NewRewardList, AllRewards} = lists:foldl(
                fun(GradeId, {AwardListTemp, RewardList}) ->
                    case check_receive_grade(Player, Type, SubType, AwardListTemp, GradeId) of
                        {true, AwardListThen, RewardCfg} ->
                            {AwardListThen, [RewardCfg|RewardList]};
                        _Other ->
                            {AwardListTemp, RewardList}
                    end
                end, {AwardList, []}, AllGrades
            ),
            case AllRewards =/= [] of
                true  ->
                    SendRewards = lists:foldl(
                        fun(#custom_act_reward_cfg{reward = Reward}, Temp) ->
                            Reward ++ Temp
                        end, [], AllRewards
                    ),
                    case lib_goods_api:can_give_goods(Player, SendRewards) of
                        true  ->
                            ActData = #custom_act_data{
                                type = Type, subtype = SubType, act_data = NewRewardList
                            },
                            lib_custom_act_data:save_custom_data(Player#player_status.id, ActData),
                            Player1 = lib_goods_api:send_reward(Player, SendRewards, custom_act, 0, "auto"),
                            [
                                log_foster_loop(Player1,Type,SubType,RewardCfg#custom_act_reward_cfg.grade,NewRewardList,RewardCfg)
                                || RewardCfg <- AllRewards
                            ],
                            {true, Player1};
                        _ ->
                            {false, ?ERRCODE(err150_no_cell)}
                    end;
                false ->
                    {false, ?ERRCODE(err333_can_not_get)}
            end;
        Other ->
            Other
    end.

check_receive_common(Player, Type, SubType) ->
    Now = utime:unixtime(),
    ActInfo = lib_custom_act_util:get_custom_act_open_info(Type, SubType),
    NeedLev = lib_module:get_open_lv(?MOD_AC_CUSTOM, Type),
    #player_status{figure = #figure{lv = RoleLev}} = Player,
    if
        not is_record(ActInfo, act_info) ->
            {false, ?ERRCODE(err331_act_closed)};
        ActInfo#act_info.etime < Now orelse Now < ActInfo#act_info.stime  ->
            {false, ?ERRCODE(err331_act_closed)};
        NeedLev > RoleLev ->
            {false, ?ERRCODE(err331_act_closed)};
        true ->
            true
    end.

check_receive_grade(_Player, Type, SubType, AwardList, AwardId) ->
    RewardCfg = lib_custom_act_util:get_act_reward_cfg_info( Type, SubType, AwardId),
    {
        AwardInfo, AwardListRest
    } = case lists:keytake(AwardId, #custom_cell_foster.award_id, AwardList) of
            {value, Info, AwardListR} -> {Info, AwardListR};
            _ -> {#custom_cell_foster{award_id = AwardId}, AwardList}
        end,
    if
        not is_record(RewardCfg, custom_act_reward_cfg) ->
            {false, ?ERRCODE(err331_no_act_reward_cfg)};
        true ->
            Status = award_status(AwardList, RewardCfg),
            if
                Status =:= ?AWARD_HAS_GET ->
                    {false, ?ERRCODE(err331_already_get_reward)};
                Status =:= ?AWARD_ACTIVATED ->
                    {false, ?ERRCODE(err333_can_not_get)};
                Status =:= ?AWARD_UNACTIVATED ->
                    {false, ?ERRCODE(err333_can_not_get)};
                true ->
                    AwardInfo2 = AwardInfo#custom_cell_foster{status = ?AWARD_HAS_GET},
                    NAwardList = [AwardInfo2 | AwardListRest],
                    {true, NAwardList, RewardCfg}
            end
    end.

handle_event(Player, #event_callback{type_id = ?EVENT_LOGIN_CAST}) ->
    act_handle_login(Player),
    {ok, Player};
handle_event(Player, #event_callback{type_id = ?EVENT_LV_UP}) ->
    act_handle_login(Player),
    {ok, Player};
handle_event(Player, #event_callback{type_id = ?EVENT_FOSTER_GRADE_UP,data={Type,Grade}}) ->
    #player_status{figure = #figure{lv = RoleLev}} = Player,
    Star = 0,
    NeedLev = lib_module:get_open_lv(?MOD_AC_CUSTOM, ?CUSTOM_ACT_TYPE_CIRCLE_FOSTER),
    case RoleLev>= NeedLev of
        true->
            case lib_custom_act_util:get_open_subtype_list(?CUSTOM_ACT_TYPE_CIRCLE_FOSTER) of
                [] ->
                    skip;
                OpenActList ->
                    NowTime = utime:unixtime(),
                    act_handle_foster_grade_up(Player, OpenActList, NowTime, Type, Grade, Star)
            end;
        _->
            skip
    end,
    {ok, Player};
handle_event(Player,#event_callback{type_id = ?EVENT_FOSTER_GRADE_STAR_UP,data={Type,Grade,Star}})->
    #player_status{figure = #figure{lv = RoleLev}} = Player,
    NeedLev = lib_module:get_open_lv(?MOD_AC_CUSTOM, ?CUSTOM_ACT_TYPE_CIRCLE_FOSTER),
    case RoleLev>= NeedLev of
        true->
            case lib_custom_act_util:get_open_subtype_list(?CUSTOM_ACT_TYPE_CIRCLE_FOSTER) of
                [] ->
                    skip;
                OpenActList ->
                    NowTime = utime:unixtime(),
                    act_handle_foster_grade_up(Player, OpenActList, NowTime, Type, Grade, Star)
            end;
        _->
            skip
    end,
    {ok, Player};
handle_event(Player,#event_callback{type_id = ?EVENT_FOSTER_LV_UP,data={Type,Lv}})->
    #player_status{figure = #figure{lv = RoleLev}} = Player,
    NeedLev = lib_module:get_open_lv(?MOD_AC_CUSTOM, ?CUSTOM_ACT_TYPE_CIRCLE_FOSTER),
    case RoleLev>= NeedLev of
        true->
            case lib_custom_act_util:get_open_subtype_list(?CUSTOM_ACT_TYPE_CIRCLE_FOSTER) of
                [] ->
                    skip;
                OpenActList ->
                    NowTime = utime:unixtime(),
                    act_handle_foster_grade_up(Player, OpenActList, NowTime, Type,0,Lv)
            end;
        _->
            skip
    end,
    {ok, Player};
handle_event(Player, #event_callback{type_id = ?EVENT_COMBAT_POWER, data = CallbackData}) ->
    #callback_combat_power_data{
        old_combat_power = OldCombatPower,
        combat_power = CombatPower
    } = CallbackData,
    #player_status{figure = #figure{lv = RoleLev}} = Player,
    NeedLev = lib_module:get_open_lv(?MOD_AC_CUSTOM, ?CUSTOM_ACT_TYPE_CIRCLE_FOSTER),
    case RoleLev>= NeedLev andalso CombatPower > OldCombatPower of
        true->
            case lib_custom_act_util:get_open_subtype_list(?CUSTOM_ACT_TYPE_CIRCLE_FOSTER) of
                [] ->
                    skip;
                OpenActList ->
                    NowTime = utime:unixtime(),
                    act_handle_foster_grade_up(Player, OpenActList, NowTime, ?RANK_TYPE_RUSH_COMBAT, 0, CombatPower)
            end;
        _->
            skip
    end,
    {ok, Player};
% handle_event(Player, #event_callback{type_id = ?EVENT_ZERO_CLOCK}) ->
%     act_handle_login(Player),
%     {ok, Player};
handle_event(Player, _) ->
    {ok, Player}.

%% 战魂、红装战力发生变化
combat_change_event(Player, Type) ->
    #player_status{figure = #figure{lv = RoleLev}} = Player,
    NeedLev = lib_module:get_open_lv(?MOD_AC_CUSTOM, ?CUSTOM_ACT_TYPE_CIRCLE_FOSTER),
    case RoleLev>= NeedLev of
        true->
            case lib_custom_act_util:get_open_subtype_list(?CUSTOM_ACT_TYPE_CIRCLE_FOSTER) of
                [] ->
                    skip;
                OpenActList ->
                    NowTime = utime:unixtime(),
                    {Combat,_,_,_,_} = lib_rush_rank:get_sel_value(Player, Type),
                    act_handle_foster_grade_up(Player, OpenActList, NowTime, Type - ?OFFSET_TYPE, 0, Combat)
            end;
        _->
            skip
    end.

%% ---------------------------------------------------------------------------
%% @doc 活动派发接口：定制活动开启或者关闭 默认处理
notify_other_module(?CUSTOM_ACT_STATUS_OPEN,_IsKfActType,ActInfo, _Stime, _Etime) ->
    lib_custom_act:broadcast_act_info(?CUSTOM_ACT_STATUS_OPEN, [ActInfo]),
    case ActInfo of
        #act_info{trigger_time = T} when T=:=1 -> %新开活动的时候会置为1发给前端
            lib_player:cast_online_role_apply(?APPLY_CAST_STATUS, ?MODULE, init_circle_foster_data, [[ActInfo]]);
        _->
            ok
    end;
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]),
    case lib_custom_act_util:get_act_cfg_info(ActType, SubType) of
        #custom_act_cfg{clear_type = ?CUSTOM_ACT_CLEAR_CLOSE} ->
            %% 活动结束清理数据的才需要
            lib_custom_act_data:db_delete_custom_act_data(ActType, SubType),
            %% 玩家身上清除活动数据
            lib_player:cast_online_role_apply(
                ?APPLY_CAST_STATUS, ?MODULE, clear_circle_foster_data, [ActType, SubType]
            );
        _ ->
            ok
    end;
notify_other_module(_, _, _, _, _) -> ok.


%%登录事件
act_handle_login(Player)->
    #player_status{figure = #figure{lv = RoleLev}} = Player,
    NeedLev = lib_module:get_open_lv(?MOD_AC_CUSTOM, ?CUSTOM_ACT_TYPE_CIRCLE_FOSTER),
    case RoleLev>= NeedLev of
        true->
            case lib_custom_act_util:get_open_subtype_list(?CUSTOM_ACT_TYPE_CIRCLE_FOSTER) of
                [] ->
                    skip;
                OpenActList ->
                    NowTime = utime:unixtime(),
                    do_act_handle_login(Player, OpenActList, NowTime)
            end;
        _->
            skip
    end.

do_act_handle_login(_Player,[],_Now)->
    ok;
do_act_handle_login(Player, [H|T], Now)->
    #act_info{key = {Type, SubType}} = H,
    AwardList = circle_foster_data(Player, Type, SubType),
    case length(AwardList)>0 of
        true->
            skip; %%已有初始化数据
        _->
            init_circle_foster_data(Player,Now,[H])
    end,
    do_act_handle_login(Player,T,Now).

%%进阶事件
act_handle_foster_grade_up(_Player, [], _NowTime, _Type, _Grade,_Star)->
    ok;
act_handle_foster_grade_up(Player, [H|T], NowTime, Type, Grade, Star)->
    #act_info{key = {ActType, SubType}} = H,
    #custom_act_cfg{condition=Condition, name = Name}=lib_custom_act_util:get_act_cfg_info(ActType,SubType),
    Result =
        case lists:keyfind(foster, 1, Condition) of
            {foster, Type} ->
                true;
            _ ->
                case lists:keyfind(combat, 1, Condition) of
                    {combat, Type} ->
                        true;
                    _ ->
                    false
                end
        end,
    if
        Result ->
            #player_status{id=RoleID}=Player,
            AwardList = circle_foster_data(Player, ActType, SubType),
            All       = lists:reverse(lib_custom_act_util:get_act_reward_grade_list(ActType, SubType)),
            case lists:keyfind(refresh,1,Condition) of
                {_,IsRe}->
                    skip;
                _->
                    IsRe = 0
            end,
            NAwardList0 = do_act_handle_foster_grade_up(Type,Grade,Star,ActType,SubType,All,AwardList),
            {NAwardList, ReturnList}  = refresh_awards(AwardList, NAwardList0, ActType, SubType, Grade, Star,IsRe),
            ActData = #custom_act_data{
                type = ActType,
                subtype = SubType,
                time = NowTime,
                act_data = NAwardList
            },
            lib_custom_act_data:save_custom_data(RoleID, ActData),
            {ok,Player1} = ask_reward_status(Player,ActType,SubType),
            Player2 =
                case ReturnList =/= [] andalso lib_goods_api:can_give_goods(Player, ReturnList) of
                    true  ->
                        spawn(
                            fun() ->
                                timer:sleep(2000),
                                lib_chat:send_sys_error_code(Player1#player_status.sid, ?ERRCODE(err331_circle_foster_return), [Name])
                            end
                        ),
                        lib_goods_api:send_reward(Player1, ReturnList, circle_foster_return);
                    false when ReturnList =/= [] ->
                        ?ERR("circle_foster_return bag full [Role:~p] [award:~p]", [RoleID, ReturnList]),
                        Player1;
                    _ ->
                        Player1
                end,
            act_handle_foster_grade_up(Player2,T,NowTime,Type,Grade,Star);
        true ->
            act_handle_foster_grade_up(Player,T,NowTime,Type,Grade,Star)
    end.

do_act_handle_foster_grade_up(_Type,_Grade,_Star,_ActType,_SubType,[],AwardList)->
    AwardList;
do_act_handle_foster_grade_up(Type,Grade,Star,ActType,SubType,[H|T],AwardList)->
    case lib_custom_act_util:get_act_reward_cfg_info(ActType,SubType,H) of
         #custom_act_reward_cfg{condition=Condition}->
           {CellT,NeedG,NeedStar} = get_cond_data(Condition),
            case CellT =:= Type of
                true->
                    CurS = get_reward_state(Type, Grade, Star, NeedG, NeedStar),
                    Pro = case lists:member(CellT,?GRADE_FOSTER) of
                        true->
                            Grade*1000+Star;
                        _->
                            Star
                    end,
                    Award = #custom_cell_foster{
                        award_id = H,
                        process = Pro,
                        status = CurS
                    },
                    NAwardList = case lists:keyfind(H,#custom_cell_foster.award_id,AwardList) of
                        #custom_cell_foster{status=?AWARD_HAS_GET}->
                            AwardList;
                        _->
                            lists:keystore(H,#custom_cell_foster.award_id,AwardList,Award)
                    end,
                    do_act_handle_foster_grade_up(Type,Grade,Star,ActType,SubType,T,NAwardList);
                _->
                    do_act_handle_foster_grade_up(Type,Grade,Star,ActType,SubType,T,AwardList)
            end;
        _->
            do_act_handle_foster_grade_up(Type,Grade,Star,ActType,SubType,T,AwardList)
    end.

get_cond_data(Condition) ->
    case lists:keyfind(foster,1,Condition) of
        {_,CellT,NeedG,NeedStar}->
            skip;
        {_,CellT,NeedStar}->
            NeedG = 0,
            skip;
        _->
            case lists:keyfind(combat,1,Condition) of
                {_,CellT,NeedCombat}->
                    NeedG = 0, NeedStar = NeedCombat;
                _->
                    CellT = 0, NeedG = 999, NeedStar = 999
            end
    end,
    {CellT, NeedG, NeedStar}.

get_reward_state(Type, Grade, Star, NeedGrade, NeedStar) ->
    case lists:member(Type, ?RANK_TYPE_COMBAT_GRADE_LIST) of
        true ->
            if
                Star >= NeedStar ->
                    ?AWARD_CAN_GET;
                true ->
                    ?AWARD_ACTIVATED
            end;
        _ ->
            case lists:member(Type,?GRADE_FOSTER) of
                true->%%强进阶
                    case  Grade>NeedGrade orelse (Grade =:= NeedGrade andalso Star >= NeedStar) of
                        true  ->
                            ?AWARD_CAN_GET;
                        _->
                            ?AWARD_ACTIVATED
                    end;
                _-> %%弱进阶等级
                    case  Star >= NeedStar of
                        true  ->
                            ?AWARD_CAN_GET;
                        _->
                            ?AWARD_ACTIVATED
                    end
            end
    end.

%%在线数据清理
clear_circle_foster_data(Player,ActType, SubType)->
    lib_custom_act_data:clean_up_custom_data(Player, ActType, SubType).

%%数据初始化
init_circle_foster_data(Player,ActInfos)->
    init_circle_foster_data(Player,utime:unixtime(),ActInfos).

init_circle_foster_data(Player,_,[]) ->
    Player;
init_circle_foster_data(Player,Time,[ActInfo|T]) ->
    do_init_circle_foster_data(Player, Time, ActInfo),
    init_circle_foster_data(Player,Time,T).

do_init_circle_foster_data(Player,Time,ActInfo)->
    NeedLev = lib_module:get_open_lv(?MOD_AC_CUSTOM, ?CUSTOM_ACT_TYPE_CIRCLE_FOSTER),
    #player_status{id=RoleID,figure = #figure{lv = RoleLev}}=Player,
    #act_info{key = {Type, SubType}} = ActInfo,
    case lib_custom_act_util:get_act_cfg_info(Type, SubType) of
        #custom_act_cfg{condition = Condition} ->
            case lists:keyfind(foster,1,Condition) of
                {_,CellT}->
                    CellT;
                _->
                    case lists:keyfind(combat,1,Condition) of
                        {_,CellT}->
                            CellT;
                        _->
                            CellT = 0
                    end
            end;
        _ ->
            CellT = 0
    end,
    case RoleLev>= NeedLev of
        true when Type =/= 0 ->
            CellTppe = ?IF(CellT == (?RANK_TYPE_RUSH_WARSOUL-?OFFSET_TYPE) orelse CellT == (?RANK_TYPE_RUSH_RED_EQUIP-?OFFSET_TYPE), CellT+?OFFSET_TYPE, CellT),
            {CurG, CurStar} = get_data_by_type(Player, CellTppe),
            All = lib_custom_act_util:get_act_reward_grade_list(Type, SubType),
            AwardList = make_cell_fosters(All,Type,SubType,CurG,CurStar,[]),
            ActData = #custom_act_data{
                type = Type,
                subtype = SubType,
                time = Time,
                act_data = AwardList
            },
            lib_custom_act_data:save_custom_data(RoleID, ActData);
        _->
            skip
    end.

get_data_by_type(Player, Type) ->
    case lists:member(Type, ?RANK_TYPE_COMBAT_GRADE_LIST) of
        true ->
            {Combat,_,_,_,_} = lib_rush_rank:get_sel_value(Player, Type),
            {0, Combat};
        _ ->
            #player_status{role_foster=RoleFoster}=Player,
            #role_foster{fosters=CellFosters}=RoleFoster,
            case lists:keyfind(Type,#cell_foster.type,CellFosters) of
                #cell_foster{grade=CurG,star=CurStar,lv=Lv}->
                    case lists:member(Type,?GRADE_FOSTER) of
                        true->
                            {CurG, CurStar};
                        _->
                            {0,Lv}
                    end;
                _->
                    {0, 0}
            end
    end.

make_cell_fosters([],_,_,_,_,AwardList)->
    AwardList;
make_cell_fosters([H|T],Type,SubType,CurG,CurStar,AwardList)->
    case lib_custom_act_util:get_act_reward_cfg_info(Type,SubType,H) of
        #custom_act_reward_cfg{condition=Condition}->
            {CellT, NeedG, NeedStar} = get_cond_data(Condition),
            CurS = get_reward_state(CellT, CurG, CurStar, NeedG, NeedStar),
            Pro = case lists:member(CellT,?GRADE_FOSTER) of
                true->
                    CurG*1000+CurStar;
                _->
                    CurStar
            end,
            Award = #custom_cell_foster{
                award_id = H,
                process = Pro,
                status = CurS
            },
            make_cell_fosters(T,Type,SubType,CurG,CurStar,[Award|AwardList]);
        _->
            make_cell_fosters(T,Type,SubType,CurG,CurStar,AwardList)
    end.

gm_test(Type,SubType,Player)->
    All = lib_custom_act_util:get_act_reward_grade_list(Type, SubType),
    case lib_custom_act_util:get_act_cfg_info(Type, SubType) of
        #custom_act_cfg{condition = Condition} ->
            case lists:keyfind(foster,1,Condition) of
                {_,CellT}->
                    CellT;
                _->
                    case lists:keyfind(combat,1,Condition) of
                        {_,CellT}->
                            CellT;
                        _->
                            CellT = 0
                    end
            end;
        _ ->
            CellT = 0
    end,
    {CurG, CurStar} = get_data_by_type(Player, CellT),
    AwardList = make_cell_fosters(All,Type,SubType,CurG,CurStar,[]),
    ?INFO("----------------:~p~n",[AwardList]).



refresh_awards(AwardList, NAwardList, ActType, SubType, Grade, Star,IsRe) when IsRe>0->
    Len1 = length([S||#custom_cell_foster{status=S}<-AwardList,S>=?AWARD_CAN_GET]),
    Len2 = length([S||#custom_cell_foster{status=S}<-NAwardList,S>=?AWARD_CAN_GET]),
    case Len1 /= Len2 of
        true->
            do_refresh_awards(NAwardList, ActType, SubType, Grade, Star,[], []);
        _->
            {NAwardList, []}
    end;
refresh_awards(_AwardList, NAwardList, _ActType, _SubType, _Grade, _Star,0)->
    {NAwardList, []}.

do_refresh_awards([], _ActType, _SubType, _Grade, _Star, NAwardList, ReturnList)->
    {NAwardList, ReturnList};
do_refresh_awards([H|T], ActType, SubType, Grade, Star, NAwardList, ReturnList)->
    case H#custom_cell_foster.status of
        ?AWARD_HAS_GET->
            do_refresh_awards(T, ActType, SubType, Grade, Star,[H#custom_cell_foster{status=?AWARD_CAN_GET}|NAwardList], ReturnList);
        ?AWARD_CAN_GET ->
            NewReturnList = case lib_custom_act_util:get_act_reward_cfg_info(ActType, SubType, H#custom_cell_foster.award_id) of
                #custom_act_reward_cfg{
                    condition = Condition,
                    reward = RewardList
                } ->
                    case lists:keyfind(foster, 1, Condition) of
                        {foster, _, Grade, Star} ->
                            ReturnList;
                        {foster, _, Star} ->
                            ReturnList;
                        _ ->
                            if
                                Star == H#custom_cell_foster.process ->
                                    ReturnList;
                                true ->
                                    RewardList ++ ReturnList
                            end
                    end;
                _ ->
                    ReturnList
            end,
            do_refresh_awards(T, ActType, SubType, Grade, Star,[H#custom_cell_foster{status=?AWARD_CAN_GET}|NAwardList], NewReturnList);
        _->
            do_refresh_awards(T, ActType, SubType, Grade, Star,[H|NAwardList], ReturnList)
    end.

%%日志整理
log_foster_loop(Role,Type,SubType,AwardId,RewardList,RewardCfg)->
    #player_status{
        id=RoleID,
        role_foster=RoleFoster,
        figure = #figure{name=RName}
    }=Role,
    #custom_act_reward_cfg{condition = Cons} = RewardCfg,
    case lib_custom_act_util:get_act_reward_cfg_info( Type, SubType, AwardId+1) of
        #custom_act_reward_cfg{condition = NextCons}->
            skip;
        _->
            NextCons = []
    end,
    #role_foster{fosters=CellFosters}=RoleFoster,
    case lists:keyfind(foster,1,Cons) of
        {_,CellT,_NeedG,_NeedStar}->
            skip;
        {_,CellT,_NeedStar}->
            skip;
        _->
            CellT = 0
    end,
    case lists:keyfind(CellT,#cell_foster.type,CellFosters) of
        #cell_foster{grade=CurG,star=CurStar}->
            skip;
        _->
            CurG=0,CurStar=0
    end,
    lib_log_api:log_foster_loop(RoleID,RName,SubType,AwardId,Cons,CurG,CurStar,NextCons,RewardList).