diff --git a/src/comms/channelhandler.py b/src/comms/channelhandler.py index 8173aeb5f..876f1494d 100644 --- a/src/comms/channelhandler.py +++ b/src/comms/channelhandler.py @@ -90,7 +90,7 @@ class ChannelCommand(command.Command): msgobj.senders = sender msgobj.channels = channel # send new message object to channel - channel.msg(msgobj, senders=sender) + channel.msg(msgobj, senders=sender, online=True) class ChannelHandler(object): """ diff --git a/src/comms/managers.py b/src/comms/managers.py index 9439f501d..d40c1c293 100644 --- a/src/comms/managers.py +++ b/src/comms/managers.py @@ -198,7 +198,7 @@ class MsgManager(models.Manager): NOTE: This can potentially be slow, so make sure to supply one of the other arguments to limit the search. dbref - (int) the exact database id of the message. This will override - all other search crieteria since it's unique and + all other search criteria since it's unique and always gives a list with only one match. """ # unique msg id @@ -299,19 +299,36 @@ class ChannelManager(models.Manager): CHANNELHANDLER.update() return None - def get_all_connections(self, channel): + def get_all_connections(self, channel, online=False): """ Return the connections of all players listening - to this channel + to this channel. If Online is true, it only returns + connected players. """ - # import here to avoid circular imports - #from src.comms.models import PlayerChannelConnection + PlayerChannelConnection = ContentType.objects.get(app_label="comms", model="playerchannelconnection").model_class() ExternalChannelConnection = ContentType.objects.get(app_label="comms", model="externalchannelconnection").model_class() - return itertools.chain(PlayerChannelConnection.objects.get_all_connections(channel), - ExternalChannelConnection.objects.get_all_connections(channel)) + # Importing here to avoid circular imports. + from src.server.sessionhandler import SESSIONS + players = [] + if online: + session_list = SESSIONS.get_sessions() + for session in session_list: + if not session.logged_in: + continue + try: + players.append(PlayerChannelConnection.objects.get(db_player=session.get_player(), + db_channel=channel)) + except PlayerChannelConnection.DoesNotExist: + pass + else: + players.extend(PlayerChannelConnection.objects.get_all_connections(channel)) + + external_connections = ExternalChannelConnection.objects.get_all_connections(channel) + + return itertools.chain(players, external_connections) def channel_search(self, ostring): """ diff --git a/src/comms/models.py b/src/comms/models.py index 3c62c3481..a0110f5ff 100644 --- a/src/comms/models.py +++ b/src/comms/models.py @@ -555,7 +555,7 @@ class Channel(SharedMemoryModel): # do the check return PlayerChannelConnection.objects.has_player_connection(player, self) - def msg(self, msgobj, header=None, senders=None, persistent=True): + def msg(self, msgobj, header=None, senders=None, persistent=True, online=False): """ Send the given message to all players connected to channel. Note that no permission-checking is done here; it is assumed to have been @@ -568,7 +568,8 @@ class Channel(SharedMemoryModel): persistent=False. persistent (bool) - ignored if msgobj is a Msg or TempMsg. If True, a Msg will be created, using header and senders keywords. If False, other keywords will be ignored. - + online (bool) - If this is set true, only messages people who are online. Otherwise, messages all players + connected. This can make things faster, but may not trigger listeners on players that are offline. """ if isinstance(msgobj, basestring): @@ -590,7 +591,7 @@ class Channel(SharedMemoryModel): msg = msgobj.message # get all players connected to this channel and send to them - for conn in Channel.objects.get_all_connections(self): + for conn in Channel.objects.get_all_connections(self, online=online): try: conn.player.msg(msg, senders) except AttributeError: