web2019/static/js/shop/count_resources.js
2024-01-09 10:14:53 +08:00

67 lines
2.0 KiB
JavaScript

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