%%% -------------------------------------------------------
%%% @author huangyongxing@yeah.net
%%% @doc
%%% 跨服分区-分区管理基础逻辑、跨服主城等
%%% @end
%%% -------------------------------------------------------
-module(pp_czone).
-export([
        handle/3
    ]).
-include("server.hrl").

%% 跨服小分区信息
handle(10300, Player, _) ->
    handle_general_result(10300, Player, fun czone_api:get_czone_info/1);

%% 跨服分区合并预告
handle(10301, Player, _) ->
    handle_general_result(10301, Player, fun czone_api:get_czone_combine_advance/1);

%% 跨服分区进度
handle(10302, Player, _) ->
    handle_general_result(10302, Player, fun czone_api:get_rezone_progress/1);

%% 请求跨服分区状态
handle(10303, Player, _) ->
    handle_general_result(10303, Player, fun czone_api:get_rezone_notice/1);

%% 跨服分区备战奖励状态查询
handle(10304, #player_status{id = RoleId, sid = Sid}, _) ->
    {Stage, RecvState} = czone_api:query_stage_award_state(RoleId),
    lib_server_send:send_to_sid(Sid, pt_103, 10304, [Stage, RecvState]);

%% 跨服分区备战奖励领取
handle(10305, Player = #player_status{sid = Sid}, _) ->
    case czone_api:recv_stage_award(Player) of
        {EvalCode, NewPlayer} ->
            lib_server_send:send_to_sid(Sid, pt_103, 10305, [EvalCode]),
            {ok, NewPlayer};
        EvalCode ->
            lib_server_send:send_to_sid(Sid, pt_103, 10305, [EvalCode]),
            ok
    end;

%% 请求跨服主城状态
handle(10310, _Player = #player_status{sid = Sid}, _) ->
    czone_api:notice_city_state(Sid),
    ok;

handle(_Cmd, _Player, _CmdData) ->
    {error, "pp_czone no match"}.

handle_general_result(Cmd, #player_status{sid = Sid, server_id = ServerId}, Fun) ->
    case Fun(ServerId) of
        {ok, Data} ->
            {ok, BinData} = pt_103:write(Cmd, Data),
            lib_server_send:send_to_sid(Sid, BinData);
        _Error ->
            ignore
    end.

