1
0

Implement Levenshtein.distance() in spells.find_spell_by_name()

This commit is contained in:
Kwarde 2024-01-15 13:40:34 +01:00
parent 6a9e3b126a
commit 5f1cdcfc7f

View File

@ -11,6 +11,10 @@ SPELL_TYPE_UNFORGIVABLE = 4
CHANCE_HEAL_PARTLY = 25 CHANCE_HEAL_PARTLY = 25
CHANCE_HEAL_FULLY = 5 CHANCE_HEAL_FULLY = 5
#Maximum Levenshtein distance. Eg if a user casts 'Pritrgo' instead of 'Protego', distance would be 2 and Protego would still be cast if MAX_LEVENSHTEIN_DISTANCE is at least 2
#Set to 0 to disable
MAX_LEVENSHTEIN_DISTANCE = 3
__INVALID_SPELL = ".@wizardduel@__spell_invalid__" #Internal usage only __INVALID_SPELL = ".@wizardduel@__spell_invalid__" #Internal usage only
## ##
@ -92,11 +96,16 @@ spell_none = Spell(__INVALID_SPELL, 0, 0, 0, "(internal) invalid spell", SPELL_T
## ##
## Standalone spell functions ## Standalone spell functions
## ##
def find_spell_by_name(input: str): 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
for i in Spell.spellList: for i in Spell.spellList:
if input.title() == i.name.title(): if input.title() == i.name.title():
return i return [i, 0]
return spell_none else:
if MAX_LEVENSHTEIN_DISTANCE > 0:
dist = distance(i.name.title(), input.title())
if dist < MAX_LEVENSHTEIN_DISTANCE:
return [i, dist]
return [spell_none, 0]
def print_spells(): def print_spells():
header_spells_useless = "== USELESS SPELLS ==" header_spells_useless = "== USELESS SPELLS =="