limit construction/training queues to 2
authorxangelo <git@xangelo.ca>
Thu, 2 Jun 2022 13:53:20 +0000 (09:53 -0400)
committerxangelo <git@xangelo.ca>
Thu, 2 Jun 2022 13:53:20 +0000 (09:53 -0400)
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 [new file with mode: 0644]
package.json
src/errors.ts
src/repository/city.ts
src/repository/training-queue.ts

diff --git a/migrations/queue-limits.ts b/migrations/queue-limits.ts
new file mode 100644 (file)
index 0000000..e55d18d
--- /dev/null
@@ -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();
index 75cb8546b985e10f7cf5e1487d220de7f9a97d39..98bdd7f39ec12d02f7de23fdbe41fd568550bcf6 100644 (file)
@@ -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",
index 1a9b605dc7f45940c8f50a12cda54ab90d0b2557..e245d9bb07f9c3437057764b8e0a196f6977c5a6 100644 (file)
@@ -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
                );
index 4becd98836c653ba22eabf986d8e416601199ee3..272ee3b960a410c018d07cc723b5620f8922da3c 100644 (file)
@@ -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<City> {
             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
index 9f30ca794d25593935aa8c6779e87b5c6673daa8..45388c66360787141452d0063122d5bced512c22 100644 (file)
@@ -22,13 +22,6 @@ export type UnitTrainingQueueWithName = {
   display: string;
 } & UnitTrainingQueue;
 
-export const FriendlyUnitNames = new Map<Unit, string>();
-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<UnitTrainingQueue> {
     constructor() {
         super('unit_training_queue');