Skip to content

Commit

Permalink
Extract equipment parser
Browse files Browse the repository at this point in the history
  • Loading branch information
tiliv committed May 20, 2024
1 parent 8a3cb2d commit 914c8cb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
48 changes: 10 additions & 38 deletions src/actions/Buy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { price, EQUIPMENT, EQUIPMENT_ORDER } from '../utils';
import { price, EQUIPMENT, groupEquipment, EQUIPMENT_ORDER } from '../utils';

export function parse(target, action) {
return {
Expand All @@ -8,48 +8,20 @@ export function parse(target, action) {
}

function makeItems(target, { text }, { inventory }) {
const entries = text.split('\n').map((item) => {
const [kind, template, rarity, name, stat, id=null] = item.split('/');
if (inventory[kind]?.find((item) => item.name === name)) {
return null;
}
const statValue = parseInt(stat, 10);
const stats = { [kind === 'weapon' ? 'A' : 'D']: statValue };
return {
name,
kind,
stats,
event: 'Buy.player',
target,
price: -price({ rarity: parseInt(rarity, 10), stats }),
consume: true,
item: {
name,
template,
id: id !== null ? parseInt(id, 10) : null,
rarity: parseInt(rarity, 10),
stats,
},
};
}).filter(Boolean);

// Break into groups by kind
const options = Object.values(entries.reduce((acc, entry) => {
const { kind } = entry;
if (!acc[kind]) {
acc[kind] = { name: EQUIPMENT[kind], _items: [], kind };
}
acc[kind]._items.push(entry);
return acc;
}, {})).sort((a, b) => {
const equipmentGroups = groupEquipment(
text,
{ target, omit: inventory },
{ event: 'Buy.player', consume: true }
).sort((a, b) => {
return EQUIPMENT_ORDER.indexOf(a.kind) - EQUIPMENT_ORDER.indexOf(b.kind);
});

// Turn sub-menu into callable based on built list
options.forEach((option) => {
option.items = ({ inventory }) => option._items.filter(({ name }) => {
equipmentGroups.forEach((option) => {
const originals = option.items;
option.items = ({ inventory }) => originals.filter(({ name }) => {
return !inventory[option.kind]?.find((item) => item.name === name)
});
});
return options;
return equipmentGroups;
};
39 changes: 39 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,42 @@ export async function loadSprite(kind, item, { offsetLeft=0, width, height }, ..
return data;
});
}


export function groupEquipment(source, { target, omit }, extra={}) {
const entries = source.split('\n').map((item) => {
const [kind, template, rarity, name, stat, id=null] = item.split('/');
if (omit[kind]?.find((item) => item.name === name)) {
return null;
}
const statValue = parseInt(stat, 10);
const stats = { [kind === 'weapon' ? 'A' : 'D']: statValue };
return {
name,
kind,
stats,
target,
price: -price({ rarity: parseInt(rarity, 10), stats }),
item: {
name,
template,
id: id !== null ? parseInt(id, 10) : null,
rarity: parseInt(rarity, 10),
stats,
},
...extra,
};
}).filter(Boolean);

// Break into groups by kind
const groups = Object.values(entries.reduce((acc, entry) => {
const { kind } = entry;
if (!acc[kind]) {
acc[kind] = { name: EQUIPMENT[kind], items: [], kind };
}
acc[kind].items.push(entry);
return acc;
}, {}))

return groups;
}

0 comments on commit 914c8cb

Please sign in to comment.