From aea461499c723ba3567301f43afe9cecf6526a16 Mon Sep 17 00:00:00 2001 From: Kwarde Date: Thu, 18 Jan 2024 15:30:08 +0100 Subject: [PATCH] Make player.py compatible with wands and spells dicts --- player.py | 58 +++++++++++++++++++++++++++++-------------------------- spells.py | 14 ++++++++------ 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/player.py b/player.py index d32a6b8..4c1553f 100644 --- a/player.py +++ b/player.py @@ -1,5 +1,6 @@ from wands import Wand -from spells import * +from spells import SPELL_TYPE_NONE, SPELL_TYPE_USELESS, SPELL_TYPE_DEFENSE, SPELL_TYPE_UNFORGIVABLE#, SPELL_TYPE_COMMON, SPELL_TYPE_POWERFUL +from spells import Spell, spells, _INVALID_SPELL MAX_PLAYER_HEALTH = 1000 MAX_STUNNED_ROUNDS = 10 @@ -10,7 +11,7 @@ class Player: self.health = MAX_PLAYER_HEALTH self.wand = wand - self.active_spell = spell_object_none + self.active_spell = spells[_INVALID_SPELL] self.active_spell_succes = False self.active_spell_levenshtein_distance = 0 # Penalty => If >0 then damage reduction, 15 per distance @@ -56,57 +57,60 @@ class Player: return output def cast_spell_result(self, spell: Spell, succes: bool): + if spell == None: + spell[None].type = SPELL_TYPE_NONE + if succes: message = "{name} casted '{spell}' with succes" else: if spell.type == SPELL_TYPE_UNFORGIVABLE: message = "{name} tried to cast '{spell}' but didn't truly mean it. Cast FAILED!" elif spell.type == SPELL_TYPE_NONE: - if spell == spell_object_none: + if spell == spells[_INVALID_SPELL]: return "{name} must not be feeling well, since they casted a non existing spell. Cast FAILED!".format(name=self.name) - elif spell == spell_object_stunned: - return " {name} can't cast anything but {spell} since they are stunned".format(name=self.name, spell=spell_finite_incantatem.name) + elif spell == None: + return " {name} can't cast anything but {spell} since they are stunned".format(name=self.name, spell=spells["Finite Incantatem"].name) else: message = "{name} tried to cast '{spell}' but mispronounced it. Cast FAILED!" - return message.format(name=self.name, spell=spell.name) + return message.format(name=self.name, spell=spell.get_spell_name()) def cast_spell(self, opponent): #: Player ? - spell_name = self.active_spell.name.lower() + spell_name = self.active_spell.get_spell_name() - if self.active_spell is spell_object_none: + if self.active_spell is None: print("- {name} does nothing".format(name=self.name)) return - if self.stunned_rounds > 0 and self.active_spell is not spell_finite_incantatem: + if self.stunned_rounds > 0 and self.active_spell is not spells["Finite Incantatem"]: print(" {name} tries to cast a spell but fails because they are stunned!".format(name=self.name)) return if self.active_spell.type == SPELL_TYPE_USELESS: - if self.active_spell is spell_lumos: + if self.active_spell is spells["Lumos"]: if self.lumos: print("- {name} casts {spell} with no effect: {name} already has a brutal shining light at the tip of their wand!".format(name=self.name, spell=spell_name)) else: print("- {name} casts {spell}! Look at that brutal shining light at the tip of their wand. Absolutely gorgeous!".format(name=self.name, spell=spell_name)) self.lumos = True - elif self.active_spell is spell_nox: + elif self.active_spell is spells["Nox"]: if not self.lumos: print("- {name} shouts: {spell}! Nothing happened".format(name=self.name, spell=spell_name.upper())) else: print("- {name} shouts {spell}! Their brutal shining light dissapears".format(name=self.name, spell=spell_name.upper())) self.lumos = False - elif self.active_spell is spell_rennervate: + elif self.active_spell is spells["Rennervate"]: if opponent.stunned_rounds == 0: print("- {name} casts {spell} with no effect, because {name_o} is not currently stunned!".format(name=self.name, spell=spell_name, name_o=opponent.name)) else: print("- {name} casts {spell}! {name_o} is no longer stunned!".format(name=self.name, spell=spell_name, name_o=opponent.name)) opponent.stunned_rounds = 0 - elif self.active_spell is spell_igni: + elif self.active_spell is spells["Igni"]: print("- Geralt casts Igni. Wait. Geralt? White Wolf? The butcher of Blaviken? What is he doing here? Is he even here? What is going on?") else: print(" [debug]{name} casted SPELL_TYPE_USELESS {spell}. Behaviour unscripted!".format(name=self.name, spell=spell_name)) elif self.active_spell.type == SPELL_TYPE_DEFENSE: spell_succes = True - if self.active_spell is spell_finite_incantatem: + if self.active_spell is spells["Finite Incantatem"]: if self.stunned_rounds > 0: if not 10 > random.random() * 100: spell_succes = False @@ -125,42 +129,42 @@ class Player: print("- {name} casts {spell}. {name_o} is looking confused. What spell did {name} try to cancel?".format(name=self.name, spell=spell_name, name_o=opponent.name)) elif not spell_succes: print("- {name}: (nothing, still stunned)".format(name=self.name)) - elif self.active_spell is spell_impendimenta: - if opponent.active_spell is spell_protego and opponent.active_spell_succes: + elif self.active_spell is spells["Impendimenta"]: + if opponent.active_spell is spells["Protego"] and opponent.active_spell_succes: print("- {name} casts {spell} on {name_o}. FAILURE! {name_o} blocks the attack!".format(name=self.name, name_o=opponent.name, spell=spell_name)) else: print("- {name} casts {spell} on {name_o}. {name_o} is slowed and their next offensive move will have a 33% slower spell speed!".format(name=self.name, name_o=opponent.name, spell=spell_name)) opponent.decreased_spell_speed = True - elif self.active_spell is spell_lumos_solem: + elif self.active_spell is spells["Lumos Solem"]: # Light still goes through the shield, therefor always succeed blinding attempts print("- {name} casts {spell} on {name_o}. {name_o} is blinded and their next offensive move will have 33% less damage!".format(name=self.name, name_o=opponent.name, spell=spell_name)) opponent.decreased_spell_damage = True - elif self.active_spell is spell_protego and self.active_spell_succes: + elif self.active_spell is spells["Protego"] and self.active_spell_succes: print("- {name} casts {spell}".format(name=self.name, spell=spell_name)) else: print(" [debug]{name} casted SPELL_TYPE_DEFENSE {spell}. Behaviour unscripted!".format(name=self.name, spell=spell_name)) elif self.active_spell.type == SPELL_TYPE_UNFORGIVABLE: - if self.active_spell is spell_avada_kedavra: + if self.active_spell is spells["Avada Kedavra"]: print("- THE NERVE! {name} casts the killing curse! See you in the after life {name_o}!".format(name=self.name, name_o=opponent.name)) opponent.health = 0 - elif self.active_spell is spell_crucio: + elif self.active_spell is spells["Crucio"]: print("- THE NERVE! {name} casts the torture curse. {name_o} is greatly hurt and falls to the ground. They are stunned for 5 (more) moves".format(name=self.name, name_o=opponent.name)) opponent.add_stunned_rounds(5) opponent.take_health(self.active_spell.damage) - elif self.active_spell is spell_imperio: + elif self.active_spell is spells["Imperio"]: print("- THE NERVE! {name} casts the Imperius curse. \"Why don't you take a nice nap for {max_stunned_rounds} moves, {name_o}?\". {name_o} submits with pleasure".format(name=self.name, name_o=opponent.name, max_stunned_rounds=MAX_STUNNED_ROUNDS)) opponent.add_stunned_rounds(MAX_STUNNED_ROUNDS) else: - if self.active_spell is spell_mimblewimble: - if opponent.active_spell is spell_protego and opponent.active_spell_succes: + if self.active_spell is spells["Mimblewimble"]: + if opponent.active_spell is spells["Protego"] and opponent.active_spell_succes: print("- {name} casts {spell} on {name_o}. FAILURE! {name_o} blocks the attack!".format(name=self.name, spell=spell_name, name_o=opponent.name)) else: print("- {name} casts {spell} on {name_o}. {name_o}'s tongue is tied in a knot. That's annoying! {name_o} is silenced for 1 (more) move".format(name=self.name, spell=spell_name, name_o=opponent.name)) opponent.add_stunned_rounds(-self.active_spell.damage) - elif self.active_spell is spell_silencio: - if opponent.active_spell is spell_protego and opponent.active_spell_succes: + elif self.active_spell is spells["Silencio"]: + if opponent.active_spell is spells["Protego"] and opponent.active_spell_succes: print("- {name} casts {spell} on {name_o}. FAILURE! {name_o} blocks the attack!".format(name=self.name, spell=spell_name, name_o=opponent.name)) else: if opponent.stunned_rounds == 0: @@ -181,8 +185,8 @@ class Player: total_damage = self.active_spell.damage * self.wand.damage * damage_modifier - damage_penalty if total_damage < 0: total_damage = 0 - if opponent.active_spell is spell_protego and opponent.active_spell_succes: + if opponent.active_spell is spells["Protego"] and opponent.active_spell_succes: print("- {name} casts {spell} on {name_o}. FAILURE! {name_o} blocks the attack!".format(name=self.name, spell=spell_name, name_o=opponent.name)) else: print("- {name} casts {spell} on {name_o} causing {dmg} damage!".format(name=self.name, spell=spell_name, name_o=opponent.name, dmg=total_damage)) - opponent.take_health(total_damage) \ No newline at end of file + opponent.take_health(total_damage) \ No newline at end of file diff --git a/spells.py b/spells.py index 87033de..ec3d534 100644 --- a/spells.py +++ b/spells.py @@ -18,7 +18,7 @@ MAX_LEVENSHTEIN_DISTANCE = 3 if MAX_LEVENSHTEIN_DISTANCE > 0: from Levenshtein import distance -__INVALID_SPELL = ".@wizardduel@__spell_invalid__" #Internal usage only +_INVALID_SPELL = ".@wizardduel@__spell_invalid__" #Internal usage only ## ## Spell class @@ -39,7 +39,6 @@ class Spell: - SPELL_TYPE_UNFORGIVABLE: deals alot of damage or takes away alot of moves from opponent """ def __init__(self, speed: int, damage: int, succes_rate: int, description: str, type: int = SPELL_TYPE_COMMON): - self.speed = speed self.damage = damage self.succes_rate = succes_rate @@ -55,10 +54,13 @@ class Spell: def chance_heal_fully_succes(self): return self.type == SPELL_TYPE_DEFENSE and CHANCE_HEAL_FULLY > random.random() * 100 + def get_spell_name(self): + return str(list(i for i in spells if spells[i] == self)).strip("[]'") + ## ## Spells ## -spell = { +spells = { # Useless spells - These don't do anything useful in combat "Lumos": Spell(100, 000, 100, "Creates a small light at the tip of your wand", SPELL_TYPE_USELESS), "Nox": Spell(100, 000, 100, "Counter spell of Lumos", SPELL_TYPE_USELESS), @@ -89,7 +91,7 @@ spell = { "Imperio": Spell(999, -1, 3, "Muddle with your opponent's mind, convincing them to stop casting spells for 10 moves", SPELL_TYPE_UNFORGIVABLE), # Internal usage - __INVALID_SPELL: Spell(0, 0, 0, "(internal) invalid spell", SPELL_TYPE_NONE) + _INVALID_SPELL: Spell(0, 0, 0, "(internal) invalid spell", SPELL_TYPE_NONE) } ## @@ -99,9 +101,9 @@ def random_combat_spell(): return random.choice([i for i in spell.items() if i[1].type == SPELL_TYPE_COMMON]) # note: returns tuple ('spell_name', spell_obj) def find_spell_by_name(input: str): # Returns a multidimensional tuple: ( ('spell_name', spell_object), levenshtein_distance ) - ret = (input, spell.get(input.title(), spell[__INVALID_SPELL])) + ret = (input, spell.get(input.title(), spell[_INVALID_SPELL])) dist = 0 - if ret[1] == spell[__INVALID_SPELL] and MAX_LEVENSHTEIN_DISTANCE > 0: + if ret[1] == spell[_INVALID_SPELL] and MAX_LEVENSHTEIN_DISTANCE > 0: for i in spell.items(): dist = distance(i[0].title(), input.title()) if dist <= MAX_LEVENSHTEIN_DISTANCE: