new building: homes
authorxangelo <git@xangelo.ca>
Tue, 24 May 2022 15:30:29 +0000 (11:30 -0400)
committerxangelo <git@xangelo.ca>
Tue, 24 May 2022 15:30:29 +0000 (11:30 -0400)
Homes give you 25 pop cap increase.

src/render/kingdom-overview.ts
src/repository/city.ts

index c035f7cbcec60921063e74f6d41c164402965f9d..2712e7643ae4cbdab9545df22863adf16e0d469e 100644 (file)
@@ -1,5 +1,5 @@
 import { Account } from "../repository/accounts";
-import { CityWithLocation } from "../repository/city";
+import { CityWithLocation, CityRepository } from "../repository/city";
 import * as _ from 'lodash';
 
 type Usage = {
@@ -9,6 +9,8 @@ type Usage = {
   energyProductionPerTick: number;
 }
 
+const cityRepo = new CityRepository();
+
 export function renderKingomOverview(city: CityWithLocation & Usage, account: Account): string {
   const foodRateOfChange = city.foodProductionPerTick - city.foodUsagePerTick;
   const energyRateOfChange = city.energyProductionPerTick - city.energyUsagePerTick;
@@ -20,7 +22,7 @@ export function renderKingomOverview(city: CityWithLocation & Usage, account: Ac
                <th>Captain</th>
                <td>${account.username}</td>
                <th>Population</th>
-               <td>${city.population.toLocaleString()}/${_.max([city.farms * 70, city.population])}</td>
+               <td>${city.population.toLocaleString()}/${cityRepo.maxPopulation(city)}</td>
        </tr>
        <tr>
                <th>Space</th>
index 43e1b7a4247e9e91a109f94b6774cdb96a740465..9c2153c263cb79655c7ce9c91c3c386ee3394b14 100644 (file)
@@ -26,6 +26,7 @@ export type City = {
     defenders: number;
     sp_attackers: number;
     sp_defenders: number;
+    homes: number;
     farms: number;
     barracks: number;
     special_attacker_trainer: number;
@@ -72,6 +73,7 @@ export class CityRepository extends Repository<City> {
             defenders: 0,
             sp_attackers: 0,
             sp_defenders: 0,
+            homes: 20,
             farms: 0,
             barracks: 0,
             special_attacker_trainer: 0,
@@ -118,9 +120,23 @@ export class CityRepository extends Repository<City> {
         return sample.sector_id;
     }
 
-    async save(city: Partial<City>) {
-        await this.Save(city, {id: city.id});
-        return city;
+    async save(city: City) {
+      const fieldsToSave = [
+        'totalSpace', 'usedSpace', 'credits', 'alloys', 'energy', 'food',
+        'poulation', 'soldiers', 'attackers', 'defenders', 'sp_attackers', 'sp_defenders',
+        'homes', 'farms', 'barracks', 'special_attacker_trainer', 'special_defender_trainer'
+      ];
+      
+      const finalData = {};
+
+      fieldsToSave.forEach(field => {
+        if(city.hasOwnProperty(field)) {
+          finalData[field] = city[field];
+        }
+      });
+
+      await this.Save(finalData, {id: city.id});
+      return city;
     }
 
     async findById(cityId: string): Promise<CityWithLocation> {
@@ -279,6 +295,10 @@ where l.sector_id = ?`, [sector_id]);
         return power
     }
 
+    maxPopulation(city: City): number {
+      return city.homes * 25;
+    }
+
     async foodProductionPerTick(city: City): Promise<number> {
       // eventually we should supply the warehouse formula 
       // to calculate the max amount of food created per tick