1
0

Make spells.py compatible with the new dictionary. Also: only import distance from Levenshtein if MAX_LEVENSHTEIN_DISTANCE > 0

This commit is contained in:
Kwarde 2024-01-17 16:50:38 +01:00
parent c138250c1c
commit 355eab297c

View File

@ -1,5 +1,4 @@
import random import random
from Levenshtein import distance
SPELL_TYPE_NONE = -1 SPELL_TYPE_NONE = -1
SPELL_TYPE_USELESS = 0 SPELL_TYPE_USELESS = 0
@ -16,6 +15,9 @@ CHANCE_HEAL_FULLY = 5
#Set to 0 to disable #Set to 0 to disable
MAX_LEVENSHTEIN_DISTANCE = 3 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
## ##
@ -45,7 +47,7 @@ class Spell:
self.type = type self.type = type
def __repr__(self): def __repr__(self):
return " ['{spell}']\n\t{desc}\n\tSUCCES RATE: {srate}%\tSPEED: {speed}\tDAMAGE: {dmg}".format(spell=self.name, type=type, desc=self.description, srate=self.succes_rate, speed=self.speed, dmg=self.damage) return "\n\t{desc}\n\tSUCCES RATE: {srate}%\tSPEED: {speed}\tDAMAGE: {dmg}".format(type=type, desc=self.description, srate=self.succes_rate, speed=self.speed, dmg=self.damage)
def chance_heal_partly_succes(self): def chance_heal_partly_succes(self):
return self.type == SPELL_TYPE_DEFENSE and CHANCE_HEAL_PARTLY > random.random() * 100 return self.type == SPELL_TYPE_DEFENSE and CHANCE_HEAL_PARTLY > random.random() * 100
@ -94,18 +96,18 @@ spell = {
## Standalone spell functions ## Standalone spell functions
## ##
def random_combat_spell(): def random_combat_spell():
return random.choice([i for i in Spell.spellList if i.type == SPELL_TYPE_COMMON]) 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 list with: [spell_object, levenshtein_distance]. If distance is greater than 0 (typos were made), damage goes down def find_spell_by_name(input: str): # Returns a multidimensional tuple: ( ('spell_name', spell_object), levenshtein_distance )
for i in Spell.spellList: ret = (input, spell.get(input.title(), spell[__INVALID_SPELL]))
if input.title() == i.name.title(): dist = 0
return [i, 0] if ret[1] == spell[__INVALID_SPELL] and MAX_LEVENSHTEIN_DISTANCE > 0:
else: for i in spell.items():
if MAX_LEVENSHTEIN_DISTANCE > 0: dist = distance(i[0].title(), input.title())
dist = distance(i.name.title(), input.title()) if dist <= MAX_LEVENSHTEIN_DISTANCE:
if dist < MAX_LEVENSHTEIN_DISTANCE: ret = i
return [i, dist] break
return [spell_object_none, 0] return (ret, dist)
def print_spells(): def print_spells():
header_spells_useless = "== USELESS SPELLS ==" header_spells_useless = "== USELESS SPELLS =="
@ -114,24 +116,29 @@ def print_spells():
header_spells_powerful = "== POWERFUL COMBAT SPELLS ==" header_spells_powerful = "== POWERFUL COMBAT SPELLS =="
header_spells_unforgivable = "== UNFORGIVABLE CURSES ==" header_spells_unforgivable = "== UNFORGIVABLE CURSES =="
for i in Spell.spellList: for i in spell.items():
if i.type == SPELL_TYPE_UNFORGIVABLE or i.type == SPELL_TYPE_USELESS or i.type == SPELL_TYPE_NONE: if i[1].type == SPELL_TYPE_UNFORGIVABLE or i[1].type == SPELL_TYPE_USELESS or i[1].type == SPELL_TYPE_NONE:
continue continue
if i.type == SPELL_TYPE_USELESS and header_spells_useless: if i[1].type == SPELL_TYPE_USELESS and header_spells_useless:
print("\n"+header_spells_useless) print("\n"+header_spells_useless)
header_spells_useless = "" header_spells_useless = ""
elif i.type == SPELL_TYPE_DEFENSE and header_spells_defensive: elif i[1].type == SPELL_TYPE_DEFENSE and header_spells_defensive:
print("\n"+header_spells_defensive) print("\n"+header_spells_defensive)
header_spells_defensive = "" header_spells_defensive = ""
elif i.type == SPELL_TYPE_COMMON and header_spells_common: elif i[1].type == SPELL_TYPE_COMMON and header_spells_common:
print("\n"+header_spells_common) print("\n"+header_spells_common)
header_spells_common = "" header_spells_common = ""
elif i.type == SPELL_TYPE_POWERFUL and header_spells_powerful: elif i[1].type == SPELL_TYPE_POWERFUL and header_spells_powerful:
print("\n"+header_spells_powerful) print("\n"+header_spells_powerful)
header_spells_powerful = "" header_spells_powerful = ""
elif i.type == SPELL_TYPE_UNFORGIVABLE and header_spells_unforgivable: elif i[1].type == SPELL_TYPE_UNFORGIVABLE and header_spells_unforgivable:
print("\n"+header_spells_unforgivable) print("\n"+header_spells_unforgivable)
header_spells_unforgivable = "" header_spells_unforgivable = ""
print(i) print(
str(i)
.strip('(')
.strip(')')
.replace(',', ':', 1)
)