-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_group.py
185 lines (168 loc) · 6.09 KB
/
chat_group.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 5 09:58:31 2015
@author: zhengzhang
"""
S_ALONE = 0
S_TALKING = 1
S_GAMING = 2
#==============================================================================
# Group class:
# member fields:
# - An array of items, each a Member class
# - A dictionary that keeps who is a chat group
# member functions:
# - join: first time in
# - leave: leave the system, and the group
# - list_my_peers: who is in chatting with me?
# - list_all: who is in the system, and the chat groups
# - connect: connect to a peer in a chat group, and become part of the group
# - disconnect: leave the chat group but stay in the system
#==============================================================================
class Group:
def __init__(self):
self.members = {}
self.chat_grps = {}
self.grp_ever = 0
self.game_grps = {}
self.game_ever = 0
def join(self, name):
self.members[name] = S_ALONE
return
def is_member(self, name):
return name in self.members.keys()
def leave(self, name):
self.disconnect(name)
del self.members[name]
return
def find_group(self, name):
found = False
group_key = 0
for k in self.chat_grps.keys():
if name in self.chat_grps[k]:
found = True
group_key = k
break
if found == False:
for k in self.game_grps.keys():
if name in self.game_grps[k]:
found = True
group_key = k
break
return found, group_key
def connect(self, me, peer):
peer_in_group = False
#if peer is in a group, join it
peer_in_group, group_key = self.find_group(peer)
if peer_in_group == True:
print(peer, "is talking already, connect!")
self.chat_grps[group_key].append(me)
self.members[me] = S_TALKING
else:
# otherwise, create a new group
print(peer, "is idle as well")
self.grp_ever += 1
group_key = self.grp_ever
self.chat_grps[group_key] = []
self.chat_grps[group_key].append(me)
self.chat_grps[group_key].append(peer)
self.members[me] = S_TALKING
self.members[peer] = S_TALKING
print(self.list_me(me))
return
def disconnect(self, me):
# find myself in the group, quit
in_group, group_key = self.find_group(me)
if in_group == True:
self.chat_grps[group_key].remove(me)
self.members[me] = S_ALONE
# peer may be the only one left as well...
if len(self.chat_grps[group_key]) == 1:
peer = self.chat_grps[group_key].pop()
self.members[peer] = S_ALONE
del self.chat_grps[group_key]
return
def find_game_group(self, name):
found = False
group_key = 0
for k in self.game_grps.keys():
if name in self.game_grps[k]:
found = True
group_key = k
break
return found, group_key
def game_connect(self, me, peer):
peer_in_group = False
#if peer is in a group, join it
peer_in_group, group_key = self.find_game_group(peer)
if peer_in_group == True:
print(peer, "is already playing!")
self.game_grps[group_key].append(me)
self.members[me] = S_GAMING
else:
# otherwise, create a new group
print(peer, "is idle as well")
self.game_ever += 1
group_key = self.game_ever
self.game_grps[group_key] = []
self.game_grps[group_key].append(me)
self.game_grps[group_key].append(peer)
self.members[me] = S_GAMING
self.members[peer] = S_GAMING
print(self.list_game_me(me))
return
def list_all(self, me):
# a simple minded implementation
full_list = "Users: ------------" + "\n"
full_list += str(self.members) + "\n"
full_list += "Groups: -----------" + "\n"
full_list += str(self.chat_grps) + "\n"
full_list += "Games: -----------" + "\n"
full_list += str(self.game_grps) + "\n"
return full_list
def game_disconnect(self, me):
# find myself in the group, quit
in_group, group_key = self.find_game_group(me)
if in_group == True:
self.game_grps[group_key].remove(me)
self.members[me] = S_ALONE
# peer may be the only one left as well...
if len(self.game_grps[group_key]) == 1:
peer = self.game_grps[group_key].pop()
self.members[peer] = S_ALONE
del self.game_grps[group_key]
return
def list_all2(self, me):
print("Users: ------------")
print(self.members)
print("Groups: -----------")
print(self.chat_grps, "\n")
print("Games: -----------")
print(self.game_grps, "\n")
member_list = str(self.members)
grp_list = str(self.chat_grps)
game_list = str(self.game_grps)
return member_list, grp_list
def list_me(self, me):
# return a list, "me" followed by other peers in my group
if me in self.members.keys():
my_list = []
my_list.append(me)
in_group, group_key = self.find_group(me)
if in_group == True:
for member in self.chat_grps[group_key]:
if member != me:
my_list.append(member)
return my_list
def list_game_me(self, me):
# return a list, "me" followed by other peers in my group
if me in self.members.keys():
my_list = []
my_list.append(me)
in_group, group_key = self.find_game_group(me)
if in_group == True:
for member in self.game_grps[group_key]:
if member != me:
my_list.append(member)
return my_list
g = Group()