Fix bug with server crashing when there's over 30k ships in fleet

This commit is contained in:
Aelita4 2024-12-22 18:45:49 +01:00
parent bf54589069
commit db7abbb997
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
1 changed files with 44 additions and 32 deletions

View File

@ -102,7 +102,12 @@ export default class FleetManager {
} else {
switch(this.data.mission) {
case 'ATTACK':
return await this.battleResults([{ id: 'fighter', amount: 100 }]);
if(!("expedition" in this.data.destination)) {
const enemyShips = this.data.destination.ships.ships;
return await this.battleResults(enemyShips.map(ship => { return { id: ship.data.id, amount: ship.amount } }));
} else {
throw new Error("Cannot attack sector.");
}
case 'TRANSPORT':
await (this.data.destination as Planet | SystemManager).resources.updateAmount(this.data.cargo);
await (this.data.destination as Planet | SystemManager).resources.sync();
@ -205,34 +210,40 @@ export default class FleetManager {
});
}
roundLoop: for(let i = 0; i < 3; i++) {
for(const playerShip of playerShipsStructure) {
const enemyShip = enemyShipsStructure[Math.floor(Math.random() * enemyShipsStructure.length)];
if(!enemyShip) break roundLoop;
const typeCount = playerShipsStructure.filter(s => s.id === playerShip.id).length;
const additionalShipAttack = typeCount > 1 ? Math.floor(Math.random() * typeCount) * playerShip.attack : 0;
const playerDamage = Math.max(0, (playerShip.attack + additionalShipAttack) - enemyShip.defense);
enemyShip.hitpoints -= playerDamage;
}
for(const enemyShip of enemyShipsStructure) {
const playerShip = playerShipsStructure[Math.floor(Math.random() * playerShipsStructure.length)];
if(!playerShip) break roundLoop;
const typeCount = enemyShipsStructure.filter(s => s.id === enemyShip.id).length;
const additionalShipAttack = typeCount > 1 ? Math.floor(Math.random() * typeCount) * enemyShip.attack : 0;
const enemyDamage = Math.max(0, (enemyShip.attack + additionalShipAttack) - playerShip.defense);
playerShip.hitpoints -= enemyDamage;
}
if(playerStats.attack > enemyStats.defense * 2 && playerStats.defense > enemyStats.attack * 2) {
enemyShipsStructure.forEach((_, index) => enemyShipsStructure.splice(index, 1));
} else if(enemyStats.attack > playerStats.defense * 2 && enemyStats.defense > playerStats.attack * 2) {
playerShipsStructure.forEach((_, index) => playerShipsStructure.splice(index, 1));
} else {
roundLoop: for(let i = 0; i < 3; i++) {
for(const playerShip of playerShipsStructure) {
const enemyShip = enemyShipsStructure[Math.floor(Math.random() * enemyShipsStructure.length)];
if(!enemyShip) break roundLoop;
const typeCount = playerShipsStructure.filter(s => s.id === playerShip.id).length;
const additionalShipAttack = typeCount > 1 ? Math.floor(Math.random() * typeCount) * playerShip.attack : 0;
const playerDamage = Math.max(0, (playerShip.attack + additionalShipAttack) - enemyShip.defense);
enemyShip.hitpoints -= playerDamage;
}
for(const enemyShip of enemyShipsStructure) {
const playerShip = playerShipsStructure[Math.floor(Math.random() * playerShipsStructure.length)];
if(!playerShip) break roundLoop;
const typeCount = enemyShipsStructure.filter(s => s.id === enemyShip.id).length;
const additionalShipAttack = typeCount > 1 ? Math.floor(Math.random() * typeCount) * enemyShip.attack : 0;
const enemyDamage = Math.max(0, (enemyShip.attack + additionalShipAttack) - playerShip.defense);
playerShip.hitpoints -= enemyDamage;
}
playerShipsStructure.forEach((ship, index) => {
if(ship.hitpoints <= 0) playerShipsStructure.splice(index, 1);
});
playerShipsStructure.forEach((ship, index) => {
if(ship.hitpoints <= 0) playerShipsStructure.splice(index, 1);
});
enemyShipsStructure.forEach((ship, index) => {
if(ship.hitpoints <= 0) enemyShipsStructure.splice(index, 1);
});
enemyShipsStructure.forEach((ship, index) => {
if(ship.hitpoints <= 0) enemyShipsStructure.splice(index, 1);
});
}
}
const playerBalance = playerStats.defense - enemyStats.attack;
@ -270,13 +281,14 @@ export default class FleetManager {
await this.sendMail(
`Battle Results (${playerBalance > enemyBalance ? "Victory" : playerBalance < enemyBalance ? "Defeat" : "Draw"})`,
`Player ships:\n${previousShips.map(ship => `${ship.amount} ${ship.id}`).join(', ')}\n
Enemy ships:\n${enemyFleet.map(ship => `${ship.amount} ${ship.id}`).join(', ')}\n\n
`Results of battle at ${(this.data.destination instanceof SystemManager ? this.data.destination.data.name : this.data.destination.name)}:\n
Player ships:\n${previousShips.map(ship => `${ship.amount} ${ship.id}`).join('\n')}\n
Enemy ships:\n${enemyFleet.map(ship => `${ship.amount} ${ship.id}`).join('\n')}\n\n
Player stats: ${playerStats.hitpoints} HP, ${playerStats.attack} ATK, ${playerStats.defense} DEF\n
Enemy stats: ${enemyStats.hitpoints} HP, ${enemyStats.attack} ATK, ${enemyStats.defense} DEF\n\n
Player ships left:\n${Object.keys(playerShipsLeft).map(key => `${key} - ${playerShipsLeft[key]}\n`)}\n
Enemy ships left:\n${Object.keys(enemyShipsLeft).map(key => `${key} - ${enemyShipsLeft[key]}\n`)}\n
${playerBalance > enemyBalance ? `Resources stolen:\n${resourcesStolen.map(res => `${res.id} - ${res.amount}\n`)}` : ""}`
Player ships left:\n${Object.keys(playerShipsLeft).map(key => `${key} - ${playerShipsLeft[key]}`).join('\n')}\n
Enemy ships left:\n${Object.keys(enemyShipsLeft).map(key => `${key} - ${enemyShipsLeft[key]}`).join('\n')}\n
${playerBalance > enemyBalance ? `Resources stolen:\n${resourcesStolen.map(res => `${res.id} - ${res.amount}`).join('\n')}` : ""}`
);
return !(playerShipsStructure.length > 0);