%% ---------------------------------------------------------------------------
%% @doc pp_team
%% @author ming_up@foxmail.com
%% @since  2016-11-01
%% @deprecated 组队协议
%% ---------------------------------------------------------------------------
-module(pp_team).
-export([handle/3]).

-include("relationship.hrl").
-include("common.hrl").
-include("server.hrl").
-include("team.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("predefine.hrl").
-include("def_module.hrl").
-include("def_fun.hrl").
-include("scene.hrl").
-include("guild.hrl").
-include("role.hrl").
-include("activitycalen.hrl").
%% 创建队伍(玩家自己组队只能组本服队伍)
handle(24000, Ps, [ActId, ActSubId, SceneId, LvMin, LvMax, JoinType, Password]) ->
    #player_status{
        figure = #figure{lv = Lv}, scene = Scene, scene_pool_id = ScenePoolId
        ,x = X, y = Y
    } = Ps,
    mod_team_enlist:clear_role_match(Ps#player_status.id),
    IsTask = case lists:member({ActId, ActSubId}, [{2,1}]) of
        true when 75 =< Lv andalso Lv =< 104 ->
            lib_role_task:is_during_task(10006000);
        _->
            false
    end,
    JoinType1 = lib_team:calc_join_type(Scene, JoinType),
    TeamArgs = #team_args{
        act_id = ActId, act_sub_id = ActSubId, scene_id = SceneId, lev_min = LvMin
        ,lev_max = LvMax, join_type = JoinType1, is_task = IsTask, password = Password
        ,leader_scene_id = Scene, leader_scene_pool_id = ScenePoolId, leader_x = X
        ,leader_y = Y
    },
    case lib_team:create_team(Ps, TeamArgs) of
        {ok, NewPS} ->
            {ok, NewPS};
        {false, ErrCode} ->
            {ErrorCodeInt, ErrorCodeArgs} = util:parse_error_code(ErrCode),
            {ok, BinData} = pt_240:write(24000, [ErrorCodeInt, ErrorCodeArgs]),
            lib_server_send:send_to_sid(Ps#player_status.sid, BinData)
    end;

%% 加入队伍
handle(24002, Ps, [TeamId, Password]) ->
    #player_status{
        figure = #figure{lv = Lv}, sid = Sid, team = #status_team{team_id = MyTeamId}
        ,change_scene_sign = IsChangeSceneSign
    } = Ps,
    OpenLv = lib_module:get_open_lv(?MOD_TEAM),
    Result = if
        MyTeamId =/= 0 -> {false, ?ERRCODE(err240_in_team)};
        IsChangeSceneSign =/= 0 -> {false, ?ERRCODE(err240_changing_scene)};
        Lv < OpenLv -> {false, ?ERRCODE(err240_not_enough_lv_to_join_team)};
        true ->
            Mb = lib_team_api:thing_to_mb(Ps),
            OpenDay = util:get_open_day(),
            Msg = {'check_password_before_join_request', Mb, OpenDay, Password},
            case mod_team:cast_to_team(TeamId, Msg) of
                false ->
                    Ret = lib_team_api:is_local_team(TeamId),
                    if Ret  -> {false, ?ERRCODE(err240_team_not_exist_1)};
                       true -> {false, ?ERRCODE(err240_cls_team_join)}
                    end;
                _A -> ok
            end
    end,
    case Result of
        {false, ErrCode} ->
            {ok, BinData} = pt_240:write(24002, [ErrCode, [], TeamId]),
            lib_server_send:send_to_sid(Sid, BinData);
        _ -> skip
    end;

%% 队长回应加入队伍请求
handle(24004, Status, [ResRequest]) ->
    #player_status{
        id = LeaderId, sid = Sid, team = #status_team{team_id=TeamId}
        ,change_scene_sign = IsChangeSceneSign
    } = Status,
    if
        IsChangeSceneSign =/= 0 ->
            lib_server_send:send_to_sid(Sid, pt_240, 24004, [?ERRCODE(err240_changing_scene)]);
        TeamId > 0 ->
            case ResRequest of
                [{IsAgree, _SerId, RoleId}] -> %% 单人处理
                    mod_team:cast_to_team(TeamId, {'join_team_response', IsAgree, RoleId, LeaderId});
                [] ->
                    mod_team:cast_to_team(TeamId, {'join_team_response', LeaderId, reject_all});
                _ ->
                    lib_server_send:send_to_sid(Sid, pt_240, 24004, [?SUCCESS])
            end;
        true ->
            lib_server_send:send_to_sid(Sid, pt_240, 24004, [?ERRCODE(err240_no_team)])
    end;

%% 离开队伍
handle(24005, Status, _Data) ->
    #player_status{id = RoleId, team = #status_team{team_id=TeamId}} = Status,
    mod_team:cast_to_team(TeamId, {'quit_team', RoleId, 0}),
    ok;

% pt_24006 _[1,0,0,1,500,[4294967358, 4294967303]]
%% 邀请别人加入队伍
handle(24006, Ps, [ActivityId, SubTypeId, SceneId, LvMin, LvMax, InviteType, InviteList]) ->
    #player_status{
        id = InviterId, sid = Sid, scene = InviteScene, x = X, y = Y
        ,team = #status_team{team_id=TeamId}, scene_pool_id = ScenePoolId
        ,figure = #figure{lv = Lv}
      } = Ps,
    %% 只能邀请本服
    LocalSvr = config:get_server_id(),
    MentorArgs = [],
    OpenLv = lib_module:get_open_lv(?MOD_TEAM),
    InviteList1 = lists:filter(fun(IdX) -> 
        SvrId = mod_player_create:get_real_serid_by_id(IdX),
        case LocalSvr == SvrId of
            true ->
                OppoLv = lib_role:get_role_lv(IdX),
                OppoLv >= OpenLv;
            _ ->
                false
        end
    end, InviteList),
    if
        InviteList1 == [] andalso InviteList == [] ->
            lib_server_send:send_to_sid(Sid, pt_240, 24006, [?ERRCODE(err240_34_no_invite_role)]);
        InviteList =/= [] andalso InviteList1 == [] ->
            lib_server_send:send_to_sid(Sid, pt_240, 24006, [?ERRCODE(err240_oppo_lv_litmit)]);
        Lv < OpenLv ->
            lib_server_send:send_to_sid(Sid, pt_240, 24006, [?ERRCODE(err112_lv_not_satisfy)]);
        TeamId > 0 ->
            mod_team:cast_to_team(TeamId, {'invite_request', InviteList1, InviterId, InviteScene, ActivityId, SubTypeId, LvMin, LvMax, MentorArgs});
        true ->
            InviteMsg = {'invite_request', InviteList1, InviterId, InviteScene, ActivityId, SubTypeId, LvMin, LvMax, InviteType, MentorArgs},
            JoinType1 = lib_team:calc_join_type(InviteScene, 2),
            TeamArgs = #team_args{
                act_id = ActivityId, act_sub_id = SubTypeId, scene_id = SceneId, lev_min = LvMin
                ,lev_max = LvMax, join_type = JoinType1, key_value_list = [{invite, InviteMsg}]
                ,leader_scene_id = InviteScene, leader_scene_pool_id = ScenePoolId, leader_x = X
                ,leader_y = Y
            },
            case lib_team:create_team(Ps, TeamArgs) of
                {ok, NewPS} ->
                    {ok, NewPS};
                _ -> ok
            end
    end;

%% 被邀请人回应邀请请求
handle(24008, Ps, [ResponseList]) ->
    #player_status{figure = #figure{lv = Lv}, sid=Sid, team=#status_team{team_id=MyTeamId}, scene=Scene, copy_id=CopyId, x=X, y=Y, action_lock = Lock} = Ps,
    OpenLv = lib_module:get_open_lv(?MOD_TEAM),
    case ResponseList of
        [{TeamId, Res}] -> %% 单人处理
            if
                Lv < OpenLv ->
                    lib_server_send:send_to_sid(Sid, pt_240, 24008, [?ERRCODE(err240_not_enough_lv_to_join_team), []]);
                MyTeamId == 0 ->
                    MbInfo = lib_team_api:thing_to_mb(Ps),
                    case mod_team:cast_to_team(TeamId,  {'invite_response', Res, MbInfo, [Scene, CopyId, X, Y], Lock}) of
                        false ->
                            Ret = lib_team_api:is_local_team(TeamId),
                            ErrCode =  if Ret -> ?ERRCODE(err240_team_disappear);
                                true -> ?ERRCODE(err240_cls_team_join)
                            end,
                            lib_server_send:send_to_sid(Sid, pt_240, 24008, [ErrCode, []]);
                        _ -> ok
                    end;
                true ->
                    lib_server_send:send_to_sid(Sid, pt_240, 24008, [?ERRCODE(err240_in_team), []])
            end;
        _ ->
            lib_server_send:send_to_sid(Sid, pt_240, 24008, [?SUCCESS, []])
    end;

%% 踢出队伍
handle(24009, Ps, [KickId]) ->
    #player_status{id = LeaderId, team=#status_team{team_id=TeamId}, sid = Sid} = Ps,
    if
        TeamId < 1 ->
            {ok, BinData} = pt_240:write(24009, [?ERRCODE(err240_no_team)]),
            lib_server_send:send_to_sid(Sid, BinData);
        LeaderId == KickId ->
            {ok, BinData} = pt_240:write(24009, [?ERRCODE(err240_kick_himself)]),
            lib_server_send:send_to_sid(Sid, BinData);
        true ->
            mod_team:cast_to_team(TeamId,  {'kick_out', KickId, LeaderId, 0})
    end;

%% 请求队伍信息
handle(24010, Ps, []) ->
    #player_status{team=#status_team{team_id=TeamId}, id = Id} = Ps,
    if
        TeamId < 1 -> skip;
        true -> mod_team:cast_to_team(TeamId,  {'get_team_info', Id})
    end;

%% 委任队长
handle(24011, Ps, [NewLeaderId]) ->
    #player_status{id = OldLeaderId, team=#status_team{team_id=TeamId}} = Ps,
    if
        OldLeaderId == NewLeaderId -> skip;
        TeamId > 0 -> mod_team:cast_to_team(TeamId, {'change_leader', NewLeaderId, OldLeaderId});
        true -> skip
    end;

%% 查看队伍
handle(24012, Ps, [ActivityId, SubTypeId, SceneId]) ->
    #player_status{sid=Sid, server_num = ServerNum} = Ps,
    ServerId = config:get_server_id(),
    ClsType = case lib_team:is_cls_target(ActivityId, SubTypeId) of
        true  -> ?NODE_CENTER;
        false -> ?NODE_GAME
    end,
    ChatZone = 0,
    OpenDay  = util:get_open_day(),
    mod_team_enlist:cast_to_team_enlist(ClsType, {'get_all_team', Sid, ActivityId, SubTypeId, SceneId, ServerNum, ServerId, ChatZone, OpenDay});

%% 申请入队（点击玩家），仅支持本服玩家
handle(24016, Ps, [PlayerId]) ->
    #player_status{
        figure = #figure{lv = Lv},
        team = #status_team{team_id=MyTeamId},
        change_scene_sign = IsChangeSceneSign} = Ps,
    Result = if
        Lv =< ?TEAM_NEED_LV -> {false, ?ERRCODE(err240_not_enough_lv_to_join_team)};
        MyTeamId /= 0 -> {false, ?ERRCODE(err240_in_team)};
        IsChangeSceneSign /= 0 -> {false, ?ERRCODE(err240_changing_scene)};
        true ->
            case misc:get_player_process(PlayerId) of
                Pid when is_pid(Pid) ->
                    Mb = lib_team_api:thing_to_mb(Ps),
                    % ?PRINT("~p~n", [Mb#mb.help_type]),
                    lib_player:apply_cast(Pid, ?APPLY_CAST_STATUS, lib_team, join_player_team, [Mb]);
                _ -> {false, ?ERRCODE(err240_other_offline)}
            end
    end,
    case Result of
        {false, ErrCode} ->
            {ok, BinData} = pt_240:write(24016, [ErrCode]),
            lib_server_send:send_to_sid(Ps#player_status.sid, BinData);
        _ -> skip
    end;

%% 更改组队目标
handle(24017, Ps, [ActivityId, SubTypeId, SceneId, LvMin, LvMax, JoinType, Password]) ->
    #player_status{id = RoleId, team=#status_team{team_id=TeamId}} = Ps,
    if
        TeamId > 0 -> 
            TeamArgs = #team_args{
                act_id = ActivityId, act_sub_id = SubTypeId, scene_id = SceneId, lev_min = LvMin
                ,lev_max = LvMax, join_type = JoinType, password = Password
            },
            mod_team:cast_to_team(TeamId, {'change_team_enlist', RoleId, TeamArgs});
        true -> skip
    end;

%% 发起投票
handle(24020, Ps, [ActivityId, SubTypeId]) ->
    #player_status{id = RoleId, team=#status_team{team_id=TeamId, positon = Postion}, sid = Sid} = Ps,
    if
        TeamId =:= 0 ->
            {ok, BinData} = pt_240:write(24020, [?ERRCODE(err240_not_on_team), "", ActivityId, SubTypeId, 0, 0]),
            lib_server_send:send_to_sid(Sid, BinData);
        Postion =/= ?TEAM_LEADER ->
            {ok, BinData} = pt_240:write(24020, [?ERRCODE(err240_not_leader_to_arbitrate_req), "", ActivityId, SubTypeId, 0, 0]),
            lib_server_send:send_to_sid(Sid, BinData);
        true ->
            case lib_team:check_start_team_target(Ps, ActivityId, SubTypeId) of
                {ok, Data} ->
                    mod_team:cast_to_team(TeamId, {'arbitrate_req', RoleId, ActivityId, SubTypeId, Data});
                {false, Code} ->
                    {ErrorCodeInt, ErrorCodeArgs} = util:parse_error_code(Code),
                    {ok, BinData} = pt_240:write(24020, [ErrorCodeInt, ErrorCodeArgs, ActivityId, SubTypeId, 0, 0]),
                    lib_server_send:send_to_sid(Sid, BinData);
                {false, _, Code} ->
                    {ErrorCodeInt, ErrorCodeArgs} = util:parse_error_code(Code),
                    {ok, BinData} = pt_240:write(24020, [ErrorCodeInt, ErrorCodeArgs, ActivityId, SubTypeId, 0, 0]),
                    lib_server_send:send_to_sid(Sid, BinData)
            end
    end;

%% 队员投票
handle(24021, Ps, [ArbitrateId, Res]) ->
    #player_status{id = RoleId, team = StatusTeam} = Ps,
    case StatusTeam of
        #status_team{team_id=TeamId, arbitrate_info = [ActivityId, SubTypeId, ArbitrateId]} when TeamId > 0 ->
            if
                Res =:= 1 ->
                    case lib_team:check_start_team_target(Ps, ActivityId, SubTypeId) of
                        {ok, Data} ->
                            mod_team:cast_to_team(TeamId, {'arbitrate_res', RoleId, ArbitrateId, {1, {ok, Data}}});
                        {false, Code} ->
                            OtherErrorCode = lib_team:my_code_to_other_code(Ps#player_status.figure#figure.name, Code),
                            Error = {false, OtherErrorCode, Code},
                            mod_team:cast_to_team(TeamId, {'arbitrate_res', RoleId, ArbitrateId, {0, Error}});
                        {false, _Code, _MyErrorCode} = Error ->
                            mod_team:cast_to_team(TeamId, {'arbitrate_res', RoleId, ArbitrateId, {0, Error}})
                    end;
                true ->
                    mod_team:cast_to_team(TeamId, {'arbitrate_res', RoleId, ArbitrateId, {Res, {ok, []}}})
            end;
        _ ->
            skip
    end;

%% 匹配队伍--zzy
handle(24023, Ps, [ActivityId, SubTypeId]) ->
    #player_status{figure = #figure{lv = Lv}, sid = Sid, team = StatusTeam, action_lock = Lock, scene = Scene} = Ps,
    SceneType = lib_scene:get_scene_type(Scene),
    if
        Lv =< ?TEAM_NEED_LV -> skip;
        StatusTeam#status_team.team_id > 0 -> skip;
        Lock =/= free ->
            {ok, BinData} = pt_240:write(24030, [Lock]),
            lib_server_send:send_to_sid(Sid, BinData);
        SceneType =/= ?SCENE_TYPE_OUTSIDE ->
            {ok, BinData} = pt_240:write(24030, [?ERRCODE(cannot_transferable_scene)]),
            lib_server_send:send_to_sid(Sid, BinData);
        ActivityId =:= 1 ->
            {ok, BinData} = pt_240:write(24030, [?ERRCODE(err240_choose_target_first)]),
            lib_server_send:send_to_sid(Sid, BinData);
        true ->
            case StatusTeam#status_team.match_info of
                [] ->
                    ActivityList = [{ActivityId, SubTypeId}],
                    case lib_team:common_check(Ps, ActivityId, SubTypeId) of
                        true ->
                            case lib_team_match:get_match_mod(ActivityId, SubTypeId) of
                                undefined ->
                                    Mb = lib_team_api:thing_to_mb(Ps),
                                    IsTask = case lists:member({ActivityId,SubTypeId},[{2,1}]) of
                                        true when 75=<Lv andalso Lv=<104 ->
                                            lib_role_task:is_during_task(10006000);
                                        _->
                                            false
                                    end,
                                    mod_team_enlist:match_teams(Mb#mb{is_task=IsTask}, Sid, ActivityList);
                                Mod ->
                                    Mod:match_teams(Ps, ActivityId, SubTypeId)
                            end,
                            {ok, Ps#player_status{team = StatusTeam#status_team{match_info = ActivityList}}};
                        {false, _OtherErrorCode, Code} when is_integer(Code) ->
                            {ok, BinData} = pt_240:write(24023, [Code, ActivityId, SubTypeId]),
                            lib_server_send:send_to_sid(Sid, BinData);
                        {false, _OtherErrorCode, {Code, Arg}} ->
                            {ok, BinData} = pt_240:write(24038, [Code, Arg]),
                            lib_server_send:send_to_sid(Sid, BinData);
                        _ ->
                            ok
                    end;
                _ ->
                    {ok, BinData} = pt_240:write(24023, [?ERRCODE(err240_change_when_matching), ActivityId, SubTypeId]),
                    lib_server_send:send_to_sid(Sid, BinData)
            end
    end;

%% 跟战
handle(24024, Ps, [Type, FollowId]) ->
    #player_status{id=Id, team=#status_team{team_id=TeamId}} = Ps,
    if
        TeamId > 0 -> mod_team:cast_to_team(TeamId, {'follow_someone', Type, Id, FollowId});
        true -> skip
    end,
    ok;

%% 查看队员场景信息
handle(24025, Ps, [InfoId]) ->
    #player_status{id=Id, team=#status_team{team_id=TeamId}} = Ps,
    if
        TeamId > 0 -> mod_team:cast_to_team(TeamId, {'get_mb_scene', Id, InfoId});
        true -> skip
    end,
    ok;

%% 查询跟战状态
handle(24026, Ps, []) ->
    #player_status{id=Id, sid=Sid, team=#status_team{team_id=TeamId}} = Ps,
    if
        TeamId > 0 -> mod_team:cast_to_team(TeamId, {'get_follow_id', Id});
        true ->
            {ok, BinData} = pt_240:write(24026, [0]),
            lib_server_send:send_to_sid(Sid, BinData)
    end,
    ok;

%% 批量查看队伍人数
handle(24032, Ps, [TeamIds]) ->
    mod_team_enlist:get_teams_num(Ps#player_status.sid, TeamIds),
    ok;

%% 取消别人跟战
handle(24039, Ps, [ ]) ->
    #player_status{id = RoleId, team=#status_team{team_id=TeamId}} = Ps,
    if
        TeamId > 0 -> mod_team:cast_to_team(TeamId, {'cancel_other_follow', RoleId});
        true -> skip
    end;


%% 获取活动剩余次数--zzy
handle(24042, Ps, [ActivityId])->
    % ?PRINT("24042~p~n",[ActivityId]),
    #player_status{id = _RoleId, team=#status_team{team_id=TeamId}} = Ps,
    if
        TeamId > 0 ->
            lib_team:send_ac_left_time(Ps,ActivityId);
        true -> skip
    end;

%% 广播队伍野外修炼
handle(24043, Ps, [SceneId, MonId, X, Y]) ->
    #player_status{team=#status_team{team_id=TeamId}} = Ps,
    if
        TeamId > 0 -> mod_team:cast_to_team(TeamId, {'outdoor_training', SceneId, MonId, X, Y});
        true -> skip
    end;

%% 附近队伍匹配
handle(24044, Ps, [TeamIds]) ->
    #player_status{sid = Sid, team=#status_team{team_id=TeamId, match_info = MatchInfo} = StatusTeam} = Ps,
    if
        TeamId > 0 -> skip;
        TeamIds =:= [] ->
            {ok, BinData} = pt_240:write(24044, [?ERRCODE(err240_no_teams_for_match)]),
            lib_server_send:send_to_sid(Sid, BinData);
        MatchInfo =:= [] ->
            Mb = lib_team_api:thing_to_mb(Ps),
            mod_team_enlist:cast_to_team_enlist({'near_teams_match', Sid, Mb, TeamIds}),
            {ok, Ps#player_status{team = StatusTeam#status_team{match_info = TeamIds}}};
        true ->
            {ok, BinData} = pt_240:write(24044, [?ERRCODE(err240_change_when_matching)]),
            lib_server_send:send_to_sid(Sid, BinData)
    end;

%% 队长召集队友
handle(24045, Ps, _) ->
    #player_status{
        id = RoleId, team = #status_team{team_id = TeamId}
        ,scene = Scene, scene_pool_id = ScenePoolId, copy_id = CopyId
        ,x = X, y = Y
    } = Ps,
    if
        TeamId > 0 -> 
            Msg = {call_together, RoleId, Scene, ScenePoolId, CopyId, X, Y},
            mod_team:cast_to_team(TeamId, Msg);
        true ->
            skip
    end;

%% 查询申请列表
handle(24047, Ps, []) ->
    #player_status{team=#status_team{team_id=TeamId, positon = Pos}, id = RoleId} = Ps,
    if
        TeamId > 0 andalso Pos =:= ?TEAM_LEADER ->
            mod_team:cast_to_team(TeamId, {'get_reqlist', RoleId});
        true -> skip
    end;

%% 广播召集-前往
handle(24048, Player, [Leader, Scene]) ->
    case lib_team:transfer_to_leader_scene(Player, Leader, Scene) of
        {false, Err} ->
            {ok, BinData} = pt_240:write(24048, [Err, 0, 0]),
            lib_server_send:send_to_sid(Player#player_status.sid, BinData);
        ok ->
            ok
    end;

handle(24050, PS, []) ->
    #player_status{team = #status_team{exp_scale = ExpScale}, sid = Sid} = PS,
    {ok, BinData} = pt_240:write(24050, [ExpScale]),
    lib_server_send:send_to_sid(Sid, BinData);

%% 1：附近玩家（每次请求只会发N条过来） 2：帮派成员（全量列表） 3：好友（全量列表）
handle(24053, PS, [ActivityId, SubTypeId, Position, Type]) ->
    #player_status{id = RoleId, figure = #figure{sex = Sex}, scene = Scene} = PS,
    if
        Type =:= 1 ->
            mod_chat_agent:get_can_team_role(
                RoleId, Sex, ActivityId, SubTypeId, Position, Type, Scene, []
            );
        Type =:= 2 ->
            Guild = PS#player_status.guild,
            GuildId = Guild#status_guild.id,
            case GuildId > 0 of
                true  ->
                    Members = mod_guild:get_guild_member_id_list(GuildId) -- [RoleId],
                    mod_chat_agent:get_can_team_role(
                        RoleId, Sex, ActivityId, SubTypeId, Position, Type, Scene, Members
                    );
                false -> ok
            end;
        Type =:= 3 ->
            Friends = lib_relationship:get_friend_id_on_dict(RoleId),
            mod_chat_agent:get_can_team_role(
                RoleId, Sex, ActivityId, SubTypeId, Position, Type, Scene, Friends
            ),
            ok;
        true -> ok
    end;
handle(24054, Ps, [ActivityId, SubTypeId]) ->
    #player_status{id = RoleId, team=#status_team{team_id=TeamId, positon = Postion}, sid = Sid} = Ps,
    MentorArgs = [],
    if
        TeamId =:= 0 ->
            {ok, BinData} = pt_240:write(24054, [?ERRCODE(err240_not_on_team)]),
            lib_server_send:send_to_sid(Sid, BinData);
        Postion =/= ?TEAM_LEADER ->
            {ok, BinData} = pt_240:write(24054, [?ERRCODE(err240_not_leader)]),
            lib_server_send:send_to_sid(Sid, BinData);
        true ->
            mod_team:cast_to_team(TeamId, {'set_matching', RoleId, ActivityId, SubTypeId, MentorArgs})
    end;

handle(24055, Ps, [JoinType]) ->
    #player_status{id = RoleId, team=#status_team{team_id=TeamId}} = Ps,
    if
        TeamId =:= 0 ->
            skip;
        true ->
            mod_team:cast_to_team(TeamId, {'set_join_type', RoleId, JoinType})
    end;
%% 提醒队长开车
handle(24056, Player, _) ->
    #player_status{sid = Sid, team=#status_team{team_id=TeamId, positon = Position}} = Player,
    if
        TeamId =:= 0 -> ignore;
        Position =:= ?TEAM_LEADER -> ignore;
        true ->
            mod_team:cast_to_team(TeamId, {'notice_leader_go', Sid})
    end,
    ok;
handle(24058, Player, [TeamId, Password, Power]) ->
    #player_status{id = RoleId, sid = Sid, team=#status_team{team_id = TeamId1, positon = Position}} = Player,
    OpenDay = util:get_open_day(),
    OpenDayResult = case data_activitycalen:get_ac(?MOD_DUNGEON, 14, 1) of
        #base_ac{open_day = [{MinOpen, _}]} ->
            OpenDay >= MinOpen;
        _ ->
            true
    end,
    if 
        TeamId   =:= 0 -> ignore;            %% 队伍id
        TeamId1  =/= TeamId -> ignore;       %% 不是当前队伍
        Position =/= ?TEAM_LEADER -> ignore; %% 不是队长
        not OpenDayResult -> ignore;
        true ->
            Node = clusters_node:get_node(), 
            ClsType = case TeamId rem 2 of 0 -> ?NODE_CENTER; _ -> ?NODE_GAME end,
            mod_team_enlist:set_up_password(ClsType, Node, Sid, RoleId, TeamId, Password, Power)
    end,
    ok;
%% 申请入队(有密码)
handle(24059, Player, [TeamId, Password]) ->
    #player_status{
        figure = #figure{lv = Lv},
        sid = Sid,
        team = #status_team{team_id=MyTeamId},
        change_scene_sign = IsChangeSceneSign} = Player,
    OpenDay = util:get_open_day(),
    OpenDayResult = case data_activitycalen:get_ac(?MOD_DUNGEON, 14, 1) of
        #base_ac{open_day = [{MinOpen, _}]} ->
            OpenDay >= MinOpen;
        _ ->
            true
    end,
    Result = if
        MyTeamId /= 0 -> {false, ?ERRCODE(err240_in_team)};
        IsChangeSceneSign /= 0 -> {false, ?ERRCODE(err240_changing_scene)};
        Lv =< ?TEAM_NEED_LV -> {false, ?ERRCODE(err240_not_enough_lv_to_join_team)};
        not OpenDayResult -> {false, ?ERRCODE(err222_6_not_enough_server_day)};
        true ->
            Mb = lib_team_api:thing_to_mb(Player),
            case mod_team:cast_to_team(TeamId,  {'join_team_request_with_password', Mb, Password}) of
                false ->
                    Ret = lib_team_api:is_local_team(TeamId),
                    if Ret -> {false, ?ERRCODE(err240_team_disappear)};
                        true -> {false, ?ERRCODE(err240_cls_team_join)}
                    end;
                _ -> ok
            end
    end,
    case Result of
        {false, ErrCode} ->
            {ok, BinData} = pt_240:write(24002, [ErrCode, [], TeamId]),
            lib_server_send:send_to_sid(Sid, BinData);
        _ -> skip
    end,
    ok;



handle(24061, Player, _) ->
    #player_status{sid = Sid} = Player,
    ServerId = config:get_server_id(),
    clusters_node:apply_cast(mod_unite_lv, send_world_lv, [ServerId, Sid]),
    ok;


handle(_Cmd, _Status, _Data) ->
    ?DEBUG("pp_team no match", []),
    {error, "pp_team no match"}.
