%% ---------------------------------------------------------------------------
%% @doc 玩家活动玩法中重连
%% @author hek
%% @since  2016-09-09
%% @deprecated 本模块提供-活动玩法中重连操作逻辑
%% ---------------------------------------------------------------------------
-module (lib_player_reconnect).
-include("server.hrl").
-include("scene.hrl").
-include("common.hrl").
-include("figure.hrl").
-include("attr.hrl").
-include("def_event.hrl").
-include("arena.hrl").
-include("predefine.hrl").
-include("relationship.hrl").
-export ([
    reconnect/2,
    repair_to_default/1,
    repair_pk_status/1
]).

%%%---------------------------------------------------------------------------
%%% 对于需要重连的活动玩法重连如下
%%% 重连条件：上线时的场景是需要处理重连场景（不需要重连的，走lib_scene:repair_xy/4修复位置规则）
%%% 具体步骤：
%%% 1.活动进程存在，且活动未结束，cast到活动进程，先执行玩家上线逻辑（如在线活动人数+1，设置动态分组等）
%%%   然后切换场景，将玩家传送到活动规则最新场景

%%% 2. 如果活动进程不存在（即misc:is_process_alive/1为false）或者活动已结束的，使用
%%%    lib_scene:change_default_scene/2将玩家切换到默认场景即可
%%%---------------------------------------------------------------------------

reconnect(PS, LoginType) when LoginType > 0 andalso LoginType =< ?RE_LOGIN ->
    #player_status{online = OnlineFlag} = PS,
    if
        OnlineFlag == ?ONLINE_OFF_HOSTING ->
            {ok, repair_to_default(PS)};
        true ->
            % ?PRINT("lib_player_reconnect reconnect: ~p~n", [[PS#player_status.id, LoginType]]),
            SetList = [
               {in_dungeon, [?NORMAL_LOGIN, ?RE_LOGIN]},
               {in_beach,   [?NORMAL_LOGIN, ?RE_LOGIN]},
               {in_guild, [?NORMAL_LOGIN]},
               {in_wasteland, [?NORMAL_LOGIN, ?RE_LOGIN]},
               {in_jump, [?NORMAL_LOGIN]},
               {in_convoy, [?NORMAL_LOGIN]},
                {in_exp_sea, [?NORMAL_LOGIN, ?RE_LOGIN]}
            ],
            case do_reconnect(SetList, PS, LoginType) of
                %% 不需要重连，修复坐标
                {no, NewPS} ->
                    if
                        LoginType == ?RE_LOGIN ->
                            {no, NewPS}; %% 重连直接进入原来的坐标
                        true ->
                            {no, repair_to_default(NewPS)}
                    end;
                %% 活动中重连
                {ok, NewPS} -> {ok, NewPS}
            end
    end.

%% 重连数据处理
do_reconnect([], PS, _LoginType) -> {no, PS};
do_reconnect([H|T], PS, LoginType) ->
    {InMod, LoginList} = H,
    case lists:member(LoginType, LoginList) of
        true ->
            case reconnect_set_data(InMod, PS, LoginType) of
                {ok, NewPS} -> {ok, NewPS};
                {next, NewPS} -> do_reconnect(T, NewPS, LoginType)
            end;
        false -> do_reconnect(T, PS, LoginType)
    end.

%% 副本
reconnect_set_data(in_dungeon, PS, LoginType) ->
    case lib_dungeon:reconnect(PS, LoginType) of
        {ok, NewPS} -> {ok, NewPS};
        {next, NewPS} -> {next, NewPS}
    end;

%% 护送
reconnect_set_data(in_beach, PS, LoginType) ->
    case lib_charm_beach:reconnect(PS, LoginType) of
        {ok, NewPS} -> {ok, NewPS};
        {next, NewPS} -> {next, NewPS}
    end;

%% 巅峰竞技
%%reconnect_set_data(in_top_pk, PS, LoginType) ->
%%    case lib_top_pk:reconnect(PS, LoginType) of
%%        {ok, NewPS} -> {ok, NewPS};
%%        {next, NewPS} -> {next, NewPS}
%%    end;
%%
%%%% 3v3
%%reconnect_set_data(in_3v3, PS, LoginType) ->
%%    case lib_3v3_local:reconnect(PS, LoginType) of
%%        {ok, NewPS} -> {ok, NewPS};
%%        {next, NewPS} -> {next, NewPS}
%%    end;
%%
%%%% 钻石擂台
%%reconnect_set_data(in_drumwar, PS, _LoginType) ->
%%    case lib_role_drum:re_login(PS) of
%%        true -> {ok, PS};
%%        false -> {next, PS}
%%    end;
%%
%%%% 1vN
%%reconnect_set_data(in_1vN, PS, LoginType) ->
%%    case lib_kf_1vn:reconnect(PS, LoginType) of
%%        {ok, NewPS} -> {ok, NewPS};
%%        {next, NewPS} -> {next, NewPS}
%%    end;

%% 公会驻地
reconnect_set_data(in_guild, PS, _LoginType) ->
    case lib_guild_api:reconnect(PS) of
        {ok, NewPS} -> {ok, NewPS};
        {next, NewPS} -> {next, NewPS}
    end;

%% 废土战场(饥饿游戏)
reconnect_set_data(in_wasteland, PS, _LoginType) ->
    case wasteland_war_api:reconnect(PS) of
        {ok, NewPS} -> {ok, NewPS};
        {next, NewPS} -> {next, NewPS}
    end;

%% 经验海
reconnect_set_data(in_exp_sea, PS, _LoginType) ->
    case exp_sea_logic:reconnect(PS) of
        {ok, NewPS} -> {ok, NewPS};
        {next, NewPS} -> {next, NewPS}
    end;

%% 跳一跳
reconnect_set_data(in_jump, PS, _LoginType) ->
    case custom_act_jump_act:reconnect(PS) of
        {ok, NewPS} -> {ok, NewPS};
        {next, NewPS} -> {next, NewPS}
    end;

%% 护送
reconnect_set_data(in_convoy, PS, _LoginType) ->
    case convoy_api:reconnect(PS) of
        {ok, NewPS}     -> {ok, NewPS};
        {next, NewPS}   -> {next, NewPS}
    end;

reconnect_set_data(_Unkown, PS, _LoginType) ->
    {next, PS}.

repair_to_default(Player) ->
    Lv   = Player#player_status.figure#figure.lv,
    OldSceneId = Player#player_status.scene,
    OldX = Player#player_status.x,
    OldY = Player#player_status.y,
    [NewSceneId, NewX, NewY] = lib_scene:repair_xy(Lv, OldSceneId, OldX, OldY),
    NewPlayer = Player#player_status{scene = NewSceneId, x = NewX, y = NewY},
    DefaultPlayer = repair_pk_status(NewPlayer),
    DefaultPlayer.


repair_pk_status(Player) ->
    #player_status{battle_attr = BA, scene = SceneId} = Player,
    RoleEnterSetting = lib_scene:get_role_enter_scene_setting(SceneId, BA),
    case RoleEnterSetting of
        [{pk, {_PkType, _IsForce}}|_] ->
            mod_server_cast:set_data_sub(RoleEnterSetting, Player);
        _ ->
            Player
    end.