Skip to content

Commit

Permalink
Add carrier limit to prevent spam
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacialCircumstances committed Feb 15, 2025
1 parent d44e636 commit ba1c84b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions server/services/carrier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import StarService from './star';
import TechnologyService from './technology';
const EventEmitter = require('events');

const CARRIER_LIMIT_MIN = 50;

export default class CarrierService extends EventEmitter {
gameRepo: Repository<Game>;
distanceService: DistanceService;
Expand Down Expand Up @@ -70,6 +72,12 @@ export default class CarrierService extends EventEmitter {
return game.galaxy.carriers.filter(carrier => carrier.orbiting && carrier.orbiting.toString() === starId.toString())
}

getCarrierLimit(game: Game, player: Player) {
const starCount = game.galaxy.stars.filter((star) => star.ownedByPlayerId && star.ownedByPlayerId.toString() === player._id.toString()).length;

return Math.max(CARRIER_LIMIT_MIN, starCount * 10);
}

createAtStar(star: Star, carriers: Carrier[], ships: number = 1): Carrier {
if (!Math.floor(star.shipsActual!)) {
throw new ValidationError('Star must have at least 1 ship to build a carrier.');
Expand Down
9 changes: 9 additions & 0 deletions server/services/starUpgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ export default class StarUpgradeService extends EventEmitter {
throw new ValidationError(`The star does not have enough ships garrisoned (${ships}) to build the carrier.`);
}

// check if player has allowed carrier count

const carrierCount = this.carrierService.listCarriersOwnedByPlayer(game.galaxy.carriers, player._id).length;
const carrierLimit = this.carrierService.getCarrierLimit(game, player);

if (!player.defeated && carrierCount + 1 > carrierLimit) {
throw new ValidationError(`The player has reached the carrier limit: ${carrierLimit}.`);
}

// Create a carrier at the star.
let carrier = this.carrierService.createAtStar(star, game.galaxy.carriers, ships);

Expand Down

0 comments on commit ba1c84b

Please sign in to comment.