%% ---------------------------------------
%% desc     :离线托管-跨服国战
%% module   :offline_hosting_cgwar.erl
%% author   :zmh
%% time     :20/08/07
%% ---------------------------------------

-module(offline_hosting_cgwar).

-export([
    host_behavior/3,
    check_zone_hosting/1,
    notice_logout/0
    ]).

-include("server.hrl").
-include("cross_guild_war.hrl").
-include("predefine.hrl").
-include("def_module.hrl").
-include("common.hrl").
-include("offline_hosting.hrl").
-include("attr.hrl").
-include("errcode.hrl").
-include("figure.hrl").

-compile([export_all]).

check_zone_hosting([])->
    ok;
check_zone_hosting([H|T])->
    #cgw_zone_guild_info{guild_list = GuildList} = H,
    check_zone_guild_hosting(GuildList),
    check_zone_hosting(T).

check_zone_guild_hosting([])->
    ok;
check_zone_guild_hosting([H|T])->
    #cgw_guild_info{
        sign_ups = Signs,
        help_ups = Helps
    }=H,
    check_hosting(Signs++Helps),
    check_zone_guild_hosting(T).


%% 开启离线托管
check_hosting(Signs) ->
    SerSigns = cross_guild_war_mgr:split_server_helper(Signs,[]),
    do_check_hosting(SerSigns).
do_check_hosting([])->
    ok;
do_check_hosting([{ServerId,Signs}|T])->
    Mfa = {?MODULE, check_hosting_local, [Signs]},
    clusters_center:apply_cast(ServerId, Mfa),
    do_check_hosting(T).

check_hosting_local(Signs)->
    Now = utime:unixtime(),
    Pid = spawn(
        fun() -> timer:sleep(2000), check_hosting_by_role(Signs, Now, 0) end
    ),
    Pid.

check_hosting_by_role([], _, _) ->
    ok;
check_hosting_by_role([RoleId|T], Now, Count) ->
    Res = offline_hosting:take_part_in(?MODULE, ?MOD_CGWAR, RoleId, []),
    case Res =:= 1 of
        true  ->
            case Count >= 10 of
                true  ->
                    NewCount = 0,
                    timer:sleep(3000);
                false ->
                    NewCount = Count + 1
            end,
            check_hosting_by_role(T, Now, NewCount);
        false ->
            check_hosting_by_role(T, Now, Count)
    end.

notice_logout()->
    offline_hosting:notice_logout(?MOD_CGWAR).

%% 状态：
%%                        进入场景 前往  打怪

%% 托管上线事件 2s后加载一下场景
host_behavior(login, Player, _) ->
    cross_guild_war_api:hosting_enter_scene(Player),
    {ok, Player1} = offline_hosting:host_behavior_load_scene(Player, [], 2000),
    {ok, Player1};

%%前往挂机点 开打
host_behavior(load_scene,Player,_)->
    #player_status{scene=PlayerSceneId,copy_id=CopyId,battle_attr=BA}=Player,
    #battle_attr{group = GroupId} = BA,
    Scene = data_scene:get(PlayerSceneId),
    {TargetX,TargetY} = cross_guild_war_api:get_battle_xy(CopyId,GroupId,Scene),
    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};

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

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

    %% 加载场景
    {ok, Player2} = pp_scene:handle(12002, Player1, []),
    {TargetX,TargetY} = cross_guild_war_api:get_battle_xy(CopyId,GroupId,Scene),
    DestinationBehavior = battle,
    {ok, Player3} = offline_hosting:host_behavior_move(Player2, TargetX, TargetY, DestinationBehavior, [], 1000),
    {ok, Player3};

host_behavior(direction,Player,{TarX,TarY}) ->
    {ok, Player1} = offline_hosting:cancel_behavior_timer(Player),
    DestinationBehavior = battle,
    {ok, Player2} = offline_hosting:host_behavior_move(Player1, TarX,TarY, DestinationBehavior, [], 1000),
    {ok, Player2};

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