From 40d2eed01bda3bb2af24bf593b33469b61f695f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sun, 20 Feb 2011 23:29:49 +0100 Subject: [PATCH] egs_users: Remove select({neighbors, User}). Use egs_zones for broadcasting. --- src/egs_game.erl | 7 +++---- src/egs_users.erl | 21 ++++++++++++--------- src/egs_zones.erl | 10 +++++++++- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/egs_game.erl b/src/egs_game.erl index bf7eca8..cbaa5c2 100644 --- a/src/egs_game.erl +++ b/src/egs_game.erl @@ -94,11 +94,10 @@ cast(Command, Data, #state{gid=GID}) case egs_users:read(GID) of {error, _Reason} -> ignore; - {ok, Self} -> - LID = Self#users.lid, + {ok, User} -> + LID = User#users.lid, Packet = << A/binary, 16#00011300:32, GID:32/little, B/binary, GID:32/little, LID:32/little, C/binary >>, - {ok, SpawnList} = egs_users:select({neighbors, Self}), - lists:foreach(fun(User) -> User#users.pid ! {egs, cast, Packet} end, SpawnList) + egs_zones:broadcast(User#users.zonepid, GID, Packet) end. %% Raw commands. diff --git a/src/egs_users.erl b/src/egs_users.erl index de51d77..f90a1c7 100644 --- a/src/egs_users.erl +++ b/src/egs_users.erl @@ -20,7 +20,7 @@ -module(egs_users). -behaviour(gen_server). --export([start_link/0, stop/0, broadcast_spawn/2, broadcast_unspawn/2, set_zone/3]). %% API. +-export([start_link/0, stop/0, broadcast_spawn/2, broadcast_unspawn/2, broadcast/2, set_zone/3]). %% API. -export([read/1, select/1, write/1, delete/1, item_nth/2, item_add/3, item_qty_add/3, shop_enter/2, shop_leave/1, shop_get/1, money_add/2]). %% Deprecated API. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). %% gen_server. @@ -50,6 +50,9 @@ broadcast_spawn(GID, PlayersGID) -> broadcast_unspawn(GID, PlayersGID) -> gen_server:cast(?SERVER, {broadcast_unspawn, GID, PlayersGID}). +broadcast(Message, PlayersGID) -> + gen_server:cast(?SERVER, {broadcast, Message, PlayersGID}). + set_zone(GID, ZonePid, LID) -> gen_server:call(?SERVER, {set_zone, GID, ZonePid, LID}). @@ -60,10 +63,8 @@ set_zone(GID, ZonePid, LID) -> read(ID) -> gen_server:call(?SERVER, {read, ID}). -%% @spec select({neighbors, User}) -> {ok, List} -%% @todo state = undefined | {wait_for_authentication, Key} | authenticated | online -select(What) -> - gen_server:call(?SERVER, {select, What}). +select(GIDsList) -> + gen_server:call(?SERVER, {select, GIDsList}). %% @spec write(User) -> ok write(User) -> @@ -112,10 +113,6 @@ handle_call({read, GID}, _From, State) -> {GID, User} = lists:keyfind(GID, 1, State#stateu.users), {reply, {ok, User}, State}; -handle_call({select, {neighbors, #users{gid=FromGID, uni=FromUni, area=FromArea}}}, _From, State) -> - Users = [User || {GID, User = #users{uni=Uni, area=Area}} - <- State#stateu.users, GID =/= FromGID, Uni =:= FromUni, Area =:= FromArea], - {reply, {ok, Users}, State}; handle_call({select, UsersGID}, _From, State) -> Users = [begin {GID, User} = lists:keyfind(GID, 1, State#stateu.users), @@ -239,6 +236,12 @@ handle_cast({broadcast_unspawn, GID, PlayersGID}, State) -> end || DestGID <- PlayersGID], {noreply, State}; +handle_cast({broadcast, Message, PlayersGID}, State) -> + [begin {GID, #users{pid=Pid}} = lists:keyfind(GID, 1, State#stateu.users), + Pid ! Message + end || GID <- PlayersGID], + {noreply, State}; + handle_cast(_Msg, State) -> {noreply, State}. diff --git a/src/egs_zones.erl b/src/egs_zones.erl index 74341d4..6d5968a 100644 --- a/src/egs_zones.erl +++ b/src/egs_zones.erl @@ -20,7 +20,7 @@ -module(egs_zones). -behaviour(gen_server). --export([start_link/4, stop/1, setid/1, enter/2, leave/2, get_all_players/2]). %% API. +-export([start_link/4, stop/1, setid/1, enter/2, leave/2, get_all_players/2, broadcast/3]). %% API. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). %% gen_server. -record(state, { @@ -56,6 +56,9 @@ leave(Pid, GID) -> get_all_players(Pid, ExcludeGID) -> gen_server:call(Pid, {get_all_players, ExcludeGID}). +broadcast(Pid, FromGID, Packet) -> + gen_server:cast(Pid, {broadcast, FromGID, Packet}). + %% gen_server. init([UniID, QuestID, ZoneID, ZoneData]) -> @@ -94,6 +97,11 @@ handle_call(stop, _From, State) -> handle_call(_Request, _From, State) -> {reply, ignored, State}. +handle_cast({broadcast, FromGID, Packet}, State) -> + PlayersGID = lists:delete(FromGID, players_gid(State#state.players)), + egs_users:broadcast({egs, cast, Packet}, PlayersGID), + {noreply, State}; + handle_cast(_Msg, State) -> {noreply, State}.