39 lines
1.2 KiB
JavaScript
39 lines
1.2 KiB
JavaScript
class AdmiralCounter {
|
|
constructor(configuration) {
|
|
this.entities = Object.entries(configuration);
|
|
this.admiralCounters = {};
|
|
}
|
|
|
|
increaseCounter(ids) {
|
|
const { typeID, parentEntityTypeID } = ids;
|
|
const id = `${typeID}_${parentEntityTypeID}`;
|
|
|
|
if (!this.admiralCounters[id]) this.admiralCounters[id] = { childKilledCount: 1, admiralAliveCount: 0 };
|
|
else this.admiralCounters[id].childKilledCount++;
|
|
|
|
const thresholds = this.entities.find(e => parseInt(e[0]) === typeID);
|
|
if (!thresholds) return;
|
|
|
|
const threshold = thresholds[1][parentEntityTypeID];
|
|
if (!threshold) return;
|
|
|
|
const counter = this.admiralCounters[id];
|
|
if (counter.childKilledCount === threshold.childToBeKilledCount) {
|
|
counter.childKilledCount = 0;
|
|
|
|
if (counter.admiralAliveCount < threshold.maximumAdmiralCount) {
|
|
counter.admiralAliveCount++;
|
|
return true;
|
|
};
|
|
};
|
|
|
|
return false;
|
|
}
|
|
|
|
descreaseAdmiralCount(id) {
|
|
const counter = this.admiralCounters[id];
|
|
if (counter) counter.admiralAliveCount--;
|
|
}
|
|
};
|
|
|
|
module.exports = AdmiralCounter; |