From 64866e9cbd70ea016bd61e940b1a61add84b2d58 Mon Sep 17 00:00:00 2001 From: xangelo Date: Thu, 2 Jun 2022 09:53:20 -0400 Subject: [PATCH] limit construction/training queues to 2 By default a colony ship can only construct/train two things at a time. This can be increased later via a skill tree. --- migrations/queue-limits.ts | 14 ++++++++++++++ package.json | 5 +++-- src/errors.ts | 2 +- src/repository/city.ts | 19 +++++++++++++++---- src/repository/training-queue.ts | 7 ------- 5 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 migrations/queue-limits.ts diff --git a/migrations/queue-limits.ts b/migrations/queue-limits.ts new file mode 100644 index 0000000..e55d18d --- /dev/null +++ b/migrations/queue-limits.ts @@ -0,0 +1,14 @@ +import { db } from '../src/lib/db'; + +const queries = [ + 'ALTER TABLE cities ADD COLUMN max_construction_queue int', + 'ALTER TABLE cities ADD COLUMN max_training_queue int', + 'UPDATE cities set max_construction_queue = 2, max_training_queue = 2' +]; + +async function main() { + queries.forEach(async sql => await db.raw(sql)); + process.exit(0); +} + +main(); diff --git a/package.json b/package.json index 75cb854..98bdd7f 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,10 @@ "private": true, "scripts": { "dev": "npx nodemon src/api.ts", + "migrate": "npx ts-node --", "setup:rebels": "npx ts-node scripts/generate-cities.ts", - "setup:database": "npx ts-node scripts/setup.ts", - "setup": "npm run setup:rebels" + "setup": "npm run setup:rebels", + "migration": "npx ts-node migrations/" }, "dependencies": { "@bull-board/api": "^3.11.0", diff --git a/src/errors.ts b/src/errors.ts index 1a9b605..e245d9b 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -29,7 +29,7 @@ export class BadInputError extends HttpError { export class InsufficientResourceError extends HttpError { constructor(resource: string, expected: number, available: number) { super( - `Insufficent ${resource}. Expected: ${expected}, only ${available} available`, + `Insufficent ${resource} available. Expected: ${expected}, only ${available} available`, 400, ERROR_CODE.INSUFFICIENT_RESOURCE ); diff --git a/src/repository/city.ts b/src/repository/city.ts index 4becd98..272ee3b 100644 --- a/src/repository/city.ts +++ b/src/repository/city.ts @@ -37,6 +37,8 @@ export type City = { special_attacker_trainer: number; special_defender_trainer: number; icon: string; + max_construction_queue: number; + max_training_queue: number; } export type CityWithLocation = { @@ -88,6 +90,8 @@ export class CityRepository extends Repository { barracks: 0, special_attacker_trainer: 0, special_defender_trainer: 0, + max_construction_queue: 2, + max_training_queue: 2, icon }; @@ -199,6 +203,12 @@ where l.sector_id = ?`, [sector_id]); throw new InsufficientResourceError('energy', building.energy, city.energy); } + // validate that they have enough empty construction queues + const concurrentConstruction = await this.buildQueue.list(city.owner); + if(concurrentConstruction.length >= city.max_construction_queue) { + throw new InsufficientResourceError('Training queues', concurrentConstruction.length + 1, city.max_construction_queue); + } + city.usedSpace += (building.land * amount); city.credits -= (building.credits * amount); city.alloys -= (building.alloys * amount); @@ -265,10 +275,11 @@ where l.sector_id = ?`, [sector_id]); throw new InsufficientResourceError('defenders', unit.defenders, city.defenders); } - console.log(city); - console.log(unit, amount); - - // validate that they have enough of the buildings to support this + // validate that they have enough empty training queues + const concurrentTraining = await this.unitTrainigQueue.list(city.owner); + if(concurrentTraining.length >= city.max_training_queue) { + throw new InsufficientResourceError('Training queues', concurrentTraining.length + 1, city.max_training_queue); + } // ok they have everything, lets update their city // and create the entry in the training queue diff --git a/src/repository/training-queue.ts b/src/repository/training-queue.ts index 9f30ca7..45388c6 100644 --- a/src/repository/training-queue.ts +++ b/src/repository/training-queue.ts @@ -22,13 +22,6 @@ export type UnitTrainingQueueWithName = { display: string; } & UnitTrainingQueue; -export const FriendlyUnitNames = new Map(); -FriendlyUnitNames.set(Unit.SOLDIERS, 'Soldiers'); -FriendlyUnitNames.set(Unit.ATTACKERS, 'Attackers'); -FriendlyUnitNames.set(Unit.DEFENDERS, 'Defenders'); -FriendlyUnitNames.set(Unit.SPECIAL_ATTACKERS, 'Special Attackers'); -FriendlyUnitNames.set(Unit.SPECIAL_DEFENDERS, 'Special Defenders'); - export class UnitTrainingQueueRepository extends Repository { constructor() { super('unit_training_queue'); -- 2.25.1