%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     跨服沙盘/据点争夺战-协议入口
%%% @end
%%%-------------------------------------------------------------------
-module(pp_cross_point_fight).
-author("liushl").

-include("czone.hrl").
-include("cross_point_fight.hrl").
-include("server.hrl").
-include("figure.hrl").
%% API
-export([handle/3]).

%% 跨服沙盘-开启状态
handle(Cmd = 60400, Player, _) ->
    Info = case czone_api:env_get_zone() of
        #czone_svr_local{stage = Stage, faction = Faction} ->
            case Stage >= ?POINT_BEGIN_CROSS_STAGE of
                true  ->
                    [1, Faction];
                false ->
                    [0, Faction]
            end;
        _ ->
            [0, 0]
    end,
    {ok, Bin} = pt_604:write(Cmd, Info),
    lib_server_send:send_to_sid(Player#player_status.sid, Bin),
    ok;
%% 跨服沙盘-全局信息
handle(_Cmd = 60401, Player, _) ->
    #player_status{role_cross_point = #role_cross_point{point_info = PointInfo}} = Player,
    Info =
        [
            
            case MaxScore > 0 of
                true  ->
                    {PointId, 1, AwardTimes};
                false ->
                    {PointId, 0, AwardTimes}
            end
            || #role_cross_point_info{point_id = PointId, single_max_score = MaxScore, award_time = AwardTimes} <- PointInfo
        ],
    cross_point_fight_center:ask_global_info(Player#player_status.sid, Info),
    ok;
%% 跨服沙盘-全局信息-单个据点-主推
handle(_Cmd = 60402, Player, [PointId]) ->
    cross_point_fight_logic:ask_point_info(Player, PointId),
    ok;
%% 跨服沙盘-查看据点攻占记录
handle(_Cmd = 60403, Player, [PointId]) ->
    #role_cross_point{point_info = PointInfo}
        = Player#player_status.role_cross_point,
    case lists:keyfind(PointId, #role_cross_point_info.point_id, PointInfo) of
        #role_cross_point_info{total_score = MyScore} ->
            MyScore;
        false ->
            MyScore = 0
    end,
    cross_point_fight_center:ask_point_log(Player#player_status.sid, PointId, MyScore, Player#player_status.id),
    ok;
%% 跨服沙盘-攻打据点
handle(Cmd = 60404, Player, [PointId, Sweep]) when Sweep =:= 1 orelse Sweep =:= 0 ->
    case cross_point_fight_logic:attack_point(Player, PointId, Sweep) of
        {false, Error} ->
            {ok, Bin} = pt_604:write(Cmd, [Error]),
            lib_server_send:send_to_sid(Player#player_status.sid, Bin);
        _ ->
            ok
    end,
    ok;
%% 跨服沙盘-插旗
handle(_Cmd = 60405, Player, [PointId, Status]) when Status =:= 1 orelse Status =:= 0 ->
    cross_point_fight_center:mass_status(Player#player_status.id, Player#player_status.figure#figure.name, PointId, Status),
    ok;
%% 跨服沙盘-订阅沙盘数据
handle(_Cmd = 60406, Player, [Status]) when Status =:= 1 orelse Status =:= 0  ->
    cross_point_fight_local:point_subscribe(Player#player_status.pid, Status),
    ok;
%% 跨服沙盘-领取据点占领奖励
handle(_Cmd = 60407, Player, [PointId]) ->
    #role_cross_point{point_info = PointInfo}
        = Player#player_status.role_cross_point,
    case lists:keyfind(PointId, #role_cross_point_info.point_id, PointInfo) of
        #role_cross_point_info{award_time = AwardTime} ->
            AwardTime;
        false ->
            AwardTime = 0
    end,
    cross_point_fight_center:get_seize_award(Player#player_status.pid, PointId, AwardTime),
    ok;
%% 跨服沙盘-我方激活的BUFF
handle(_Cmd = 60408, Player, [])  ->
    cross_point_fight_local:ask_my_buff(Player#player_status.pid),
    ok;
%% 跨服沙盘-副本内信息
handle(_Cmd = 60409, Player, []) ->
    lib_dungeon_cross_point:ask_dun_point_info(Player),
    ok;
handle(Cmd, _Player, Data) ->
    erlang:display({"[LIUSHL]", erlang:localtime(), ?MODULE, ?LINE, Cmd, Data}),
    ok.