%%%-------------------------------------------------------------------
%%% @doc      无尽托管
%%% Module  : offline_hosting_void_fam.erl
%%% Created : 2020-09-18
%%% @Author : zmh
%%%-------------------------------------------------------------------
-module(offline_hosting_void_fam).

%% -callback
-export([
    check_hosting/0,
    check_hosting/1,
    host_behavior/3,
    notice_logout/0,
    cast_load_scene_behavior/1
]).

-compile(export_all).

-include("server.hrl").
-include("charm_beach.hrl").
-include("predefine.hrl").
-include("def_module.hrl").
-include("def_goods.hrl").
-include("common.hrl").
-include("offline_hosting.hrl").
-include("attr.hrl").
-include("errcode.hrl").
-include("figure.hrl").
-include_lib("stdlib/include/ms_transform.hrl").

%% 获取设置了托管并且不在线的玩家,
%% 此处获取所有设置了托管的玩家,是否在线以及托管值是否足够等条件判断
%% 交给take_part_in
%% 活动开启10秒后,每5秒进入10个玩家
%% @param: return - Pid  返回托管管理进程Id

%%跨服活动期间启动同步回来开启的 本地服启动的时候 需要留更多的时间180s

check_hosting() ->
    check_hosting(false).
check_hosting(IsDelay)->
    Delay = case IsDelay of
        true->
            180000;
        _->
            10000
    end,
    Pid = offline_hosting:take_all_part_in(?MODULE, ?MOD_VOID_FAM, [], Delay),
    Pid.

%% 活动结束,触发所有托管玩家退出
notice_logout() ->
    offline_hosting:notice_logout(?MOD_VOID_FAM).

%% ------------------------------- callback function ------------------------------------------------
%% 托管行为
%% 状态：
%%                        进入场景 前往  发呆

%% 托管上线事件 2s后加载一下场景
host_behavior(login, Player, _) ->
    #player_status{id = RoleId, figure = Figure, battle_attr = BattleAttr} = Player,
    #battle_attr{hp_lim = HpLim} = BattleAttr,
    #figure{name=Name,lv = Lv,vip=Vip,sex=Sex}=Figure,
    mod_void_fam_local:enter_scene(RoleId,Name,Lv,HpLim,Vip,Sex,Figure),
    {ok, Player1} = offline_hosting:host_behavior_load_scene(Player, [], 2000),
    {ok, Player1};

%%前往挂机点 开打
host_behavior(load_scene,Player,_)->
    {_,_,[{TargetX,TargetY}|_]} = data_void_fam:get_cfg(high_drop),
    DestinationBehavior = battle,
    {ok, Player2} = offline_hosting:host_behavior_move(Player, TargetX, TargetY, DestinationBehavior, [], 1000),
    {ok, Player2};

host_behavior(battle, Player, _) ->
    {ok, Player1} = offline_hosting:host_behavior_battle(Player),
    {ok, Player1};

%% 死亡后安全区复活-15秒
host_behavior(die, Player, _) ->
    {ok, Player1} = offline_hosting:host_behavior_delay(Player, revive, [], 15000),
    {ok, Player1};

%% 复活->前往目标挂机
host_behavior(revive, Player, _) ->
    #player_status{scene=PlayerSceneId,battle_attr = #battle_attr{hp_lim = Hp} = BA} = Player,
    %% 通知离开场景
    lib_scene:leave_scene(Player),
    {X, Y} = lib_void_fam:get_scene_born_xy(PlayerSceneId),
    Player1 = Player#player_status{x = X, y = Y, battle_attr = BA#battle_attr{hp = Hp}},

    %% 加载场景
    {ok, Player2} = pp_scene:handle(12002, Player1, []),
    {_,_,[{TargetX,TargetY}|_]} = data_void_fam:get_cfg(high_drop),
    DestinationBehavior = battle,
    {ok, Player3} = offline_hosting:host_behavior_move(Player2, TargetX, TargetY, DestinationBehavior, [], 1000),
    {ok, Player3};

host_behavior(idle,Player,_)->
    {ok,Player};

host_behavior(_Behavior, Player, _Any) ->
    {ok, Player}.

%% 切换到下一轮场景
cast_load_scene_behavior(RoleId) ->
    lib_player:apply_cast(RoleId, ?APPLY_CAST_STATUS, ?MODULE, load_scene_behavior, []).

load_scene_behavior(Player) ->
    #player_status{offline_hosting = OffLineHosting} = Player,
    #offline_hosting{handler = Handler} = OffLineHosting,
    case Handler == ?MODULE of
        true ->
            {ok, Player1} = offline_hosting:host_behavior_load_scene(Player, [], 3000); %% 10秒后加载到场景,等待切换场景回调
        _->
            Player1 = Player
    end,
    {ok, Player1}.