egs_game_server: Properly leave the zone when the user is disconnecting.

This commit is contained in:
Loïc Hoguin 2011-02-19 22:23:42 +01:00
parent 2de4359c32
commit 378e9a9927
2 changed files with 13 additions and 17 deletions

View File

@ -45,15 +45,9 @@ on_exit(Pid) ->
[egs_users:delete(NPCGID) || {_Spot, NPCGID} <- NPCList],
psu_party:stop(PartyPid)
end,
egs_zones:leave(User#users.zonepid, User#users.gid),
egs_universes:leave(User#users.uni),
egs_users:delete(User#users.gid),
case User#users.uni of
undefined ->
ignore;
UniID ->
egs_universes:leave(UniID),
{ok, List} = egs_users:select({neighbors, User}),
lists:foreach(fun(Other) -> Other#users.pid ! {egs, player_unspawn, User} end, List)
end,
io:format("game (~p): quit~n", [User#users.gid]);
{error, _Reason} ->
ignore

View File

@ -48,8 +48,10 @@ setid(Pid) ->
enter(Pid, GID) ->
gen_server:call(Pid, {enter, GID}).
leave(undefined, _GID) ->
ok;
leave(Pid, GID) ->
gen_server:cast(Pid, {leave, GID}).
gen_server:call(Pid, {leave, GID}).
get_all_players(Pid, ExcludeGID) ->
gen_server:call(Pid, {get_all_players, ExcludeGID}).
@ -75,6 +77,14 @@ handle_call({enter, GID}, _From, State) ->
egs_users:broadcast_spawn(GID, PlayersGID),
{reply, LID, State#state{players=[{GID, LID}|Players], freelids=FreeLIDs}};
handle_call({leave, GID}, _From, State) ->
{_, LID} = lists:keyfind(GID, 1, State#state.players),
Players = lists:delete({GID, LID}, State#state.players),
PlayersGID = players_gid(Players),
FreeLIDs = State#state.freelids,
egs_users:broadcast_unspawn(GID, PlayersGID),
{reply, ok, State#state{players=Players, freelids=[LID|FreeLIDs]}};
handle_call({get_all_players, ExcludeGID}, _From, State) ->
{reply, lists:delete(ExcludeGID, players_gid(State#state.players)), State};
@ -84,14 +94,6 @@ handle_call(stop, _From, State) ->
handle_call(_Request, _From, State) ->
{reply, ignored, State}.
handle_cast({leave, GID}, State) ->
{_, LID} = lists:keyfind(GID, 1, State#state.players),
Players = lists:delete({GID, LID}, State#state.players),
PlayersGID = players_gid(Players),
FreeLIDs = State#state.freelids,
egs_users:broadcast_unspawn(GID, PlayersGID),
{noreply, State#state{players=Players, freelids=[LID|FreeLIDs]}};
handle_cast(_Msg, State) ->
{noreply, State}.