web2019/static/js/shop/count_resources.js

67 lines
1.9 KiB
JavaScript
Raw 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.slotOccupied;
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.slotOccupied || 0;
}
const count_item_occupied_clock = (item) => {
return (item.options_data && item.options_data.ext_clk === true) ? 0 : (item.clockOccupied || 0);
}
const count_item_occupied_idc = (item) => {
return item.idcOccupied || 0;
}
export const item_occupied_counters = {
"eem": count_item_occupied_eem,
"clk": count_item_occupied_clock,
"idc": count_item_occupied_idc,
}
function CounterFactory(name) {
return (data, index) => {
let count = 0;
for (let i = index + 1; i < data.length; i++) {
if (data[i].resources && !!data[i].resources.find((value, _i) => value.name === name)) break;
count += item_occupied_counters[name](data[i]);
}
return count;
}
}
const resource_counters = {
"eem": CounterFactory("eem"),
"clk": CounterFactory("clk"),
"idc": CounterFactory("idc")
}
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) {
return data.map((element, index) => {
element.counted_resources = CountResources(data, index);
return element;
})
}