150 lines
5.8 KiB
Python
150 lines
5.8 KiB
Python
#! /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.")
|