UNPKG

poe-item-parser

Version:
384 lines (383 loc) 12.8 kB
var d = Object.defineProperty; var p = (u, e, t) => e in u ? d(u, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : u[e] = t; var a = (u, e, t) => p(u, typeof e != "symbol" ? e + "" : e, t); const i = { ITEM_CLASS: /Item Class: (.*)/, ITEM_RARITY: /Rarity: (.*)/, ITEM_LEVEL: /Item Level: (.*)/, REQUIREMENT_LEVEL: /^Level: (.*)/m, REQUIREMENT_STR: /(?:Str|Strength): (\d+)/, REQUIREMENT_DEX: /(?:Dex|Dexterity): (\d+)/, REQUIREMENT_INT: /(?:Int|Intelligence): (\d+)/, SOCKETS: /Sockets: (.*)/, QUALITY: /Quality(?: \(([^)]+)\))?: \+(\d+)%(?: \(augmented\))?/, BLOCK_CHANCE: /Block chance: (\d+)%/, ENERGY_SHIELD: /Energy Shield: (\d+)/, SPIRIT: /Spirit: (\d+)/, EVASION_RATING: /Evasion Rating: (\d+)/, ARMOUR: /Armour: (\d+)/, RUNES: /(.*) \(rune\)/g, IMPLICIT: /(.*) \(implicit\)/g, ENCHANT: /(.*) \(enchant\)/g, CHARM_SLOTS: /Charm Slots: (\d+)/, ATTACKS_PER_SECOND: /Attacks per Second: (\d+(\.\d+)?)(?: \(augmented\))?/, CRITICAL_HIT_CHANCE: /Critical Hit Chance: (\d+)%?/, RELOAD_TIME: /Reload Time: (\d+(\.\d+)?)(?: \(augmented\))?/, LIMITED_TO: /Limited to: (\d+)/, RADIUS: /Radius: (.*)/, FIRE_DAMAGE_PRESENT: /Fire Damage: (.*)/, COLD_DAMAGE_PRESENT: /Cold Damage: (.*)/, LIGHTNING_DAMAGE_PRESENT: /Lightning Damage: (.*)/, ELEMENTAL_DAMAGE_PRESENT: /Elemental Damage: (.*)/, ELEMENTAL_DAMAGE: /(\d+)-?(\d+)?/g, PHYSICAL_DAMAGE_PRESENT: /Physical Damage: (.*)/, PHYSICAL_DAMAGE: /(\d+)-(\d+)/g, CORRUPTED: /^Corrupted$/m, MIRRORED: /^Mirrored$/m, UNIDENTIFIED: /^Unidentified$/m, LASTS_X_SECONDS: /Lasts (\d+) Seconds/, CHARGE_CONSUMPTION: /Consumes (\d+) of (\d+) Charges on use/, NOTE_PRESENT: /Note: (.*)/, RECOVERS_CHARGES: /(?<first>\d+)(?: \(augmented\))? (?<identifier>\w+) over (?<second>\d+(?:,\d+)?)(?: \(augmented\))?/ }; class l { constructor(e) { a(this, "iLevelIndex", -1); a(this, "iRarityIndex", -1); a(this, "itemClass"); a(this, "corrupted"); a(this, "mirrored"); a(this, "identified"); a(this, "rarity"); a(this, "indexesOfDashes", []); a(this, "hasNote", !1); this.input = e, this.fixIfUnfulfilledRequirements(), this.indexesOfDashes = this.getIndexesOf("--------"), this.hasNote = this.parseItemNote(); } // Some types have some extra text below them hasTutorialText() { var e; return this.itemClass || this.parseItemClass(), this.itemClass && ["Jewels", "Quivers", "Relics"].includes(this.itemClass) || ((e = this.itemClass) == null ? void 0 : e.endsWith("Flasks")); } fixIfUnfulfilledRequirements() { this.input.includes("You cannot use this item. Its stats will be ignored") && (this.input = this.input.replace("You cannot use this item. Its stats will be ignored", "").replace("--------", "")); } parseItemNote() { return !!this.input.match(i.NOTE_PRESENT); } parseItemClass() { const e = this.input.match(i.ITEM_CLASS), t = e ? e[1] : void 0; return this.itemClass = t, t; } parseItemLevel() { const e = this.input.match(i.ITEM_LEVEL); if (!e) return; const t = e[1]; return this.iLevelIndex = this.input.indexOf(e[0]) + e[0].length, Number(t); } parseRarity() { const e = this.input.match(i.ITEM_RARITY); if (!e) return; const t = e[1]; return this.iRarityIndex = this.input.indexOf(e[0]) + e[0].length, this.rarity = t, t; } parseCorrupted() { const e = this.input.match(i.CORRUPTED); return this.corrupted = !!e, !!e; } parseMirrored() { const e = this.input.match(i.MIRRORED); return this.mirrored = !!e, !!e; } parseItemName() { let e = this.iRarityIndex || this.iLevelIndex; e || this.parseRarity(); const t = this.input.indexOf("--", e), r = this.input.slice(e, t); return { name: r.replaceAll(` `, " ").trim(), lines: r.split(` `).filter((s) => s !== "") }; } getIndexesOf(e) { const t = []; let r = 0; for (; r < this.input.length; ) { const s = this.input.indexOf(e, r); if (s === -1) break; t.push(s), r = s + 1; } return t; } parseAffixes() { if (this.identified === void 0 && (this.identified = this.parseIdentified(), !this.identified)) return []; if (this.corrupted === void 0 && (this.corrupted = this.parseCorrupted()), this.mirrored === void 0 && (this.mirrored = this.parseMirrored()), this.rarity || this.parseRarity(), this.itemClass || this.parseItemClass(), this.rarity === "Normal") return []; let e = -1, t = 0; if (this.hasNote && (t += 1), this.hasTutorialText() && (t += 1), this.rarity === "Unique") this.corrupted ? e = this.indexesOfDashes[this.indexesOfDashes.length - 3 - t] : e = this.indexesOfDashes[this.indexesOfDashes.length - 2 - t]; else if (!this.corrupted && !this.mirrored) e = this.indexesOfDashes[this.indexesOfDashes.length - 1 - t]; else { let n = 0; this.corrupted && (n += 1), this.mirrored && (n += 1), e = this.indexesOfDashes[this.indexesOfDashes.length - 1 - t - n]; } const r = this.input.indexOf(` `, e); let s = this.input.indexOf("--", r); return s === -1 && (s = this.input.length), this.input.slice(r, s).split(` `).filter((n) => n !== "").map((n) => this.parseAffix(n)); } parseAffix(e) { let t = e.indexOf("["); for (; t !== -1; ) { const r = e.indexOf("]", t + 1); if (r === -1) return e; const s = e.slice(t + 1, r), h = s.split("|"); e = e.replace(`[${s}]`, h[0]), t = e.indexOf("[", t + 1); } return e; } parseIntelligenceRequirement() { const e = this.input.match(i.REQUIREMENT_INT); return e ? Number(e[1]) : void 0; } parseStrengthRequirement() { const e = this.input.match(i.REQUIREMENT_STR); return e ? Number(e[1]) : void 0; } parseDexterityRequirement() { const e = this.input.match(i.REQUIREMENT_DEX); return e ? Number(e[1]) : void 0; } parseLevelRequirement() { const e = this.input.match(i.REQUIREMENT_LEVEL); return e ? Number(e[1]) : void 0; } parseSockets() { const e = this.input.match(i.SOCKETS), t = e ? e[1] : void 0; return t ? t.trim().split(" ").length : 0; } parseRunes() { const e = this.input.matchAll(i.RUNES); return Array.from(e).map((t) => t[1]); } parseImplicits() { const e = this.input.matchAll(i.IMPLICIT); return Array.from(e).map((t) => t[1]); } parseQuality() { const e = i.QUALITY.exec(this.input); if (e) return Number(e[2]); } parseQualityType() { const e = i.QUALITY.exec(this.input); return e ? e[1] : void 0; } parseEnergyShield() { const e = this.input.match(i.ENERGY_SHIELD); return e ? Number(e[1]) : void 0; } parseEvasionRating() { const e = this.input.match(i.EVASION_RATING); return e ? Number(e[1]) : void 0; } parseArmour() { const e = this.input.match(i.ARMOUR); return e ? Number(e[1]) : void 0; } parseCharmSlots() { const e = this.input.match(i.CHARM_SLOTS); return e ? Number(e[1]) : void 0; } parseAttacksPerSecond() { const e = this.input.match(i.ATTACKS_PER_SECOND); return e ? Number(e[1]) : void 0; } parseCriticalHitChance() { const e = this.input.match(i.CRITICAL_HIT_CHANCE); return e ? Number(e[1]) : void 0; } parseReloadTime() { const e = this.input.match(i.RELOAD_TIME); return e ? Number(e[1]) : void 0; } parseLimitedTo() { const e = this.input.match(i.LIMITED_TO); return e ? Number(e[1]) : void 0; } parseRadius() { const e = this.input.match(i.RADIUS); return e ? e[1] : void 0; } parseElementalDamage() { const e = this.input.match(i.ELEMENTAL_DAMAGE_PRESENT); if (!e) return; const r = e[1].matchAll(i.ELEMENTAL_DAMAGE); return Array.from(r).map((s) => ({ min: Number(s[1]), max: s[2] ? Number(s[2]) : void 0 })); } parseFireDamage() { const e = this.input.match(i.FIRE_DAMAGE_PRESENT); if (!e) return; const r = e[1].matchAll(i.ELEMENTAL_DAMAGE); return Array.from(r).map((s) => ({ min: Number(s[1]), max: Number(s[2]) })); } parseColdDamage() { const e = this.input.match(i.COLD_DAMAGE_PRESENT); if (!e) return; const r = e[1].matchAll(i.ELEMENTAL_DAMAGE); return Array.from(r).map((s) => ({ min: Number(s[1]), max: Number(s[2]) })); } parseLightningDamage() { const e = this.input.match(i.LIGHTNING_DAMAGE_PRESENT); if (!e) return; const r = e[1].matchAll(i.ELEMENTAL_DAMAGE); return Array.from(r).map((s) => ({ min: Number(s[1]), max: Number(s[2]) })); } parsePhysicalDamage() { const e = this.input.match(i.PHYSICAL_DAMAGE_PRESENT); if (!e) return; const r = e[1].matchAll(i.PHYSICAL_DAMAGE); return Array.from(r).map((s) => ({ min: Number(s[1]), max: Number(s[2]) })); } parseFlavorText() { if (this.corrupted || (this.corrupted = this.parseCorrupted()), this.parseRarity() !== "Unique") return; this.itemClass || this.parseItemClass(); const t = this.getIndexesOf("--------"); let r = -1, s = 0; this.hasNote && (s += 1), this.hasTutorialText() && (s += 1), this.corrupted ? r = t[t.length - 2 - s] : r = t[t.length - 1 - s]; const h = this.input.indexOf(` `, r); let n = this.input.indexOf("--", h); n === -1 && (n = this.input.length); const m = this.input.slice(h, n).trim(); return { flavorText: m.replace(` `, " ").trim(), lines: m.split(` `).filter((c) => c !== "") }; } parseDuration() { const e = this.input.match(i.LASTS_X_SECONDS); return e ? Number(e[1]) : void 0; } parseChargeConsumption() { const e = this.input.match(i.CHARGE_CONSUMPTION); return e ? { consumes: Number(e[1]), max: Number(e[2]) } : void 0; } parseEnchants() { const e = this.input.matchAll(i.ENCHANT); return Array.from(e).map((t) => t[1]); } parseBlockChance() { const e = this.input.match(i.BLOCK_CHANCE); return e ? Number(e[1]) : void 0; } parseIdentified() { return !this.input.match(i.UNIDENTIFIED); } parseSpirit() { const e = this.input.match(i.SPIRIT); return e ? Number(e[1]) : void 0; } // Dirty, but does the job! parseFlaskRecovery() { var r, s, h, n, m; const e = this.input.match(i.RECOVERS_CHARGES); if (!e) return; const t = { over: Number((r = e.groups) == null ? void 0 : r.second.replace(",", ".")) }; switch ((s = e.groups) == null ? void 0 : s.identifier) { case "Mana": t.mana = Number((h = e.groups) == null ? void 0 : h.first); break; case "Life": t.life = Number((n = e.groups) == null ? void 0 : n.first); break; case "Energy Shield": t.energyShield = Number((m = e.groups) == null ? void 0 : m.first); break; default: return; } return t; } getItem() { return { itemClass: this.parseItemClass(), itemRarity: this.parseRarity(), itemLevel: this.parseItemLevel(), affixes: this.parseAffixes(), corrupted: this.parseCorrupted(), flavorText: this.parseFlavorText(), itemName: this.parseItemName(), requirements: { intelligence: this.parseIntelligenceRequirement(), strength: this.parseStrengthRequirement(), dexterity: this.parseDexterityRequirement(), level: this.parseLevelRequirement() }, sockets: this.parseSockets(), runes: this.parseRunes(), implicits: this.parseImplicits(), quality: this.parseQuality(), qualityType: this.parseQualityType(), stats: { energyShield: this.parseEnergyShield(), evasionRating: this.parseEvasionRating(), armour: this.parseArmour(), spirit: this.parseSpirit() }, charmSlots: this.parseCharmSlots(), attacksPerSecond: this.parseAttacksPerSecond(), criticalHitChance: this.parseCriticalHitChance(), reloadTime: this.parseReloadTime(), limitedTo: this.parseLimitedTo(), radius: this.parseRadius(), elementalDamage: this.parseElementalDamage(), fireDamage: this.parseFireDamage(), coldDamage: this.parseColdDamage(), lightningDamage: this.parseLightningDamage(), physicalDamage: this.parsePhysicalDamage(), duration: this.parseDuration(), charges: this.parseChargeConsumption(), enchants: this.parseEnchants(), blockChance: this.parseBlockChance(), identified: this.parseIdentified(), flaskRecovery: this.parseFlaskRecovery(), mirrored: this.parseMirrored() }; } } export { l as PoE2ItemParser };