for part in input_parts:
if "Atk" in part:
var part_words = part.split(" ")
var attack = {}
var is_attack = false
for i in range(part_words.size()):
if part_words[i] == "Atk" or part_words[i] == "Full":
is_attack = true
continue
if is_attack:
if part_words[i] == "melee":
attack["range"] = 5
attack["attack bonus"] = int(part_words[i - 1])
attack["damage"] = part_words[i + 1].replace("(", "").replace(",", "")
attack["type"] = part_words[i + 4].replace(",", "").replace(")", "")
attack["effects"] = [part_words[i + 2].replace(",", "")]
break
if attack:
attacks.append(attack)
print(attacks)
var ghoul_data = "Ghoul: CR 1; Medium undead; HD 2d12; hp 13; Init +2; Spd 30 ft. (6 squares); AC 14 (+2 dex, +2 natural), touch 12, flat-footed 12; Base Atk +1; Grp +2; Atk +2 melee (1d6+1 plus paralysis, bite); Full Atk +2 melee (1d6+1 plus paralysis, bite) and +0 melee (1d3 plus paralysis, 2 claws); Space/Reach 5 ft./5 ft.; SA Ghoul fever, paralysis; SQ Darkvision 60 ft., undead traits, +2 turn resistance; AL CE; SV Fort +0, Ref +2, Will +5; Str 13, Dex 15, Con -, Int 13, Wis 14, Cha 12"
var attacks = parse_attacks(ghoul_data)
The if condition if part_words[i] == "melee" should be if part_words[i + 1] == "melee".
The if condition if is_attack: should be if is_attack:.
The code doesn't handle the case when the attack type is not "melee".
The if condition for attack type should be if part_words[i + 4].replace(",", "").replace(")", "") instead of if part_words[i + 4].replace(",", "").replace(")", "") because the attack type comes after 4 words from the word "melee".
The code doesn't handle the case when the attack has multiple effects.
The corrected code:
code
func parse_attacks(data: String):
var input = data
var input_parts = input.split(";")
var attacks = []
for part in input_parts:
if "Atk" in part:
var part_words = part.split(" ")
var attack = {}
var is_attack = false
for i in range(part_words.size()):
if part_words[i] == "Atk" or part_words[i] == "Full":
is_attack = true
continue
if is_attack:
if part_words[i + 1] == "melee":
attack["range"] = 5
attack["attack bonus"] = int(part_words[i - 1])
attack["damage"] = part_words[i + 2].replace("(", "").replace(",", "")
attack["type"] = part_words[i + 4].replace(",", "").replace(")", "")
attack["effects"] = [part_words[i + 3].replace(",", "")]
elif part_words[i + 1] == "ranged":
attack["range"] = part_words[i + 2].replace(" ft.", "")
attack["attack bonus"] = int(part_words[i - 1])
attack["damage"] = part_words[i + 4].replace("(", "").replace(",", "")
attack["type"] = part_words[i + 6].replace(",", "").replace(")", "")
attack["effects"] = [part_words[i + 5].replace(",", "")]
break
if attack:
attacks.append(attack)
print(attacks)
var ghoul_data = "Ghoul: CR 1; Medium undead; HD 2d12; hp 13; Init +2; Spd 30 ft. (6 squares); AC 14 (+2 dex, +2 natural), touch 12, flat-footed 12; Base Atk +1; Grp +2; Atk +2 melee (1d6+1 plus paralysis, bite); Full Atk +2 melee (1d6+1 plus paralysis, bite) and +0 melee (1d3 plus paralysis, 2 claws); Space/Reach 5 ft./5 ft.; SA Ghoul fever, paralysis; SQ Darkvision 60 ft., undead traits, +2 turn resistance; AL CE; SV Fort +0, Ref +2, Will +5; Str 13, Dex 15, Con -, Int 13, Wis 14, Cha 12"
var attacks = parse_attacks(ghoul_data)
This code should correctly parse the ghoul data and produce the desired output.