Membership is managed entirely by the mediator, so leaving is just as centralized as joining. When a user leaves, the room forgets them and they stop receiving broadcasts.
Implement ChatRoom.unregister(user). It must remove the user from the room's list of users. Removing a user who is not registered must be safe and raise no error.
After a user is unregistered, a later broadcast must not reach them.
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):
for user in self.users:
if user.name == target_name:
user.inbox.append(message)
return
def unregister(self, user):
"""Remove the user so they no longer receive broadcasts."""
# TODO: remove the user from self.users if present; do nothing
# if they are not registered
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Membership is managed entirely by the mediator, so leaving is just as centralized as joining. When a user leaves, the room forgets them and they stop receiving broadcasts.
Implement ChatRoom.unregister(user). It must remove the user from the room's list of users. Removing a user who is not registered must be safe and raise no error.
After a user is unregistered, a later broadcast must not reach them.
Press Run Tests or ββ© to check your solution.