bptf-listing-parser
Version:
Parse bptf listings into readable objects containing spells, paints and parts.
56 lines (45 loc) • 1.31 kB
text/typescript
import parts from './lib/parts';
import spells from './lib/spells';
import paints from './lib/paints';
export interface ParseResult {
paint: string;
spells: string[];
parts: string[];
}
export interface Listing {
item: {
attributes: Attributes[];
};
}
export type Attributes = {
float_value?: number;
defindex: number;
value?: number | string;
};
export const parseListing = (listing: Listing): ParseResult => {
const parsed: ParseResult = {
parts: [],
paint: '',
spells: [],
};
if (!listing.item.attributes) return parsed;
for (let i = 0; i < listing.item.attributes.length; i++) {
const attribute = listing.item.attributes[i];
if (!attribute.defindex) continue;
if (attribute.defindex == 142) {
parsed.paint = paints[attribute.float_value];
} else if ([380, 382, 384].includes(attribute.defindex)) {
parsed.parts.push(parts[attribute.float_value]);
} else if (spells[attribute.defindex]) {
parsed.spells.push(
spells[attribute.defindex][attribute.float_value]
);
}
}
return parsed;
};
export const data = {
parts,
spells,
paints,
};