%% ---------------------------------------------------------------------------
%% @doc 玩家活动玩法中登出
%% @author hek
%% @since  2016-09-09
%% @deprecated 本模块提供-活动玩法中登出操作逻辑
%% ---------------------------------------------------------------------------
-module (lib_player_logout).
-include("server.hrl").
-include("scene.hrl").
-include("common.hrl").
-include("attr.hrl").
-include("predefine.hrl").
-include("battle.hrl").
-include("guild.hrl").
-include("def_fun.hrl").
-include("errcode.hrl").
-include("def_event.hrl").
-include("figure.hrl").

-export ([logout/2]).

%% 延迟退出或者是正常退出
-define(LOGOUT_TYPE_LIST, [?NORMAL_LOGOUT, ?DELAY_LOGIN]).

%% 下线退出和离线挂机退出各种活动和副本的统一处理接口
%% 因为各种情况都是互斥的，前面条件满足就认为玩家不会在其他活动
%% 任何活动和副本退出最好匹配两种退出处理
%% ========================================================
%% 延迟退出类型：是要将玩家活动数据清理，然后将玩家退出到野外或者主城 =
%% 直接退出类型：不需要将玩家切到野外或者主城，直接将玩家活动数据清理 =
%% ========================================================
%% 新增set类型处理，需要注意返回值，设定程序是否继续往下走 {ok, PS} | {next, PS}
%% ========================================================
logout(PS, LogOutType) when LogOutType > 0 andalso LogOutType =< ?DELAY_LOGIN ->
    ?PRINT("lib_player_logout logout: ~p,~p,~p~n", [PS#player_status.id, PS#player_status.ip, LogOutType]),
    SetList = [
        in_dun, common, kf_3v3, drumwar, dun_multi, limit_goal,
        kf_1vn, in_husong],
    {ok, NewPS} = do_logout(SetList, PS, LogOutType),
    {ok, NewPS};
logout(PS, LogOutType) ->
    ?ERR("err logout type :~p~n", [LogOutType]),
    {ok, PS}.

%% 登出数据处理
do_logout([], PS, _LogutType) -> {ok, PS};
do_logout([InMod|T], PS, LogOutType) ->
    case lists:member(LogOutType, ?LOGOUT_TYPE_LIST) of
        true ->
            case logout_set_data(InMod, PS, LogOutType) of
                {next, NewPS} when is_record(NewPS, player_status) ->
                    do_logout(T, NewPS, LogOutType);
                {ok, NewPS} when is_record(NewPS, player_status) ->
                    do_logout(T, NewPS, LogOutType);
                _R ->
                    ?ERR("do_logout error:~p~n", [{InMod, _R}]),
                    do_logout(T, PS, LogOutType)
            end;
        false ->
            do_logout(T, PS, LogOutType)
    end.

%% 副本退出
logout_set_data(in_dun, PS, LogOutType) ->
    case lib_dungeon:is_on_dungeon(PS) of
        true ->
            #player_status{id = RoleId, copy_id = CopyId,
                           battle_attr = #battle_attr{hp = Hp, hp_lim = HpLim} } = PS,
            if
                LogOutType == ?NORMAL_LOGOUT ->
                    mod_dungeon:logout(CopyId, RoleId, Hp, HpLim),
                    {next, PS};
                LogOutType == ?DELAY_LOGIN ->
                    mod_dungeon:delay_logout(CopyId, RoleId, Hp, HpLim),
                    {next, PS};
                true ->
                    {next, PS}
            end;
        false ->
            {next, PS}
    end;

%% 本服boss退出
%logout_set_data(in_boss, PS, LogOutType) ->
%    case lib_boss:is_in_boss(PS#player_status.scene) of
%        false -> {next, PS};
%        true -> %% 退出或者离开
%            mod_boss:logout_boss(PS#player_status.scene, PS#player_status.id, PS#player_status.boss, LogOutType),
%            {next, PS}
%    end;

%% 跨服组队退出
logout_set_data(dun_multi, PS, _LogOutType) ->
    case lib_dun_multi:logout(PS) of
        false -> {next, PS};
        true ->
            {next, PS}
    end;

%% 通用退出
%% 注: 这个接口可能会调用多次
logout_set_data(common, PS, LogOutType) ->
    #player_status{id = RoleId, scene = SceneId,figure = #figure{lv = Lv}, guild = Guild, scene_pool_id = PoolId, copy_id = CopyId, x = X, y = Y} = PS,
    IsVoidFamScene = lib_void_fam:is_in_void_fam(SceneId),
    if
        IsVoidFamScene ->   %% 无尽领域退出
            mod_void_fam_local:exit_scene(RoleId, Lv,PS#player_status.battle_attr#battle_attr.hp_lim);
        true -> skip
    end,
    #status_guild{id = GuildId} = Guild,
    %% 魅力海滩
    lib_charm_beach:leave_beach(RoleId, SceneId, PoolId, CopyId, X, Y),
    %% 跨服团战
    camp_war_api:leave_scene(RoleId, SceneId, PoolId, GuildId),
    case LogOutType of
        ?NORMAL_LOGOUT ->
            ?TRY(mod_smashed_egg:update_online_time(RoleId, PS#player_status.online, PS#player_status.last_login_time));
        _ -> skip
    end,

    {next, PS};

%% 限时目标 只在真实下线的时候处理
logout_set_data(limit_goal, PS, ?NORMAL_LOGOUT) ->
    case catch lib_limit_goal:logout(PS#player_status.id, PS#player_status.limit_goal) of
        ok -> skip;
        Err ->
            ?ERR("limit goal logout err:~p", [Err])
    end,
    {next, PS};

logout_set_data(in_husong, PS, ?NORMAL_LOGOUT) ->
    case convoy_api:logout(PS#player_status.id) of
        ok -> skip;
        Err ->
            ?ERR("husong logout err:~p", [Err])
    end,
    {next, PS};

logout_set_data(kf_3v3, PS, _) ->
    lib_3v3_local:logout(PS),
    {next, PS};

logout_set_data(drumwar, PS, _) ->
    lib_role_drum:role_logout(PS, 0),
    {next, PS};

logout_set_data(kf_1vn, PS, _) ->
    lib_kf_1vn:logout(PS),
    {next, PS};

logout_set_data(_Unkown, PS, _LogOutType) ->
    {next, PS}.
