egs_users: Introduce broadcast_all/1 to broadcast messages to all online users.

This commit is contained in:
Loïc Hoguin 2011-02-21 02:30:39 +01:00
parent 6c8b831fd2
commit d69fe073a8
3 changed files with 11 additions and 8 deletions

View File

@ -53,14 +53,12 @@ global(Message) ->
if length(Message) > 511 -> if length(Message) > 511 ->
io:format("global: message too long~n"); io:format("global: message too long~n");
true -> true ->
{ok, List} = egs_users:select(all), egs_users:broadcast_all({egs, notice, top, Message})
lists:foreach(fun(User) -> User#users.pid ! {egs, notice, top, Message} end, List)
end. end.
%% @doc Warp all players to a new map. %% @doc Warp all players to a new map.
warp(QuestID, ZoneID, MapID, EntryID) -> warp(QuestID, ZoneID, MapID, EntryID) ->
{ok, List} = egs_users:select(all), egs_users:broadcast_all({egs, warp, QuestID, ZoneID, MapID, EntryID}).
lists:foreach(fun(User) -> User#users.pid ! {egs, warp, QuestID, ZoneID, MapID, EntryID} end, List).
%% @doc Warp one player to a new map. %% @doc Warp one player to a new map.
warp(GID, QuestID, ZoneID, MapID, EntryID) -> warp(GID, QuestID, ZoneID, MapID, EntryID) ->

View File

@ -235,9 +235,7 @@ event({chat, _FromTypeID, FromGID, _FromName, Modifiers, ChatMsg}, #state{gid=Us
[TmpMessage|_] = re:split(ChatMsg, "\\0\\0", [{return, binary}]), [TmpMessage|_] = re:split(ChatMsg, "\\0\\0", [{return, binary}]),
LogMessage = re:replace(TmpMessage, "\\n", " ", [global, {return, binary}]), LogMessage = re:replace(TmpMessage, "\\n", " ", [global, {return, binary}]),
log("chat from ~s: ~s", [[re:replace(LogName, "\\0", "", [global, {return, binary}])], [re:replace(LogMessage, "\\0", "", [global, {return, binary}])]]), log("chat from ~s: ~s", [[re:replace(LogName, "\\0", "", [global, {return, binary}])], [re:replace(LogMessage, "\\0", "", [global, {return, binary}])]]),
%% broadcast egs_users:broadcast_all({egs, chat, UserGID, BcastTypeID, BcastGID, BcastName, Modifiers, ChatMsg});
{ok, List} = egs_users:select(all),
lists:foreach(fun(X) -> X#users.pid ! {egs, chat, UserGID, BcastTypeID, BcastGID, BcastName, Modifiers, ChatMsg} end, List);
%% @todo There's at least 9 different sets of locations. Handle all of them correctly. %% @todo There's at least 9 different sets of locations. Handle all of them correctly.
event(counter_background_locations_request, _State) -> event(counter_background_locations_request, _State) ->

View File

@ -20,7 +20,7 @@
-module(egs_users). -module(egs_users).
-behaviour(gen_server). -behaviour(gen_server).
-export([start_link/0, stop/0, broadcast/2, find_by_pid/1, set_zone/3]). %% API. -export([start_link/0, stop/0, broadcast/2, broadcast_all/1, find_by_pid/1, set_zone/3]). %% API.
-export([read/1, select/1, write/1, delete/1, item_nth/2, item_add/3, item_qty_add/3, -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. 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. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). %% gen_server.
@ -47,6 +47,9 @@ stop() ->
broadcast(Message, PlayersGID) -> broadcast(Message, PlayersGID) ->
gen_server:cast(?SERVER, {broadcast, Message, PlayersGID}). gen_server:cast(?SERVER, {broadcast, Message, PlayersGID}).
broadcast_all(Message) ->
gen_server:cast(?SERVER, {broadcast_all, Message}).
find_by_pid(Pid) -> find_by_pid(Pid) ->
gen_server:call(?SERVER, {find_by_pid, Pid}). gen_server:call(?SERVER, {find_by_pid, Pid}).
@ -225,6 +228,10 @@ handle_cast({broadcast, Message, PlayersGID}, State) ->
end || GID <- PlayersGID], end || GID <- PlayersGID],
{noreply, State}; {noreply, State};
handle_cast({broadcast_all, Message}, State) ->
[Pid ! Message || {_GID, #users{pid=Pid}} <- State#stateu.users],
{noreply, State};
handle_cast(_Msg, State) -> handle_cast(_Msg, State) ->
{noreply, State}. {noreply, State}.