%% ---------------------------------------------------------------------------
%% @doc 跨服竞榜活动
%% @author zhaoyu
%% @since  2018-12-03
%% ---------------------------------------------------------------------------
-module(pp_race_rank).

-include("server.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("predefine.hrl").

-export([handle/3]).



%% 竞榜活动信息
handle(33300, Role, _) ->
    race_rank:opening_race_rank_act_info(Role),
    {ok,Role};

%% 竞榜活动信息
handle(33301, Role, [ActId]) ->
    race_rank:race_rank_act_info(Role, ActId);

%% 竞榜抽奖
handle(
    33302, Role = #player_status{sid = Sid}, [ActId, Times, IsAutoBuy]
) when Times =:= 1 orelse Times =:= 10 ->
    {Reason, Score, DrawTime, Stage, AwardIdList, NewRole} =
    case race_rank:draw(Role, ActId, Times, IsAutoBuy) of
        {false, Rea} ->
            {Rea, 0, 0, 0, [], Role};
        {ok, Role2, Score2, DrawTime2, Stage2, AwardIdList2} ->
            {?SUCCESS, Score2, DrawTime2, Stage2, AwardIdList2, Role2}
    end,
    Data = [Reason, ActId, Times, Score, DrawTime, Stage, AwardIdList],
    {ok, BinData} = pt_333:write(33302, Data),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok, NewRole};

%% 次数奖励领取
handle(33303, Role = #player_status{sid = Sid}, [ActId, AwardId]) ->
    {Reason, NewRole} =
    case race_rank:times_award(Role, ActId, AwardId) of
        {false, Rea} ->
            {Rea, Role};
        {ok, Role2} ->
            {?SUCCESS, Role2}
    end,
    {ok, BinData1} = pt_333:write(33303, [Reason, ActId, AwardId]),
    lib_server_send:send_to_sid(Sid, BinData1),
    {ok, NewRole};

handle(33304,Status,[ActId])->
    race_rank:fetch_rank_data(Status, ActId);

% %%许愿池面板
% handle(33305,Status,[ActId])->
%     race_rank:wish_rank_act_info(Status, ActId);

% %%祈愿
% handle(33306,Status,[ActId,OpTy,Auto])->
%     #player_status{sid=Sid}=Status,
%     {Reason, Stage, AwardIdList, NewStatus} =
%     case race_rank:wish(Status, ActId, OpTy,Auto) of
%         {false, Rea} ->
%             {Rea, 0,[],Status};
%         {ok, Status0,Stage0, AwardIdList0} ->
%             {?SUCCESS, Stage0, AwardIdList0, Status0}
%     end,
%     Data = [Reason,ActId,OpTy,Stage,AwardIdList],
%     {ok, BinData} = pt_333:write(33306, Data),
%     lib_server_send:send_to_sid(Sid, BinData),
%     {ok, NewStatus};

handle(_Cmd, _Status, _Data) ->
    {error, "pp_race_rank no match"}.


