-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientStorage.py
92 lines (78 loc) · 2.68 KB
/
ClientStorage.py
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
import uuid
from threading import RLock
class User:
def __init__(self, sid, name = ''):
'''
Each user is allowed to spawn nrOfThreadsAllowed threads.
'''
self.sid = sid
self.name = name
self.nrOfSidUpdates = 0
self.uniqueID = uuid.uuid4().hex
self.lock = RLock()
self.nrOfThreadsSpawned = 0
self.nrOfThreadsAllowed = 1
self.gameObject = None
self.playerObject = None
def add_Name(self, name):
self.name = name
def update_Sid(self, sid):
self.sid = sid
self.nrOfSidUpdates
def update_Thread_Number(self, increase, verbose = False):
with self.lock:
if self.nrOfThreadsSpawned >= self.nrOfThreadsAllowed and increase:
if verbose: print('User {} is not allowed to spawn another thread'.format(self.name))
return False
elif increase:
if verbose: print('User {} just spawned a thread'.format(self.name))
self.nrOfThreadsSpawned += 1
return True
else:
if verbose: print('User {} just removed a thread'.format(self.name))
self.nrOfThreadsSpawned -= 1
return True
def resetUser(self):
self.gameObject=None
self.playerObject=None
self.name=''
class Clients:
def __init__(self):
self.users = []
self.nrOfClients = 0
def add_User(self, sid, name = ''):
user = User(sid,name)
self.users.append(user)
self.nrOfClients += 1
return user
def removeUser(self, name):
for i in range(self.users):
if self.users[i].name == name:
del self.users[i]
nrOfClients -= 1
def find_User_By_uniqueID(self, uniqueID):
for u in self.users:
if u.uniqueID == uniqueID:
return u
return None
def find_User_By_Name(self, name):
for u in self.users:
if u.name == name:
return u
return None
def find_User_By_Sid(self, sid):
for u in self.users:
if u.sid == sid:
return u
return None
def __str__(self):
ret = ''
ret+=('_________________CLIENTS_____________\n')
for u in self.users:
ret+=('-------------------------------------\n')
ret+=('User name: {}\n'.format(u.name))
ret+=('SID: {}\n'.format(u.sid))
ret+=('UniqueID: {}\n'.format(u.uniqueID))
ret+=('Nr. threads: {}\n'.format(u.nrOfThreadsSpawned))
ret+=('_____________________________________')
return ret