new building: homes
[browser-rts.git] / src / repository / city.ts
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