egs_accounts: Add tmp_gid/0. Remove the table/record counters.

This commit is contained in:
Loïc Hoguin 2011-02-20 02:42:40 +01:00
parent 69a07dfad2
commit 344b88eec4
6 changed files with 16 additions and 12 deletions

View File

@ -101,9 +101,6 @@
faceboxx=65535, faceboxy=65535 faceboxx=65535, faceboxy=65535
}). }).
%% @doc Table containing counters current values.
-record(counters, {name, id}).
%% @doc Character main or class level data structure. %% @doc Character main or class level data structure.
-record(level, {number, exp}). -record(level, {number, exp}).

View File

@ -20,7 +20,7 @@
-module(egs_accounts). -module(egs_accounts).
-behaviour(gen_server). -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. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). %% gen_server.
-define(SERVER, ?MODULE). -define(SERVER, ?MODULE).
@ -36,8 +36,9 @@
}). }).
-record(state, { -record(state, {
accounts = [] :: list(#accounts{}), accounts = [] :: list(#accounts{}),
next_gid = 10000001 :: integer() next_gid = 10000001 :: integer(),
tmp_gid = 16#ff000001 :: integer()
}). }).
%% API. %% API.
@ -76,6 +77,11 @@ key_auth_timeout(GID) ->
login_auth(Username, Password) -> login_auth(Username, Password) ->
gen_server:call(?SERVER, {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. %% gen_server.
init([]) -> init([]) ->
@ -109,6 +115,10 @@ handle_call({login_auth, Username, Password}, _From, State) ->
Account = #accounts{gid=GID, username=Username, password=Password}, Account = #accounts{gid=GID, username=Username, password=Password},
{reply, {ok, GID}, State#state{next_gid=GID + 1, accounts=[{GID, Account}|State#state.accounts]}}; {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) -> handle_call(stop, _From, State) ->
{stop, normal, stopped, State}; {stop, normal, stopped, State};

View File

@ -67,7 +67,6 @@ db_init() ->
error_logger:info_report("mnesia schema created"), error_logger:info_report("mnesia schema created"),
error_logger:info_report("starting mnesia"), error_logger:info_report("starting mnesia"),
mnesia:start(), mnesia:start(),
mnesia:create_table(counters, [{attributes, record_info(fields, counters)}]),
mnesia:create_table(users, [{attributes, record_info(fields, users)}]), mnesia:create_table(users, [{attributes, record_info(fields, users)}]),
error_logger:info_report("mnesia tables created"), error_logger:info_report("mnesia tables created"),
ok. ok.

View File

@ -58,7 +58,6 @@ on_exit(Pid) ->
init(Socket) -> init(Socket) ->
egs_game_server_exit_mon ! {link, self()}, egs_game_server_exit_mon ! {link, self()},
timer:send_interval(5000, {egs, keepalive}), timer:send_interval(5000, {egs, keepalive}),
TmpGID = 16#ff000000 + mnesia:dirty_update_counter(counters, tmpgid, 1), State = #state{socket=Socket, gid=egs_accounts:tmp_gid()},
State = #state{socket=Socket, gid=TmpGID},
psu_proto:send_0202(State), psu_proto:send_0202(State),
egs_network:recv(<< >>, egs_login, State). egs_network:recv(<< >>, egs_login, State).

View File

@ -35,7 +35,6 @@ on_exit(_Pid) ->
%% @doc Initialize the game state and start receiving messages. %% @doc Initialize the game state and start receiving messages.
init(Socket) -> init(Socket) ->
TmpGID = 16#ff000000 + mnesia:dirty_update_counter(counters, tmpgid, 1), State = #state{socket=Socket, gid=egs_accounts:tmp_gid()},
State = #state{socket=Socket, gid=TmpGID},
psu_proto:send_0202(State), psu_proto:send_0202(State),
egs_network:recv(<< >>, egs_login, State). egs_network:recv(<< >>, egs_login, State).

View File

@ -61,7 +61,7 @@ handle_call(all, _From, State) ->
%% @todo Handle stats, experience, based on level. %% @todo Handle stats, experience, based on level.
handle_call({create, NPCid, BaseLevel}, _From, State) -> 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), #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 >>, TmpUCS2Name = << << X:8, 0:8 >> || X <- Name >>,
Padding = 8 * (64 - byte_size(TmpUCS2Name)), Padding = 8 * (64 - byte_size(TmpUCS2Name)),