Fixed various bugs in packets.py, and rejiggered spelling_mp.py to use QuickStreamClient and QuickStreamServer.
This commit is contained in:
parent
56f53dccfe
commit
d5f984e687
3 changed files with 68 additions and 35 deletions
Binary file not shown.
47
packets.py
47
packets.py
|
@ -104,47 +104,51 @@ class PacketUtility:
|
||||||
class QuickStreamServer(PacketUtility):
|
class QuickStreamServer(PacketUtility):
|
||||||
"""Get a server set up easily! Note: This kind of server is NOT always ideal."""
|
"""Get a server set up easily! Note: This kind of server is NOT always ideal."""
|
||||||
|
|
||||||
def __init__(self, port, expectedClients = 1, ipAddress = ''):
|
def __init__(self, port = 1028, expectedClients = 1, ipAddress = ''):
|
||||||
"""Creates a quick server using the specified port and number of expected clients."""
|
"""Creates a quick server using the specified port and number of expected clients."""
|
||||||
self.clients = [] # Start with an empty list of clients
|
self.clients = [] # Start with an empty list of clients
|
||||||
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
if len(ipAddress) >= 7:
|
if len(ipAddress) >= 6:
|
||||||
address = (ipAddress, port)
|
address = (ipAddress, port)
|
||||||
else:
|
else:
|
||||||
address = (socket.gethostname(), port)
|
address = (socket.gethostname(), port)
|
||||||
serverSocket.bind(address)
|
serverSocket.bind(address)
|
||||||
serverSocket.listen(expectedClients)
|
serverSocket.listen(1)
|
||||||
for i in range(expectedClients):
|
for i in range(expectedClients):
|
||||||
self.clients.append(serverSocket.accept())
|
self.clients.append(serverSocket.accept())
|
||||||
serverSocket.close()
|
serverSocket.close()
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
for i in range(len(self.clients)):
|
self.close()
|
||||||
self.clients[i][0].close()
|
|
||||||
|
def close(self):
|
||||||
|
if self.clients:
|
||||||
|
for i in range(len(self.clients)):
|
||||||
|
self.clients[i][0].close()
|
||||||
|
|
||||||
def getByte(self):
|
def getByte(self):
|
||||||
"""Get a tuple of byte-size ints from each client."""
|
"""Get a list of byte-size ints from each client."""
|
||||||
ret = []
|
ret = []
|
||||||
for i in range(len(self.clients)):
|
for i in range(len(self.clients)):
|
||||||
ret.append(QuickStreamServer.get_int(self.clients[i][0], 1))
|
ret.append(QuickStreamServer.get_int(self.clients[i][0], 1))
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def getShort(self):
|
def getShort(self):
|
||||||
"""Get a tuple of short ints from each client."""
|
"""Get a list of short ints from each client."""
|
||||||
ret = []
|
ret = []
|
||||||
for i in range(len(self.clients)):
|
for i in range(len(self.clients)):
|
||||||
ret.append(QuickStreamServer.get_int(self.clients[i][0], 2))
|
ret.append(QuickStreamServer.get_int(self.clients[i][0], 2))
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def getInt(self):
|
def getInt(self):
|
||||||
"""Get a tuple of ints from each client."""
|
"""Get a list of ints from each client."""
|
||||||
ret = []
|
ret = []
|
||||||
for i in range(len(self.clients)):
|
for i in range(len(self.clients)):
|
||||||
ret.append(QuickStreamServer.get_int(self.clients[i][0], 4))
|
ret.append(QuickStreamServer.get_int(self.clients[i][0], 4))
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def getLong(self):
|
def getLong(self):
|
||||||
"""Get a tuple of long ints from each client."""
|
"""Get a list of long ints from each client."""
|
||||||
ret = []
|
ret = []
|
||||||
for i in range(len(self.clients)):
|
for i in range(len(self.clients)):
|
||||||
ret.append(QuickStreamServer.get_int(self.clients[i][0], 8))
|
ret.append(QuickStreamServer.get_int(self.clients[i][0], 8))
|
||||||
|
@ -159,21 +163,25 @@ class QuickStreamServer(PacketUtility):
|
||||||
|
|
||||||
def sendByte(self, message):
|
def sendByte(self, message):
|
||||||
"""Send a tuple of byte-size ints to each client."""
|
"""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)):
|
for i in range(len(self.clients)):
|
||||||
QuickStreamServer.send_int(self.clients[i][0], message, 1)
|
QuickStreamServer.send_int(self.clients[i][0], message, 1)
|
||||||
|
|
||||||
def sendShort(self, message):
|
def sendShort(self, message):
|
||||||
"""Send a tuple of short ints to each client."""
|
"""Send a tuple of short ints to each client."""
|
||||||
|
if not isinstance(message, tuple): message = tuple(message)
|
||||||
for i in range(len(self.clients)):
|
for i in range(len(self.clients)):
|
||||||
QuickStreamServer.send_int(self.clients[i][0], message, 2)
|
QuickStreamServer.send_int(self.clients[i][0], message, 2)
|
||||||
|
|
||||||
def sendByte(self, message):
|
def sendByte(self, message):
|
||||||
"""Send a tuple of ints to each client."""
|
"""Send a tuple of ints to each client."""
|
||||||
|
if not isinstance(message, tuple): message = tuple(message)
|
||||||
for i in range(len(self.clients)):
|
for i in range(len(self.clients)):
|
||||||
QuickStreamServer.send_int(self.clients[i][0], message, 4)
|
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."""
|
"""Send a tuple of long ints to each client."""
|
||||||
|
if not isinstance(message, tuple): message = tuple(message)
|
||||||
for i in range(len(self.clients)):
|
for i in range(len(self.clients)):
|
||||||
QuickStreamServer.send_int(self.clients[i][0], message, 8)
|
QuickStreamServer.send_int(self.clients[i][0], message, 8)
|
||||||
|
|
||||||
|
@ -185,13 +193,18 @@ class QuickStreamServer(PacketUtility):
|
||||||
class QuickStreamClient(PacketUtility):
|
class QuickStreamClient(PacketUtility):
|
||||||
"""Get a client set up easily! Note: This kind of client is NOT always ideal."""
|
"""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."""
|
"""Creates a quick client using the specified IP address and port."""
|
||||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
server.connect((ipAddress, port))
|
self.server.connect((ipAddress, port))
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
server.close()
|
if self.server:
|
||||||
|
self.server.close()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self.server:
|
||||||
|
self.server.close()
|
||||||
|
|
||||||
def getByte(self):
|
def getByte(self):
|
||||||
"""Get a tuple of byte-size ints from the server."""
|
"""Get a tuple of byte-size ints from the server."""
|
||||||
|
@ -215,23 +228,27 @@ class QuickStreamClient(PacketUtility):
|
||||||
|
|
||||||
def sendByte(self, message):
|
def sendByte(self, message):
|
||||||
"""Send a tuple of byte-size ints to the server."""
|
"""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)
|
QuickStreamClient.send_int(self.server, message, 1)
|
||||||
|
|
||||||
def sendShort(self, message):
|
def sendShort(self, message):
|
||||||
"""Send a tuple of short ints to the server."""
|
"""Send a tuple of short ints to the server."""
|
||||||
|
if not isinstance(message, tuple): message = tuple(message)
|
||||||
QuickStreamClient.send_int(self.server, message, 2)
|
QuickStreamClient.send_int(self.server, message, 2)
|
||||||
|
|
||||||
def sendInt(self, message):
|
def sendInt(self, message):
|
||||||
"""Send a tuple of ints to the server."""
|
"""Send a tuple of ints to the server."""
|
||||||
|
if not isinstance(message, tuple): message = tuple(message)
|
||||||
QuickStreamClient.send_int(self.server, message, 4)
|
QuickStreamClient.send_int(self.server, message, 4)
|
||||||
|
|
||||||
def sendLong(self, message):
|
def sendLong(self, message):
|
||||||
"""Send a tuple of long ints to the server."""
|
"""Send a tuple of long ints to the server."""
|
||||||
|
if not isinstance(message, tuple): message = tuple(message)
|
||||||
QuickStreamClient.send_int(self.server, message, 8)
|
QuickStreamClient.send_int(self.server, message, 8)
|
||||||
|
|
||||||
def sendString(self, message):
|
def sendString(self, message):
|
||||||
"""Send a string to the server."""
|
"""Send a string to the server."""
|
||||||
QuickStreamClient.sent_str(self.server, message)
|
QuickStreamClient.send_str(self.server, message)
|
||||||
|
|
||||||
# Legacy support:
|
# Legacy support:
|
||||||
get_int = PacketUtility.get_int
|
get_int = PacketUtility.get_int
|
||||||
|
|
|
@ -57,21 +57,25 @@ def server():
|
||||||
player2 = Player()
|
player2 = Player()
|
||||||
|
|
||||||
#print("Tell your friend to connect to " + socket.gethostbyname(socket.gethostname()))
|
#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: ")
|
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((socket.gethostname(), 1028))
|
||||||
server_socket.bind((ip, 1028))
|
#server_socket.bind((ip, 1028))
|
||||||
server_socket.listen(1)
|
#server_socket.listen(1)
|
||||||
(client_socket, address) = server_socket.accept()
|
#(client_socket, address) = server_socket.accept()
|
||||||
server_socket.close()
|
#server_socket.close()
|
||||||
|
|
||||||
|
client_socket = packets.QuickStreamServer(ipAddress = ip)
|
||||||
|
|
||||||
player1.name = input("What's your name? ")
|
player1.name = input("What's your name? ")
|
||||||
print("Waiting for the other player...")
|
print("Waiting for the other player...")
|
||||||
|
|
||||||
player2.name = packets.get_str(client_socket)
|
#player2.name = packets.get_str(client_socket)
|
||||||
packets.send_str(client_socket, player1.name)
|
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):
|
while(min(player1.hp, player2.hp) > 0):
|
||||||
print(player1.name + ": " + str(player1.hp) + " hp", end = " ")
|
print(player1.name + ": " + str(player1.hp) + " hp", end = " ")
|
||||||
|
@ -79,16 +83,21 @@ def server():
|
||||||
else: print()
|
else: print()
|
||||||
spell1, word1 = player1.cast_spell()
|
spell1, word1 = player1.cast_spell()
|
||||||
print("Waiting for " + player2.name + "'s move...")
|
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)
|
spell2 = spell.Spell(word2)
|
||||||
spell.compare_spells(spell1, spell2)
|
spell.compare_spells(spell1, spell2)
|
||||||
spell1.affect(spell2, player1)
|
spell1.affect(spell2, player1)
|
||||||
spell2.affect(spell1, player2)
|
spell2.affect(spell1, player2)
|
||||||
status.apply(player1)
|
status.apply(player1)
|
||||||
packets.send_int(client_socket, (spell2.total_damage, spell1.values["o"],
|
#packets.send_int(client_socket, (spell2.total_damage, spell1.values["o"],
|
||||||
spell1.total_damage, spell2.values["o"],
|
# spell1.total_damage, spell2.values["o"],
|
||||||
player1.hp, player2.hp, player2.status))
|
# player1.hp, player2.hp, player2.status))
|
||||||
packets.send_str(client_socket, word1)
|
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("\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 + " took " + str(spell2.total_damage) + " damage, and healed " + str(spell1.values["o"]) + " hp!")
|
||||||
print(player1.name + " now has " + str(player1.hp) + " hp!\n")
|
print(player1.name + " now has " + str(player1.hp) + " hp!\n")
|
||||||
|
@ -105,14 +114,18 @@ def client():
|
||||||
player2 = Player()
|
player2 = Player()
|
||||||
|
|
||||||
ip_addr = input("IP or hostname of your host: ")
|
ip_addr = input("IP or hostname of your host: ")
|
||||||
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
#client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
client_socket.connect((ip_addr, 1028))
|
#client_socket.connect((ip_addr, 1028))
|
||||||
|
|
||||||
|
client_socket = packets.QuickStreamClient(ip_addr)
|
||||||
|
|
||||||
player2.name = input("What's your name? ")
|
player2.name = input("What's your name? ")
|
||||||
print("Waiting for the other player...")
|
print("Waiting for the other player...")
|
||||||
|
|
||||||
packets.send_str(client_socket, player2.name)
|
#packets.send_str(client_socket, player2.name)
|
||||||
player1.name = packets.get_str(client_socket)
|
client_socket.sendString(player2.name)
|
||||||
|
#player1.name = packets.get_str(client_socket)
|
||||||
|
player1.name = client_socket.getString()
|
||||||
|
|
||||||
while(min(player1.hp, player2.hp) > 0):
|
while(min(player1.hp, player2.hp) > 0):
|
||||||
print(player2.name + ": " + str(player2.hp) + " hp", end = " ")
|
print(player2.name + ": " + str(player2.hp) + " hp", end = " ")
|
||||||
|
@ -120,13 +133,16 @@ def client():
|
||||||
else: print()
|
else: print()
|
||||||
spell2, word2 = player2.cast_spell() #spell 2 ends up getting thrown out
|
spell2, word2 = player2.cast_spell() #spell 2 ends up getting thrown out
|
||||||
print("Waiting for " + player1.name + "'s move...")
|
print("Waiting for " + player1.name + "'s move...")
|
||||||
packets.send_str(client_socket, word2)
|
#packets.send_str(client_socket, word2)
|
||||||
packet = packets.get_int(client_socket)
|
client_socket.sendString(word2)
|
||||||
|
#packet = packets.get_int(client_socket)
|
||||||
|
packet = client_socket.getShort()
|
||||||
player1.hp = packet[4]
|
player1.hp = packet[4]
|
||||||
player2.hp = packet[5]
|
player2.hp = packet[5]
|
||||||
player2.status = packet[6]
|
player2.status = packet[6]
|
||||||
status.apply(player2)
|
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("\n" + player1.name + " casted \"" + word1 + "\"!\n")
|
||||||
print(player1.name + " took " + str(packet[0]) + " damage, and healed " + str(packet[1]) + " hp!")
|
print(player1.name + " took " + str(packet[0]) + " damage, and healed " + str(packet[1]) + " hp!")
|
||||||
print(player1.name + " now has " + str(packet[4]) + " hp!\n")
|
print(player1.name + " now has " + str(packet[4]) + " hp!\n")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue