diff --git a/include/records.hrl b/include/records.hrl index 7c338e3..a4b6f92 100644 --- a/include/records.hrl +++ b/include/records.hrl @@ -101,9 +101,6 @@ faceboxx=65535, faceboxy=65535 }). -%% @doc Table containing counters current values. --record(counters, {name, id}). - %% @doc Character main or class level data structure. -record(level, {number, exp}). diff --git a/src/egs_accounts.erl b/src/egs_accounts.erl index 6f39a70..1f43215 100644 --- a/src/egs_accounts.erl +++ b/src/egs_accounts.erl @@ -20,7 +20,7 @@ -module(egs_accounts). -behaviour(gen_server). --export([start_link/0, stop/0, get_folder/1, key_auth/2, key_auth_init/1, key_auth_timeout/1, login_auth/2]). %% API. +-export([start_link/0, stop/0, get_folder/1, key_auth/2, key_auth_init/1, key_auth_timeout/1, login_auth/2, tmp_gid/0]). %% API. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). %% gen_server. -define(SERVER, ?MODULE). @@ -36,8 +36,9 @@ }). -record(state, { - accounts = [] :: list(#accounts{}), - next_gid = 10000001 :: integer() + accounts = [] :: list(#accounts{}), + next_gid = 10000001 :: integer(), + tmp_gid = 16#ff000001 :: integer() }). %% API. @@ -76,6 +77,11 @@ key_auth_timeout(GID) -> login_auth(Username, Password) -> gen_server:call(?SERVER, {login_auth, Username, Password}). +-spec tmp_gid() -> GID::integer(). +%% @doc Return an unused temporary GID for initial connection and APC characters. +tmp_gid() -> + gen_server:call(?SERVER, tmp_gid). + %% gen_server. init([]) -> @@ -109,6 +115,10 @@ handle_call({login_auth, Username, Password}, _From, State) -> Account = #accounts{gid=GID, username=Username, password=Password}, {reply, {ok, GID}, State#state{next_gid=GID + 1, accounts=[{GID, Account}|State#state.accounts]}}; +handle_call(tmp_gid, _From, State) -> + GID = State#state.tmp_gid, + {reply, GID, State#state{tmp_gid=GID + 1}}; + handle_call(stop, _From, State) -> {stop, normal, stopped, State}; diff --git a/src/egs_app.erl b/src/egs_app.erl index 8702d6e..8d95a4f 100644 --- a/src/egs_app.erl +++ b/src/egs_app.erl @@ -67,7 +67,6 @@ db_init() -> error_logger:info_report("mnesia schema created"), error_logger:info_report("starting mnesia"), mnesia:start(), - mnesia:create_table(counters, [{attributes, record_info(fields, counters)}]), mnesia:create_table(users, [{attributes, record_info(fields, users)}]), error_logger:info_report("mnesia tables created"), ok. diff --git a/src/egs_game_server.erl b/src/egs_game_server.erl index 1a3a380..2a0c262 100644 --- a/src/egs_game_server.erl +++ b/src/egs_game_server.erl @@ -58,7 +58,6 @@ on_exit(Pid) -> init(Socket) -> egs_game_server_exit_mon ! {link, self()}, timer:send_interval(5000, {egs, keepalive}), - TmpGID = 16#ff000000 + mnesia:dirty_update_counter(counters, tmpgid, 1), - State = #state{socket=Socket, gid=TmpGID}, + State = #state{socket=Socket, gid=egs_accounts:tmp_gid()}, psu_proto:send_0202(State), egs_network:recv(<< >>, egs_login, State). diff --git a/src/egs_login_server.erl b/src/egs_login_server.erl index 62d75d4..b8a72dc 100644 --- a/src/egs_login_server.erl +++ b/src/egs_login_server.erl @@ -35,7 +35,6 @@ on_exit(_Pid) -> %% @doc Initialize the game state and start receiving messages. init(Socket) -> - TmpGID = 16#ff000000 + mnesia:dirty_update_counter(counters, tmpgid, 1), - State = #state{socket=Socket, gid=TmpGID}, + State = #state{socket=Socket, gid=egs_accounts:tmp_gid()}, psu_proto:send_0202(State), egs_network:recv(<< >>, egs_login, State). diff --git a/src/egs_npc_db.erl b/src/egs_npc_db.erl index 3408051..42e17c8 100644 --- a/src/egs_npc_db.erl +++ b/src/egs_npc_db.erl @@ -61,7 +61,7 @@ handle_call(all, _From, State) -> %% @todo Handle stats, experience, based on level. handle_call({create, NPCid, BaseLevel}, _From, State) -> - NPCGID = 16#ff000000 + mnesia:dirty_update_counter(counters, tmpgid, 1), + NPCGID = egs_accounts:tmp_gid(), #npc{name=Name, race=Race, gender=Gender, class=Class, level_diff=LevelDiff, appearance=Appearance} = proplists:get_value(NPCid, ?NPC), TmpUCS2Name = << << X:8, 0:8 >> || X <- Name >>, Padding = 8 * (64 - byte_size(TmpUCS2Name)),