web2019/static/js/shop/count_resources.js

82 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

const count_item_occupied_eem = (item) => {
if (!item.options_data
|| item.options_data.ext_pwr === false
|| item.options_data.mono_eem === false
)
return (item.consumes && item.consumes.eem) || 0;
else if (item.options_data.ext_pwr === true)
return 0;
else if (item.options_data.mono_eem === true || item.options_data.n_eem === "1 EEM")
return 1;
else if (item.options_data.n_eem === "3 EEM")
return 3;
return (item.consumes && item.consumes.eem) || 0;
}
const count_item_occupied_clock = (item) => {
return (item.options_data && (item.options_data.ext_clk === true || (item.options_data.ext_clk && item.options_data.ext_clk.checked === true)) ) ? 0 : ((item.consumes && item.consumes.clk) || 0);
}
const count_item_occupied_idc = (item) => {
return (item.consumes && item.consumes.idc) || 0;
}
const count_item_occupied_hp = (item) => {
return (item.consumes && item.consumes.hp) || 0;
}
const count_item_occupied_tec = (item) => {
return (item.options_data && item.options_data.tec === false) ? 0 : (item.consumes && item.consumes.tec);
}
export const item_occupied_counters = {
"eem": count_item_occupied_eem,
"clk": count_item_occupied_clock,
"idc": count_item_occupied_idc,
"tec": count_item_occupied_tec,
"hp": count_item_occupied_hp,
}
function CounterFactory(name) {
return (data, index) => {
let count = 0;
for (let i = index + 1; i < data.length; i++) {
count += item_occupied_counters[name](data[i]);
if (data[i].resources && !!data[i].resources.find((value, _i) => value.name === name)) break;
}
return count;
}
}
export const resource_counters = {
"eem": CounterFactory("eem"),
"clk": CounterFactory("clk"),
"idc": CounterFactory("idc"),
"hp": CounterFactory("hp"),
"tec": CounterFactory("tec"),
}
export function CountResources(data, index) {
if (!data[index].resources) return null;
let result = [];
data[index].resources.forEach((item, _) => {
if (resource_counters[item.name]) result.push({
name: item.name,
occupied: resource_counters[item.name](data, index),
max: item.max
});
});
return result;
}
export function FillResources(data, disabled) {
return data.map((element, index) => {
element.counted_resources = disabled ? [] : CountResources(data, index);
return element;
})
}
export function hp_to_slots(hp) {
return Math.trunc(hp / 4);
}