70 lines
1.8 KiB
JavaScript
70 lines
1.8 KiB
JavaScript
const DoubleLinkedList = require("../Utility/DoubleLinkedList");
|
|
|
|
const constants = require("../Data/constants");
|
|
|
|
const { TIMING_EVENTS } = constants;
|
|
const { TIMING_EVENT_RESPAWN } = TIMING_EVENTS;
|
|
|
|
const Game = require("./Game");
|
|
const { guilds } = Game;
|
|
|
|
class GuildIsland {
|
|
constructor(info) {
|
|
this.id = info.id;
|
|
this.locationMapID = info.locationMapID;
|
|
this.index = info.index;
|
|
this.position = info.position;
|
|
this.guild = info.guild;
|
|
this.towers = [];
|
|
this.totalDamageReceived = new DoubleLinkedList();
|
|
};
|
|
|
|
updateReceivedDamage(guildID, damage) {
|
|
this.totalDamageReceived.accumulateItem(guildID, damage);
|
|
}
|
|
|
|
getTower(id) {
|
|
const tower = this.towers.find(t => t.id === id);
|
|
return tower;
|
|
}
|
|
|
|
addTower(tower) {
|
|
this.towers.push(tower);
|
|
}
|
|
|
|
updateOwnership() {
|
|
const towersAlive = this.towers.filter(t => t.hp.current);
|
|
if (towersAlive.length) return null;
|
|
|
|
let maximumDamage = 0;
|
|
let newGuildID = 0;
|
|
|
|
this.totalDamageReceived.clearEach((guildID, damage) => {
|
|
if (damage > maximumDamage) {
|
|
maximumDamage = damage;
|
|
newGuildID = guildID;
|
|
};
|
|
});
|
|
|
|
this.towers.forEach(tower => tower.setEvent(TIMING_EVENT_RESPAWN, 10000));
|
|
|
|
const topicOld = this.guild.topic;
|
|
|
|
const guildOld = guilds[this.guild.id];
|
|
const guildNew = newGuildID ? guilds[newGuildID] : { tag: new ArrayBuffer(0), id: 0 };
|
|
|
|
const topicNew = guildNew.topic;
|
|
|
|
this.guild = guildNew;
|
|
|
|
return {
|
|
guildOld,
|
|
guildNew,
|
|
topicOld,
|
|
topicNew
|
|
};
|
|
}
|
|
};
|
|
|
|
module.exports = GuildIsland;
|