%%%------------------------------------
%%% @Module  : mod_arena
%%% @Author  :  xiaoxiang
%%% @Created :  2017-03-23
%%% @Description: arena
%%%------------------------------------
-module(mod_arena).
-export([]).

-compile(export_all).

-include("common.hrl").
-include("arena.hrl").

cast_to_arena(F, A) ->
    gen_server:cast(?MODULE, {F, A}).

%% --------------------------------- 公共函数 ----------------------------------
%% @doc Starts the server
-spec start_link() -> {ok, Pid} | ignore | {error, Error} when
    Pid :: pid(),
    Error :: term().
%% ---------------------------------------------------------------------------
start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).


%% @doc Initializes the server
-spec init(Args) ->
    {ok, State}
    | {ok, State, Timeout}
    | ignore
    | {stop, Reason} when
    Args    :: term() ,
    State   :: term(),
    Timeout :: non_neg_integer() | infinity,
    Reason  :: term().
%% ---------------------------------------------------------------------------
init([]) ->
    erlang:process_flag(trap_exit, true),
    State = lib_arena_util:do_init(),
    {ok, State}.


-spec handle_call(Request, From, State) ->
    {reply, Reply, State}
    | {reply, Reply, State, Timeout}
    | {noreply, State}
    | {noreply, State, Timeout}
    | {stop, Reason, Reply, State}
    | {stop, Reason, State} when
    Request :: term(),
    From :: pid(),
    State :: term(),
    Reply :: term(),
    State :: term(),
    Timeout :: non_neg_integer() | infinity.
%% ---------------------------------------------------------------------------
handle_call(_Request, _From, State) ->
    {reply, true, State}.

%% @doc Handling cast messages
-spec handle_cast(Msg, State) ->
    {noreply, State}
    | {noreply, State, Timeout}
    | {stop, Reason, State} when
    Msg :: term(),
    State :: term(),
    Timeout :: non_neg_integer() | infinity,
    Reason :: term().
%% ---------------------------------------------------------------------------
handle_cast({F,A}, State) ->
    NewState =
        case catch apply(mod_arena_cast, F, [State|A]) of
            #arena_state{} = State1 ->
                State1;
            {'EXIT', ERR} ->
                ?ERR("handle_cast error:~p,fun:~p~n", [ERR, F]),
                State;
            _R ->
                ?ERR("handle_cast error:~p,fun:~p~n", [_R, F]),
                State
    end,
    {noreply, NewState};


handle_cast(_Msg, State) ->
    {noreply, State}.

%% ---------------------------------------------------------------------------
%% @doc Handling all non call/cast messages
-spec handle_info(Info, State) ->
    {noreply, State}
    | {noreply, State, Timeout}
    | {stop, Reason, State} when
    Info :: term(),
    State :: term(),
    Timeout :: non_neg_integer() | infinity,
    Reason :: term().
%% ---------------------------------------------------------------------------
%% 赛季结算
handle_info({season_settlement}, State) ->
    NewState = try lib_arena_util:season_settlement(State) of
        State2 -> State2
    catch Class:Reason ->
        Error = {Class, Reason, erlang:get_stacktrace()},
        ?ERR("~w:season_settlement_error:~p~n", [?MODULE, Error]),
        State
    end,
    {noreply, NewState};

handle_info(_Info, State) ->
    % ?ERR1("Handle unkown info[~w]~n", [_Info]),
    {noreply, State}.

%% ---------------------------------------------------------------------------
%% @doc called by a gen_server when it is about to terminate
-spec terminate(Reason, State) -> ok when
    Reason :: term(),
    State :: term().
%% ---------------------------------------------------------------------------
terminate(_Reason, _State) ->
    ok.


%% ---------------------------------------------------------------------------
%% @doc Convert process state when code is changed
-spec code_change(OldVsn, State, Extra) -> {ok, NewState} when
    OldVsn :: term(),
    State :: term(),
    Extra :: term(),
    NewState :: term().
%% ---------------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.
