%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     跳一跳：协议入口
%%% @end
%%%-------------------------------------------------------------------
-module(pp_jump_act).
-author("liushl").

-include("server.hrl").
-include("def_module.hrl").
-include("custom_act.hrl").
-include("jump_act.hrl").
-include("common.hrl").
%% API
-export([handle/3]).

%% 跳一跳：活动数据
handle(_Cmd = 45901, Player, [SubType]) ->
    Type = ?CUSTOM_ACT_TYPE_JUMP_ACT,
    custom_act_jump_act:ask_act_info(Player, Type, SubType),
    ok;
%% 跳一跳：设置匹配状态
handle(Cmd = 45902, Player, [SubType, 1]) ->
    Type = ?CUSTOM_ACT_TYPE_JUMP_ACT,
    case custom_act_jump_act:match_start(Player, Type, SubType) of
        {false, Error} ->
            {ok, Bin} = pt_459:write(Cmd, [Error, 1]),
            lib_server_send:send_to_sid(Player#player_status.sid, Bin);
        _ ->
            ok
    end,
    ok;
%% 取消匹配
handle(_Cmd = 45902, Player, [_SubType, 0]) ->
    {true, custom_act_jump_act:cancel_match(Player)};
%% 跳一跳：跳跃状态
handle(_Cmd = 45909, Player, [RoomId, Status, SubType])
    when Status =:= 1 orelse Status =:= 2 ->
    case custom_act_jump_act:is_in_jump_room(Player, RoomId) of
        true when Status =:= 1  ->
            jump_act_center:ready_jump(SubType, RoomId, Player#player_status.id);
        true when Status =:= 2  ->
            jump_act_center:jump_end(SubType, RoomId, Player#player_status.id);
        false ->
            ok
    end,
    ok;
%% 跳一跳：跳跃
handle(_Cmd = 45910, Player, [RoomId, X, Y, Status, Time, TK, SubType])
    when Status =:= 1 orelse Status =:= 0 ->
    Hex = util:md5(lists:concat([RoomId, X, Y, Status, Time, ?JUMP_ACT_TICKET])),
    case Hex =:= TK of
        true  ->
            custom_act_jump_act:is_in_jump_room(Player, RoomId)
                andalso jump_act_center:jump(SubType, RoomId, Player#player_status.id, X, Y, Status, Time);
        false ->
            ?ERR("wrong ticket:~p", [{[RoomId, X, Y, Status, Time, TK]}]),
            ok
    end,
    ok;
%% 跳一跳：退出活动
handle(_Cmd = 45913, Player, []) ->
    case custom_act_jump_act:is_in_jump_scene(Player#player_status.scene) of
        true  ->
            Arg = custom_act_jump_act:out_arg(),
            NewPlayer = lib_scene:change_scene(Player, 0, 0, 0, 0, 0, Arg),
            jump_act_center:leave(Player#player_status.copy_id, Player#player_status.id),
            {true, NewPlayer};
        false ->
            ok
    end;
%% 跳一跳：单人进入
handle(Cmd = 45919, Player, [SubType]) ->
    Type = ?CUSTOM_ACT_TYPE_JUMP_ACT,
    case custom_act_jump_act:single_jump(Player, Type, SubType) of
        {false, Error} ->
            {ok, Bin} = pt_459:write(Cmd, [Error]),
            lib_server_send:send_to_sid(Player#player_status.sid, Bin);
        _ ->
            ok
    end,
    ok;
handle(_Cmd, _Player, _Arg) ->
    erlang:display({"[LIUSHL]", erlang:localtime(), ?MODULE, ?LINE, _Cmd, _Arg}),
    ok.