leaveRoom method
bool
leaveRoom( - Channel channel, {
- Channel? currentId,
- Uint8List? roomId,
})
Implementation
bool leaveRoom(Channel channel, {Channel? currentId, Uint8List? roomId}) {
if (roomId != null && _joined[channel]?.roomId != roomId) return false;
final room = _joined.remove(channel);
if (room == null) return false;
if (currentId != null && room.owner != currentId) return false;
final roomChannel = room.getChannel(channel);
if (roomChannel == null) return false;
room._playerChannels.remove(channel);
_sendPacketToRoom(
room,
RpcNetworkerPacket.named(
name: SwampEvent.playerLeft,
data: Uint8List.fromList([roomChannel >> 8, roomChannel & 0xFF]),
),
);
if (room.isEmpty) {
_rooms.remove(room);
return true;
}
if (roomChannel == kAuthorityChannel) {
for (final player in room.players) {
_joined.remove(player);
_sendKickMessage(player, KickReason.hostLeft);
}
room.players.clear();
_rooms.remove(room);
}
return true;
}