About issue #2, when anyone answer the problem,user list would show
This commit is contained in:
parent
62fc5838da
commit
2153c0021e
2
draw.py
2
draw.py
@ -8,7 +8,7 @@ def drawUserList(nowUserList,screen):
|
|||||||
|
|
||||||
# [['127.0.0.1', 52362], ['127.0.0.1', 52370]]
|
# [['127.0.0.1', 52362], ['127.0.0.1', 52370]]
|
||||||
# [{'name': ['127.0.0.1', 55888], 'score': 0}, {'name': ['127.0.0.1', 55900], 'score': 0}]
|
# [{'name': ['127.0.0.1', 55888], 'score': 0}, {'name': ['127.0.0.1', 55900], 'score': 0}]
|
||||||
print(list)
|
print(listJSON)
|
||||||
y = 100
|
y = 100
|
||||||
cross = 20
|
cross = 20
|
||||||
pygame.draw.rect(screen,(255,255,255),[900,100,200,380])
|
pygame.draw.rect(screen,(255,255,255),[900,100,200,380])
|
||||||
|
|||||||
25
main.py
25
main.py
@ -95,6 +95,7 @@ class Room:
|
|||||||
print(self.problem)
|
print(self.problem)
|
||||||
def start(self):
|
def start(self):
|
||||||
# Build a room's socket to start game
|
# Build a room's socket to start game
|
||||||
|
self.allPeerName = []
|
||||||
listensock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
listensock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
listensock.bind((self.ip,self.portNum))
|
listensock.bind((self.ip,self.portNum))
|
||||||
print("\t{}:{}".format(self.ip,self.portNum))
|
print("\t{}:{}".format(self.ip,self.portNum))
|
||||||
@ -104,14 +105,14 @@ class Room:
|
|||||||
sock,sockname = listensock.accept()
|
sock,sockname = listensock.accept()
|
||||||
print("[ {} ]{} has connected.".format(self.portNum,sockname))
|
print("[ {} ]{} has connected.".format(self.portNum,sockname))
|
||||||
|
|
||||||
allPeerName = []
|
|
||||||
self.sockList.append(sock) # 把sock放入list
|
self.sockList.append(sock) # 把sock放入list
|
||||||
|
|
||||||
# Send socket list to client to build a user list and put it beside picture.
|
# Send socket list to client to build a user list and put it beside picture.
|
||||||
for i in self.sockList:
|
|
||||||
allPeerName.append({'name':i.getpeername(),'score':0})
|
self.allPeerName.append({'name':sock.getpeername(),'score':0})
|
||||||
for sock in self.sockList:
|
for sock in self.sockList:
|
||||||
sock.send("[list] {}".format(json.dumps(allPeerName)).encode('utf-8'))
|
sock.send("[list] {}".format(json.dumps(self.allPeerName)).encode('utf-8'))
|
||||||
receiveDataThread = threading.Thread(target=self.receiveData,args=(sock,),daemon=True)
|
receiveDataThread = threading.Thread(target=self.receiveData,args=(sock,),daemon=True)
|
||||||
# 負責與client通信,傳輸遊戲所必須的指令
|
# 負責與client通信,傳輸遊戲所必須的指令
|
||||||
receiveDataThread.start()
|
receiveDataThread.start()
|
||||||
@ -162,20 +163,22 @@ class Room:
|
|||||||
else: # it is from other client. He/she want to send answer to check the answer
|
else: # it is from other client. He/she want to send answer to check the answer
|
||||||
if data == self.problem:
|
if data == self.problem:
|
||||||
sock.send('y'.encode('utf-8'))
|
sock.send('y'.encode('utf-8'))
|
||||||
|
|
||||||
|
for peer in self.allPeerName:
|
||||||
|
if sock.getpeername() == peer['name']:
|
||||||
|
peer['score'] = peer['score']+1
|
||||||
elif data == 'exit':
|
elif data == 'exit':
|
||||||
sock.send('exitok'.encode('utf-8'))
|
sock.send('exitok'.encode('utf-8'))
|
||||||
self.sockList.remove(sock)
|
self.sockList.remove(sock)
|
||||||
allPeerName = []
|
for peer in self.allPeerName:
|
||||||
# Send socket list to client to build a user list and put it beside picture.
|
if sock.getpeername() == peer['name']:
|
||||||
for i in self.sockList:
|
self.allPeerName.remove(peer)
|
||||||
allPeerName.append({'name':i.getpeername(),'score':0})
|
|
||||||
for sock in self.sockList:
|
|
||||||
sock.send("[list] {}".format(json.dumps(allPeerName)).encode('utf-8'))
|
|
||||||
# Close Process
|
|
||||||
if len(self.sockList):
|
if len(self.sockList):
|
||||||
self.startFlag = False
|
self.startFlag = False
|
||||||
else:
|
else:
|
||||||
sock.send('n'.encode('utf-8'))
|
sock.send('n'.encode('utf-8'))
|
||||||
|
for clientSock in self.sockList:
|
||||||
|
clientSock.send("[list] {}".format(json.dumps(self.allPeerName)).encode('utf-8'))
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
def __init__(self,ip,port):
|
def __init__(self,ip,port):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user