-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbingo.py
More file actions
238 lines (204 loc) · 7.02 KB
/
bingo.py
File metadata and controls
238 lines (204 loc) · 7.02 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import argparse, socket, zmq, time, random, threading
def checkBingo(clientList, currentList):
check = [False] * 25
for i in range(25):
for j in range(25):
if currentList[i] == clientList[j]:
check[j] = True
line = 0
for i in range(5):
true = True
for j in range(5):
if check[j*5+i] == False:
true = False
if true == False:
break
if true == True:
line+=1
for j in range(5):
true = True
for i in range(5):
if check[j*5+i] == False:
true = False
if true == False:
break
if true == True:
line+=1
true = True
for i in range(5):
if check[i*5+i] == False:
true = False
if true == False:
break
if true == True:
line+=1
true = True
for i in range(5):
if check[(4-i)*5+i] == False:
true = False
if true == False:
break
if true == True:
line+=1
#print("You already have {} lines.".format(line))
if line >= 5:
return True
else:
return False
numbers= 0
maps = {} #username : chessboard[]
second = 60
Chessboard = []
currentChessboard = [0]*25
new_connection = True
end = False
winner = ''
def server_broadcast_socket(host, port):
global numbers, maps, second, Chessboard, currentChessboard, new_connection, end, winner
url = "tcp://{}:{}".format(host, port)
context = zmq.Context()
b_socket = context.socket(zmq.PUB)
b_socket.bind(url)
for i in range(60):
if i % 5 == 0:
print("[PUB] Waiting for {} seconds...".format(second))
b_socket.send_string("Time,{}".format(second))
time.sleep(1)
second-=1
print("\nGame Start\n")
b_socket.send_string("\nGame Start !\n")
new_connection = False
Chessboard = list(range(1,26))
random.shuffle(Chessboard)
for i in range(25):
print(Chessboard[i], end =' ')
if i % 5 == 4:
print()
print()
i = 0
while i < 25:
if end:
break
currentChessboard[i] = Chessboard[i]
b_socket.send_string('Num,{}'.format(str(Chessboard[i])))
print('[PUB] Number is {}'.format(Chessboard[i]))
time.sleep(3)
i += 1
b_socket.send_string("Bingo,{}".format(winner))
print("[PUB] Congratulations to '{}' win the game!".format(winner))
print("[PUB] End")
#set to deamon
def server_ordinary_socket(host, port):
global numbers, maps, second, Chessboard, currentChessboard, new_connection, end, winner
url = "tcp://{}:{}".format(host, port)
context = zmq.Context()
socket = context.socket(zmq.REP)
#socket.setsockopt(zmq.LINGER, 0)
socket.bind(url)
poller = zmq.Poller()
poller.register(socket, zmq.POLLIN)
while new_connection:#change to poll
sockets = dict(poller.poll(500)) #poll #idle for 0.5 second
if socket in sockets:
data = socket.recv_string()
#data = socket.recv_string()
splitText= data.split(',')
thisMap = list(map(int,splitText[1:]))
name = splitText[0]
maps[splitText[0]] = thisMap #this
numbers += 1
message = "{},{}".format(numbers, second)
socket.send_string(message)
print("'{}' chooses: {}".format(name, maps[name]))
while True:
data = socket.recv_string()
splitData = data.split(',')
if splitData[0] == 'Bingo':
if checkBingo(maps[splitData[1]], currentChessboard):
winner = splitData[1] #name
end = True
socket.send_string("You are the Winner!")
break
else:
socket.send_string("You are wrong!")
def publisher(interface = '127.0.0.1', port = '11129'):
if interface == '':
interface = '*'
t1 = threading.Thread(target=server_ordinary_socket, args=((interface, port)))
t2 = threading.Thread(target=server_broadcast_socket, args=(interface, port+1))
t1.start()
t2.start()
def client_ordinary_socket(host, port, ready):
global Chessboard, end
url = "tcp://{}:{}".format(host, port)
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect(url)
text = input("Please enter your name, and 25 different numbers from 1 to 25 in random order, seperated by ','\n")
socket.send_string(text)
textList = text.split(',')
Chessboard = list(map(int,textList[1:]))
name = textList[0]
print("\nThis is your chessboard:\n")
for i in range(25):
print('%2d' % Chessboard[i], end=' ')
if i%5==4:
print()
print()
message = socket.recv_string()
mesList = message.split(',')
print("You are the {} player.\nThe game will start in {} seconds.".format(mesList[0],mesList[1]))
ready.set()
while True:
if end:
socket.send_string('Bingo,{}'.format(name))
data = socket.recv_string()
print(data)
if data != 'Failure!':
break
def client_broadcast_socket(host, port, ready):
global Chessboard, currentChessboard, end
ready.wait()
url = "tcp://{}:{}".format(host, port)
context = zmq.Context()
b_socket = context.socket(zmq.SUB)
b_socket.connect(url)
b_socket.setsockopt_string(zmq.SUBSCRIBE,'')
i = 0
while True:
#print("here!")
data = b_socket.recv_string();
splitdata = data.split(',')
if splitdata[0] == 'Bingo':
print("Congratulations to '{}' win the game!".format(splitdata[1]))
break
elif splitdata[0] == 'Time':
print('{} seconds left to start.'.format(splitdata[1]))
elif splitdata[0] == 'Num':
currentChessboard[i] = int(splitdata[1])
print('Number is :', splitdata[1])
i += 1
if checkBingo(Chessboard, currentChessboard):
end = True
print('Bingo!(You)')
else :
print(data)
def subscriber(network, port):
ready = threading.Event()
if network == '':
network = '*'
t1 = threading.Thread(target=client_ordinary_socket, args=(network, port, ready), daemon=True) #daemon
t2 = threading.Thread(target=client_broadcast_socket, args=(network, port+1, ready))
t1.start()
t2.start()
if __name__ == '__main__':
choices = {'subscriber': subscriber, 'publisher': publisher}
parser = argparse.ArgumentParser(description='Send, receive UDP broadcast')
parser.add_argument('role', choices=choices, help='which role to take')
parser.add_argument('host', help='interface the server listens at;'
' network the client sends to')
parser.add_argument('-p', metavar='port', type=int, default=1060,
help='TCP port (default 1060)')
args = parser.parse_args()
function = choices[args.role]
function(args.host, args.p)