From d5f984e68708152407a117f764d37c9af123fa2c Mon Sep 17 00:00:00 2001 From: Patrick Marsee Date: Tue, 27 Dec 2016 14:07:13 -0500 Subject: [PATCH] Fixed various bugs in packets.py, and rejiggered spelling_mp.py to use QuickStreamClient and QuickStreamServer. --- __pycache__/packets.cpython-34.pyc | Bin 11112 -> 11839 bytes packets.py | 47 ++++++++++++++++-------- spelling_mp.py | 56 ++++++++++++++++++----------- 3 files changed, 68 insertions(+), 35 deletions(-) diff --git a/__pycache__/packets.cpython-34.pyc b/__pycache__/packets.cpython-34.pyc index cbd4f7f628ac7d72b46d517fe2da5e3b40714ab1..aacc5b66e5e1659fcb56389b0a34a43c80f2facf 100644 GIT binary patch delta 2496 zcmcJQT}&KR6oAj2o!N!mVSm{FzhM`!6%ed#5rOhoFxVDQODeWef$m)u#3jsZO-n#D z@X!}+^eCEW6HWBR#6)fML5*mlF+OPI!3Sv?)1)Rw%2#IY5>qpVtw@7Y7j5z`U1DZk`2zNz#YKL zU15_~XM_^NFn4l$n~gviN!+gmb=AuBQHMJ~f`2ny~Wt zNiu?0y`$`DZ1*j@>bH2V`SR={o~(SyUAsA&ntm9+^(HXjA7q#Ctp7!S-8N@xYC89b zjMyI-V#_q?e!LvG$}Z!DVDCd?RAZjHLY<%nC}0`Fpj&DNHGuCcU!V#Cl?5C6)m9^oMVW;OU8rf1EVz_y) zdsP-`D=DQlV%|k{ytBQQ1j{J+RApeV>$wr>DygR{FhK9p2PNf*jVrUVIWC+y9sQDh zj4iQS@RiyWYsH0F(km8W9uw-gP$$r*M{p%pek4J+ISqZNv!vYW()M9(Qa}TmYC!jVT{;T;8~Gv#IqvXXt2mO;#rYx6i{Rv@vO);3NEsZ zuvKImJxFZZSAZKajNlgAU|3urO%&l3U>mM8@a_26QOUE6C|~@Gj;R#|w z(@7*vCrFZMv~cchFsWi+V&tS|ipR`DLLDZxX163y)*PAXTrQi**V)WA;XEQ#yHJvs zQaQwJC#la#ER_@}ar`xLs4+?AIvJzu1{JbM{MsNMVO22)DNM38=I^uCzSSKhL&T1Nw z*$s9bS5h5~chCTB;|^FQrqXvs^jt+(x<9OKLdcXW+EU1(fY5I*PLy}Q|;yMNi=dw1{dX7jhvwo(I{)TA+KO&emWDOw5YR`;xykY?-M zRjMTPAweoWh>oIy(tzkg(AE(9P*6k=^r2z|5vdg|2*wvh5Q=ZkoK0_Q7IMzc?Ci{Z zb7#&y-+un+=EJ@}1G?{|efJ~p_OAIJ2>4u#u@RK|hp&DYhgUM*GeTp_3Y&2;;by|a zmKmGTm}q0dD{XZ8nD9&6!$g1yUD}usWFjPOFB4%VBGSg>C=>0{_A?P1X)@M1#Ycf<6@<3Qlv+`bR+*hu*O69>6J1?}c16 ze!ygloGmamtFQ&m(gI~xWlN0lXAt4E%TC~z9U--s6qP9{_8_c9((UE4 zsLqwk&!ewSC&hRMu0`UI3WuRD_9Wi}Kj(_D8jHYEWQs3>FWv+HMr!ff#;c9|6Zyuo zr{=2p+Ee-ZiJIMjbFmD(6+Ob2VKe$>sO$eZ7|p_J`y_uEuD8Fz`{3__g_Xa9g83jU&{qG%{%DD@qt zDH!`@tzrEFWy-@>G8}#~zT%(2d*jdYXNR)M1DOQe(T(JV7V}Re-yd zXfWUkc$}>T$ zkUci-vMFlcNva>!6;Gu$KVPjh@ZJxS^Bz+BDB`YH?dPht?Iw>@9!BX(fH^`g~+8SNZv!4&ybtZzZNM?=mSEtuV1 zLoFB!R=T|WSM1t?%U#(POrm?V7306sR= 7: + if len(ipAddress) >= 6: address = (ipAddress, port) else: address = (socket.gethostname(), port) serverSocket.bind(address) - serverSocket.listen(expectedClients) + serverSocket.listen(1) for i in range(expectedClients): self.clients.append(serverSocket.accept()) serverSocket.close() def __del__(self): - for i in range(len(self.clients)): - self.clients[i][0].close() + self.close() + + def close(self): + if self.clients: + for i in range(len(self.clients)): + self.clients[i][0].close() def getByte(self): - """Get a tuple of byte-size ints from each client.""" + """Get a list of byte-size ints from each client.""" ret = [] for i in range(len(self.clients)): ret.append(QuickStreamServer.get_int(self.clients[i][0], 1)) return ret def getShort(self): - """Get a tuple of short ints from each client.""" + """Get a list of short ints from each client.""" ret = [] for i in range(len(self.clients)): ret.append(QuickStreamServer.get_int(self.clients[i][0], 2)) return ret def getInt(self): - """Get a tuple of ints from each client.""" + """Get a list of ints from each client.""" ret = [] for i in range(len(self.clients)): ret.append(QuickStreamServer.get_int(self.clients[i][0], 4)) return ret def getLong(self): - """Get a tuple of long ints from each client.""" + """Get a list of long ints from each client.""" ret = [] for i in range(len(self.clients)): ret.append(QuickStreamServer.get_int(self.clients[i][0], 8)) @@ -159,21 +163,25 @@ class QuickStreamServer(PacketUtility): def sendByte(self, message): """Send a tuple of byte-size ints to each client.""" + if not isinstance(message, tuple): message = tuple(message) for i in range(len(self.clients)): QuickStreamServer.send_int(self.clients[i][0], message, 1) def sendShort(self, message): """Send a tuple of short ints to each client.""" + if not isinstance(message, tuple): message = tuple(message) for i in range(len(self.clients)): QuickStreamServer.send_int(self.clients[i][0], message, 2) def sendByte(self, message): """Send a tuple of ints to each client.""" + if not isinstance(message, tuple): message = tuple(message) for i in range(len(self.clients)): QuickStreamServer.send_int(self.clients[i][0], message, 4) - def sendShort(self, message): + def sendLong(self, message): """Send a tuple of long ints to each client.""" + if not isinstance(message, tuple): message = tuple(message) for i in range(len(self.clients)): QuickStreamServer.send_int(self.clients[i][0], message, 8) @@ -185,13 +193,18 @@ class QuickStreamServer(PacketUtility): class QuickStreamClient(PacketUtility): """Get a client set up easily! Note: This kind of client is NOT always ideal.""" - def __init__(self, ipAddress, port): + def __init__(self, ipAddress, port = 1028): """Creates a quick client using the specified IP address and port.""" - server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - server.connect((ipAddress, port)) + self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.server.connect((ipAddress, port)) def __del__(self): - server.close() + if self.server: + self.server.close() + + def close(self): + if self.server: + self.server.close() def getByte(self): """Get a tuple of byte-size ints from the server.""" @@ -215,23 +228,27 @@ class QuickStreamClient(PacketUtility): def sendByte(self, message): """Send a tuple of byte-size ints to the server.""" + if not isinstance(message, tuple): message = tuple(message) QuickStreamClient.send_int(self.server, message, 1) def sendShort(self, message): """Send a tuple of short ints to the server.""" + if not isinstance(message, tuple): message = tuple(message) QuickStreamClient.send_int(self.server, message, 2) def sendInt(self, message): """Send a tuple of ints to the server.""" + if not isinstance(message, tuple): message = tuple(message) QuickStreamClient.send_int(self.server, message, 4) def sendLong(self, message): """Send a tuple of long ints to the server.""" + if not isinstance(message, tuple): message = tuple(message) QuickStreamClient.send_int(self.server, message, 8) def sendString(self, message): """Send a string to the server.""" - QuickStreamClient.sent_str(self.server, message) + QuickStreamClient.send_str(self.server, message) # Legacy support: get_int = PacketUtility.get_int diff --git a/spelling_mp.py b/spelling_mp.py index 744d714..7fa7dcb 100644 --- a/spelling_mp.py +++ b/spelling_mp.py @@ -57,21 +57,25 @@ def server(): player2 = Player() #print("Tell your friend to connect to " + socket.gethostbyname(socket.gethostname())) - #Linus only, duct tape quick fix: + #Linus only, duck tape quick fix: ip = input("Your ip addr: ") - server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + #server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #server_socket.bind((socket.gethostname(), 1028)) - server_socket.bind((ip, 1028)) - server_socket.listen(1) - (client_socket, address) = server_socket.accept() - server_socket.close() + #server_socket.bind((ip, 1028)) + #server_socket.listen(1) + #(client_socket, address) = server_socket.accept() + #server_socket.close() + + client_socket = packets.QuickStreamServer(ipAddress = ip) player1.name = input("What's your name? ") print("Waiting for the other player...") - player2.name = packets.get_str(client_socket) - packets.send_str(client_socket, player1.name) + #player2.name = packets.get_str(client_socket) + player2.name = client_socket.getString()[0] + #packets.send_str(client_socket, player1.name) + client_socket.sendString(player1.name) while(min(player1.hp, player2.hp) > 0): print(player1.name + ": " + str(player1.hp) + " hp", end = " ") @@ -79,16 +83,21 @@ def server(): else: print() spell1, word1 = player1.cast_spell() print("Waiting for " + player2.name + "'s move...") - word2 = packets.get_str(client_socket) + #word2 = packets.get_str(client_socket) + word2 = client_socket.getString()[0] spell2 = spell.Spell(word2) spell.compare_spells(spell1, spell2) spell1.affect(spell2, player1) spell2.affect(spell1, player2) status.apply(player1) - packets.send_int(client_socket, (spell2.total_damage, spell1.values["o"], - spell1.total_damage, spell2.values["o"], - player1.hp, player2.hp, player2.status)) - packets.send_str(client_socket, word1) + #packets.send_int(client_socket, (spell2.total_damage, spell1.values["o"], + # spell1.total_damage, spell2.values["o"], + # player1.hp, player2.hp, player2.status)) + client_socket.sendShort((spell2.total_damage, spell1.values["o"], + spell1.total_damage, spell2.values["o"], + player1.hp, player2.hp, player2.status)) + #packets.send_str(client_socket, word1) + client_socket.sendString(word1) print("\n" + player2.name + " casted \"" + word2 + "\"!\n") print(player1.name + " took " + str(spell2.total_damage) + " damage, and healed " + str(spell1.values["o"]) + " hp!") print(player1.name + " now has " + str(player1.hp) + " hp!\n") @@ -105,14 +114,18 @@ def client(): player2 = Player() ip_addr = input("IP or hostname of your host: ") - client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - client_socket.connect((ip_addr, 1028)) + #client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + #client_socket.connect((ip_addr, 1028)) + + client_socket = packets.QuickStreamClient(ip_addr) player2.name = input("What's your name? ") print("Waiting for the other player...") - packets.send_str(client_socket, player2.name) - player1.name = packets.get_str(client_socket) + #packets.send_str(client_socket, player2.name) + client_socket.sendString(player2.name) + #player1.name = packets.get_str(client_socket) + player1.name = client_socket.getString() while(min(player1.hp, player2.hp) > 0): print(player2.name + ": " + str(player2.hp) + " hp", end = " ") @@ -120,13 +133,16 @@ def client(): else: print() spell2, word2 = player2.cast_spell() #spell 2 ends up getting thrown out print("Waiting for " + player1.name + "'s move...") - packets.send_str(client_socket, word2) - packet = packets.get_int(client_socket) + #packets.send_str(client_socket, word2) + client_socket.sendString(word2) + #packet = packets.get_int(client_socket) + packet = client_socket.getShort() player1.hp = packet[4] player2.hp = packet[5] player2.status = packet[6] status.apply(player2) - word1 = packets.get_str(client_socket) + #word1 = packets.get_str(client_socket) + word1 = client_socket.getString() print("\n" + player1.name + " casted \"" + word1 + "\"!\n") print(player1.name + " took " + str(packet[0]) + " damage, and healed " + str(packet[1]) + " hp!") print(player1.name + " now has " + str(packet[4]) + " hp!\n")