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 { } else {
switch(this.data.mission) { switch(this.data.mission) {
case 'ATTACK': 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': case 'TRANSPORT':
await (this.data.destination as Planet | SystemManager).resources.updateAmount(this.data.cargo); await (this.data.destination as Planet | SystemManager).resources.updateAmount(this.data.cargo);
await (this.data.destination as Planet | SystemManager).resources.sync(); 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++) { if(playerStats.attack > enemyStats.defense * 2 && playerStats.defense > enemyStats.attack * 2) {
for(const playerShip of playerShipsStructure) { enemyShipsStructure.forEach((_, index) => enemyShipsStructure.splice(index, 1));
const enemyShip = enemyShipsStructure[Math.floor(Math.random() * enemyShipsStructure.length)]; } else if(enemyStats.attack > playerStats.defense * 2 && enemyStats.defense > playerStats.attack * 2) {
playerShipsStructure.forEach((_, index) => playerShipsStructure.splice(index, 1));
if(!enemyShip) break roundLoop; } else {
const typeCount = playerShipsStructure.filter(s => s.id === playerShip.id).length; roundLoop: for(let i = 0; i < 3; i++) {
const additionalShipAttack = typeCount > 1 ? Math.floor(Math.random() * typeCount) * playerShip.attack : 0; for(const playerShip of playerShipsStructure) {
const playerDamage = Math.max(0, (playerShip.attack + additionalShipAttack) - enemyShip.defense); const enemyShip = enemyShipsStructure[Math.floor(Math.random() * enemyShipsStructure.length)];
enemyShip.hitpoints -= playerDamage;
} if(!enemyShip) break roundLoop;
const typeCount = playerShipsStructure.filter(s => s.id === playerShip.id).length;
for(const enemyShip of enemyShipsStructure) { const additionalShipAttack = typeCount > 1 ? Math.floor(Math.random() * typeCount) * playerShip.attack : 0;
const playerShip = playerShipsStructure[Math.floor(Math.random() * playerShipsStructure.length)]; const playerDamage = Math.max(0, (playerShip.attack + additionalShipAttack) - enemyShip.defense);
enemyShip.hitpoints -= playerDamage;
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; for(const enemyShip of enemyShipsStructure) {
const enemyDamage = Math.max(0, (enemyShip.attack + additionalShipAttack) - playerShip.defense); const playerShip = playerShipsStructure[Math.floor(Math.random() * playerShipsStructure.length)];
playerShip.hitpoints -= enemyDamage;
} 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) => { playerShipsStructure.forEach((ship, index) => {
if(ship.hitpoints <= 0) playerShipsStructure.splice(index, 1); if(ship.hitpoints <= 0) playerShipsStructure.splice(index, 1);
}); });
enemyShipsStructure.forEach((ship, index) => { enemyShipsStructure.forEach((ship, index) => {
if(ship.hitpoints <= 0) enemyShipsStructure.splice(index, 1); if(ship.hitpoints <= 0) enemyShipsStructure.splice(index, 1);
}); });
}
} }
const playerBalance = playerStats.defense - enemyStats.attack; const playerBalance = playerStats.defense - enemyStats.attack;
@ -270,13 +281,14 @@ export default class FleetManager {
await this.sendMail( await this.sendMail(
`Battle Results (${playerBalance > enemyBalance ? "Victory" : playerBalance < enemyBalance ? "Defeat" : "Draw"})`, `Battle Results (${playerBalance > enemyBalance ? "Victory" : playerBalance < enemyBalance ? "Defeat" : "Draw"})`,
`Player ships:\n${previousShips.map(ship => `${ship.amount} ${ship.id}`).join(', ')}\n `Results of battle at ${(this.data.destination instanceof SystemManager ? this.data.destination.data.name : this.data.destination.name)}:\n
Enemy ships:\n${enemyFleet.map(ship => `${ship.amount} ${ship.id}`).join(', ')}\n\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 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 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 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]}\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}\n`)}` : ""}` ${playerBalance > enemyBalance ? `Resources stolen:\n${resourcesStolen.map(res => `${res.id} - ${res.amount}`).join('\n')}` : ""}`
); );
return !(playerShipsStructure.length > 0); return !(playerShipsStructure.length > 0);