From c5b00e01dfbe2f3d7907e76e65796e6149d90059 Mon Sep 17 00:00:00 2001 From: Patrick Marsee Date: Fri, 25 Nov 2016 11:01:11 -0500 Subject: [PATCH] Initial upload --- __pycache__/packets.cpython-34.pyc | Bin 0 -> 2748 bytes __pycache__/spell.cpython-34.pyc | Bin 0 -> 7629 bytes __pycache__/status.cpython-34.pyc | Bin 0 -> 443 bytes packets.py | 291 +++++++++++++++++++++++++++++ packets.py~ | 287 ++++++++++++++++++++++++++++ spell.py | 227 ++++++++++++++++++++++ spelling.py | 82 ++++++++ spelling_mp.py | 150 +++++++++++++++ status.py | 13 ++ 9 files changed, 1050 insertions(+) create mode 100644 __pycache__/packets.cpython-34.pyc create mode 100644 __pycache__/spell.cpython-34.pyc create mode 100644 __pycache__/status.cpython-34.pyc create mode 100644 packets.py create mode 100644 packets.py~ create mode 100644 spell.py create mode 100644 spelling.py create mode 100644 spelling_mp.py create mode 100644 status.py diff --git a/__pycache__/packets.cpython-34.pyc b/__pycache__/packets.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..73aa1ba19c6ee3f1882a37b730a831ea63c91b7a GIT binary patch literal 2748 zcma)8&u<%55T57tu7A|QC81413*7{v#Ly&^OArEt)I>lJsZacWrMH?>cW^ zNNOb)QvL(Pl^YT_PW%J>3EcL|sS@JC4K92$Ydf(Cf~|MD``)~ndHa3yz5HI8D*rV7 zv0be5wtqJ7097@xzkbwt82f^kg;WbkZpOOZde^v%cRHu)~6#3($N+iP&JT5HS*nNl2;qK6WiRX9+NQx1O zG5d9n=EnjZJy6^5uGb3vG&Xs($d~D6U5pNbZYH|?U3?BwQ_19YMR$U^UJjI zTvg5D|EjvFZmA01Ts19kcXRjDs?x8* zyUAaSF9)}cTUh?~faL%f)Du7fE7NeC@yv2tSPmXoLzV+~;T%RlgN)_Eg)x>3K`vPi zrbrWAVfnDBndReondL$v?kli-*jLQ*EGhbb$Z|}gH!&#O_|LNHV{tIQ!uH8Dr%0t$ph1DX4t&K<`$dVY?xu0na5McBdmKJ zFUc~@|KbtYr0>GF$$R6A3*$0{MZe%7Q_0g6mV%AlH(4fu&`2Oy3-K`rqNiArj;0`U zz-*{GtTp3an3{znB!eaP0SgP95NDR@@Qu8KfIu8SXuM6VFu-md{zrZ$VyGiSydaW< z^?-U<2f{H=e&)4xgAmY6kz{^{DlOH~=zx1%mP|Y;N*U%OJO%x&{_QYo#$5=S3EW;o zqzZ~+YK078#xMg{AQiv`X8yRfqTrg^i8*ey^vmqa@b1{A1Lg_JY1zc4ap-Z-v;Y-s zg}OTlc#%bnm17;N6J`St&rPW}pcwPejv2L}s<}HVpVKbpMyKJJPJE9U#xiSOImv(( zFduzMMiQ^+HNeB5*-QZjO#pzafYz4x5ok~o=MpsZ70IiroC5>l`mS>$Put~>WVmIi?*Eo%| zqckSBoW93qq9-$mQ0^m4Cfv!CtEk0sa27F|7Co=q2qVvHRH57%r}%g%Ka3*-79{VN zU0j2+VmUjW?(<__+_K>zTSjYzfBy*Ko^%2oxJxp-UpW%gL0cI5C|Bejl~bLtRL}NnY;vp!}LyNzB*H>R+Lrv E8%e!8KmY&$ literal 0 HcmV?d00001 diff --git a/__pycache__/spell.cpython-34.pyc b/__pycache__/spell.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..61e5e2942d6dca163efd04fbbf581b2d331b9a68 GIT binary patch literal 7629 zcmb7J-ESOM6~8n4;q`hQJ9gs4b=pi@nq=G5AyG-1DsAi}P6-4zt>dIDwA1mehKtdoP(Km#|1LCEx2#G&no{*4`{K7jt@%x=Sv%7Xoo2)(O z-npOW`<#2{-<9F=q2K>;@u$xz^-tCJ%b`AlC;9}ySE>eRDc@EtOVv=xDL=11MZ2xu z_6y1{@&)jbt!g!?t9YUl0Kjrvsi}vSij`Ucrg*ow;X_+( z+kC|)3srl5hf7rLq^P~nZ?7c-ESq6m&DDzaWiPA;wShHnv$?jhR?GX^d+@o^1x)#` zb7r}{7Mxl4V%@B-o_WyLzI)F@ONL@HY?gG*G6O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XfxHcmzuXbpo)YUunfe8DizI{xT}jYXG<)K#tz`XOB!) za=ZR2l~*f8wB#!Tl>%x7Sz(tJQD6Yt)Wf)B%-l2;{`o@5qW2$ z;iEVYZIG-v?Qjvi^hP7v_j)AsXX_yD_j=@}JL^G;-s_P(KI=h--s_QzsI&*o4Sq($ zC3DGe{;QeJZ*393Se;GFb9-ImfqHul-Z9`UQ>zvIl}vj9fFI7vNTY_kCF}ivni&Vb z=d2<1b=}U7Wd(dLEMG+uio7CvqwL_f>%-hB^L?%=J?3MW=xpzcT3?U36z^-0q2_%T z)~96av!~6~?71ouPF{b@Rhmg$=?6`^$_pt$U5q0stZOE_y@1ix%cOM4e4lzNhbeM= WEZP;jQWzf^A0Pj9WxQhLivI(WtBq0s literal 0 HcmV?d00001 diff --git a/__pycache__/status.cpython-34.pyc b/__pycache__/status.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6dbd8d7621479aac62e0bebaf5e84aba508d1dc8 GIT binary patch literal 443 zcmYjNO-lnY5PivhwAvOy5D~n1l0~pr4v| zht$U#k(6-6QSiKD%A{PH^J2B8Ui8M~T`d=#?=5XJAb0^Xpsc(y4>J$?^#4dz0W3B$ zzv2UAp_}9ATK8SIPwjN;G2aMj&N%XVmOZRF*+(_om?uv{$+DE%*uWH0DZ3ZDFkMVU zUnMK4ZP4x9b-Fg(%x%MNrMLBXa5EUjoj78nu&AW(&+C|bU= 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'.""") diff --git a/packets.py~ b/packets.py~ new file mode 100644 index 0000000..3a0fb9f --- /dev/null +++ b/packets.py~ @@ -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'.""") diff --git a/spell.py b/spell.py new file mode 100644 index 0000000..6ced8c5 --- /dev/null +++ b/spell.py @@ -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)""") diff --git a/spelling.py b/spelling.py new file mode 100644 index 0000000..7dcd8b9 --- /dev/null +++ b/spelling.py @@ -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.") diff --git a/spelling_mp.py b/spelling_mp.py new file mode 100644 index 0000000..6030736 --- /dev/null +++ b/spelling_mp.py @@ -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.") diff --git a/status.py b/status.py new file mode 100644 index 0000000..a24cbdb --- /dev/null +++ b/status.py @@ -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