Initial upload
This commit is contained in:
commit
c5b00e01df
9 changed files with 1050 additions and 0 deletions
BIN
__pycache__/packets.cpython-34.pyc
Normal file
BIN
__pycache__/packets.cpython-34.pyc
Normal file
Binary file not shown.
BIN
__pycache__/spell.cpython-34.pyc
Normal file
BIN
__pycache__/spell.cpython-34.pyc
Normal file
Binary file not shown.
BIN
__pycache__/status.cpython-34.pyc
Normal file
BIN
__pycache__/status.cpython-34.pyc
Normal file
Binary file not shown.
291
packets.py
Normal file
291
packets.py
Normal file
|
@ -0,0 +1,291 @@
|
||||||
|
import socket
|
||||||
|
|
||||||
|
class PacketUtility:
|
||||||
|
"""The base class of QuickStreamServer and QuickStreamClient."""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def getMyIP():
|
||||||
|
return socket.gethostbyname(socket.getfqdn())
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def getMyDomainName():
|
||||||
|
return socket.getfqdn()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_int(socket, int_length = 2):
|
||||||
|
"""Get a tuple of ints from THE OTHER SIDE!"""
|
||||||
|
size = b""
|
||||||
|
message = b""
|
||||||
|
msg_len = int(0)
|
||||||
|
bytes_recd = int(0)
|
||||||
|
ret = []
|
||||||
|
|
||||||
|
while bytes_recd < int_length:
|
||||||
|
size += socket.recv(int_length - bytes_recd)
|
||||||
|
if size == b"":
|
||||||
|
raise RuntimeError("socket connection broken")
|
||||||
|
bytes_recd = len(size)
|
||||||
|
msg_len = int.from_bytes(size, byteorder = "big")
|
||||||
|
bytes_recd = int(0)
|
||||||
|
|
||||||
|
while bytes_recd < msg_len:
|
||||||
|
message += socket.recv(min(msg_len - bytes_recd, 1024))
|
||||||
|
if bytes_recd == len(message): # if the message wasn't added to
|
||||||
|
raise RuntimeError("socket connection broken")
|
||||||
|
bytes_recd = len(message)
|
||||||
|
socket.send(bytes_recd.to_bytes(int_length, byteorder = "big"))
|
||||||
|
|
||||||
|
for i in range(0, int(msg_len), int_length):
|
||||||
|
ret.append(int.from_bytes(message[i:i+int_length], byteorder = "big"))
|
||||||
|
return tuple(ret)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_str(socket):
|
||||||
|
"""get a string from THE OTHER SIDE!"""
|
||||||
|
size = b""
|
||||||
|
message = b""
|
||||||
|
msg_len = int(0)
|
||||||
|
bytes_recd = int(0)
|
||||||
|
#ret = []
|
||||||
|
|
||||||
|
while bytes_recd < 2:
|
||||||
|
size += socket.recv(2 - bytes_recd)
|
||||||
|
if size == b"":
|
||||||
|
raise RuntimeError("socket connection broken")
|
||||||
|
bytes_recd = len(size)
|
||||||
|
msg_len = int.from_bytes(size, byteorder = "big")
|
||||||
|
bytes_recd = int(0)
|
||||||
|
|
||||||
|
while bytes_recd < msg_len:
|
||||||
|
message += socket.recv(min(msg_len - bytes_recd, 1024))
|
||||||
|
if bytes_recd == len(message):
|
||||||
|
raise RuntimeError("socket connection broken")
|
||||||
|
bytes_recd = len(message)
|
||||||
|
socket.send(bytes_recd.to_bytes(2, byteorder = "big"))
|
||||||
|
ret = str(message, "utf-8")
|
||||||
|
return ret
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def send_int(socket, stuff, int_length = 2): #tuple stuff
|
||||||
|
"""Send a tuple of ints to THE OTHER SIDE!"""
|
||||||
|
message = b""
|
||||||
|
|
||||||
|
for thing in stuff:
|
||||||
|
message += int(thing).to_bytes(int_length, byteorder = "big")
|
||||||
|
msg_len = len(message)
|
||||||
|
message = msg_len.to_bytes(int_length, byteorder = "big") + message
|
||||||
|
total_sent = 0
|
||||||
|
|
||||||
|
while total_sent < msg_len + int_length:
|
||||||
|
total_sent += socket.send(message[total_sent:])
|
||||||
|
confirm = socket.recv(int_length)
|
||||||
|
|
||||||
|
if int.from_bytes(confirm, byteorder = "big") == msg_len:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
raise RuntimeError("incomplete packet sent")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def send_str(socket, stuff):
|
||||||
|
"""Send a string to THE OTHER SIDE!"""
|
||||||
|
msg_len = len(stuff)
|
||||||
|
message = msg_len.to_bytes(2, byteorder = "big") + bytes(stuff, "utf-8")
|
||||||
|
total_sent = 0
|
||||||
|
|
||||||
|
while total_sent < msg_len + 2:
|
||||||
|
total_sent += socket.send(message[total_sent:])
|
||||||
|
confirm = socket.recv(2)
|
||||||
|
|
||||||
|
if int.from_bytes(confirm, byteorder = "big") == msg_len:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
raise RuntimeError("incomplete packet sent")
|
||||||
|
|
||||||
|
class QuickStreamServer(PacketUtility):
|
||||||
|
"""Get a server set up easily! Note: This kind of server is NOT always ideal."""
|
||||||
|
|
||||||
|
def __init__(self, port, expectedClients = 1, ipAddress = ''):
|
||||||
|
"""Creates a quick server using the specified port and number of expected clients."""
|
||||||
|
self.clients = [] # Start with an empty list of clients
|
||||||
|
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
if len(ipAddress) >= 7:
|
||||||
|
address = (ipAddress, port)
|
||||||
|
else
|
||||||
|
address = (socket.gethostname(), port)
|
||||||
|
serverSocket.bind(address)
|
||||||
|
serverSocket.listen(expectedClients)
|
||||||
|
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()
|
||||||
|
|
||||||
|
def getByte(self):
|
||||||
|
"""Get a tuple 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 tuple(ret)
|
||||||
|
|
||||||
|
def getShort(self):
|
||||||
|
"""Get a tuple 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 tuple(ret)
|
||||||
|
|
||||||
|
def getInt(self):
|
||||||
|
"""Get a tuple of ints from each client."""
|
||||||
|
ret = []
|
||||||
|
for i in range(len(self.clients)):
|
||||||
|
ret.append(QuickStreamServer.get_int(self.clients[i][0], 4)
|
||||||
|
return tuple(ret)
|
||||||
|
|
||||||
|
def getLong(self):
|
||||||
|
"""Get a tuple of long ints from each client."""
|
||||||
|
ret = []
|
||||||
|
for i in range(len(self.clients)):
|
||||||
|
ret.append(QuickStreamServer.get_int(self.clients[i][0], 8)
|
||||||
|
return tuple(ret)
|
||||||
|
|
||||||
|
def getString(self):
|
||||||
|
"""Get a string from each client."""
|
||||||
|
ret = []
|
||||||
|
for i in range(len(self.clients)):
|
||||||
|
ret.append(QuickStreamServer.get_str(self.clients[i][0]))
|
||||||
|
return tuple(ret)
|
||||||
|
|
||||||
|
def sendByte(self, message):
|
||||||
|
"""Send a tuple of byte-size ints to each client."""
|
||||||
|
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."""
|
||||||
|
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."""
|
||||||
|
for i in range(len(self.clients)):
|
||||||
|
QuickStreamServer.send_int(self.clients[i][0], message, 4)
|
||||||
|
|
||||||
|
def sendShort(self, message):
|
||||||
|
"""Send a tuple of long ints to each client."""
|
||||||
|
for i in range(len(self.clients)):
|
||||||
|
QuickStreamServer.send_int(self.clients[i][0], message, 8)
|
||||||
|
|
||||||
|
def sendString(self, message):
|
||||||
|
"""Send a string to each client."""
|
||||||
|
for i in range(len(self.clients)):
|
||||||
|
QuickStreamServer.send_str(self.clients[i][0], message)
|
||||||
|
|
||||||
|
class QuickStreamClient(PacketUtility):
|
||||||
|
"""Get a client set up easily! Note: This kind of client is NOT always ideal."""
|
||||||
|
|
||||||
|
def __init__(self, ipAddress, port):
|
||||||
|
"""Creates a quick client using the specified IP address and port."""
|
||||||
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
server.connect((ipAddress, port))
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
server.close()
|
||||||
|
|
||||||
|
def getByte(self):
|
||||||
|
"""Get a tuple of byte-size ints from the server."""
|
||||||
|
return QuickStreamClient.get_int(self.server, 1)
|
||||||
|
|
||||||
|
def getShort(self):
|
||||||
|
"""Get a tuple of short ints from the server."""
|
||||||
|
return QuickStreamClient.get_int(self.server, 2)
|
||||||
|
|
||||||
|
def getInt(self):
|
||||||
|
"""Get a tuple of ints from the server."""
|
||||||
|
return QuickStreamClient.get_int(self.server, 4)
|
||||||
|
|
||||||
|
def getLong(self):
|
||||||
|
"""Get a tuple of long ints from the server."""
|
||||||
|
return QuickStreamClient.get_int(self.server, 8)
|
||||||
|
|
||||||
|
def getString(self):
|
||||||
|
"""Get a string from the server."""
|
||||||
|
return QuickStreamClient.get_str(self.server)
|
||||||
|
|
||||||
|
def sendByte(self, message):
|
||||||
|
"""Send a tuple of byte-size ints to the server."""
|
||||||
|
QuickStreamClient.send_int(self.server, message, 1)
|
||||||
|
|
||||||
|
def sendShort(self, message):
|
||||||
|
"""Send a tuple of short ints to the server."""
|
||||||
|
QuickStreamClient.send_int(self.server, message, 2)
|
||||||
|
|
||||||
|
def sendInt(self, message):
|
||||||
|
"""Send a tuple of ints to the server."""
|
||||||
|
QuickStreamClient.send_int(self.server, message, 4)
|
||||||
|
|
||||||
|
def sendLong(self, message):
|
||||||
|
"""Send a tuple of long ints to the server."""
|
||||||
|
QuickStreamClient.send_int(self.server, message, 8)
|
||||||
|
|
||||||
|
def sendString(self, message):
|
||||||
|
"""Send a string to the server."""
|
||||||
|
QuickStreamClient.sent_str(self.server, message)
|
||||||
|
|
||||||
|
# Legacy support:
|
||||||
|
get_int = PacketUtility.get_int
|
||||||
|
get_str = PacketUtility.get_str
|
||||||
|
send_int = PacketUtility.send_int
|
||||||
|
send_str = PacketUtility.send_str
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("""packets.py
|
||||||
|
Contents:
|
||||||
|
Class PacketUtility: The base class of QuickStreamServer and QuickStreamClient.
|
||||||
|
Methods:
|
||||||
|
getMyIP()
|
||||||
|
get_int(socket, int_length = 2)
|
||||||
|
get_str(socket)
|
||||||
|
send_int(socket, stuff, int_length = 2)
|
||||||
|
send_str(socket, stuff)
|
||||||
|
|
||||||
|
Class QuickStreamServer: Get a server set up easily! Note: This kind of server is NOT always ideal.
|
||||||
|
Methods:
|
||||||
|
QuickStreamServer(port, expectedClients = 1, ipAddress = '')
|
||||||
|
getByte()
|
||||||
|
getShort()
|
||||||
|
getInt()
|
||||||
|
getLong()
|
||||||
|
getString()
|
||||||
|
sendByte(message)
|
||||||
|
sendShort(message)
|
||||||
|
sendInt(message)
|
||||||
|
sendLong(message)
|
||||||
|
sendString(message)
|
||||||
|
Fields:
|
||||||
|
clients
|
||||||
|
|
||||||
|
Class QuickStreamClient: Get a client set up easily! Note: This kind of client is NOT always ideal.
|
||||||
|
Methods:
|
||||||
|
QuickStreamClient(ipAddress, port)
|
||||||
|
getByte()
|
||||||
|
getShort()
|
||||||
|
getInt()
|
||||||
|
getLong()
|
||||||
|
getString()
|
||||||
|
sendByte(message)
|
||||||
|
sendShort(message)
|
||||||
|
sendInt(message)
|
||||||
|
sendLong(message)
|
||||||
|
sendString(message)
|
||||||
|
Fields:
|
||||||
|
server
|
||||||
|
|
||||||
|
Legacy:
|
||||||
|
get_int(socket, int_length = 2)
|
||||||
|
get_str(socket)
|
||||||
|
send_int(socket, stuff, int_length = 2)
|
||||||
|
send_str(socket, stuff)
|
||||||
|
|
||||||
|
This module is meant to be imported along with 'socket'.""")
|
287
packets.py~
Normal file
287
packets.py~
Normal file
|
@ -0,0 +1,287 @@
|
||||||
|
import socket
|
||||||
|
|
||||||
|
class PacketUtility:
|
||||||
|
"""The base class of QuickStreamServer and QuickStreamClient."""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def getMyIP():
|
||||||
|
return socket.gethostbyname(socket.getfqdn())
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_int(socket, int_length = 2):
|
||||||
|
"""Get a tuple of ints from THE OTHER SIDE!"""
|
||||||
|
size = b""
|
||||||
|
message = b""
|
||||||
|
msg_len = int(0)
|
||||||
|
bytes_recd = int(0)
|
||||||
|
ret = []
|
||||||
|
|
||||||
|
while bytes_recd < int_length:
|
||||||
|
size += socket.recv(int_length - bytes_recd)
|
||||||
|
if size == b"":
|
||||||
|
raise RuntimeError("socket connection broken")
|
||||||
|
bytes_recd = len(size)
|
||||||
|
msg_len = int.from_bytes(size, byteorder = "big")
|
||||||
|
bytes_recd = int(0)
|
||||||
|
|
||||||
|
while bytes_recd < msg_len:
|
||||||
|
message += socket.recv(min(msg_len - bytes_recd, 1024))
|
||||||
|
if bytes_recd == len(message): # if the message wasn't added to
|
||||||
|
raise RuntimeError("socket connection broken")
|
||||||
|
bytes_recd = len(message)
|
||||||
|
socket.send(bytes_recd.to_bytes(int_length, byteorder = "big"))
|
||||||
|
|
||||||
|
for i in range(0, int(msg_len), int_length):
|
||||||
|
ret.append(int.from_bytes(message[i:i+int_length], byteorder = "big"))
|
||||||
|
return tuple(ret)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_str(socket):
|
||||||
|
"""get a string from THE OTHER SIDE!"""
|
||||||
|
size = b""
|
||||||
|
message = b""
|
||||||
|
msg_len = int(0)
|
||||||
|
bytes_recd = int(0)
|
||||||
|
#ret = []
|
||||||
|
|
||||||
|
while bytes_recd < 2:
|
||||||
|
size += socket.recv(2 - bytes_recd)
|
||||||
|
if size == b"":
|
||||||
|
raise RuntimeError("socket connection broken")
|
||||||
|
bytes_recd = len(size)
|
||||||
|
msg_len = int.from_bytes(size, byteorder = "big")
|
||||||
|
bytes_recd = int(0)
|
||||||
|
|
||||||
|
while bytes_recd < msg_len:
|
||||||
|
message += socket.recv(min(msg_len - bytes_recd, 1024))
|
||||||
|
if bytes_recd == len(message):
|
||||||
|
raise RuntimeError("socket connection broken")
|
||||||
|
bytes_recd = len(message)
|
||||||
|
socket.send(bytes_recd.to_bytes(2, byteorder = "big"))
|
||||||
|
ret = str(message, "utf-8")
|
||||||
|
return ret
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def send_int(socket, stuff, int_length = 2): #tuple stuff
|
||||||
|
"""Send a tuple of ints to THE OTHER SIDE!"""
|
||||||
|
message = b""
|
||||||
|
|
||||||
|
for thing in stuff:
|
||||||
|
message += int(thing).to_bytes(int_length, byteorder = "big")
|
||||||
|
msg_len = len(message)
|
||||||
|
message = msg_len.to_bytes(int_length, byteorder = "big") + message
|
||||||
|
total_sent = 0
|
||||||
|
|
||||||
|
while total_sent < msg_len + int_length:
|
||||||
|
total_sent += socket.send(message[total_sent:])
|
||||||
|
confirm = socket.recv(int_length)
|
||||||
|
|
||||||
|
if int.from_bytes(confirm, byteorder = "big") == msg_len:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
raise RuntimeError("incomplete packet sent")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def send_str(socket, stuff):
|
||||||
|
"""Send a string to THE OTHER SIDE!"""
|
||||||
|
msg_len = len(stuff)
|
||||||
|
message = msg_len.to_bytes(2, byteorder = "big") + bytes(stuff, "utf-8")
|
||||||
|
total_sent = 0
|
||||||
|
|
||||||
|
while total_sent < msg_len + 2:
|
||||||
|
total_sent += socket.send(message[total_sent:])
|
||||||
|
confirm = socket.recv(2)
|
||||||
|
|
||||||
|
if int.from_bytes(confirm, byteorder = "big") == msg_len:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
raise RuntimeError("incomplete packet sent")
|
||||||
|
|
||||||
|
class QuickStreamServer(PacketUtility):
|
||||||
|
"""Get a server set up easily! Note: This kind of server is NOT always ideal."""
|
||||||
|
|
||||||
|
def __init__(self, port, expectedClients = 1, ipAddress = ''):
|
||||||
|
"""Creates a quick server using the specified port and number of expected clients."""
|
||||||
|
self.clients = [] # Start with an empty list of clients
|
||||||
|
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
if len(ipAddress) >= 7:
|
||||||
|
address = (ipAddress, port)
|
||||||
|
else
|
||||||
|
address = (socket.gethostname(), port)
|
||||||
|
serverSocket.bind(address)
|
||||||
|
serverSocket.listen(expectedClients)
|
||||||
|
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()
|
||||||
|
|
||||||
|
def getByte(self):
|
||||||
|
"""Get a tuple 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 tuple(ret)
|
||||||
|
|
||||||
|
def getShort(self):
|
||||||
|
"""Get a tuple 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 tuple(ret)
|
||||||
|
|
||||||
|
def getInt(self):
|
||||||
|
"""Get a tuple of ints from each client."""
|
||||||
|
ret = []
|
||||||
|
for i in range(len(self.clients)):
|
||||||
|
ret.append(QuickStreamServer.get_int(self.clients[i][0], 4)
|
||||||
|
return tuple(ret)
|
||||||
|
|
||||||
|
def getLong(self):
|
||||||
|
"""Get a tuple of long ints from each client."""
|
||||||
|
ret = []
|
||||||
|
for i in range(len(self.clients)):
|
||||||
|
ret.append(QuickStreamServer.get_int(self.clients[i][0], 8)
|
||||||
|
return tuple(ret)
|
||||||
|
|
||||||
|
def getString(self):
|
||||||
|
"""Get a string from each client."""
|
||||||
|
ret = []
|
||||||
|
for i in range(len(self.clients)):
|
||||||
|
ret.append(QuickStreamServer.get_str(self.clients[i][0]))
|
||||||
|
return tuple(ret)
|
||||||
|
|
||||||
|
def sendByte(self, message):
|
||||||
|
"""Send a tuple of byte-size ints to each client."""
|
||||||
|
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."""
|
||||||
|
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."""
|
||||||
|
for i in range(len(self.clients)):
|
||||||
|
QuickStreamServer.send_int(self.clients[i][0], message, 4)
|
||||||
|
|
||||||
|
def sendShort(self, message):
|
||||||
|
"""Send a tuple of long ints to each client."""
|
||||||
|
for i in range(len(self.clients)):
|
||||||
|
QuickStreamServer.send_int(self.clients[i][0], message, 8)
|
||||||
|
|
||||||
|
def sendString(self, message):
|
||||||
|
"""Send a string to each client."""
|
||||||
|
for i in range(len(self.clients)):
|
||||||
|
QuickStreamServer.send_str(self.clients[i][0], message)
|
||||||
|
|
||||||
|
class QuickStreamClient(PacketUtility):
|
||||||
|
"""Get a client set up easily! Note: This kind of client is NOT always ideal."""
|
||||||
|
|
||||||
|
def __init__(self, ipAddress, port):
|
||||||
|
"""Creates a quick client using the specified IP address and port."""
|
||||||
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
server.connect((ipAddress, port))
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
server.close()
|
||||||
|
|
||||||
|
def getByte(self):
|
||||||
|
"""Get a tuple of byte-size ints from the server."""
|
||||||
|
return QuickStreamClient.get_int(self.server, 1)
|
||||||
|
|
||||||
|
def getShort(self):
|
||||||
|
"""Get a tuple of short ints from the server."""
|
||||||
|
return QuickStreamClient.get_int(self.server, 2)
|
||||||
|
|
||||||
|
def getInt(self):
|
||||||
|
"""Get a tuple of ints from the server."""
|
||||||
|
return QuickStreamClient.get_int(self.server, 4)
|
||||||
|
|
||||||
|
def getLong(self):
|
||||||
|
"""Get a tuple of long ints from the server."""
|
||||||
|
return QuickStreamClient.get_int(self.server, 8)
|
||||||
|
|
||||||
|
def getString(self):
|
||||||
|
"""Get a string from the server."""
|
||||||
|
return QuickStreamClient.get_str(self.server)
|
||||||
|
|
||||||
|
def sendByte(self, message):
|
||||||
|
"""Send a tuple of byte-size ints to the server."""
|
||||||
|
QuickStreamClient.send_int(self.server, message, 1)
|
||||||
|
|
||||||
|
def sendShort(self, message):
|
||||||
|
"""Send a tuple of short ints to the server."""
|
||||||
|
QuickStreamClient.send_int(self.server, message, 2)
|
||||||
|
|
||||||
|
def sendInt(self, message):
|
||||||
|
"""Send a tuple of ints to the server."""
|
||||||
|
QuickStreamClient.send_int(self.server, message, 4)
|
||||||
|
|
||||||
|
def sendLong(self, message):
|
||||||
|
"""Send a tuple of long ints to the server."""
|
||||||
|
QuickStreamClient.send_int(self.server, message, 8)
|
||||||
|
|
||||||
|
def sendString(self, message):
|
||||||
|
"""Send a string to the server."""
|
||||||
|
QuickStreamClient.sent_str(self.server, message)
|
||||||
|
|
||||||
|
# Legacy support:
|
||||||
|
get_int = PacketUtility.get_int
|
||||||
|
get_str = PacketUtility.get_str
|
||||||
|
send_int = PacketUtility.send_int
|
||||||
|
send_str = PacketUtility.send_str
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("""packets.py
|
||||||
|
Contents:
|
||||||
|
Class PacketUtility: The base class of QuickStreamServer and QuickStreamClient.
|
||||||
|
Methods:
|
||||||
|
getMyIP()
|
||||||
|
get_int(socket, int_length = 2)
|
||||||
|
get_str(socket)
|
||||||
|
send_int(socket, stuff, int_length = 2)
|
||||||
|
send_str(socket, stuff)
|
||||||
|
|
||||||
|
Class QuickStreamServer: Get a server set up easily! Note: This kind of server is NOT always ideal.
|
||||||
|
Methods:
|
||||||
|
QuickStreamServer(port, expectedClients = 1, ipAddress = '')
|
||||||
|
getByte()
|
||||||
|
getShort()
|
||||||
|
getInt()
|
||||||
|
getLong()
|
||||||
|
getString()
|
||||||
|
sendByte(message)
|
||||||
|
sendShort(message)
|
||||||
|
sendInt(message)
|
||||||
|
sendLong(message)
|
||||||
|
sendString(message)
|
||||||
|
Fields:
|
||||||
|
clients
|
||||||
|
|
||||||
|
Class QuickStreamClient: Get a client set up easily! Note: This kind of client is NOT always ideal.
|
||||||
|
Methods:
|
||||||
|
QuickStreamClient(ipAddress, port)
|
||||||
|
getByte()
|
||||||
|
getShort()
|
||||||
|
getInt()
|
||||||
|
getLong()
|
||||||
|
getString()
|
||||||
|
sendByte(message)
|
||||||
|
sendShort(message)
|
||||||
|
sendInt(message)
|
||||||
|
sendLong(message)
|
||||||
|
sendString(message)
|
||||||
|
Fields:
|
||||||
|
server
|
||||||
|
|
||||||
|
Legacy:
|
||||||
|
get_int(socket, int_length = 2)
|
||||||
|
get_str(socket)
|
||||||
|
send_int(socket, stuff, int_length = 2)
|
||||||
|
send_str(socket, stuff)
|
||||||
|
|
||||||
|
This module is meant to be imported along with 'socket'.""")
|
227
spell.py
Normal file
227
spell.py
Normal file
|
@ -0,0 +1,227 @@
|
||||||
|
#! /usr/bun/python3
|
||||||
|
|
||||||
|
"""spell.py"""
|
||||||
|
|
||||||
|
import random
|
||||||
|
|
||||||
|
def random_chance(chance):
|
||||||
|
maximum = 1 / chance
|
||||||
|
draw = random.randint(1, maximum)
|
||||||
|
return (draw == 1)
|
||||||
|
|
||||||
|
class Spelling_Player:
|
||||||
|
"""A base player that can be used as an abstract class for other players."""
|
||||||
|
LEVEL = 16
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.hp = 100
|
||||||
|
self.status = 0
|
||||||
|
self.name = ""
|
||||||
|
self.max_letters = Spelling_Player.LEVEL
|
||||||
|
self.casted = {}
|
||||||
|
|
||||||
|
class Letter:
|
||||||
|
"""A single letter of a spell."""
|
||||||
|
VALUES = {"a" : 16, "e" : 8, "i" : 12, "o" : 6, "u" : 4}
|
||||||
|
STATUSES = {"h" : 1, "j" : 2, "l" : 4, "m" : 8, "n" : 16, "q" : 32, "r" : 64, "w" : 128}
|
||||||
|
|
||||||
|
def __init__(self, character = ""):
|
||||||
|
self.character = character
|
||||||
|
if(self.character in Letter.VALUES.keys()):
|
||||||
|
self.value = Letter.VALUES[self.character]
|
||||||
|
else:
|
||||||
|
self.value = 0
|
||||||
|
if(self.character in Letter.STATUSES.keys()):
|
||||||
|
self.status = Letter.STATUSES[self.character]
|
||||||
|
else:
|
||||||
|
self.status = 0
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return str(self.character)
|
||||||
|
|
||||||
|
class Spell:
|
||||||
|
"""A spell."""
|
||||||
|
#constants:
|
||||||
|
VOWELS = "aeiouy"
|
||||||
|
UNPAIREDS = "hjlmnqrw"
|
||||||
|
XBUFFS = "bd"
|
||||||
|
IBUFFS = "gvz"
|
||||||
|
XNERFS = "fks"
|
||||||
|
INERFS = "pt"
|
||||||
|
SPECIALS = "cx"
|
||||||
|
|
||||||
|
def __init__(self, word = ""):
|
||||||
|
valid = False
|
||||||
|
for i in Spell.VOWELS:
|
||||||
|
if(i in word):
|
||||||
|
valid = True
|
||||||
|
break
|
||||||
|
if(not valid): raise ValueError("No vowels are present.")
|
||||||
|
self.word = []
|
||||||
|
for i in word:
|
||||||
|
if(i == "y"):
|
||||||
|
i = random.choice("aeiou") #if the next letter is "y", change it to any other vowel
|
||||||
|
elif(self.is_special(i)):
|
||||||
|
i = random.choice(Spell.UNPAIREDS + Spell.XBUFFS + Spell.IBUFFS + Spell.XNERFS + Spell.INERFS) #unpaireds, buffs, & nerfs
|
||||||
|
else: pass
|
||||||
|
self.word.append(Letter(i))
|
||||||
|
if(len(self.word) >= 16): break
|
||||||
|
self.values = {"a" : 0, "e" : 0, "i" : 0, "o" : 0, "u" : 0}
|
||||||
|
self.xnerfs = {"a" : 1, "e" : 1, "i" : 1, "o" : 1, "u" : 1}
|
||||||
|
self.inerfs = {"a" : 0, "e" : 0, "i" : 0, "o" : 0, "u" : 0}
|
||||||
|
self.stats = 0
|
||||||
|
self.last_vowel = None
|
||||||
|
self.vowel_coef = 1
|
||||||
|
self.total_damage = 0
|
||||||
|
self.absorb = 0
|
||||||
|
|
||||||
|
def is_vowel(self, char):
|
||||||
|
return (char in Spell.VOWELS)
|
||||||
|
|
||||||
|
def is_unpaired(self, char):
|
||||||
|
return (char in Spell.UNPAIREDS)
|
||||||
|
|
||||||
|
def is_xbuff(self, char):
|
||||||
|
return (char in Spell.XBUFFS)
|
||||||
|
|
||||||
|
def is_ibuff(self, char):
|
||||||
|
return (char in Spell.IBUFFS)
|
||||||
|
|
||||||
|
def is_xnerf(self, char):
|
||||||
|
return (char in Spell.XNERFS)
|
||||||
|
|
||||||
|
def is_inerf(self, char):
|
||||||
|
return (char in Spell.INERFS)
|
||||||
|
|
||||||
|
def is_special(self, char):
|
||||||
|
return (char in Spell.SPECIALS)
|
||||||
|
|
||||||
|
def parse(self):
|
||||||
|
"""Initial parser"""
|
||||||
|
next_syl = []
|
||||||
|
for next_letter in self.word:
|
||||||
|
if(self.is_vowel(next_letter.character)):
|
||||||
|
if(self.last_vowel):
|
||||||
|
self.syl(next_syl)
|
||||||
|
next_syl = []
|
||||||
|
self.last_vowel = next_letter.character
|
||||||
|
next_syl.append(next_letter)
|
||||||
|
self.syl(next_syl) #remember not to let the last syllable drop off the Earth!
|
||||||
|
|
||||||
|
def syl(self, syllable):
|
||||||
|
"""Process a single syllable.
|
||||||
|
In this game, a syllable contains exactly one vowel,
|
||||||
|
all consonants adjacent to && after it (until the next vowel),
|
||||||
|
and, if it's the first one, all consonants in front as well."""
|
||||||
|
vowel = None
|
||||||
|
xvalue = 1
|
||||||
|
ivalue = 0
|
||||||
|
xnerf = 1
|
||||||
|
inerf = 0
|
||||||
|
status = 0
|
||||||
|
for next_letter in syllable:
|
||||||
|
|
||||||
|
if(self.is_vowel(next_letter.character)): #vowels
|
||||||
|
vowel = next_letter.character
|
||||||
|
xvalue *= max(int((next_letter.value / self.vowel_coef) + 0.5), 1)
|
||||||
|
ivalue *= next_letter.value
|
||||||
|
inerf *= int(next_letter.value / 2)
|
||||||
|
self.vowel_coef *= 2
|
||||||
|
|
||||||
|
elif(self.is_unpaired(next_letter.character)): #unpairedes
|
||||||
|
if(random_chance(1 / 8)):
|
||||||
|
status = status | next_letter.status
|
||||||
|
|
||||||
|
elif(self.is_xbuff(next_letter.character)): #xbuffs
|
||||||
|
xvalue *= 2
|
||||||
|
|
||||||
|
elif(self.is_ibuff(next_letter.character)): #ibuffs
|
||||||
|
if(vowel): ivalue += Letter.VALUES[vowel]
|
||||||
|
else: ivalue += 1
|
||||||
|
|
||||||
|
elif(self.is_xnerf(next_letter.character)): #xnerfs
|
||||||
|
xnerf *= 2
|
||||||
|
|
||||||
|
elif(self.is_inerf(next_letter.character)): #inerfs
|
||||||
|
if(vowel): inerf += int(Letter.VALUES[vowel] / 2)
|
||||||
|
else: inerf += 1
|
||||||
|
|
||||||
|
else: continue
|
||||||
|
|
||||||
|
self.values[vowel] += (xvalue + ivalue)
|
||||||
|
self.xnerfs[vowel] *= xnerf
|
||||||
|
self.inerfs[vowel] += inerf
|
||||||
|
self.stats = self.stats | status
|
||||||
|
|
||||||
|
def parse_nerfs(self, opponent): #opponent is the other guy's spell
|
||||||
|
for k in opponent.xnerfs.keys():
|
||||||
|
if(self.values[k] > 0):
|
||||||
|
self.values[k] = int(max((self.values[k] - opponent.inerfs[k]), 1)) #incremental nerfs are applied
|
||||||
|
self.values[k] = int(max((self.values[k] / opponent.xnerfs[k]) + 0.5, 1)) #multiplicative nerfs are applied (is "multiplicative" a word?)
|
||||||
|
|
||||||
|
def parse_atk(self):
|
||||||
|
self.total_damage = self.values["a"]
|
||||||
|
|
||||||
|
def parse_ret_def(self, opponent):
|
||||||
|
opponent.absorb = min(self.total_damage, opponent.values["u"])
|
||||||
|
self.total_damage = max(self.total_damage - opponent.values["u"], 0)
|
||||||
|
|
||||||
|
def parse_def(self, opponent):
|
||||||
|
#print(self.total_damage)
|
||||||
|
total = min(self.total_damage, opponent.values["i"])
|
||||||
|
self.total_damage -= total #total damage is decremented by the opponent's defense
|
||||||
|
opponent.values["i"] -= total
|
||||||
|
#print(self.total_damage)
|
||||||
|
|
||||||
|
def parse_defatk_def(self, opponent):
|
||||||
|
defense = min(opponent.values["e"], self.total_damage) #do defatk defense first
|
||||||
|
opponent.values["e"] -= defense
|
||||||
|
self.total_damage -= defense
|
||||||
|
|
||||||
|
def parse_defatk_atk(self, opponent):
|
||||||
|
self.total_damage += self.values["e"]
|
||||||
|
total = min(self.total_damage, opponent.values["i"])
|
||||||
|
self.total_damage -= total
|
||||||
|
opponent.values["i"] -= total
|
||||||
|
|
||||||
|
def parse_ret_atk(self, opponent):
|
||||||
|
self.total_damage += self.absorb
|
||||||
|
self.total_damage -= opponent.values["i"]
|
||||||
|
if(self.total_damage < 0): self.total_damage = 0
|
||||||
|
|
||||||
|
def affect(self, opponent, caster): #opponent is the other guy's spell, caster is the player who cast the spell
|
||||||
|
caster.hp -= opponent.total_damage
|
||||||
|
if(caster.hp <= 0):
|
||||||
|
caster.hp = 0
|
||||||
|
return
|
||||||
|
caster.hp += self.values["o"]
|
||||||
|
if(caster.hp > 100): caster.hp = 100
|
||||||
|
caster.status = opponent.stats
|
||||||
|
|
||||||
|
def compare_spells(spell1, spell2):
|
||||||
|
spell1.parse()
|
||||||
|
spell2.parse()
|
||||||
|
spell1.parse_nerfs(spell2)
|
||||||
|
spell2.parse_nerfs(spell1)
|
||||||
|
spell1.parse_atk()
|
||||||
|
spell2.parse_atk()
|
||||||
|
spell1.parse_ret_def(spell2)
|
||||||
|
spell2.parse_ret_def(spell1)
|
||||||
|
spell1.parse_def(spell2)
|
||||||
|
spell2.parse_def(spell1)
|
||||||
|
spell1.parse_defatk_def(spell2)
|
||||||
|
spell2.parse_defatk_def(spell1)
|
||||||
|
spell1.parse_defatk_atk(spell2)
|
||||||
|
spell2.parse_defatk_atk(spell1)
|
||||||
|
spell1.parse_ret_atk(spell2)
|
||||||
|
spell2.parse_ret_atk(spell1)
|
||||||
|
return spell1, spell2
|
||||||
|
|
||||||
|
if(__name__ == "__main__"):
|
||||||
|
print("""spell.py
|
||||||
|
Contents:
|
||||||
|
random_chance(chance)
|
||||||
|
class Spelling_Player
|
||||||
|
class Letter
|
||||||
|
class Spell
|
||||||
|
compare_spells(spell1, spell2)""")
|
82
spelling.py
Normal file
82
spelling.py
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
#! /usr/bin/python3
|
||||||
|
|
||||||
|
"""This is just a POC test of the spelling game.
|
||||||
|
Thus, the name is temporary.
|
||||||
|
v0.1:0067"""
|
||||||
|
|
||||||
|
import random
|
||||||
|
import spell
|
||||||
|
import status
|
||||||
|
|
||||||
|
class Player(spell.Spelling_Player):
|
||||||
|
"""A player."""
|
||||||
|
|
||||||
|
def cast_spell(self):
|
||||||
|
prompt = "Cast a spell! (up to " + str(self.max_letters) + " letters) "
|
||||||
|
casted_spell = str(input(prompt)).lower()
|
||||||
|
if(len(casted_spell) > self.max_letters):
|
||||||
|
casted_spell = casted_spell[:self.max_letters]
|
||||||
|
|
||||||
|
while(not self.is_valid(casted_spell)):
|
||||||
|
prompt = "Cast a different spell -\nYou can't cast that one more than " + str(max(16 / len(casted_spell), 1))
|
||||||
|
if(max(16 / len(casted_spell), 1) == 1): prompt += " time! "
|
||||||
|
else: prompt += " times! "
|
||||||
|
casted_spell = str(input(prompt)).lower()
|
||||||
|
if(len(casted_spell) > self.max_letters):
|
||||||
|
casted_spell = casted_spell[:self.max_letters]
|
||||||
|
|
||||||
|
return spell.Spell(casted_spell), casted_spell
|
||||||
|
|
||||||
|
def is_valid(self, spell):
|
||||||
|
if(spell in self.casted):
|
||||||
|
if(self.casted[spell] < max(16 / len(spell), 1)):
|
||||||
|
self.record(spell)
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
is_vowel = False
|
||||||
|
for i in "aeiouy":
|
||||||
|
if(i in spell):
|
||||||
|
is_vowel = True
|
||||||
|
break
|
||||||
|
if(not is_vowel): return False
|
||||||
|
else:
|
||||||
|
self.record(spell)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def record(self, spell):
|
||||||
|
if(spell in self.casted):
|
||||||
|
self.casted[spell] += 1
|
||||||
|
else:
|
||||||
|
self.casted[spell] = 1
|
||||||
|
|
||||||
|
def main():
|
||||||
|
player1 = Player()
|
||||||
|
player2 = Player()
|
||||||
|
|
||||||
|
while(min(player1.hp, player2.hp) > 0):
|
||||||
|
print("Player 1: " + str(player1.hp) + " hp", end = " ")
|
||||||
|
if(player1.status): print("status " + str(player1.status))
|
||||||
|
else: print()
|
||||||
|
spell1, word1 = player1.cast_spell()
|
||||||
|
print("Player 2: " + str(player2.hp) + " hp", end = " ")
|
||||||
|
if(player2.status): print("status " + str(player2.status))
|
||||||
|
else: print()
|
||||||
|
spell2, word2 = player2.cast_spell()
|
||||||
|
spell.compare_spells(spell1, spell2)
|
||||||
|
spell1.affect(spell2, player1)
|
||||||
|
spell2.affect(spell1, player2)
|
||||||
|
status.apply(player1)
|
||||||
|
status.apply(player2)
|
||||||
|
print("\nPlayer 1 took " + str(spell2.total_damage) + " damage, and healed " + str(spell1.values["o"]) + " hp!")
|
||||||
|
print("Player 1 now has " + str(player1.hp) + " hp!\n")
|
||||||
|
print("Player 2 took " + str(spell1.total_damage) + " damage, and healed " + str(spell2.values["o"]) + " hp!")
|
||||||
|
print("Player 2 now has " + str(player2.hp) + " hp!\n")
|
||||||
|
|
||||||
|
if(player1.hp > 0): print("Player 1 wins!")
|
||||||
|
elif(player2.hp > 0): print("Player 2 wins!")
|
||||||
|
else: print("Draw!")
|
||||||
|
|
||||||
|
main()
|
||||||
|
input("Press any key to exit.")
|
150
spelling_mp.py
Normal file
150
spelling_mp.py
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
#! /usr/bin/python3
|
||||||
|
|
||||||
|
"""This is just a POC test of the spelling game.
|
||||||
|
Thus, the name is temporary.
|
||||||
|
v0.1:0085"""
|
||||||
|
|
||||||
|
import random
|
||||||
|
import socket
|
||||||
|
import packets
|
||||||
|
import spell
|
||||||
|
import status
|
||||||
|
|
||||||
|
class Player(spell.Spelling_Player):
|
||||||
|
"""A player."""
|
||||||
|
|
||||||
|
def cast_spell(self):
|
||||||
|
prompt = "Cast a spell! (up to " + str(self.max_letters) + " letters) "
|
||||||
|
casted_spell = str(input(prompt)).lower()
|
||||||
|
if(len(casted_spell) > self.max_letters):
|
||||||
|
casted_spell = casted_spell[:self.max_letters]
|
||||||
|
while(not self.is_valid(casted_spell)):
|
||||||
|
prompt = "Cast a different spell -\nYou can't cast that one more than " + str(max(16 / len(casted_spell), 1))
|
||||||
|
if(max(16 / len(casted_spell), 1) == 1): prompt += " time! "
|
||||||
|
else: prompt += " times! "
|
||||||
|
casted_spell = str(input(prompt)).lower()
|
||||||
|
if(len(casted_spell) > self.max_letters):
|
||||||
|
casted_spell = casted_spell[:self.max_letters]
|
||||||
|
return spell.Spell(casted_spell), casted_spell
|
||||||
|
|
||||||
|
def is_valid(self, spell):
|
||||||
|
if(spell in self.casted):
|
||||||
|
if(self.casted[spell] < max(16 / len(spell), 1)):
|
||||||
|
#always 16, not self.max_letters
|
||||||
|
self.record(spell)
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
is_vowel = False
|
||||||
|
for i in "aeiouy":
|
||||||
|
if(i in spell):
|
||||||
|
is_vowel = True
|
||||||
|
break
|
||||||
|
if(not is_vowel): return False
|
||||||
|
else:
|
||||||
|
self.record(spell)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def record(self, spell):
|
||||||
|
if(spell in self.casted):
|
||||||
|
self.casted[spell] += 1
|
||||||
|
else:
|
||||||
|
self.casted[spell] = 1
|
||||||
|
|
||||||
|
def server():
|
||||||
|
player1 = Player()
|
||||||
|
player2 = Player()
|
||||||
|
|
||||||
|
print("Tell your friend to connect to " + socket.gethostbyname(socket.gethostname()))
|
||||||
|
|
||||||
|
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
server_socket.bind((socket.gethostname(), 1028))
|
||||||
|
server_socket.listen(1)
|
||||||
|
(client_socket, address) = server_socket.accept()
|
||||||
|
server_socket.close()
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
while(min(player1.hp, player2.hp) > 0):
|
||||||
|
print(player1.name + ": " + str(player1.hp) + " hp", end = " ")
|
||||||
|
if(player1.status): print("status " + str(player1.status))
|
||||||
|
else: print()
|
||||||
|
spell1, word1 = player1.cast_spell()
|
||||||
|
print("Waiting for " + player2.name + "'s move...")
|
||||||
|
word2 = packets.get_str(client_socket)
|
||||||
|
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)
|
||||||
|
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")
|
||||||
|
print(player2.name + " took " + str(spell1.total_damage) + " damage, and healed " + str(spell2.values["o"]) + " hp!")
|
||||||
|
print(player2.name + " now has " + str(player2.hp) + " hp!\n")
|
||||||
|
|
||||||
|
if(player1.hp > 0): print(player1.name + " wins!")
|
||||||
|
elif(player2.hp > 0): print(player2.name + " wins!")
|
||||||
|
else: print("Draw!")
|
||||||
|
client_socket.close()
|
||||||
|
|
||||||
|
def client():
|
||||||
|
player1 = Player()
|
||||||
|
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))
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
while(min(player1.hp, player2.hp) > 0):
|
||||||
|
print(player2.name + ": " + str(player2.hp) + " hp", end = " ")
|
||||||
|
if(player2.status): print("status " + str(player2.status))
|
||||||
|
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)
|
||||||
|
player1.hp = packet[4]
|
||||||
|
player2.hp = packet[5]
|
||||||
|
player2.status = packet[6]
|
||||||
|
status.apply(player2)
|
||||||
|
word1 = packets.get_str(client_socket)
|
||||||
|
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")
|
||||||
|
print(player2.name + " took " + str(packet[2]) + " damage, and healed " + str(packet[3]) + " hp!")
|
||||||
|
print(player2.name + " now has " + str(packet[5]) + " hp!\n")
|
||||||
|
|
||||||
|
if(player1.hp > 0): print(player1.name + " wins!")
|
||||||
|
elif(player2.hp > 0): print(player2.name + " wins!")
|
||||||
|
else: print("Draw!")
|
||||||
|
client_socket.close()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
while(True):
|
||||||
|
print("1. Host")
|
||||||
|
print("2. Join")
|
||||||
|
print("3. Exit")
|
||||||
|
choice = input("What'll it be? ")
|
||||||
|
if(choice == "1"): server()
|
||||||
|
elif(choice == "2"): client()
|
||||||
|
elif(choice == "3"): break
|
||||||
|
else: print("Input one of the numbers corresponding to the option.")
|
||||||
|
|
||||||
|
main()
|
||||||
|
input("Press any key to exit.")
|
13
status.py
Normal file
13
status.py
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#! usr/bin/python3
|
||||||
|
|
||||||
|
"status.py"
|
||||||
|
|
||||||
|
import spell
|
||||||
|
|
||||||
|
def apply(player = spell.Spelling_Player()):
|
||||||
|
|
||||||
|
if(player.max_letters != spell.Spelling_Player.LEVEL):
|
||||||
|
player.max_letters = spell.Spelling_Player.LEVEL
|
||||||
|
|
||||||
|
if(player.status & spell.Letter.STATUSES["q"]):
|
||||||
|
player.max_letters //= 2
|
Loading…
Add table
Add a link
Reference in a new issue