-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrooms_manager.py
More file actions
115 lines (81 loc) · 3.31 KB
/
rooms_manager.py
File metadata and controls
115 lines (81 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import uuid
class RoomsManager:
def __init__(self):
print('Rooms Manager: Initialized')
self.rooms_list = []
#Either joins an existing room or creates a new one if none exist w/ specified name
def join_or_create_room(self, room_name, username, sid):
exists = False
for room in self.rooms_list:
if room.name == room_name:
exists = True
if(room.add_user(username,sid)):
return True
else:
return False
if len(self.rooms_list) == 0 or exists == False:
print('Rooms Manager: A new room has been created with name "' + room_name + '"')
new_room = Room(room_name)
self.rooms_list.append(new_room)
if(new_room.add_user(username, sid)):
return True
else:
return False
return False
#Removes user with specified request id from room, returns string of room that user was in
def leave_room(self, sid):
for room in self.rooms_list:
if sid in room.users:
room.remove_user(sid)
if(room.get_user_count() == 0):
self.rooms_list.remove(room)
print('Rooms Manager: Room is empty, removing.')
return room.name
return False
#returns users count for specified room
def get_user_count_for_room(self, room_name):
room = self.get_room(room_name)
if(room):
return room.get_user_count()
else:
return 0
#returns the room object for the name specified
def get_room(self, room_name):
room = None
for r in self.rooms_list:
if r.name == room_name:
room = r
return room
class Room:
def __init__(self, name):
self.users = {}
self.name = name
#adds sid and user to users dictionary
def add_user(self, username, sid):
for val in self.users.values():
if val == username:
print('Room {}: {} username exists!'.format(self.name, username))
return False
self.users[sid] = username
print('Room {}: {} has joined the room.'.format(self.name, username))
self.print_current_users()
return True
#removes user with specified request id from room
def remove_user(self, sid):
if sid in self.users.keys():
print('Room {}: {} has left the room.'.format(self.name, self.users[sid]))
del self.users[sid]
self.print_current_users()
return
#processes a message from the client that was sent to this room
def process_client_message(self, data):
processed_data = data
processed_data['messageID'] = uuid.uuid4().hex[:10] #10 digit message id so messages are unique
print('Room {}: {}: {}'.format(self.name, processed_data['username'], processed_data['message']))
return processed_data
#gets count of users dictionary
def get_user_count(self):
return len(self.users)
#prints our list of currently connected users in console
def print_current_users(self):
print('Room {}: Current Users: {}'.format(self.name, self.users))