%%%===================================================================
%%% @author zmh
%%% @doc 擂台赛  元宝擂台(钻石争霸战)
%%% @end
%%% @update by zzy 2017-11-08
%%%===================================================================
-module(lib_role_drum).
-include("server.hrl").
-include("predefine.hrl").
-include("drumwar.hrl").
-include("battle.hrl").
-include("common.hrl").
-include("scene.hrl").
-include("skill.hrl").
-include("figure.hrl").
-include("guild.hrl").
-include("attr.hrl").
-include("goods.hrl").
-include("errcode.hrl").
-include("def_event.hrl").
-include("rec_event.hrl").
-include("def_fun.hrl").
-include("def_module.hrl").
-include("fake_role.hrl").
-include("chat.hrl").
-include("transform.hrl").

-compile(export_all).

%%复活
revive(PS) ->
    #player_status{x=X,y=Y} = PS,
    case is_in_drumwar(PS) of
        true->
            {XA,YA} = urand:list_rand(mod_drumwar_mgr:get_kv(?REVIVE_LOCATION)),
            {XA,YA};
        false-> {X, Y}
    end.

%%断线重连
re_login(PS) ->
    #player_status{id = RoleId, scene = Scene} = PS,
    AllScene = mod_drumwar_mgr:get_kv(?READY_SCENE) ++ mod_drumwar_mgr:get_kv(?WAR_SCENE),
    case lists:member(Scene,AllScene) of
        true->
            lib_scene:player_change_default_scene(RoleId, [{change_scene_hp_lim,1}]),true;
        false-> false
    end.

%%上线
login(RoleID)->
    case db:get_row(io_lib:format(?SQL_DRUM_GET,[RoleID])) of
        [DrumID, One, Zid, Act, Deal, Choice, _Time]->
            case DrumID == get_id() of
                true-> %%本期数据 不清除
                    RoleDrum = #role_drum{
                        drumid = DrumID, one = One,
                        zone = Zid, action = Act,
                        deal = Deal, choice = util:bitstring_to_term(Choice)
                    };
                _-> %%上期数据 非活动日不清理
                    db:execute(io_lib:format(<<"DELETE FROM role_drum where `rid`=~p">>,[RoleID])),
                    RoleDrum = #role_drum{}
            end;
        _ ->
            RoleDrum = #role_drum{}
    end,
    RoleDrum.

%%计算当前期ID
get_id()->
    {{Y,M,D}, _} = calendar:local_time(),
    Y*10000+M*100+D.

%% -------------------------------- 每日12点刷新 ---------------------------------
daily_clear(0) ->
    spawn(fun()-> daily_clear_help() end);
daily_clear(_Other) ->
    ok.

daily_clear_help() ->
    Day = utime:day_of_week(),
    [OpenT] = data_drumwar:get_open(),
    case lists:keyfind(Day, 1, OpenT) of
        false-> ok;
        _->
            OnlineList = ets:tab2list(?ETS_ONLINE),
            mod_drumwar_mgr:clear_role_info(OnlineList)
    end.

%%获取活动状态
get_clock(Sid)->
    mod_drumwar_mgr:cast_center([{mod,?MODULE,get_clock_mgr,[node(),Sid]}]).

get_clock_mgr([Node,Sid],State)->
    #drumwar_mgr{state=S,etime=EndTime}=State,
    Now = utime:unixtime(),
    ReTime = max(EndTime,Now+1),
    % ?PRINT("~p:~p~n",[S,ReTime]),
    {ok, Bin} = pt_137:write(13700,[S,ReTime]),
    send_to_role(Node,Sid,Bin),
    State.

%%获取报名情况
get_sign(Uid,RoleDrum,Sid)->
    #role_drum{zone=Z,action=Act,deal=Deal,one=One,choice=OChoice}=RoleDrum,
    Choice = case utime:unixtime()>Deal of
        true->%%逾期
            [];
        _->
            OChoice
    end,
    mod_drumwar_mgr:cast_center([{mod,?MODULE,get_sign_mgr,[node(),Uid,Sid,One,Z,Act,Choice]}]).

get_sign_mgr([Node,RoleId,Sid,_One,Z,Act,Choice],State)->
    #drumwar_mgr{signs=Signs,history=History}=State,
    case lists:keyfind(RoleId,#drum_role.rid,Signs) of
        #drum_role{action = RAct, lose = Lose}->
            Res = 1;
        _->
            Lose = RAct = Res = 0
    end,
    Drums = [D||{D,_}<-History],
    ShowAct = case RAct =/= Act andalso Lose == 0 andalso RAct <12 of
        true -> Act;
        false -> max(Act, RAct)
    end,
    WinType = ?IF(Lose >=1, 0, 1),
    % ?PRINT("~p:~p:~p:~p:~p~n",[Res,One,Z,Act,Choice]),
    {ok,Bin} = pt_137:write(13701,[Res,Drums,WinType,Z,ShowAct,Choice]),
    send_to_role(Node,Sid,Bin),
    State.

%%报名
sign_up(Status)->
    #player_status{id=Uid,figure = #figure{lv = Lv}}=Status,
    CKList = [
        {check_lv,Lv},
        {check_cost, [{0, mod_drumwar_mgr:get_kv(?DRUMITEM), 1}], Status},
        {check_con},
        {check_open_day},
        {check_cast_center, get({?MODULE, sign_up})}
    ],
    case checklist(CKList) of
        true->
            put({?MODULE, sign_up}, true),
            mod_drumwar_mgr:cast_center([{mod, ?MODULE, check_sign_up, [node(), Uid]}]),
            {ok, Status};
        {false,Err}->
            {false,Err}
    end.

%%检查报名
check_sign_up([Node, Uid], #drumwar_mgr{state = ?SIGN} = State) ->
    case get_drum_role([Uid], State) of
        #drum_role{} ->
            {ok, Bin} = pt_137:write(13702, [?ERRCODE(err137_has_sign)]),
            ApplyArgs = [Uid, ?APPLY_CAST_STATUS, ?MODULE, sign_up_fail, [Bin]];
        false ->
            ApplyArgs = [Uid, ?APPLY_CAST_STATUS, ?MODULE, sign_up_cost, []]
    end,
    apply_to_local_role(Node, lib_player, apply_cast, ApplyArgs),
    State;
check_sign_up([Node, Uid], State) ->
    {ok, Bin} = pt_137:write(13702, [?ERRCODE(err137_not_sign_state)]),
    ApplyArgs = [Uid, ?APPLY_CAST_STATUS, ?MODULE, sign_up_fail, [Bin]],
    apply_to_local_role(Node, lib_player, apply_cast, ApplyArgs),
    State.

%%失败
sign_up_fail(Status, Bin) ->
    erase({?MODULE, sign_up}),
    lib_server_send:send_to_sid(Status#player_status.sid, Bin),
    Status.

%%回调玩家进程扣钱
sign_up_cost(Status) ->
    erase({?MODULE, sign_up}),
    case lib_goods_api:cost_object_list_with_check(Status, [{0, mod_drumwar_mgr:get_kv(?DRUMITEM), 1}], drumwar_sign, "drumwar_sign") of
        {true, NStatus,_}->
            DrumRole = make_drum_role(Status),
            mod_drumwar_mgr:cast_center([{mod, ?MODULE, sign_up_mgr, [DrumRole]}]),
            {ok,NStatus};
        _->
            {ok, Bin} = pt_137:write(13702, [?ERRCODE(err137_tool_not_enough)]),
            lib_server_send:send_to_sid(Status#player_status.sid, Bin),
            Status
    end.

%%模块回调  把逻辑放到这里
sign_up_mgr([DrumRole], State) when State#drumwar_mgr.state /= ?SIGN ->
    ?INFO("sign_up_mgr fail,~p~n", [DrumRole#drum_role.rid]),
    #drum_role{sid = Sid, node = Node}=DrumRole,
    {ok, Bin} = pt_137:write(13702, [?ERRCODE(err137_not_sign_state)]),
    send_to_role(Node, Sid, Bin),
    State;
sign_up_mgr([DrumRole], State)->
    #drum_role{rid = Uid, sid = Sid, node = Node}=DrumRole,
    #drumwar_mgr{signs = Signs} = State,
    case lists:keyfind(Uid, #drum_role.rid, Signs) of
        #drum_role{}->
            {ok, Bin} = pt_137:write(13702, [?ERRCODE(err137_has_sign)]),
            send_to_role(Node, Sid, Bin),
            State;
        _->
            {ok, Bin} = pt_137:write(13702, [?SUCCESS]),
            send_to_role(Node, Sid, Bin),
            %%写进db
            db_replace_drum_sign(DrumRole),
            log_drum_sign(DrumRole),
            NSigns = lists:keystore(Uid, #drum_role.rid, Signs, DrumRole),
            % ?PRINT("sign_up_mgr~p~n",[DrumRole]),
            State#drumwar_mgr{signs = NSigns}
    end.

%%进入准备区 进入的时候顺便更新一下数据
enter_ready(Status)->
    #player_status{scene=Scene,figure = #figure{lv=Lv}, action_lock=_S}=Status,
    CKList = [
        {check_lv,Lv},
        {check_con},
        {check_scene,Scene},
        {check_state,Status}
    ],
    case checklist(CKList) of
        true->
            DrumRole = make_drum_role(Status),
            mod_drumwar_mgr:cast_center([{mod, ?MODULE, enter_ready_mgr, [DrumRole]}]),
            ok;
        {false,Err}->
            {false,Err}
    end.

%%
enter_ready_mgr([DrumRole],State)->
    #drumwar_mgr{action = Act, state = _S, signs = Signs, sub_state = SubState, sub_time = SubEndTime, ready_out = ReadyOut, tip_info = TipInfo}=State,
    #drum_role{rid = RoleID, sid = _Sid, node = Node}=DrumRole,
    ODrumRole = lists:keyfind(RoleID, #drum_role.rid, Signs),
    CKList = [
        {check_ready_out, RoleID, ReadyOut},
        {check_sign, ODrumRole}
        % {check_live_and_action, ODrumRole, Act, SubState},
        % {check_drum_state, S, SubState}
    ],
    case checklist(CKList) of
        true->
            %%重新进入刷新下
            NDrumRole = enter_make_record(DrumRole, ODrumRole),
            NSigns = lists:keystore(RoleID, #drum_role.rid, Signs, NDrumRole),
            #drum_role{zid = Zid} = NDrumRole,
            WinList = mod_drumwar_mgr:get_zone_win(),
            {TipType, TipEndTime} = TipInfo,
            case lists:keyfind(Zid, 1, WinList) of
                {_, WinId, WinName} ->
                    {ok, Bin1} = pt_137:write(13712, [WinId, Zid, WinName]),
                    {ok, Bin2} = pt_137:write(13717, [Act, TipType, TipEndTime]),
                    Bin = << Bin1/binary, Bin2/binary >>;
                _->
                    {ok, Bin} = pt_137:write(13717, [Act, TipType, TipEndTime])
            end,
            ApplyArgs = [RoleID, ?APPLY_CAST_STATUS, ?MODULE, enter_ready_role, [Act, NDrumRole, SubState, SubEndTime, Bin]],
            apply_to_local_role(Node, lib_player, apply_cast, ApplyArgs),
            State#drumwar_mgr{signs = NSigns};
        {false, Err}->
            {ok, Bin} = pt_137:write(13704, [Err]),
            send_to_role(Node, RoleID, Bin),
            State
    end.

enter_ready_role(Status, Act, DrumRole, SubState, SubEndTime, Bin3)->
    #player_status{id = RoleId, sid = Sid, scene = OldScene, scene_pool_id = ScenePoolId,
        copy_id = CopyId, x = OldX, y = OldY, role_drum = RoleDrum} =Status,
    Scene = urand:list_rand(mod_drumwar_mgr:get_kv(?READY_SCENE)),
    {RandX, RandY, Circle} = urand:list_rand(mod_drumwar_mgr:get_kv(?READY_LOCATION)),
    {X, Y} = umath:rand_point_in_circle(RandX, RandY, Circle, 1),
    case is_in_drumready(Status) of
        true->
            Status;
        _->
            {ok, Bin0} = pt_137:write(13704, [?SUCCESS]),
            {ok, Bin1} = pt_137:write(13705, [Act, DrumRole]),
            {ok, Bin2} = pt_137:write(13703, [SubState, SubEndTime]),
            % ?PRINT("~p~n",[[SubState, SubEndTime]]),
            lib_server_send:send_to_sid(Sid, Bin0),
            lib_server_send:send_to_sid(Sid, Bin1),
            lib_server_send:send_to_sid(Sid, Bin2),
            lib_server_send:send_to_sid(Sid, Bin3),
            mod_drumwar_mgr:cast_center([{mod, ?MODULE, change_scene, [RoleId, Scene]}]),
            KeyValList = [{group,0}, {change_scene_hp_lim, 1}, {action_lock, ?ERRCODE(err137_can_not_change_scene_in_drumwar)}],
            NewStatus = lib_scene:change_scene(Status, Scene, 0, 0, X, Y, KeyValList),
            Out = #out{scene = OldScene, pool_id = ScenePoolId, copy_id = CopyId, x = OldX, y = OldY},
            NewRoleDrum = RoleDrum#role_drum{out = Out},
            LastStatus = NewStatus#player_status{role_drum = NewRoleDrum},
            lib_activitycalen:role_success_end_activity(LastStatus, ?MOD_DRUMWAR, 0, 1)
    end.

%%主动请求命数信息
apply_live_info(Status)->
    #player_status{sid=_Sid,id=RoleID}=Status,
    Uid = RoleID,
    mod_drumwar_mgr:cast_center([{mod,?MODULE,apply_live_mgr,[node(), Uid, RoleID]}]).

%%主动请求命数
apply_live_mgr([Node, Uid, RoleID],State)->
    #drumwar_mgr{signs = Signs}=State,
    case lists:keyfind(Uid, #drum_role.rid, Signs) of
        #drum_role{live = ALive, ai = 0, ruid = RUid}->
            BLive = mod_drumwar_mgr:get_alive(RUid, Signs);
        #drum_role{live = ALive, ai = 1}->
            BLive = 1, RUid = 0;
        _->
            ALive = 1, BLive = 1, RUid = 0
    end,
    {ok,Bin} = pt_137:write(13710,[ALive, RUid, BLive]),
    lib_role_drum:send_to_role(Node, RoleID, Bin),
    State.


%%进入擂台区
enter_war_local(RoleID, Uid, Group, Pos, ActionSt)->
    case lib_player:is_online_global(RoleID) of
        true->
            lib_player:apply_cast(RoleID, ?APPLY_CAST_STATUS, ?MODULE, enter_war_role, [Group, Uid, Pos, ActionSt]);
        _-> %%临阵不在 输 通知对方回准备区
            mod_drumwar_mgr:cast_center([{mod, ?MODULE, role_logout_mgr, [true, RoleID, 0]}])
    end.
enter_war_role(Status, Group, _Uid, Pos, ActionSt)->
    #player_status{
        id = RoleId, sid = Sid, skill = Skill,
        combat_power = Power, figure = #figure{lv = Lv}, battle_attr = BattleAttr
    }=Status,
    Scene = get_war(Group),
    case Pos of
        1-> RoleGroup = 1, {X, Y} = right_pos();
        _-> RoleGroup = 2, {X, Y} = left_pos()
    end,
    % ?PRINT("role ~p:~p:~p:~p~n",[Scene,Group,X,Y]),
    case is_in_drumready(Status) of
        true->
            EnterSoulList = [],
            AttrList = attr_conv:to_attr_tuple_list(BattleAttr#battle_attr.attr),
            BaseAttrList = lib_player_attr:get_list_base_attr_kv(AttrList),
            Args = {RoleId, Power, Lv, BaseAttrList, Scene, EnterSoulList, X, Y, Group, RoleGroup, ActionSt},
            mod_drumwar_mgr:cast_center([{mod, ?MODULE, enter_war_handle, Args}]),
            {ok, Bin} = pt_137:write(13703, [?TYPE_FIGHT, ActionSt+5]),
            lib_server_send:send_to_sid(Sid, Bin),
            #status_skill{tmp_skill_list = TmpSkill} = Skill,
            NewTmpSkill = add_tmp_skill(mod_drumwar_mgr:get_kv(?DRUM_SKILLS), TmpSkill),
            NStatus = Status#player_status{skill = Skill#status_skill{tmp_skill_list = NewTmpSkill}},
            lib_scene:change_scene(NStatus, Scene, 0, Group, X, Y, [{change_scene_hp_lim,1}, {group,RoleGroup}]);
        _->
            case is_in_drumwar(Status) of
                true->
                    %%异常,拉回主城
                    ?INFO("enter_war_role error,~p~n",[RoleId]),
                    lib_scene:player_change_default_scene(RoleId, [{action_free, ?ERRCODE(err137_can_not_change_scene_in_drumwar)}]),
                    mod_drumwar_mgr:cast_center([{mod, ?MODULE, role_logout_mgr, [true, RoleId, 0]}]),
                    Status;
                _-> %%此时人不在准备区和擂台区,修改状态和输
                    mod_drumwar_mgr:cast_center([{mod, ?MODULE, role_logout_mgr, [true, RoleId, 0]}]),
                    Status
            end
    end.

%%进入擂台操作
enter_war_handle(Args, State) ->
    #drumwar_mgr{signs = Signs, mons = _Mons} = State,
    {RoleId, _Power, _Lv, _BaseAttrList, Scene, _SoulList, _X, _Y, Group, _RoleGroup, _ActionSt} = Args,
    case lists:keyfind(RoleId, #drum_role.rid, Signs) of
        #drum_role{group = Group, pos = _Pos} = DrumRole->
            NDrumRole = DrumRole#drum_role{scene = Scene},
            NSigns = lists:keyreplace(RoleId, #drum_role.rid, Signs, NDrumRole),
            NewState = State#drumwar_mgr{signs = NSigns};
        _-> NewState = State
    end,
    NewState.

%%对手弃权
role_away_mgr([Uid],State)->
    #drumwar_mgr{action=Act,signs=Signs,choice=Choice,choose=Choose, sub_time = SubTime, sub_state = SubState, state = S}=State,
    {NSigns,NChoose}=case lists:keyfind(Uid,#drum_role.rid,Signs) of
        #drum_role{node=Node,rid=RoleID,sid=_Sid,action=Act,zid=Zid,win=W,figure=Figure,ruid=LUid,calc_type=0,scene = Scene}=D->%%得胜 回准备
            #figure{name=WName} = Figure,
            NdrumR=D#drum_role{action=Act+1,win=W+1,calc_type=1},
            {ok,Bin}=pt_137:write(13708,[1,4,Act]),
            {ok,Bin1} = pt_137:write(13705,[Act,NdrumR#drum_role{action=Act}]),
            [RankWar] = data_drumwar:get_rank_war(),
            [SeaWarT] = data_drumwar:get_sea_war(),
            ResultTime = case SubState == ?TYPE_FIGHT_ENTER of
                true -> ?IF(S == ?SEAWAR, SubTime + SeaWarT + 1, SubTime + RankWar + 1);
                false -> SubTime
            end,
            {ok,Bin2} = pt_137:write(13703,[?TYPE_SUCCESS,ResultTime]),
            BinData = << Bin/binary, Bin1/binary, Bin2/binary >>,
            lib_role_drum:send_to_role(Node, RoleID, BinData),
            lib_role_drum:update_role_drum(Node,RoleID,Zid,Act),
            case data_drumwar:get_war(Zid,Act) of
               [{WAward,_}] -> lib_role_drum:war_award(Node, Act, 1, RoleID, WAward);
                _-> ok
            end,
            case is_in_drumready(Scene) of
                false ->
                    spawn(fun()->
                        timer:sleep(5000),
                        clusters_center:apply_cast(Node,mod_drumwar_mgr,enter_ready_local,[RoleID])
                    end);
                true -> ok
            end,
            mod_drumwar_mgr:log_drum_war(?LOG_LOGOUT, Uid, LUid, Signs),
            %%第九场之后的要结算竞猜
            Act>=9 andalso begin
                spawn(fun()->mod_drumwar_mgr:account_guess_result(Zid,Act,Uid,WName,LUid,Choice) end)
            end,
            Signs1 = lists:keyreplace(Uid,#drum_role.rid,Signs,NdrumR),
            Act >= 12 andalso begin
                TypeId = lib_role_drum:get_zone(Zid),
                OpenLv = data_drumwar:get_kv(?DRUMLV),
                clusters_center:apply_to_all_node(lib_chat, send_TV, [{all_lv, {OpenLv, 9999}},137,TypeId,[WName]], 600),
                {ok,Bin0} = pt_137:write(13712,[Uid,Zid,WName]),
                mod_drumwar_mgr:add_zone_win(Zid, Uid, WName),
                spawn(fun()->mod_drumwar_mgr:show_champion(Bin0,Zid,Signs1) end)
            end,
            Choose1 = case  Act >= 12  of
                true->%%决胜局 追加冠军
                    Choose0 = mod_drumwar_mgr:add_results([Uid],Signs1,Choose),
                    mod_drumwar_mgr:brocast_guess_msg(0,Choose0),
                    Choose0;
                _->
                    Choose
            end,
            {Signs1,Choose1};
        _->
            {Signs,Choose}
    end,
    State#drumwar_mgr{signs=NSigns,choose=NChoose}.

%%购买命数
buy_live(Num, Status)->
    #player_status{scene = Scene, id = Uid, figure = #figure{lv = Lv}} = Status,
    Cost = get_buy_live_cost(Status, Num),
    CKList = [
        {check_lv, Lv},
        {check_ready, Scene},
        {check_cast_center, get({?MODULE, buy_live})},
        {check_buy, Num},
        {check_cost, Cost, Status}
    ],
    case checklist(CKList) of
        true->
            put({?MODULE, buy_live}, true),
            mod_drumwar_mgr:cast_center([{mod, ?MODULE, check_buy_live, [node(), Uid, Num]}]),
            {ok, Status};
        {false, Err}->
            {false, Err}
    end.

%%获取购买命数的消耗
get_buy_live_cost(Status, Num) ->
    ItemNum = case lib_goods_api:get_goods_num(Status, [mod_drumwar_mgr:get_kv(?DRUMITEM)]) of
        [{_, N}|_]-> N;
        _-> 0
    end,
    NewNum = case Num =< ItemNum of
        true-> 0;
        _-> Num-ItemNum
    end,
    case NewNum =< 0 of
        true-> [{0, mod_drumwar_mgr:get_kv(?DRUMITEM), Num}];
        false when ItemNum =< 0->
            BaseCost = mod_drumwar_mgr:get_kv(?LIVE_BUY_COST),
            lib_goods_util:goods_object_multiply_by(BaseCost, Num);
        false ->
            BaseCost = mod_drumwar_mgr:get_kv(?LIVE_BUY_COST),
            MoneyCost = lib_goods_util:goods_object_multiply_by(BaseCost, NewNum),
            [{0, mod_drumwar_mgr:get_kv(?DRUMITEM), ItemNum}| MoneyCost]
    end.

%%购买命数检查
check_buy_live([Node, Uid, Num], State) ->
    case get_drum_role([Uid], State) of
        #drum_role{live = L} when L =< 0 ->
            {ok, Bin} = pt_137:write(13706, [?ERRCODE(err137_be_kill_not_buy), 0]),
            ApplyArgs = [Uid, ?APPLY_CAST_STATUS, ?MODULE, buy_live_fail, [Bin]];
        #drum_role{live = L} when L + Num =< ?BUYMAX->
            ApplyArgs = [Uid, ?APPLY_CAST_STATUS, ?MODULE, buy_live_cost, [Num]];
        _ ->
            {ok, Bin} = pt_137:write(13706, [?ERRCODE(err137_is_limit), 0]),
            ApplyArgs = [Uid, ?APPLY_CAST_STATUS, ?MODULE, buy_live_fail, [Bin]]
    end,
    apply_to_local_role(Node, lib_player, apply_cast, ApplyArgs),
    State.

%%判断失败
buy_live_fail(Status, Bin) ->
    erase({?MODULE, buy_live}),
    lib_server_send:send_to_sid(Status#player_status.sid, Bin),
    Status.

%%回调玩家进程扣钱
buy_live_cost(Status, Num) ->
    erase({?MODULE, buy_live}),
    Cost = get_buy_live_cost(Status, Num),
    case lib_goods_api:cost_object_list_with_check(Status, Cost, drumwar_live, "drumwar_live") of
        {true, NStatus,_}->
            log_drum_cost(Status#player_status.id, 1, Cost),
            mod_drumwar_mgr:cast_center([{mod, ?MODULE, add_live_mgr, [Status#player_status.id, Num]}]),
            {ok, NStatus};
        _->
            {ok, Bin} = pt_137:write(13706, [?ERRCODE(err137_tool_not_enough), 0]),
            lib_server_send:send_to_sid(Status#player_status.sid, Bin),
            Status
    end.

%%增加命数
add_live_mgr([Uid, Num], State)->
    #drumwar_mgr{signs = Signs}=State,
    case lists:keyfind(Uid, #drum_role.rid, Signs) of
        #drum_role{node = Node, live = L} = DrumRole->
            NDrumRole = DrumRole#drum_role{live = L + Num},
            NSigns = lists:keyreplace(Uid, #drum_role.rid, Signs, NDrumRole),
            {ok, Bin} = pt_137:write(13706, [?SUCCESS, L + Num]),
            send_to_role(Node, Uid, Bin),
            State#drumwar_mgr{signs = NSigns};
        _->
            State
    end.

%%获取玩家战场信息
get_drum_role([Uid],State)->
    #drumwar_mgr{signs=Signs}=State,
    lists:keyfind(Uid,#drum_role.rid,Signs).

%%离开 区分离开准备还是擂台
leave_drum(Type,Status)->
    #player_status{
        id = Uid
    } = Status,
    case is_in_drumready(Status) of
        true when Type =<0 ->%%离开准备区
            {SceneId, ScenePoolId, CopyId, X, Y} = get_leave_scene(Status),
            KeyValList = [{group,0}, {change_scene_hp_lim, 1}, {pk,{?PK_PEACE,false}}, {action_free, ?ERRCODE(err137_can_not_change_scene_in_drumwar)}],
            NStatus = lib_scene:change_scene(Status, SceneId, ScenePoolId, CopyId, X, Y, KeyValList),
            % mod_drumwar_mgr:cast_center([{mod, ?MODULE, leave_war_mgr, [ready, Uid, 0]}]),
            mod_drumwar_mgr:cast_center([{mod, ?MODULE, role_logout_mgr, [false, Uid, 0]}]),
            {ok,NStatus};
        _-> %%正常流程不会走这里
            case is_in_drumwar(Status) of
                true when Type>=1 ->%%离开擂台区 进入准备区 主动离开 表示认输
                    RScene = urand:list_rand(mod_drumwar_mgr:get_kv(?READY_SCENE)),
                    {RandX, RandY, Circle} = urand:list_rand(mod_drumwar_mgr:get_kv(?READY_LOCATION)),
                    {Rx, Ry} = umath:rand_point_in_circle(RandX, RandY, Circle, 1),
                    NStatus = lib_scene:change_scene(Status, RScene, 0, 0, Rx, Ry, [{group,0}, {pk,{?PK_PEACE,false}}, {change_scene_hp_lim, 1}]),
                    % mod_drumwar_mgr:cast_center([{mod, ?MODULE, leave_war_mgr, [war, Uid, RScene]}]),
                    mod_drumwar_mgr:cast_center([{mod, ?MODULE, role_logout_mgr, [true, Uid, 0]}]),
                    {ok, NStatus};
                true ->
                    ?ERR("leave_drum:error,~p,~p~n",[Uid, Status#player_status.scene]),
                    mod_drumwar_mgr:cast_center([{mod, ?MODULE, role_logout_mgr, [true, Uid, 0]}]),
                    %%异常切回默认场景
                    NStatus = lib_scene:change_default_scene(Status, [{action_free, ?ERRCODE(err137_can_not_change_scene_in_drumwar)}]),
                    {ok, NStatus};
                _ ->
                    ?ERR("leave_drum:error,~p,~p~n",[Uid, Status#player_status.scene]),
                    {false, ?ERRCODE(err137_not_need_to_leave)}
            end
    end.

%%离开擂台（正常离开流程暂无走到这里）
% leave_war_mgr([Type, Uid, Scene], State)->
%     #drumwar_mgr{action = CurAct, choice = Choice, choose = Choose, signs = Signs}=State,
%     [NSigns, NChoose] = case Type of
%         ready->%%离开准备区
%             case lists:keyfind(Uid, #drum_role.rid, Signs) of
%                 DrumR when is_record(DrumR, drum_role)->
%                     NDrumR = DrumR#drum_role{online = 0, scene = Scene},
%                     [lists:keyreplace(Uid, #drum_role.rid, Signs, NDrumR), Choose];
%                 _->
%                     [Signs, Choose]
%             end;
%         _->%%离开擂台
%             case lists:keyfind(Uid,#drum_role.rid,Signs) of
%                 #drum_role{action=Act}=DrumR when CurAct<Act -> %%已结算 直接退出
%                     NDrumR = DrumR#drum_role{online = 0, scene = Scene},
%                     Signs1 = lists:keyreplace(Uid,#drum_role.rid,Signs,NDrumR),
%                     [Signs1,Choose];
%                 #drum_role{live=Live,lose=L}=DrumR when Live=<0 andalso L>0 ->%%命数为0,战败退出 已结算
%                     NDrumR = DrumR#drum_role{online = 0, lose = L + 1, scene=Scene},
%                     Signs1 = lists:keyreplace(Uid,#drum_role.rid,Signs,NDrumR),
%                     [Signs1,Choose];
%                 #drum_role{node=Node,zid=Lzone,rid=LRid,figure=#figure{name=_LName},action=Act,sid=Sid,lose=L,ruid=RUid}=DrumR->%%未结算玩家,算输
%                     NDrumR = DrumR#drum_role{online = 0, lose = L + 1, scene = Scene},
%                     {ok,Bin} = pt_137:write(13708,[1,2,Act]),
%                     send_to_role(Node,Sid,Bin),
%                     Signs0 = lists:keyreplace(Uid,#drum_role.rid,Signs,NDrumR),
%                     [{_,LAward}] = data_drumwar:get_war(Lzone,Act),
%                     lib_role_drum:war_award(Node, CurAct, 0, LRid, LAward),
%                     case lists:keyfind(RUid,#drum_role.rid,Signs0) of
%                         #drum_role{action=RAct} when CurAct<RAct -> %%已结算 不管
%                             [Signs0,Choose];
%                         #drum_role{node=N,zid=Z,figure=#figure{name=WName},rid=Rid,action=A,sid=S,win=W}=DrumR0->
%                             NDrumR0 = DrumR0#drum_role{win=W+1,action=A+1},
%                             {ok,Bin0} = pt_137:write(13708,[1,1,A]),
%                             send_to_role(N,S,Bin0),
%                             spawn(fun()->
%                                 timer:sleep(1000),
%                                 clusters_center:apply_cast(N,mod_drumwar_mgr,enter_ready_local,[Rid])
%                             end),
%                             [{WAward,_}] = data_drumwar:get_war(Z,A),
%                             lib_role_drum:war_award(N, CurAct, 1, Rid, WAward),
%                             %%第九场之后的要结算竞猜
%                             CurAct>=9 andalso begin
%                                 spawn(fun()->mod_drumwar_mgr:account_guess_result(Z,CurAct,RUid,WName,LRid,Choice) end)
%                             end,
%                             Signs1 = lists:keyreplace(RUid,#drum_role.rid,Signs0,NDrumR0),
%                             Choose1 = case  CurAct >= 12  of
%                                 true->%%决胜局 追加冠军
%                                     Choose0 = mod_drumwar_mgr:add_results([RUid],Signs1,Choose),
%                                     mod_drumwar_mgr:brocast_guess_msg(0,Choose0),
%                                     Choose0;
%                                 _->
%                                     Choose
%                             end,
%                             [Signs1,Choose1];
%                         _->
%                             [Signs0,Choose]
%                     end;
%                 _->
%                     [Signs,Choose]
%             end
%     end,
%     State#drumwar_mgr{signs=NSigns,choose=NChoose}.

%%获取战报
war_report(DrumID,Status)->
    #player_status{sid=Sid}=Status,
    mod_drumwar_mgr:cast_center([{mod,?MODULE,war_report_mgr,[node(),DrumID,Sid]}]).

war_report_mgr([Node,DrumID,Sid],State)->
    #drumwar_mgr{history=History}=State,
    NRanks = case lists:keyfind(DrumID,1,History) of
        {_,Ranks}->
            war_report_mgr_help(Ranks,[]);
        _->
            []
    end,
    {ok,Bin}=pt_137:write(13711,[DrumID,NRanks]),
    lib_role_drum:send_to_role(Node,Sid,Bin),
    State.

war_report_mgr_help([],Result) -> lists:reverse(Result);
war_report_mgr_help([Role|T],Result) ->
    #rank_role{
        zone=Zone,rank=Rank,rid=RoleId,
        server_id=SId,platform=PForm,servernum=SNum,
        name=Name,gname=Gname,vip=Vip,power=Power,career=Career
    } = Role,
    Info = {Zone,Rank,RoleId,SId,PForm,SNum,Name,Gname,Vip,Power,Career},
    war_report_mgr_help(T,[Info|Result]).

%%激活假人
active_mon(Status)->
    #player_status{id=Uid}=Status,
    % case clusters_api:is_connected_center() of
    %     true->
            case is_in_drumwar(Status) of
                true->
                    mod_drumwar_mgr:cast_center([{active_mon,Uid}]);
                _->
                    ignore
            end.
    %     _->
    %         ignore
    % end.

%%获取竞猜面板数据
fetch_guess(Status)->
    #player_status{id = RoleID, figure= #figure{lv = Lv}}=Status,
    CKList = [
        {check_lv,Lv},
        {check_con}
    ],
    case checklist(CKList) of
        true-> mod_drumwar_mgr:cast_center([{mod, ?MODULE, get_fetch_guess, [choose, node(), RoleID]}]);
        {false,_Err}->
            {ok, Bin} = pt_137:write(13713,[0, []]),
            lib_server_send:send_to_uid(RoleID, Bin)
    end.

get_fetch_guess([Type, Node, RoleId], State) ->
    case get_drumwar_state([Type], State) of
        [CTime, Guess] -> ok;
        _ -> CTime = 0, Guess = []
    end,
    {ok, Bin} = pt_137:write(13713,[CTime, Guess]),
    send_to_role(Node, RoleId, Bin),
    State.

get_drumwar_state([Type],State)->
    #drumwar_mgr{choose = Guess, choice = Choice, action = Act, cstate = CState, ctime = CTime}=State,
    case Type of
        choose->
            [CTime, Guess];
        choice->
            [Act, CState, Guess, Choice];
        _->
            State
    end.

%%竞猜
choose_drum(Type,Uid,Status)->
    #player_status{id=RoleID,figure = #figure{lv=Lv} = Figure, role_drum=RoleDrum}=Status,
    RUid = RoleID,
    #role_drum{deal=_Deal,choice=OChoice}=RoleDrum,
    CKList = [
        {check_choose_lv,Lv},
        {check_con},
        {check_open_day},
        {check_type,Type}
        % {check_repeat,Uid,Choice}
    ],
    case checklist(CKList) of
        true->
            Zid = mod_drumwar_mgr:trans_zone(Figure),
            catch case mod_drumwar_mgr:call_center([{mod,?MODULE,get_drumwar_state,[choice]}]) of
                [Act,CState,_,_] when CState=<0 orelse Act=<8 ->
                    {false,?ERRCODE(err137_not_guess_state)};
                [Act,_,Guess,MChoice]->
                    case check_choose(RUid,Zid,Act,Guess,Uid,MChoice) of
                        true->
                            case check_choice(RUid,Uid,MChoice) of
                                true->
                                    [Cost, _] = mod_drumwar_mgr:get_award(Act, Type),
                                    case lib_goods_api:cost_object_list_with_check(Status, Cost, drumwar_choose, "drumwar_choose") of
                                        {true, NStatus,_}->
                                            mod_drumwar_mgr:cast_center([{mod,?MODULE,choose_drum_mgr,[RUid,node(),RoleID,Uid,Type]}]),
                                            NChoice = case lists:keyfind(Act,1,OChoice) of
                                                false-> [{Act,[Uid]}|OChoice];
                                                {_,OList}->
                                                    lists:keyreplace(Act,1,OChoice,{Act,[Uid|OList]})
                                            end,
                                            [RankPrepare] = data_drumwar:get_rank_prepare(),
                                            NDeal = utime:unixtime()+RankPrepare,
                                            NRoleDrum = RoleDrum#role_drum{drumid = get_id(), deal=NDeal,choice=NChoice},
                                            db_replace_role_drum(RoleID,NRoleDrum),
                                            {ok,NStatus#player_status{role_drum=NRoleDrum},Act};
                                        {false,_Err,_}->
                                            {false,?GOLD_NOT_ENOUGH}
                                    end;
                                {false,Err}->
                                    {false,Err}
                            end;
                        {false,Err}->
                            {false,Err}
                    end;
                _->
                    {false,?ERRCODE(err137_not_guess_state)}
            end;
        {false,Err}->
            {false,Err}
    end.

choose_drum_mgr([RUid,Node,RoleID,Uid,Type],State)->
    #drumwar_mgr{choice=Choice}=State,
    NChoice = case lists:keyfind(Uid,1,Choice) of
        {_,UChoice}->
            NUChoice = lists:keystore(RUid,1,UChoice,{RUid,Node,RoleID,Type}),
            lists:keyreplace(Uid,1,Choice,{Uid,NUChoice});
        _->
            NUChoice = [{RUid,Node,RoleID,Type}],
            lists:keystore(Uid,1,Choice,{Uid,NUChoice})
    end,
    State#drumwar_mgr{choice=NChoice}.

%%绑钻不足，钻石来补
% combine_cost(Cost,Gold,Bgold) ->
%     [{_,_,Num}] = Cost,
%     case Bgold>=Num of
%         true-> Cost;
%         false->
%             LeftGold = Num - Bgold,
%             case Gold>= LeftGold of
%                 true-> [{?TYPE_BGOLD,0,Bgold},{?TYPE_GOLD,0,LeftGold}];
%                 false -> {false,?GOLD_NOT_ENOUGH}
%             end
%     end.

%%验证候选人信息 是否候选人
check_choose(Ruid,Zid,Act,Guess,Uid,Choice)->
    case lists:keyfind(Zid,1,Guess) of
        {_,ZGuess}->
            case lists:keyfind(Act,1,ZGuess) of
                {_,ActGuess}->
                    check_guess(Ruid,Uid,ActGuess,Choice);
                _->
                    {false,?ERRCODE(err137_not_guess_zone_role)}
            end;
        _->
            {false,?ERRCODE(err137_not_guess_act_role)}
    end.

check_guess(_RUid,_Uid,[],_)->
    {false,?ERRCODE(err137_not_guess_act_role)};
check_guess(RUid,Uid,[Value|ActGuess],Choice)->
    case Value of
        {#drum_role{rid=Uid}}->
            {false,?ERRCODE(err137_not_select_empty_role)};
        {#drum_role{rid=Uid,ruid=UidA},_}->
            case check_choice(RUid,UidA,Choice) of
                true->
                    true;
                _->
                    {false,?ERRCODE(err137_not_select_same_plat)}
            end;
        {_,#drum_role{rid=Uid,ruid=UidA}}->
            case check_choice(RUid,UidA,Choice) of
                true->
                    true;
                _->
                    {false,?ERRCODE(err137_not_select_same_plat)}
            end;
        _->
            check_guess(RUid,Uid,ActGuess,Choice)
    end.

%%验证竞选情况 是否选过
check_choice(RUid,Uid,Choice)->
    case lists:keyfind(Uid,1,Choice) of
        {_,UChoice}->
            case lists:keyfind(RUid,1,UChoice) of
                false->
                    true;
                _->
                    {false,?ERRCODE(err137_not_allow_select_same)}
            end;
        _->
            true
    end.

%%断线处理
role_logout(Status, OldLine)->
    #player_status{
        scene = Scene,
        id = RoleID,
        figure = #figure{lv = Lv}
    } = Status,
    case Lv >= mod_drumwar_mgr:get_kv(?DRUMLV) of
        true->
            case is_drum(Scene) of
                true->
                    mod_drumwar_mgr:cast_center([{mod, ?MODULE, role_logout_mgr, [is_in_drumwar(Status), RoleID, OldLine]}]);
                _->
                    ignore
            end;
        _->
            ignore
    end.

role_logout_mgr([IsWar, Uid, IsOn], State)->
    #drumwar_mgr{signs = Signs, action = DAct, state = _NowState, sub_state = SubState, sub_time = SubTime}=State,
    case lists:keyfind(Uid, #drum_role.rid, Signs) of
        #drum_role{live = Live, lose = Lose, calc_type = CalcType} = DRole->
            % CheckList = [{check_live_and_action, DRole, DAct, SubState}, {check_drum_state, NowState, SubState}],
            Utime = utime:unixtime(),
            %%进场时间7s屏蔽
            IsEnterTime = SubState == ?TYPE_FIGHT_ENTER andalso Utime + 7 >= SubTime,
            {NewLive, NewLose, IsCalc, IsCalcType} = ?IF(CalcType == 0 andalso (IsWar orelse IsEnterTime), {0, 1, 1, 0}, {Live, Lose, CalcType, 1}),
            NDRole = DRole#drum_role{online = IsOn, live = NewLive, lose = NewLose, scene = 0, calc_type = IsCalc},
            NSigns = lists:keyreplace(Uid, #drum_role.rid, Signs, NDRole),
            State0 = State#drumwar_mgr{signs = NSigns};
        _->
            IsCalcType = 1,
            State0 = State
    end,
    % ?PRINT("~p~n",[[IsWar, Uid, IsOn, IsCalcType]]),
    case IsCalcType == 0 of
        true->%弃权
            case lists:keyfind(Uid, #drum_role.rid, Signs) of
                %%判断退出同场次才处理（防止刚好在结束时候触发，在超时结算后再次处理）
                #drum_role{node = Node, zid = Zone, action = DAct, ruid = RUid} = D->
                    case data_drumwar:get_war(Zone, DAct) of
                        [{_, Award}] ->
                            lib_role_drum:war_award(Node, DAct, 0, Uid, Award);
                        _ ->
                            ?INFO("role_change_mgr,zone:~p,act:~p",[Zone, DAct]),
                            ok
                    end,
                    lib_role_drum:update_role_drum(Node, Uid, Zone, DAct),
                    ?IF(RUid == 0, mod_drumwar_mgr:log_drum_war(?LOG_LOGOUT, D), ok),
                    {ok, Bin} = pt_137:write(13705, [DAct, D]),
                    send_to_role(Node, Uid, Bin),
                    role_away_mgr([RUid], State0);
                _->
                    State0
            end;
        _->
            State0
    end.

%%切换场景
change_scene([RoleId, Scene], State) ->
    #drumwar_mgr{signs = Signs}=State,
    NewState = case lists:keyfind(RoleId, #drum_role.rid, Signs) of
        false -> State;
        #drum_role{} = DrumRole->
            NDrumRole = DrumRole#drum_role{scene = Scene},
            NSigns = lists:keyreplace(RoleId, #drum_role.rid, Signs, NDrumRole),
            State#drumwar_mgr{signs = NSigns}
    end,
    NewState.

%%回调本地发消息给玩家
send_to_role(Node,Rid,Bin) when is_integer(Rid)->
    clusters_center:apply_cast(Node, lib_server_send, send_to_uid, [Rid,Bin]);
send_to_role(Node,Sid,Bin)->
    clusters_center:apply_cast(Node, lib_server_send, send_to_sid, [Sid,Bin]).

%%调用会游戏服
apply_to_local_role(none, M, F, Args) ->
    apply(M, F, Args);
%% 跨服调M F A
apply_to_local_role(Node, M, F, Args) ->
    SelfNode = node(),
    case SelfNode == Node of
        true-> apply(M, F, Args);
        false-> clusters_center:apply_cast(Node, M, F, Args)
    end.

%%所有战区推送传闻
send_tv(Act,_Signs) ->
    Length = mod_drumwar_mgr:ideal_value(Act),
    [{Reward,_}] = data_drumwar:get_war(1,Act),
    [{_,_,Num}| _T] = Reward,
    OpenLv = data_drumwar:get_kv(?DRUMLV),
    clusters_center:apply_to_all_node(lib_chat, send_TV, [{all_lv, {OpenLv, 9999}},137,2,[Act,Length,Length*Num]], 600).


%%各战区推送传闻
% send_tv(Act,Signs) ->
%     List = calc_send_zone(Signs,Act,[]),
%     send_zone_tv(Act,List).

%%分战区人
calc_send_zone([],_Act,Result)->Result;
calc_send_zone([#drum_role{zid=Zone}=Role|T],Act,Result) ->
    case lists:keyfind(Zone,1,Result) of
        false-> NewResult = [{Zone,[Role]}|Result];
        {_,RoleList}->
            NewResult = lists:keyreplace(Zone,1,Result,{Zone,[Role|RoleList]})
    end,
    calc_send_zone(T,Act,NewResult).

%%战区传闻
send_zone_tv(_Act,[]) -> ok;
send_zone_tv(Act,[{Zone,Roles}|T]) ->
    % Length = length(Num),
    Length = mod_drumwar_mgr:ideal_value(Act),
    [{Reward,_}] = data_drumwar:get_war(Zone,Act),
    [{_,_,Num}] = Reward,
    BinData = lib_chat:make_tv(137, 2, [Act,Length,Length*Num]),
    send_tv_to_role(Roles,BinData),
    send_zone_tv(Act,T).

%%推送传闻
send_tv_to_role([],_BinData) -> ok;
send_tv_to_role([#drum_role{node=Node,rid=Rid,online=Online}|T],BinData) ->
    case Online == 1 of
        true-> send_to_role(Node,Rid,BinData);
        false-> skip
    end,
    send_tv_to_role(T,BinData).

%%战场奖励
war_award(_Node, _Act, _IsWin, _RoleID, [])->
    ignore;
war_award(Node, Act, IsWin, RoleID, Award)->
    clusters_center:apply_cast(Node, ?MODULE, war_award_local,[Act, IsWin, RoleID, Award]).
war_award_local(Act, IsWin, RoleID, Award)->
    Title = utext:get(1370013),
    Content = case IsWin of
        1-> utext:get(1370014, [Act]);
        _ -> utext:get(1370015, [Act])
    end,
    lib_mail_api:send_sys_mail([RoleID], ?MOD_DRUMWAR, Title, Content, Award).

%%邮件结算
mail_award(_Node,_RoleID,_Zone,_Rank,[])->
    ignore;
mail_award(Node,RoleID,Zone,Rank,Award)->
    clusters_center:apply_cast(Node,?MODULE,mail_award_local,[RoleID,Zone,Rank,Award]).
mail_award_local(RoleID,Zone,Rank,Award)->
    Mid = get_mail_zone(Zone),
    Title = utext:get(1370008),
    Content = utext:get(Mid,[Rank]),
    lib_mail_api:send_sys_mail([RoleID], ?MOD_DRUMWAR, Title, Content, Award),
    case Rank=<1 of
        true->%%冠军数据更新
            Sql = io_lib:format(?SQL_DRUM_UPDATE, [1,RoleID]),
            db:execute(Sql),
            lib_player:apply_cast(RoleID, ?APPLY_CAST_STATUS, ?MODULE, update_drum, [1]);
        _->
            ignore
    end.

get_mail_zone(1)->1370009;
get_mail_zone(2)->1370010;
get_mail_zone(3)->1370011;
get_mail_zone(4)->1370012;
get_mail_zone(_)->1370012.

get_zone(1)->3;
get_zone(2)->5;
get_zone(3)->6;
get_zone(4)->7;
get_zone(_)->7.

update_drum(Status,One)->
    #player_status{id = _RoleID,role_drum=RoleDrum}=Status,
    Status#player_status{role_drum=RoleDrum#role_drum{one=One}}.

%%更新玩家擂台赛信息
update_role_drum(Node, RoleID, Zone, Act)->
    ApplyArgs = [RoleID, ?APPLY_CAST_STATUS, ?MODULE, update_role_drum_role, [Zone, Act]],
    apply_to_local_role(Node, lib_player, apply_cast, ApplyArgs).
update_role_drum_role(Status, Zone, Act)->
    Choice = Status#player_status.role_drum#role_drum.choice,
    Out = Status#player_status.role_drum#role_drum.out,
    RoleDrum = #role_drum{ zone = Zone, drumid = get_id(), action = Act, choice = Choice, out = Out},
    %%写进db
    db_replace_role_drum(Status#player_status.id,RoleDrum),
    Status#player_status{role_drum = RoleDrum}.

%%数据拼接
make_drum_role(Status)->
    #player_status{
        id = RoleID,
        sid = Sid,
        server_id = ServerId,
        platform = PForm,
        server_num = SNum,
        figure = Figure,
        combat_power = Power
    } = Status,
    #drum_role{
        rid = RoleID,
        sid = Sid,
        server_id = ServerId,
        platform = PForm,
        server_num = SNum,
        node = ServerId,
        figure = Figure,
        action = 1,
        power = Power
    }.

%%获取原场景信息
get_leave_scene(Status) ->
    #player_status{role_drum = #role_drum{out = Out}} = Status,
    case Out of
        #out{scene = SceneId, copy_id = CopyId, pool_id = PoolId, x = X, y = Y} when SceneId > 0 ->
            case CopyId == 0 orelse misc:is_process_alive(CopyId) of
                true -> {SceneId, PoolId, CopyId, X, Y};
                false -> default_leave_scene()
            end;
        _ -> default_leave_scene()
    end.

default_leave_scene() ->
    [X, Y] = lib_scene:get_born_xy(?MAIN_CITY_SCENE),
    {0, 0, 0, X, Y}.

%%进入刷新
enter_make_record(DrumRole, ODrumRole) ->
    #drum_role{
        sid = Sid,
        server_id = ServerId,
        platform = PForm,
        server_num = SNum,
        node = ServerId,
        figure = Figure,
        power = Power
    } = DrumRole,
    ODrumRole#drum_role{
        sid = Sid,
        server_id = ServerId,
        platform = PForm,
        server_num = SNum,
        node = ServerId,
        figure = Figure,
        power = Power,
        online = 1
    }.

%%是否准备场景
is_in_drumready(Scene) when is_integer(Scene) ->
    lists:member(Scene,mod_drumwar_mgr:get_kv(?READY_SCENE));
is_in_drumready(Status)->
    lists:member(Status#player_status.scene,mod_drumwar_mgr:get_kv(?READY_SCENE)).

%%是否擂台战场
is_in_drumwar(Status)->
    lists:member(Status#player_status.scene,mod_drumwar_mgr:get_kv(?WAR_SCENE)).

is_drum(Scene)->
    AllScenes = mod_drumwar_mgr:get_kv(?READY_SCENE)++mod_drumwar_mgr:get_kv(?WAR_SCENE),
    lists:member(Scene,AllScenes).

%%是否回准备场景
% back_drum(Status)->
%     #player_status{id=RoleID,pid=RolePid}=Status,
%     Uid = RoleID,
%     [Scene|_] = lib_player:get_player_state_data(RoleID),
%     case lists:member(Scene,mod_drumwar_mgr:get_kv(?WAR_SCENE)) of
%         true->
%             ignore;
%         _->
%             case lists:member(Scene,mod_drumwar_mgr:get_kv(?READY_SCENE)) of
%                 true->
%                     % case clusters_api:is_connected_center() of
%                     %     true->
%                             %%活动状态标识和竞猜标识
%                             catch case mod_drumwar_mgr:call_center([{mod,?MODULE,get_enter_state,[Uid]}]) of
%                                 {Ds,0} when Ds == ?SEAWAR orelse Ds == ?RANKWAR ->  %%海选 排位 非竞猜时段
%                                     lib_player:apply_cast(RolePid,?APPLY_CAST_STATUS, ?MODULE,back_drum_ready,[]);
%                                 _-> %准备区掉线的 自己操作进入
%                                     ignore
%                             end;
%                     %     _->
%                     %         ignore
%                     % end;
%                 _->
%                     ignore
%             end
%     end.

% back_drum_ready(Status)->
%     DrumRole = make_drum_role(Status),
%     mod_drumwar_mgr:cast_center([{mod,?MODULE,back_ready_mgr,[DrumRole]}]),
%     Status.

% back_ready_mgr([DrumRole],State)->
%     #drumwar_mgr{action=Act,signs=Signs}=State,
%     #drum_role{rid=RoleID,node=Node}=DrumRole,
%     Uid = RoleID,
%     NDrumRole = case lists:keyfind(Uid,#drum_role.rid,Signs) of
%         #drum_role{}=DRole->
%             DRole#drum_role{online=1};
%         _->
%             DrumRole#drum_role{online=1}
%     end,
%     NSigns = lists:keyreplace(Uid,#drum_role.rid,Signs,NDrumRole),
%     clusters_center:apply_cast(Node,?MODULE,enter_ready_local,[RoleID,Act,NDrumRole]),
%     State#drumwar_mgr{signs=NSigns}.


% get_enter_state([Uid],State)->
%     #drumwar_mgr{action=Act,state=Ds,cstate=Cs,signs=Signs}=State,
%     case lists:keyfind(Uid,#drum_role.rid,Signs) of
%         #drum_role{action=A} when A>=Act ->
%             {Ds,Cs};
%         _->
%             {0,0}
%     end.

%%使用技能
use_skill(SKillID, Status)->
    #player_status{id =_RoleId, copy_id = _Room, bgold = _Gold, battle_attr = #battle_attr{hp = Hp} }=Status,
    case lib_role_drum:is_in_drumwar(Status) of
        true->
            Cost = mod_drumwar_mgr:get_kv(?SKILL_COST),
            IsMember = lists:member(SKillID, mod_drumwar_mgr:get_kv(?DRUM_SKILLS)),
            IsCost = check({check_cost, Cost, Status}),
            NowSecs  = utime:unixtime(),
            UsedSecs = get_skill_used_time(SKillID),
            CD = mod_drumwar_mgr:get_kv(?SKILL_CD),
            if
                Hp =< 0 -> {?ERRCODE(player_die), 0, Status};
                IsMember == false -> {?ERRCODE(err137_error_skill), 0, Status};
                NowSecs =< UsedSecs + CD -> {?ERRCODE(err137_skill_cd), 0, Status};
                IsCost =/= true -> {?MONEY_NOT_ENOUGH, 0, Status};
                true ->
                    case lib_goods_api:cost_object_list(Status, Cost, drumwar_skill, "drumwar_skill") of
                        {true,Status0}->
                            log_drum_cost(_RoleId, 2, Cost),
                            set_skill_used_time(SKillID,NowSecs),
                            {?SUCCESS, NowSecs+CD, Status0};
                        {false,Err,_}->
                            {Err,0,Status}
                    end
            end;
        _->
            {?ERRCODE(err137_fight_allow_use), 0, Status}
    end.

%%增加零时技能
add_tmp_skill([],TmpSkill)->TmpSkill;
add_tmp_skill([Skill|T],TmpSkill) -> add_tmp_skill(T,[{Skill,1}|TmpSkill]).

%%移除技能
remove_tmp_skill([],TmpSkill) -> TmpSkill;
remove_tmp_skill([Skill|T],TmpSkill) -> remove_tmp_skill(T,lists:keydelete(Skill,1,TmpSkill)).

get_skill_used_time(SKillID) ->
    case get({guild_drum_skill,SKillID}) of
        undefined -> 0;
        UsedTime  -> UsedTime
    end.

set_skill_used_time(SKillID,Secs) ->
    put({guild_drum_skill,SKillID}, Secs).

%% 发送弹幕
send_ready_scene_msg(PS, Msg, TkTime) ->
    #player_status{sid = Sid, scene = Scene} = PS,
    CKList = [
        {check_ready, Scene},
        {check_msg, Msg}
    ],
    case checklist(CKList) of
        true->
            case pp_chat:handle(11001, PS, [?CHAT_CHANNEL_SCENE, 0, Msg, [], TkTime, "ticket"]) of
                {ok, NewPS} ->
                    lib_server_send:send_to_sid(Sid, pt_137, 13718, [?SUCCESS]),
                    {ok, NewPS};
                _ ->
                    lib_server_send:send_to_sid(Sid, pt_137, 13718, [?FAIL]),
                    ok
            end;
        {false, Err}->
            lib_server_send:send_to_sid(Sid, pt_137, 13718, [Err])
    end.

%%玩家死亡
player_die(Atter,Status) ->
    #battle_return_atter{
        id  = AUid,
        sign  = AtterSign
    } = Atter,
    #player_status{id=DeadUid}=Status,
    case is_in_drumwar(Status) of
        false ->
            ignore;
        true ->
            %%被玩家打死有效
            case AtterSign == ?BATTLE_SIGN_PLAYER of
                true->
                    mod_drumwar_mgr:cast_center([{war_result,AUid,DeadUid}]);
                _->
                    mod_drumwar_mgr:cast_center([{war_result,lose,DeadUid}])
            end
    end.

%%假人死亡
kill_mon(Scene, Uid, Sign)->
    case lists:member(Scene,mod_drumwar_mgr:get_kv(?WAR_SCENE)) andalso Sign == ?BATTLE_SIGN_DUMMY of
        true->
            mod_drumwar_mgr:cast_mgr({war_result,win,Uid});
        _->
            ignore
    end.

%%2 未连接跨服
%%3 未达到参赛等级
%%4 不需要重复报名
%%5 非报名阶段
%%6 未报名
%%7 非准备阶段
%%8 已在准备区
%%9 已达到购买上限
%%10 不在可购买阶段
%%11 命数虽好,不可贪多
%%12 非擂台赛场景,不需退出
%%13 道具不足
%%14 非竞猜时段
%%15 非可选战区候选人
%%16 非可选战场候选人
%%17 轮空玩家不可选
%%18 不可重复选中一人
%%19 不可同时选同台候选人
%%20 技能未冷却
%%21 技能擂台中可使用
%%22 非法技能
%%23 参数错误

checklist([]) -> true;
checklist([H|T]) ->
    case check(H) of
        true -> checklist(T);
        {false, Res} -> {false, Res}
    end.

check({check_con})-> true;
check({check_buy,Num})->
    case Num > 0 andalso Num =< ?BUYMAX of
        true->
            true;
        _->
            {false,?ERRCODE(err137_buy_limit)}
    end;
check({check_gold,Cost,Has})->
    case Cost =< Has of
        true->
            true;
        _->
            {false,?GOLD_NOT_ENOUGH}
    end;
check({check_sign, DrumRole})->
    case is_record(DrumRole, drum_role) of
        true->
            true;
        _->
            {false,?ERRCODE(err137_not_sign)}
    end;
%%淘汰不能再进
check({check_live_and_action, DrumRole, Act, SubState}) ->
    if
        DrumRole#drum_role.live =< 0 -> {false, ?ERRCODE(err137_not_enter_lose)};
        DrumRole#drum_role.action == 13 -> {false, ?ERRCODE(err137_has_winner)};
        Act > DrumRole#drum_role.action -> {false, ?ERRCODE(err137_not_enter_lose)};
        Act == DrumRole#drum_role.action andalso SubState == ?TYPE_SUCCESS -> %%晋级但是过了进入时间
            {false, ?ERRCODE(err137_not_enter_sub_state)};
        true -> true
    end;
check({check_drum_state, State, SubState}) ->
    IsAllowState = [?READY, ?SEAWAR, ?RANKWAR],
    IsAllowSubState = [?TYPE_SEA_START, ?TYPE_FIGHT_START, ?TYPE_SUCCESS, ?TYPE_RANK_START],
    case lists:member(State, IsAllowState) of
        true ->
            ?IF(lists:member(SubState, IsAllowSubState), true, {false, ?ERRCODE(err137_not_enter_sub_state)});
        false -> {false, ?ERRCODE(err137_not_enter_state)}
    end;
check({check_state, PS})->
    lib_player_check:check_all(PS);
check({check_scene,Scene})->
    case lists:member(Scene, mod_drumwar_mgr:get_kv(?READY_SCENE) ++ mod_drumwar_mgr:get_kv(?WAR_SCENE)) of
        true->
            {false,?ERRCODE(err137_is_in_prepare)};
        _->
            true
    end;
check({check_ready,Scene})->
    case lists:member(Scene, mod_drumwar_mgr:get_kv(?READY_SCENE)) of
        true->
            true;
        _->
            {false,?ERRCODE(err137_not_in_prepare)}
    end;
% check({check_repeat,Uid,Choice})->
%     case lists:member(Uid,Choice) of
%         true->
%             {false,?ERRCODE(err137_not_allow_select_same)};
%         _->
%             true
%     end;
check({check_type,Type})->
    TypeList = data_drumwar:get_choose_types(),
    case lists:member(Type, TypeList) of
        false->
            {false,?ERRCODE(err137_cfg_error)};
        _->
            true
    end;
check({check_choose_lv,Lv})->
    case Lv>=mod_drumwar_mgr:get_kv(?CHOOSELV) of
        true->
            true;
        _->
            {false,?ERRCODE(err137_lv_not_enough)}
    end;
check({check_cost, Cost, PS}) ->
    Result = lib_goods_api:check_object_list(PS, Cost),
    case Result of
        true->
            true;
        {false,Res}->
            {false,Res};
        _Othre->
            {false, ?FAIL}
    end;
check({check_ready_out,RoleId,ReadyOut})->
    case lists:member(RoleId,ReadyOut) of
        true->{false,?ERRCODE(err137_power_out)};
        false->true
    end;

check({check_lv,Lv})->
    case Lv>= mod_drumwar_mgr:get_kv(?DRUMLV) of
        true->
            true;
        _->
            {false,?ERRCODE(err137_lv_not_enough)}
    end;

check({check_cast_center, Type}) ->
    case Type of
        true-> {false, ?ERRCODE(err137_quick_point)};
        _ -> true
    end;
check({check_open_day}) ->
    OpenDay = util:get_open_day(),
    NeedOpenDay = mod_drumwar_mgr:get_kv(?OPEN_DAY),
    case OpenDay >= NeedOpenDay of
        true -> true;
        false -> {false, ?ERRCODE(err157_9_open_day_limit)}
    end;
check({check_msg, Msg}) ->
    case lib_word:check_keyword(Msg) of
        true-> {false, ?ERRCODE(err172_wrong_msg_format)};
        _ -> true
    end;
check(_)->true.

%%分组划分擂台场景
get_war(Group) when 0 =< Group andalso Group =< 105-> get_war_scene(1);
get_war(Group) when 106=<Group andalso Group=<210-> get_war_scene(2);
get_war(Group) when 211=<Group andalso Group=<315-> get_war_scene(3);
get_war(Group) when 316=<Group andalso Group=<420-> get_war_scene(4);
get_war(_) -> get_war_scene(1).

get_war_scene(Nth) ->
    SceneList = mod_drumwar_mgr:get_kv(?WAR_SCENE),
    case length(SceneList) >= Nth of
        true -> lists:nth(Nth, SceneList);
        false when SceneList =/= [] -> hd(SceneList);
        false ->
            ?INFO("get_war_scene fail~p,~p~n",[Nth, SceneList]),
            7001
    end.

%%假人模拟创建测试
% test(RoleID)->
%     lib_player:apply_cast(RoleID,?APPLY_CAST_STATUS,?MODULE,test_mon,[]).
% test_mon(Status)->
%     #player_status{guild = RoleGuild} = Status,
%     #status_guild{id = GuildId} = RoleGuild,
%     DrumRole0 = make_drum_role(Status),
%     DrumRole  = DrumRole0#drum_role{group=GuildId},
%     create_mon(DrumRole),
%     Status.
% %% 创建假人
% test_create_mon(DrumRole,_X,_Y) ->
%     #drum_role{
%         platform=Platform,
%         server_num = ServerNum
%         ,figure = #figure{lv=Lv, vip_lv=Vip, role_trains=RoleTrainsFigure}
%         ,group = Group
%         ,power = Power
%         ,train_object = TrainObj
%     } = DrumRole,
%     Rand = urand:rand(5, 10),
%     MonPower = util:ceil((Power*(100-Rand)/100)),
%     Name = case data_1v1:get_1v1_ai_name() of
%         [] ->  utext:get(203);
%         [OneName] -> OneName;
%         AllName -> [OneName|_] = ulists:list_shuffle(AllName),
%             OneName
%     end,
%     Career = case urand:rand(1,100) > 50 of
%         true  -> ?ASSASSIN;
%         false -> ?SWORDSMAN
%     end,
%     MonAttr = get_mon_attr(MonPower),
%     Skill = [ {SkillId, 1} || {SkillId, _, _} <- data_skill:get_ids(Career) ], %mod_arena_battle:get_default_skills(MonSex),
%     #career{sex=Sex} = data_career:get(Career),
%     MonServerNum = max(1, ServerNum-1),
%     MonVip = urand:rand(max(0, Vip-2), min(Vip, Vip+2)),
%     Figure = #figure{name=Name, lv=Lv, career=Career, sex=Sex, vip_lv=MonVip, lv_model=lib_player:get_model_list(Career,Lv), role_trains=RoleTrainsFigure},
%     Args = [{skill, Skill}, {train_object, TrainObj}, {group, 2}, {platformInfo, Platform, MonServerNum}],
%     {B_X, B_Y} = left_pos(),
%     WarScene = get_war(Group),
%     BattleAttr = #battle_attr{hp=MonAttr#attr.hp, hp_lim=MonAttr#attr.hp, speed=?SPEED_VALUE, att_speed=?ATT_SPEED_VALUE, attr=MonAttr},
%     ?PRINT("dumy ~p:~p~n",[WarScene,Group]),
%     case lib_scene_object:sync_create_a_dummy(WarScene, 0, B_X, B_Y, Group, 1, Figure, BattleAttr, Args) of
%         DummyId when is_integer(DummyId) -> ?PRINT("COM1...........~n",[]),
%             case get_mon_aid(WarScene, DummyId) of
%                 ignore -> %%找不到怪物AI 通知玩家胜利
%                     ?PRINT("COM2...........~n",[]),ignore;
%                 Aid ->?PRINT("COM3...........~n",[]),
%                     Aid
%             end;
%         0 -> %%创建失败 通知玩家胜利
%            ?PRINT("COM4...........~n",[]), ignore
%     end.

% 创建假人
create_mon(DrumRole, ActionSt) ->
    #drum_role{
        platform=Platform,
        server_num = ServerNum
        ,figure = #figure{lv = Lv, vip = Vip}
        ,group = Group
    } = DrumRole,
    TmpCareerList = data_career:get_all_career(),
    CareerList = lists:keydelete(3, 1, TmpCareerList),
    {Career, Sex} = urand:list_rand(CareerList),
    MonServerNum = max(1, ServerNum-1),
    MonVip = urand:rand(max(0, Vip-2), min(Vip, Vip+2)),
    Turn = urand:list_rand(lists:seq(0, ?MAX_TURN)),
    LvModel = lib_player:get_model_list(Career, Sex, Turn, Lv),
    RandId = urand:rand(0,length(data_drumwar:get_drum_ai_id())),
    Name = data_drumwar:get_robot_name(RandId),
    MonFigure = #figure{name = Name, lv = Lv, career = Career, sex = Sex, vip = MonVip, turn = Turn, lv_model = LvModel},
    % MonFigure = lib_dummy_api:change_figure(Figure, [create_name]),
    Skill = lib_skill_api:get_career_active_skill_ids(Career, Sex),
    Time = max(1, ActionSt + 5 - utime:unixtime()),
    {B_X, B_Y} = left_pos(),
    WarScene = get_war(Group),
    % ?PRINT("dumy ~p:~p:~p:~p~n",[WarScene,Group,B_X,B_Y]),
    {BattleAttr, LastPower, _MonLv} = get_mon_attr(DrumRole),
    Args = [{skill, Skill}, {group, 2}, {find_target, Time * 1000}, {platformInfo, Platform, MonServerNum}, {combat_power, LastPower}],
    case lib_scene_object:sync_create_a_dummy(WarScene, 0, B_X, B_Y, Group, 1, MonFigure, BattleAttr, Args) of
        DummyId when is_integer(DummyId) ->
            case get_mon_aid(WarScene, DummyId) of
                ignore -> %%找不到怪物AI 通知玩家胜利
                    {0, 0};
                Aid ->
                    {Aid, DummyId}
            end;
        0 -> %%创建失败 通知玩家胜利
           {0, 0}
    end.

%% 创建假人属性
get_mon_attr(DrumRole) ->
    #drum_role{zid = Zone, action = Action, figure = #figure{lv = Lv}, power = Power} = DrumRole,
    case data_drumwar:get_mon_info(Zone, Action, Lv) of
        #base_mon{mon_lv = MonLv, ratio = Ratio, power = CfgPower} ->
            {PMin, PMax} = CfgPower,
            MonPower = urand:rand(PMin, PMax),
            BattleAttr = lib_fake_role:get_faker_role_attr(MonLv, MonPower, (Ratio / 100));
        _ ->
            MonPower = round(Power * 0.8),
            MonLv = Lv,
            BattleAttr = lib_fake_role:make_default_battle_attr(Power)
    end,
    {BattleAttr, MonPower, MonLv}.

%%获取假人AI
get_mon_aid(Scene, MonId) ->
    case lib_scene_object:lookup(Scene, 0, MonId) of
        #scene_object{aid = MonAid} ->
            MonAid;
        _ -> ignore
    end.


%%站位坐标
% left_pos() -> {737,1049}.
% right_pos() -> {1757,551}.
left_pos() -> mod_drumwar_mgr:get_kv(?LEFT_POS).
right_pos() -> mod_drumwar_mgr:get_kv(?RIGHT_POS).

%% ---------------------------------------------------------------------------
%% 第一步，让怪物移动
mon_move(MonAid, X, Y) when is_pid(MonAid)->
    MonAid ! {move, X-200, Y, 1, 0};

mon_move(_MonAid, _X, _Y) ->
    self() ! stop,
    ignore.
%%mon_move(MonAid, X, Y) ->
%%  MonAid ! {move, X-200, Y, 1, 0}.
%% 第二步，冲刺
mon_rush(MonAid, X, Y) when is_pid(MonAid) ->
    MonAid ! {move, X-600, Y, 1, 2};

mon_rush(_MonAid, _X, _Y) ->
    % self() ! stop,
    ignore.

%%mon_rush(MonAid, X, Y) ->
%%  MonAid ! {move, X-600, Y, 1, 2}.
%% 第三步，开打
mon_battle(MonAid, Group) ->
    Scene = get_war(Group),
    case lib_scene:get_scene_user(Scene, Group) of
        [#ets_scene_user{
            id = RoleID,
            platform = PlatForm,
            server_num = ServerNum,
            pid = Pid}] ->
            case  misc:is_process_alive(MonAid) of
                true ->
                    MonAid ! {'ATTACK_ONE', [[RoleID, PlatForm, ServerNum], Pid, 2]};
                false ->
                    % self() ! stop,
                    ignore
            end;
            _ -> ignore
    end.

%% 计算移动耗时（单位：毫秒）
calc_move_time({OldX, OldY}, {NewX, NewY}, Speed) ->
    Dis = calc_target_distances(OldX, OldY, NewX, NewY),
    round(Dis / Speed * 1000).

%% @doc 与目标点的直线距离
%% X1,Y1：起始坐标;X2,Y2:目标坐标
%% 两者直线距离
%% @end
calc_target_distances(X1, Y1, X2, Y2)->
    D_x = abs(X1 - X2),
    D_y = abs(Y1 - Y2),
    AttLen = round(math:sqrt(D_x*D_x+D_y*D_y)),
    AttLen.

%% ----------------------------------db更新-----------------------------------------
db_replace_role_drum(RoleId,RoleDrum)->
    #role_drum{
        drumid = DrumId,
        one  = One,
        zone = Zone,
        action = Action,
        deal   = Deal,
        choice = Choise,
        time = PayTime
    }
    = RoleDrum,
    LastChoice = util:term_to_bitstring(Choise),
    Sql = io_lib:format(?SQL_DRUM_INSERT, [RoleId,DrumId,One,Zone,Action,Deal,LastChoice,PayTime]),
    db:execute(Sql).

db_replace_drum_sign(DrumRole) ->
    DrumId = get_id(),
    #drum_role{
        rid = RoleId,
        server_id  = ServerId,
        platform = Platform,
        server_num = ServerNum,
        figure = #figure{name=Name,lv=Lv,career=Career,picture_ver=PictureV,turn = Turn},
        power = Power
    }
    = DrumRole,
    Sql = io_lib:format(?SQL_DRUMWAR_SIGN_BATCH, [RoleId,ServerId,Platform,ServerNum,DrumId,Name,PictureV,Turn,Lv,Career,Power]),
    db:execute(Sql).

%% ----------------------------------事件处理-----------------------------------------
%%充值事件
% handle_event(PS, #event_callback{type_id = ?EVENT_RECHARGE})  ->
%     #player_status{id=RoleId,role_drum=RoleDrum} = PS,
%     #role_drum{pay_time=PayTime} = RoleDrum,
%     Day = utime:day_of_week(),
%     [OpenT] = data_drumwar:get_open(),
%     IsSameDay = lists:keyfind(Day,1,OpenT),
%     case utime:is_logic_same_day(PayTime) == false andalso IsSameDay=/= false of
%         true->
%             Title = utext:get(326),
%             Content = utext:get(327),
%             Award = [{?TYPE_GOODS,?DRUMITEM,1}],
%             lib_mail_api:send_sys_mail([RoleId], ?MOD_DRUMWAR, Title, Content, Award),
%             NewRoleDrum = RoleDrum#role_drum{pay_time=utime:unixtime()},
%             db_replace_role_drum(RoleId,NewRoleDrum),
%             NewPs = PS#player_status{role_drum=NewRoleDrum};
%         false-> NewPs = PS
%     end,
%     {ok, NewPs};
handle_event(PS, _) -> {ok, PS}.

%% ----------------------------------日志-----------------------------------------
log_drum_sign(DrumRole) ->
    #drum_role{
        rid = RoleId,
        figure = Figure
    }
    = DrumRole,
    lib_log_api:log_drum_sign(Figure#figure.name,RoleId,Figure#figure.lv).

log_drum_cost(RoleId,Type,Cost) ->
    lib_log_api:log_drum_cost(RoleId,Type,Cost).

% trans_skill_id_to_type(1000009)->2;
% trans_skill_id_to_type(1000010)->3;
% trans_skill_id_to_type(1000011)->4;
% trans_skill_id_to_type(_)->2.

log_drum_refuce(Node,RoleID,Sid,Power) ->
    lib_log_api:log_drum_refuce(Node,RoleID,Sid,Power).

log_drum_refuce_mail(RoleID)->
    lib_log_api:log_drum_refuce_mail(RoleID).
