%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     2018_12_06
%%% @end
%%%-------------------------------------------------------------------
-module(fix_team_id).
-author("liushl").
-include("predefine.hrl").
-include("common.hrl").
-include("server.hrl").
-include("team.hrl").
%% API
-export([
    fix_team_id/0,
    fix_my_local_team/1
]).

fix_team_id() ->
    Onlines = ets:tab2list(?ETS_ONLINE),
    [
        lib_player:apply_cast(Pid, ?APPLY_CAST_STATUS, ?MODULE, fix_my_local_team, [])
        || #ets_online{pid = Pid} <- Onlines
    ].

fix_my_local_team(Player) ->
    #player_status{team = #status_team{team_id = TeamId}, sid = Sid} = Player,
    if
        TeamId =:= 0 -> Player;
        true ->
            case TeamId rem 2 of
                2 -> Player;
                1 ->
                    case ets:lookup(?ETS_TEAM, TeamId) of
                        [] ->
                            {ok, Bin} = pt_240:write(24005, [1]),
                            lib_server_send:send_to_sid(Sid, Bin),
                            Player#player_status{team = #status_team{}};
                        _  ->
                            Player
                    end;
                _ -> Player
            end
    end.