The mediator can route messages, not just broadcast them. Add directed delivery so a user can message exactly one other user by name.
Implement ChatRoom.send_private(sender, target_name, message). It must deliver the message only to the registered user whose name equals target_name, by appending it to that user's inbox. If no registered user has that name, do nothing and raise no error.
Only the named target receives the message; nobody else does.
class User:
def __init__(self, name):
self.name = name
self.inbox = []
self.room = None
def send(self, message):
self.room.broadcast(self, message)
class ChatRoom:
def __init__(self):
self.users = []
def register(self, user):
self.users.append(user)
user.room = self
def broadcast(self, sender, message):
for user in self.users:
if user is not sender:
user.inbox.append(message)
def send_private(self, sender, target_name, message):
"""Deliver message only to the user named target_name."""
# TODO: find the user whose name matches target_name and append
# the message to that user's inbox; ignore an unknown target
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
The mediator can route messages, not just broadcast them. Add directed delivery so a user can message exactly one other user by name.
Implement ChatRoom.send_private(sender, target_name, message). It must deliver the message only to the registered user whose name equals target_name, by appending it to that user's inbox. If no registered user has that name, do nothing and raise no error.
Only the named target receives the message; nobody else does.
Press Run Tests or ββ© to check your solution.