%%%-------------------------------------------------------------------
%%% @author : tyl
%%% @doc     自选扭蛋
%%% Module  : select_capsule_egg.erl
%%% Created : 2021-06-01
%%% @end
%%%-------------------------------------------------------------------
-module(select_capsule_egg).

-include("select_capsule_egg.hrl").
-include("custom_act.hrl").
-include("server.hrl").
-include("common.hrl").
-include("def_module.hrl").
-include("predefine.hrl").
-include("def_goods.hrl").
-include("goods.hrl").
-include("figure.hrl").
-include("rec_event.hrl").
-include("def_event.hrl").
-include("errcode.hrl").

%% API
-export([
	capsule_egg_info/2          %% 奖励信息
	,select_capsule_egg/3       %% 选择奖励
	,cancel_capsule_egg/3       %% 取消奖励
	,quick_select_capsule_egg/3 %% 快速选奖
	,reset_reward/2             %% 重置奖励
	,draw_reward/2              %% 抽取奖励
	,ensure_reward/2            %% 确认奖励
	,gm_reset_times/2
]).

%% --------------------------------------------------------------
%% @doc
%%  查询奖励信息
%% @end
%% --------------------------------------------------------------
capsule_egg_info(#player_status{sid = Sid} = _Player, SubType) ->
	case lib_custom_act_api:is_open_act(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType) of
		true ->
			#custom_act_data{act_data = ActData} =
				lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType),
			case trans_select_egg(ActData) of
				#select_egg_data{
					reward_list = RewardList, select_list = SelectList, draw_times = DrawTimes,
					reset_times = ResetTimes, is_ensure = IsEnsure
				} ->
					PackList    = pack_info_list(SelectList -- RewardList, #{}),
					PackSelList = pack_info_list(SelectList, #{}),
					PackCfgList = pack_cfg_list(SubType),
					CostList    = calc_cost_list(SubType, DrawTimes+1),
					case CostList of
						[{_, RewardId, Num}|_] -> ok;
						_ -> RewardId = 0, Num = 0
					end;
				_  ->
					PackList    = [],
					PackSelList = [],
					PackCfgList = pack_cfg_list(SubType),
					CostList    = calc_cost_list(SubType, 1),
					case CostList of
						[{_, RewardId, Num}|_] -> ok;
						_ -> RewardId = 0, Num = 0
					end,
					ResetTimes = 0,
					IsEnsure   = 0
			end,
			case lib_custom_act_util:get_act_cfg_info(
				?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType
			) of
				#custom_act_cfg{condition = Condition} ->
					case lists:keyfind(refresh, 1, Condition) of
						{_, CfgTimes} -> ok;
						_ -> CfgTimes = 0
					end;
				_ -> CfgTimes = 0
			end,
			LeftRestTimes = max(0, CfgTimes - ResetTimes),
			lib_server_send:send_to_sid(
				Sid, pt_336, 33600, [SubType, PackList, PackSelList, PackCfgList, RewardId, Num, LeftRestTimes, IsEnsure]
			);
		false ->
			lib_server_send:send_to_sid(
				Sid, pt_331, 33100, [?ERRCODE(err331_act_closed)]
			)
	end,
	ok.

pack_info_list([], PackMap) ->
	PackList = maps:to_list(PackMap),
	[{PoolId, util:term_to_string(Rewards)}||{PoolId, Rewards}<-PackList];
pack_info_list([RewardId|TList], PackMap) ->
	case data_select_capsule_egg:get_reward(RewardId) of
		#base_select_egg_reward{pool_id = PoolId, goods = [{_, GoodsTypId, Num}]} ->
			RewardList = maps:get(PoolId, PackMap, []),
			PackMap1 = maps:put(PoolId, [{RewardId, GoodsTypId, Num}|RewardList], PackMap),
			pack_info_list(TList, PackMap1);
		_ ->
			pack_info_list(TList, PackMap)
	end.

pack_cfg_list(SubType) ->
	%% 以活动开启的天数来作为标准
	OpenDay = get_act_open_day(SubType),
	RewardIds   = get_reward_ids(SubType, OpenDay),
	PackCfgList = pack_info_list(RewardIds, #{}),
	PackCfgList.

get_act_open_day(SubType) ->
	OpenDay =
	case lib_custom_act_util:get_act_cfg_info(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType) of
		#custom_act_cfg{start_time = StartTime, opday_lim = OpenDayLimit} ->
			%% 开始时间为0的时候读取活动的开启天数
			case StartTime =/= 0 of
				true -> get_open_day(StartTime);
				_ ->
					case OpenDayLimit of
						[{StartDay, _}] -> StartDay;
						_ -> 1
					end
			end;
		_ ->
			1
	end,
	%% 只允许在 1 - 1999天数范围内，超出的修正为1
	OpenDay1 = case 1 =< OpenDay andalso OpenDay =< 1999 of
				   true ->
					   OpenDay;
				   _ ->
					   1
	           end,
	OpenDay1.

get_open_day(Now) ->
	OpenTime = util:get_open_time(),
	day_span(Now, OpenTime).

day_span(Unixtime1, Unixtime2) ->
	diff_day(Unixtime1, Unixtime2) + 1.

diff_day(Unixtime1, Unixtime2) ->
	{Date1, _} = utime:unixtime_to_localtime(Unixtime1),
	{Date2, _} = utime:unixtime_to_localtime(Unixtime2),
	calendar:date_to_gregorian_days(Date1) - calendar:date_to_gregorian_days(Date2).

calc_cost_list(SubType, DrawTimes) ->
	OpenDay  = util:get_open_day(),
	CostList = data_select_capsule_egg:get_cost(SubType, DrawTimes, OpenDay),
	CostList1 = case CostList of
		            [_|_] -> CostList;
		            _ -> data_select_capsule_egg:get_cost(SubType, ?POOLS_NUM, OpenDay)
	            end,
	CostList1.

%% --------------------------------------------------------------
%% @doc
%%  选择奖励
%% @end
%% --------------------------------------------------------------
select_capsule_egg(#player_status{sid = Sid, id = RoleId} = _Player, SubType, RewardId) ->
	case lib_custom_act_api:is_open_act(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType) of
		true ->
			#custom_act_data{act_data = ActData} = CustomActData =
				lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType),
			ActData1 = trans_select_egg(ActData),
			#select_egg_data{reward_list = RewardList, draw_times = DrawTimes} = ActData1,
			CheckList = [
				{can_select, DrawTimes}, {check_reward, SubType, RewardId},
				{has_select, RewardId, RewardList}
				,{has_finish_select, RewardId, RewardList}
			],
			case check_select_reward(CheckList) of
				true ->
					NewRewardList = [RewardId|RewardList],
					NewActData = ActData1#select_egg_data{
						reward_list = NewRewardList,
						select_list = NewRewardList
					},
					NewCustomActData = CustomActData#custom_act_data{
						time = utime:unixtime(), act_data = NewActData
					},
					lib_custom_act_data:save_custom_data(RoleId, NewCustomActData),
					PackList = pack_info_list(NewRewardList, #{}),
					lib_server_send:send_to_sid(
						Sid, pt_336, 33601, [SubType, PackList]
					);
				{false, ErrCode} ->
					lib_server_send:send_to_sid(
						Sid, pt_331, 33100, [ErrCode]
					)
			end;
		false ->
			lib_server_send:send_to_sid(
				Sid, pt_331, 33100, [?ERRCODE(err331_act_closed)]
			)
	end,
	ok.

%% --------------------------------------------------------------
%% @doc
%%  取消奖励
%% @end
%% --------------------------------------------------------------
cancel_capsule_egg(#player_status{sid = Sid, id = RoleId} = _Player, SubType, RewardId) ->
	case lib_custom_act_api:is_open_act(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType) of
		true ->
			#custom_act_data{act_data = ActData} = CustomActData =
				lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType),
			ActData1 = trans_select_egg(ActData),
			#select_egg_data{select_list = RewardList, draw_times = DrawTimes, is_ensure = IsEnsure} = ActData1,
			CheckList = [
				{can_cancel, DrawTimes}, {has_cancel_reward, RewardId, RewardList},
				{has_ensure, IsEnsure}
			],
			case check_cancel_reward(CheckList) of
				true ->
					NewRewardList = lists:delete(RewardId, RewardList),
					NewActData = ActData1#select_egg_data{
						reward_list = NewRewardList,
						select_list = NewRewardList
					},
					NewCustomActData = CustomActData#custom_act_data{
						time = utime:unixtime(), act_data = NewActData
					},
					lib_custom_act_data:save_custom_data(RoleId, NewCustomActData),
					PackList = pack_info_list(NewRewardList, #{}),
					lib_server_send:send_to_sid(
						Sid, pt_336, 33601, [SubType, PackList]
					);
				{false, ErrCode} ->
					lib_server_send:send_to_sid(
						Sid, pt_331, 33100, [ErrCode]
					)
			end;
		false ->
			lib_server_send:send_to_sid(
				Sid, pt_331, 33100, [?ERRCODE(err331_act_closed)]
			)
	end,
	ok.

%% --------------------------------------------------------------
%% @doc
%%  快速选择奖励
%% @end
%% --------------------------------------------------------------
quick_select_capsule_egg(#player_status{sid = Sid, id = RoleId} = _Player, SubType, MySelectList) ->
	case lib_custom_act_api:is_open_act(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType) of
		true ->
			#custom_act_data{act_data = ActData} = CustomActData =
				lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType),
			ActData1 = trans_select_egg(ActData),
			#select_egg_data{reward_list = RewardList, select_list = SelectList, draw_times = DrawTimes} = ActData1,
			CheckList = [
				{can_select, DrawTimes}, {check_select_list, SubType, MySelectList},
				{has_select_list, SelectList, MySelectList}
				,{has_finish_select_list, SelectList, MySelectList}
			],
			case check_select_reward(CheckList) of
				true ->
					NewRewardList = MySelectList ++ RewardList,
					NewActData = ActData1#select_egg_data{
						reward_list = NewRewardList,
						select_list = NewRewardList
					},
					NewCustomActData = CustomActData#custom_act_data{
						time = utime:unixtime(), act_data = NewActData
					},
					lib_custom_act_data:save_custom_data(RoleId, NewCustomActData),
					PackList = pack_info_list(NewRewardList, #{}),
					lib_server_send:send_to_sid(
						Sid, pt_336, 33604, [SubType, PackList]
					);
				{false, ErrCode} ->
					lib_server_send:send_to_sid(
						Sid, pt_331, 33100, [ErrCode]
					)
			end;
		false ->
			lib_server_send:send_to_sid(
				Sid, pt_331, 33100, [?ERRCODE(err331_act_closed)]
			)
	end,
	ok.

%% --------------------------------------------------------------
%% @doc
%%  重置奖励
%% @end
%% --------------------------------------------------------------
reset_reward(#player_status{sid = Sid, id = RoleId} = _Player, SubType) ->
	case lib_custom_act_api:is_open_act(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType) of
		true ->
			#custom_act_data{act_data = ActData} = CustomActData =
				lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType),
			case lib_custom_act_util:get_act_cfg_info(
				?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType
			) of
				#custom_act_cfg{condition = Condition} ->
					case lists:keyfind(refresh, 1, Condition) of
						{_, CfgTimes} -> ok;
						_ -> CfgTimes = 0
					end;
				_ -> CfgTimes = 0
			end,
			ActData1 = trans_select_egg(ActData),
			#select_egg_data{reset_times = ResetTimes, draw_times = DrawTimes,
				reward_list = RewardList
			} = ActData1,
			case DrawTimes == 0 andalso RewardList == [] of
				true ->
					LeftRestTimes = max(0, CfgTimes - ResetTimes),
					lib_server_send:send_to_sid(
						Sid, pt_336, 33602, [?ERRCODE(err331_not_need_reset), SubType, LeftRestTimes]
					);
				_ ->
					case ResetTimes >= CfgTimes of
						true ->
							lib_server_send:send_to_sid(
								Sid, pt_336, 33602, [?ERRCODE(err331_reset_time_not_enough), SubType, 0]
							);
						_ ->
							NewResetTimes = ResetTimes + 1,
							NewActData = #select_egg_data{reset_times = NewResetTimes},
							NewCustomActData = CustomActData#custom_act_data{
								time = utime:unixtime(), act_data = NewActData
							},
							lib_custom_act_data:save_custom_data(RoleId, NewCustomActData),
							LeftRestTimes = max(0, CfgTimes - NewResetTimes),
							lib_server_send:send_to_sid(
								Sid, pt_336, 33602, [?SUCCESS, SubType, LeftRestTimes]
							)
					end
			end;
		false ->
			lib_server_send:send_to_sid(
				Sid, pt_336, 33602, [?ERRCODE(err331_act_closed), SubType, 0]
			)
	end,
	ok.

%% --------------------------------------------------------------
%% @doc
%%  抽奖
%% @end
%% --------------------------------------------------------------
draw_reward(#player_status{sid = Sid, id = RoleId} = Player, SubType) ->
	case lib_custom_act_api:is_open_act(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType) of
		true ->
			#custom_act_data{act_data = ActData} = CustomActData =
				lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType),
			ActData1 = trans_select_egg(ActData),
			#select_egg_data{draw_times = DrawTimes, reward_list = RewardList, is_ensure = IsEnsure} = ActData1,
			CheckList = [{finish_select_reward, DrawTimes, RewardList}, {is_ensure, IsEnsure}, {has_reward, RewardList}],
			case check_draw_reward(CheckList) of
				true ->
					NewDrawTimes  = DrawTimes + 1,
					FilterList = filter_reward_list(RewardList, NewDrawTimes),
					RandRewardId = urand:rand_with_weight(FilterList),
					case is_integer(RandRewardId) of
						true ->
							#base_select_egg_reward{goods = SendReward, pool_id = PoolId} =
								data_select_capsule_egg:get_reward(RandRewardId),
							CostList = calc_cost_list(SubType, NewDrawTimes),
							%% 第一抽可能免费
							ResCost =
							case NewDrawTimes of
								1 ->
									case CostList of
										[{_, _, 0}] ->
											{true, Player};
										_ ->
											case lib_goods_api:cost_objects_with_auto_buy(Player, CostList, select_egg_draw, "") of
												{true, Player1, _} -> {true, Player1};
												{false, ErrorCode, _PS} -> {false, ErrorCode}
											end
									end;
								_ ->
									case lib_goods_api:cost_objects_with_auto_buy(Player, CostList, select_egg_draw, "") of
										{true, Player1, _} -> {true, Player1};
										{false, ErrorCode, _PS} -> {false, ErrorCode}
									end
							end,
							case ResCost of
								{true, TPlayer} ->
									Produce = #produce{reward = SendReward, type = select_egg_draw},
									NewRewardList = RewardList -- [RandRewardId],
									NewActData = ActData1#select_egg_data{
										draw_times = NewDrawTimes, reward_list = NewRewardList
									},
									NewCustomActData = CustomActData#custom_act_data{
										time = utime:unixtime(), act_data = NewActData
									},
									lib_custom_act_data:save_custom_data(RoleId, NewCustomActData),
									{ok, NewPlayer} = lib_goods_api:send_reward_with_mail(TPlayer, Produce, ?MOD_AC_CUSTOM),
									NewCostList = calc_cost_list(SubType, NewDrawTimes + 1),
									case NewCostList of
										[{_, CostGoodId, Num}|_] -> ok;
										_ -> CostGoodId = 0, Num = 0
									end,
									SendClientReward = case SendReward of
										                   [{_, SendRewardId, SendNum}|_] ->
											                   [{RandRewardId, SendRewardId, SendNum}];
										                   _ -> SendReward
									                   end,
									lib_server_send:send_to_sid(Sid, pt_336, 33603,
										[SubType, PoolId, util:term_to_string(SendClientReward), CostGoodId, Num, IsEnsure]
									),
									{ok, NewPlayer};
								{false, Error} ->
									lib_server_send:send_to_sid(
										Sid, pt_331, 33100, [Error]
									),
									{ok, Player}
							end;
						_ ->
							lib_server_send:send_to_sid(
								Sid, pt_331, 33100, [?ERRCODE(err331_select_egg_reward_err)]
							),
							{ok, Player}
					end;
				{false, ErrCode} ->
					lib_server_send:send_to_sid(
						Sid, pt_331, 33100, [ErrCode]
					),
					{ok, Player}
			end;
		_ ->
			lib_server_send:send_to_sid(
				Sid, pt_331, 33100, [?ERRCODE(err331_act_closed)]
			),
			{ok, Player}
	end.

filter_reward_list(RewardList, DrawTimes) ->
	F = fun(RewardId, TList) ->
		case data_select_capsule_egg:get_reward(RewardId) of
			#base_select_egg_reward{need_times = NeedTimes, weight = Weight}
				when DrawTimes >= NeedTimes ->
				[{Weight, RewardId}|TList];
			_ ->
				TList
		end end,
	FilterList = lists:foldl(F, [], RewardList),
	FilterList.

%% --------------------------------------------------------------
%% @doc
%%  确认奖励
%% @end
%% --------------------------------------------------------------
ensure_reward(#player_status{sid = Sid, id = RoleId} = _Player, SubType) ->
	case lib_custom_act_api:is_open_act(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType) of
		true ->
			#custom_act_data{act_data = ActData} = CustomActData =
				lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType),
			ActData1 = trans_select_egg(ActData),
			#select_egg_data{select_list = SelectList, draw_times = DrawTimes} = ActData1,
			RewardLen = erlang:length(SelectList),
			case DrawTimes > 0 of
				true ->
					ignore;
				_ ->
					case RewardLen == ?POOLS_NUM of
						false ->
							lib_server_send:send_to_sid(
								Sid, pt_336, 33605, [SubType, ?ERRCODE(err331_not_finish_select)]
							);
						_ ->
							NewActData = ActData1#select_egg_data{is_ensure = 1},
							NewCustomActData = CustomActData#custom_act_data{
								time = utime:unixtime(), act_data = NewActData
							},
							lib_custom_act_data:save_custom_data(RoleId, NewCustomActData),
							lib_server_send:send_to_sid(
								Sid, pt_336, 33605, [SubType, ?SUCCESS]
							)
					end
			end;
		false ->
			lib_server_send:send_to_sid(
				Sid, pt_336, 33602, [?ERRCODE(err331_act_closed), SubType, 0]
			)
	end,
	ok.

%% --------------------------------------------------------------
trans_select_egg(#select_egg_data{} = SelectEggData) -> SelectEggData;
trans_select_egg(_) -> #select_egg_data{}.

%% 筛选符合开服天数的奖励配置Id
get_reward_ids(SubType, OpenDay) ->
	AllRewards = data_select_capsule_egg:get_rewards(SubType),
	F = fun(RewardId, TList) ->
		case data_select_capsule_egg:get_reward(RewardId) of
			#base_select_egg_reward{day_min = DayMin, day_max = DayMax}
			when DayMin =< OpenDay, OpenDay =< DayMax ->
				[RewardId|TList];
			_ ->
				TList
		end end,
	AccordList = lists:foldl(F, [], AllRewards),
	AccordList.

%% --------------------------- builtin func --------------------------------------
%% 检查是否符合条件
check_select_reward([]) -> true;
check_select_reward([Check|T]) ->
	case do_check(Check) of
		true -> check_select_reward(T);
		{false, Err} -> {false, Err}
	end.

check_cancel_reward([]) -> true;
check_cancel_reward([Check|T]) ->
	case do_check(Check) of
		true -> check_cancel_reward(T);
		{false, Err} -> {false, Err}
	end.

check_draw_reward([]) -> true;
check_draw_reward([Check|T]) ->
	case do_check(Check) of
		true -> check_draw_reward(T);
		{false, Err} -> {false, Err}
	end.

do_check({can_select, DrawTimes}) ->
	case DrawTimes > 0 of
		true -> {false, ?ERRCODE(err331_cannot_select)};
		_ -> true
	end;
do_check({check_reward, SubType, RewardId}) ->
	OpenDay = get_act_open_day(SubType),
	CfgList = get_reward_ids(SubType, OpenDay),
	case lists:member(RewardId, CfgList) of
		true -> true;
		_ -> {false, ?ERRCODE(err331_select_egg_reward_err)}
	end;
do_check({has_select, RewardId, RewardList}) ->
	case lists:member(RewardId, RewardList) of
		true -> {false, ?ERRCODE(err331_has_select)};
		_ -> true
	end;
do_check({has_finish_select, RewardId, RewardList}) ->
	RewardPool = case data_select_capsule_egg:get_reward(RewardId) of
		             #base_select_egg_reward{pool_id = TPool} -> TPool;
		             _ -> 0
	             end,
	RewardLen = erlang:length(RewardList),
	LimitNum = limit_num(RewardPool),
	PoolRewardMap = trans_to_pool_map(RewardList),
	PoolRewards = maps:get(RewardPool, PoolRewardMap, []),
	case RewardPool > 0 of
		true ->
			case RewardLen < ?POOLS_NUM
				andalso erlang:length(PoolRewards) < LimitNum
			of
				true -> true;
				_ -> {false, ?ERRCODE(err331_finished_select)}
			end;
		_ ->
			{false, ?ERRCODE(err331_select_egg_reward_err)}
	end;
do_check({finish_select_reward, DrawTimes, RewardList}) ->
	case DrawTimes == 0 of
		true ->
			case erlang:length(RewardList) =/= ?POOLS_NUM of
				true ->
					{false, ?ERRCODE(err331_not_finish_select)};
				_ ->
					true
			end;
		_ ->
			true
	end;
do_check({is_ensure, IsEnsure}) ->
	case IsEnsure > 0 of
		true -> true;
		_ -> {false, ?ERRCODE(err331_select_egg_hasnot_ensure)}
	end;
do_check({has_reward, RewardList}) ->
	case RewardList == [] of
		true -> {false, ?ERRCODE(err331_select_egg_reward_none)};
		_ -> true
	end;
do_check({check_select_list, SubType, MySelectList}) ->
	OpenDay = get_act_open_day(SubType),
	CfgList = get_reward_ids(SubType, OpenDay),
	F = fun(RewardId) ->
		case lists:member(RewardId, CfgList) of
			true -> true;
			_ -> false
		end end,
	case lists:all(F, MySelectList) of
		true -> true;
		_ -> {false, ?ERRCODE(err331_select_egg_reward_none)}
	end;
do_check({has_select_list, SelectList, MySelectList}) ->
	F = fun(RewardId) ->
		case lists:member(RewardId, SelectList) of
			true -> true;
			_ -> false
		end end,
	case lists:any(F, MySelectList) of
		true -> {false, ?ERRCODE(err331_has_select)};
		_ -> true
	end;
do_check({has_finish_select_list, SelectList, MySelectList}) ->
	F = fun(RewardId) ->
		case do_check({has_finish_select, RewardId, SelectList}) of
			true -> true;
			_ -> false
		end end,
	case lists:all(F, MySelectList) of
		true -> true;
		_ -> {false, ?ERRCODE(err331_finished_select)}
	end;
do_check({can_cancel, DrawTimes}) ->
	case DrawTimes > 0 of
		true -> {false, ?ERRCODE(err331_cannot_cancel)};
		_ -> true
	end;
do_check({has_cancel_reward, RewardId, RewardList}) ->
	case lists:member(RewardId, RewardList) of
		true -> true;
		_ -> {false, ?ERRCODE(err331_has_not_reward)}
	end;
do_check({has_ensure, IsEnsure}) ->
	case IsEnsure > 0 of
		true -> {false, ?ERRCODE(err331_has_not_reward)};
		_ -> true
	end;
do_check(_) ->
	{false, ?FAIL}.

trans_to_pool_map(RewardList) ->
	F = fun(RewardId, TMap) ->
		case data_select_capsule_egg:get_reward(RewardId) of
			#base_select_egg_reward{pool_id = PoolId} ->
				TList = maps:get(PoolId, TMap, []),
				maps:put(PoolId, [RewardId|TList], TMap);
			_ ->
				TMap
		end end,
	lists:foldl(F, #{}, RewardList).

limit_num(PoolId) ->
	case PoolId of
		1 -> ?POOL_TYPE1_NUM;
		2 -> ?POOL_TYPE2_NUM;
		_ -> ?POOL_TYPE3_NUM
	end.

%% ----------------------------- gm -----------------------------------
gm_reset_times(Player, SubType) ->
	case lib_custom_act_api:is_open_act(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType) of
		true ->
			#custom_act_data{act_data = _ActData} = CustomActData =
				lib_custom_act_data:custom_act_data(?CUSTOM_ACT_TYPE_SELECT_CAPSULE_EGG, SubType),
			NewActData = #select_egg_data{},
			NewCustomActData = CustomActData#custom_act_data{
				time = utime:unixtime(), act_data = NewActData
			},
			lib_custom_act_data:save_custom_data(Player#player_status.id, NewCustomActData),
			capsule_egg_info(Player, SubType),
			ok;
		_ ->
			ignore
	end.