web2019/static/js/shop/count_resources.js

71 lines
2.0 KiB
JavaScript
Raw Normal View History

export 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;
}
export const count_item_occupied_clock = (item) => {
return (item.options_data && item.options_data.ext_clk === true) ? 0 : (item.clockOccupied || 0);
}
export const count_item_occupied_idc = (item) => {
return item.idcOccupied || 0;
}
function EEMCounter(data, index) {
let count = 0;
for (let i = index + 1; i < data.length; i++) {
if (!!data[i].resources.find((value, _i) => value.name === "eem")) break;
count += count_item_occupied_eem(data[i]);
}
return count;
}
function ClockCounter(data, index) {
let count = 0;
for (let i = index + 1; i < data.length; i++) {
if (!!data[i].resources.find((value, _i) => value.name === "clk")) break;
count += count_item_occupied_clock(data[i]);
}
return count;
}
function IDCCounter(data, index) {
let count = 0;
for (let i = index + 1; i < data.length; i++) {
if (!!data[i].resources.find((value, _i) => value.name === "idc")) break;
count += count_item_occupied_idc(data[i]);
}
return count;
}
const resource_counters = {
"eem": EEMCounter,
"clk": ClockCounter,
"idc": IDCCounter
}
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;
}