diff --git a/include/records.hrl b/include/records.hrl index e69ae33..983673a 100644 --- a/include/records.hrl +++ b/include/records.hrl @@ -19,4 +19,4 @@ %% EGS database schema. -record(ids, {type, id}). --record(users, {gid, pid, socket, auth, time, folder, charnumber, charname, lid, quest, maptype, mapnumber, mapentry, direction, coords}). +-record(users, {gid, pid, socket, auth, time, folder, charnumber, charname, lid, instanceid, quest, maptype, mapnumber, mapentry, direction, coords}). diff --git a/src/egs_db.erl b/src/egs_db.erl index 0f6879d..948b054 100644 --- a/src/egs_db.erl +++ b/src/egs_db.erl @@ -65,6 +65,18 @@ users_select_all() -> users_select_others(GID) -> do(qlc:q([X || X <- mnesia:table(users), X#users.gid /= GID, X#users.charnumber /= undefined])). +%% @doc Select all other users in the same area. Return a list of #users records. + +users_select_others_in_area(Self) -> + do(qlc:q([X || X <- mnesia:table(users), + X#users.gid /= Self#users.gid, + X#users.charnumber /= undefined, + X#users.instanceid =:= Self#users.instanceid, + X#users.quest =:= Self#users.quest, + X#users.maptype =:= Self#users.maptype, + X#users.mapnumber =:= Self#users.mapnumber + ])). + %% @doc Insert or update an user. users_insert(User) -> diff --git a/src/egs_game.erl b/src/egs_game.erl index 7f676c1..a07f34e 100644 --- a/src/egs_game.erl +++ b/src/egs_game.erl @@ -217,7 +217,7 @@ counter_load(CSocket, GID, Quest, MapType, MapNumber, MapEntry) -> lobby_load(CSocket, GID, Quest, MapType, MapNumber, MapEntry) -> OldUser = egs_db:users_select(GID), - User = OldUser#users{quest=Quest, maptype=MapType, mapnumber=MapNumber, mapentry=MapEntry}, + User = OldUser#users{instanceid=undefined, quest=Quest, maptype=MapType, mapnumber=MapNumber, mapentry=MapEntry}, egs_db:users_insert(User), [{status, 1}, {char, Char}, {options, Options}] = char_load(User#users.folder, User#users.charnumber), [{name, AreaName}, {quest, QuestFile}, {zone, ZoneFile}, {entries, _}] = proplists:get_value([Quest, MapType, MapNumber], ?MAPS, @@ -262,7 +262,7 @@ lobby_load(CSocket, GID, Quest, MapType, MapNumber, MapEntry) -> mission_load(CSocket, GID, Quest, MapType, MapNumber, MapEntry) -> OldUser = egs_db:users_select(GID), - User = OldUser#users{quest=Quest, maptype=MapType, mapnumber=MapNumber, mapentry=MapEntry}, + User = OldUser#users{instanceid=GID, quest=Quest, maptype=MapType, mapnumber=MapNumber, mapentry=MapEntry}, egs_db:users_insert(User), [{status, 1}, {char, Char}, {options, _}] = char_load(User#users.folder, User#users.charnumber), [{name, AreaName}, {quest, QuestFile}, {zone, ZoneFile}, {entries, _}] = proplists:get_value([Quest, MapType, MapNumber], ?MAPS), @@ -472,7 +472,7 @@ broadcast(Command, GID, Packet) LID = Self#users.lid, SafePacket = << A/binary, 16#00011300:32, GID:32/little-unsigned-integer, B/binary, GID:32/little-unsigned-integer, LID:32/little-unsigned-integer, C/binary >>, - lists:foreach(fun(User) -> User#users.pid ! {psu_broadcast, SafePacket} end, egs_db:users_select_others(GID)) + lists:foreach(fun(User) -> User#users.pid ! {psu_broadcast, SafePacket} end, egs_db:users_select_others_in_area(Self)) end. %% @doc Movement (non-broadcast) handler. Do nothing.