diff --git a/src/egs_game_server.erl b/src/egs_game_server.erl index f075af6..1a3a380 100644 --- a/src/egs_game_server.erl +++ b/src/egs_game_server.erl @@ -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 diff --git a/src/egs_zones.erl b/src/egs_zones.erl index 93ccccc..74341d4 100644 --- a/src/egs_zones.erl +++ b/src/egs_zones.erl @@ -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}.