Resource fixes!
[browser-rts.git] / src / repository / city.ts
index 7b8818510e9b45761473d6da7411c27735ce4299..43e1b7a4247e9e91a109f94b6774cdb96a740465 100644 (file)
@@ -16,10 +16,10 @@ export type City = {
     owner: string;
     totalSpace: number;
     usedSpace: number;
-    gold: number;
-    ore: number;
-    logs: number;
-    bushels: number;
+    credits: number;
+    alloys: number;
+    energy: number;
+    food: number;
     population: number;
     soldiers: number;
     attackers: number;
@@ -62,10 +62,10 @@ export class CityRepository extends Repository<City> {
             owner: accountId,
             totalSpace: 100,
             usedSpace: 0,
-            gold: 10000,
-            ore: 10000,
-            logs: 10000,
-            bushels: 10000,
+            credits: 10000,
+            alloys: 10000,
+            energy: 10000,
+            food: 10000,
             population: 1000,
             soldiers: 100,
             attackers: 0,
@@ -161,22 +161,22 @@ where l.sector_id = ?`, [sector_id]);
             throw new InsufficientResourceError('land', building.land, freeSpace);
         }
 
-        if(city.gold < building.gold) {
-            throw new InsufficientResourceError('gold', building.gold, city.gold);
+        if(city.credits < building.credits) {
+            throw new InsufficientResourceError('credits', building.credits, city.credits);
         }
 
-        if(city.ore < building.ore) {
-            throw new InsufficientResourceError('ore', building.ore, city.ore);
+        if(city.alloys < building.alloys) {
+            throw new InsufficientResourceError('alloys', building.alloys, city.alloys);
         }
 
-        if(city.logs < building.logs) {
-            throw new InsufficientResourceError('logs', building.logs, city.logs);
+        if(city.energy < building.energy) {
+            throw new InsufficientResourceError('energy', building.energy, city.energy);
         }
 
         city.usedSpace += (building.land * amount);
-        city.gold -= (building.gold * amount);
-        city.ore -= (building.ore * amount);
-        city.logs -= (building.logs * amount);
+        city.credits -= (building.credits * amount);
+        city.alloys -= (building.alloys * amount);
+        city.energy -= (building.energy * amount);
 
         await this.save(city);
 
@@ -215,12 +215,12 @@ where l.sector_id = ?`, [sector_id]);
     }
 
     async train(unit: Unit, amount: number, city: City): Promise<UnitTrainingQueue> {
-        if(city.gold < unit.gold) {
-            throw new InsufficientResourceError('gold', unit.gold, city.gold);
+        if(city.credits < unit.credits) {
+            throw new InsufficientResourceError('credits', unit.credits, city.credits);
         }
 
-        if(city.bushels < unit.bushels) {
-            throw new InsufficientResourceError('bushels', unit.bushels, city.bushels);
+        if(city.food < unit.food) {
+            throw new InsufficientResourceError('food', unit.food, city.food);
         }
 
         if(city.population < coalesce(unit.population, 0)) {
@@ -244,8 +244,8 @@ where l.sector_id = ?`, [sector_id]);
         // ok they have everything, lets update their city 
         // and create the entry in the training queue
 
-        city.gold -= unit.gold * amount;
-        city.bushels -= unit.bushels * amount;
+        city.credits -= unit.credits * amount;
+        city.food -= unit.food * amount;
         city.population -= coalesce(unit.population, 0) * amount;
         city.soldiers -= coalesce(unit.soldiers, 0) * amount;
         city.attackers -= coalesce(unit.attackers, 0) * amount;
@@ -279,6 +279,33 @@ where l.sector_id = ?`, [sector_id]);
         return power
     }
 
+    async foodProductionPerTick(city: City): Promise<number> {
+      // eventually we should supply the warehouse formula 
+      // to calculate the max amount of food created per tick
+      return _.max([
+        city.population + _.round(city.farms * 50)
+      ])
+    }
+
+    async foodUsagePerTick(city: City): Promise<number> {
+      return (
+        (city.soldiers * 0.5) + 
+        (city.population * 0.25) + 
+        (city.attackers * 0.75) + 
+        (city.attackers * 0.75) + 
+        (city.sp_attackers * 1.3) + 
+        (city.sp_defenders * 1.3)
+      )
+    }
+
+    async energyProductionPerTic(city: City): Promise<number> {
+      return 0;
+    }
+
+    async energyUsagePerTick(city: City): Promise<number> {
+      return 0;
+    }
+
     async attack(attacker: CityWithLocation, attacked: CityWithLocation, army: Army): Promise<ArmyQueue> {
         // validate the user has enough of a military! 
         if(attacker.soldiers < army.soldiers) {