%%-----------------------------------------------------------------------------
%% module  :       pp_void_fam
%% @author  :       Czc
%% email   :       389853407@qq.com
%% Created :       2017-11-14
%% description:
%%-----------------------------------------------------------------------------
-module(pp_void_fam).

-include("server.hrl").
-include("errcode.hrl").
-include("predefine.hrl").
-include("figure.hrl").
-include("attr.hrl").
-include("void_fam.hrl").
-include("common.hrl").

-export([
    handle/3
    , send_error_code/2
    ]).

%% 获取活动信息
handle(60001, Status, _) ->
    #player_status{id = RoleId} = Status,
    mod_void_fam_local:send_act_info(RoleId);

%% 进入场景
handle(60002, Status, _) ->
    #player_status{sid = Sid, id = RoleId, scene = Scene, figure = Figure, battle_attr = BattleAttr} = Status,
    #battle_attr{hp_lim = HpLim} = BattleAttr,
    #figure{name=Name,lv = Lv,vip=Vip,sex=Sex}=Figure,
    CheckList = [
        {check_already_in,Scene},
        {check_act_open},
        {check_cfg_lv,Lv},
        {check_transferable,Status}
    ],
    case util:check_lists(lib_void_fam,CheckList) of
        true->
            mod_void_fam_local:enter_scene(RoleId,Name,Lv,HpLim,Vip,Sex,Figure);
        {false,ErrCode}->
            send_error_code(Sid, ErrCode)
    end,
    {ok,Status};

%% 退出场景
handle(60003, Status, _) ->
    #player_status{sid = Sid, id = RoleId, scene = Scene, figure = #figure{lv = Lv},battle_attr = BattleAttr} = Status,
    #battle_attr{hp_lim = HpLim} = BattleAttr,
    IsInVoidFam = lib_void_fam:is_in_void_fam(Scene),
    if
        IsInVoidFam == false ->
            send_error_code(Sid, ?ERRCODE(err600_no_in_act_scene));
        true ->
            mod_void_fam_local:exit_scene(RoleId,Lv,HpLim)
    end;

%% 楼层信息
handle(60004, Status, _) ->
    #player_status{id = RoleId} = Status,
    mod_void_fam_local:send_floor_info(RoleId);

%%复活
%%@param Type   1元宝原地复活 2绑定元宝原地复活 3回城复活 4原地满血复活
handle(60008, Status, [Type]) when Type>0, Type=<11 ->
    %% 数据初始化
    #player_status{
        id = RoleID,
        scene=SceneId,
        battle_attr = BattleAttr
    } = Status,
    #battle_attr{hp_lim=HpLim} = BattleAttr,
    case lib_void_fam:get_floor(SceneId) of
        %低于这个值认为死完 可请求复活 避免发包
        Floor when Floor>0->
            case data_void_fam:get_floor_cfg(Floor) of
                #void_fam_floor_cfg{drate=DRate0}->
                    R = case Type =:= 1 orelse Type =:= 2 of
                        true->
                            0; %%花钱不掉
                        _->
                            DRate0
                    end,
                    case urand:rand(1,1000) =< R of
                        true->%%踢到前一层
                            SerID = config:get_server_id(),
                            {PreScene,PoolID} = lib_void_fam:get_pre_scene(SceneId,Floor,SerID),
                            {X, Y} = lib_void_fam:get_scene_born_xy(PreScene),
                            lib_scene:change_player_scene(RoleID, PreScene, PoolID, 0, X, Y, true, [{group, 0},{pk,{?PK_ALL, true}},{change_scene_hp_lim,HpLim}]),
                            {ok, BinData} = pt_200:write(20004, [Type, 1]),
                            lib_server_send:send_to_uid(RoleID, BinData),
                            mod_void_fam_local:update_role_scene(RoleID,Floor-1,PreScene),
                            %% 推送到达新楼层协议
                            lib_server_send:send_to_uid(RoleID, pt_600, 60007, [Floor-1]),
                            {ok, Status};
                        _->
                            % ?ERR("M:~p,L:~p,msg:~p~n", [?MODULE,?LINE,{Type,Stage}]),
                            {Result, NStatus0} = lib_revive:revive(Status, Type),
                            %% 告诉客户端复活成功
                            {ok, BinData} = pt_200:write(20004, [Type, Result]),
                            lib_server_send:send_to_uid(RoleID, BinData),
                            {ok,NStatus0}
                    end;
                _->
                    {Result, NStatus0} = lib_revive:revive(Status, Type),
                    %% 告诉客户端复活成功
                    {ok, BinData} = pt_200:write(20004, [Type, Result]),
                    lib_server_send:send_to_uid(RoleID, BinData),
                    {ok,NStatus0}
            end;
        _->
            {ok,Status}
    end;

handle(60010,Status,[])->
    #player_status{id = RoleID} = Status,
    mod_void_fam_local:send_rank_info(RoleID);

%%
handle(60011, Status, _) ->
    #player_status{id = RoleId} = Status,
    mod_void_fam_local:send_drop_info(RoleId);

handle(_Cmd, _Status, _Data) ->
    io:format("----:~p~n",[_Status#player_status.old_scene_info]),
    {error, "pp_void_fam no match"}.

%% 发送错误码
send_error_code(Sid, ErrorCode) ->
    {ok, BinData} = pt_600:write(60000, [ErrorCode]),
    lib_server_send:send_to_sid(Sid, BinData).
