%%%-------------------------------------------------------------------
%%% @doc    跨服军衔排行榜 - 本地管理
%%% Module  : mod_military_rank_local.erl
%%% Created : 2021-02-07
%%% @Author : tyl
%%%-------------------------------------------------------------------
-module(mod_military_rank_local).

-include("kf_legion.hrl").
-include("common.hrl").
-include("server.hrl").
-include("errcode.hrl").
-include("czone.hrl").
-include("predefine.hrl").

-behaviour(gen_server).

%% API
-export([
	update_military_pos/5
	,update_role_list/1
]).

-export([start_link/0, cast/1 ,call/1 ,info/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-record(military_rank_local, {
	role_list      = []
}).

%% 更新军衔,记录玩家数据。已经更新过的,再次登录不需要更新
update_military_pos(RoleId, ZoneId, RoleInfo, Pos, Time) ->
	cast({update_military_pos, RoleId, ZoneId, RoleInfo, Pos, Time}).

%% 合分区后重新更新role_list
update_role_list(RoleList) ->
	cast({update_role_list, RoleList}).

%% ---------------------------------------------------------------------------
start_link() ->
	gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

cast(Msg) ->
	gen_server:cast(?MODULE, Msg).

call(Msg) ->
	gen_server:call(?MODULE, Msg).

info(Msg) ->
	erlang:send(?MODULE, Msg).

init([]) ->
	process_flag(trap_exit, true),
	State = #military_rank_local{},
	{ok, State}.

handle_call(Msg, From, State) ->
	try do_handle_call(Msg, From, State) of
		Reply ->
			Reply
	catch ?EXCEPTION(Class, Reason, StackTrace) ->
		Error = {Class, Reason, StackTrace},
		?ERR("~w:handle_call error:~p~n", [?MODULE, Error]),
		{error, Reason}
	end.

handle_cast(Msg, State) ->
	try do_handle_cast(Msg, State) of
		Result ->
			Result
	catch ?EXCEPTION(Class, Reason, StackTrace) ->
		Error = {Class, Reason, StackTrace},
		?ERR("~w:handle_cast error:~p~n", [?MODULE, Error]),
		{noreply, State}
	end.

handle_info(Info, State) ->
	try do_handle_info(Info, State) of
		Result ->
			Result
	catch ?EXCEPTION(Class, Reason, StackTrace) ->
		Error = {Class, Reason, StackTrace},
		?ERR("~w:handle_info error:~p~n", [?MODULE, Error]),
		{noreply, State}
	end.

terminate(_Reason, _State) ->
	ok.

code_change(_OldVsn, State, _Extra) ->
	{ok, State}.

%% ---------------------------------------------------------------------------
do_handle_call(_Msg, _From, State) ->
	{reply, ok, State}.

%% ---------------------------------------------------------------------------
do_handle_cast({update_military_pos, RoleId, ZoneId, RoleInfo, UpPos, Time}, State) ->
	#military_rank_local{role_list = RoleList} = State,
	case lists:member(RoleId, RoleList) of
		true -> NewState = State;
		_ ->
			clusters_node:apply_cast(mod_military_rank_mgr, update_military_pos,
				[ZoneId, RoleInfo, UpPos, Time]),
			NewState = State#military_rank_local{role_list = [RoleId|RoleList]}
	end,
	{noreply, NewState};

do_handle_cast({update_role_list, RoleList}, State) ->
	NewState = State#military_rank_local{role_list = RoleList},
	{noreply, NewState};

do_handle_cast(_Msg, State) ->
	?ERR("Handle unknow msg[~w]~n", [_Msg]),
	{noreply, State}.

%% ---------------------------------------------------------------------------
do_handle_info(_Info, State) ->
	?ERR("Handle unknow msg[~w]~n", [_Info]),
	{noreply, State}.
