add solar_panels and calculate energy gen/use
authorxangelo <git@xangelo.ca>
Wed, 25 May 2022 13:47:35 +0000 (09:47 -0400)
committerxangelo <git@xangelo.ca>
Wed, 25 May 2022 13:47:39 +0000 (09:47 -0400)
src/api.ts
src/lib/util.ts
src/render/kingdom-overview.ts
src/repository/city.ts

index f59ec0e3a83a173840346cd289829b236c4942c6..54f44825be11f63f8b7b5bba0b8abd377be52362 100644 (file)
@@ -100,8 +100,8 @@ server.get<{}, string>('/poll/overview', async req => {
   const usage = {
     foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
     foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
-    energyUsagePerTick: 0,
-    energyProductionPerTick: 0
+    energyUsagePerTick: await cityRepo.energyUsagePerTick(city),
+    energyProductionPerTick: await cityRepo.energyProductionPerTick(city)
   }
 
        return renderKingomOverview({
@@ -119,8 +119,8 @@ server.get<{}, string>('/poll/construction', async req => {
   const usage = {
     foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
     foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
-    energyUsagePerTick: 0,
-    energyProductionPerTick: 0
+    energyUsagePerTick: await cityRepo.energyUsagePerTick(city),
+    energyProductionPerTick: await cityRepo.energyProductionPerTick(city)
   }
        return renderLandDevelopment(city, buildings, buildQueues) + topbar({...city, ...usage});
 });
@@ -134,8 +134,8 @@ server.get<{}, string>('/poll/unit-training', async req => {
   const usage = {
     foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
     foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
-    energyUsagePerTick: 0,
-    energyProductionPerTick: 0
+    energyUsagePerTick: await cityRepo.energyUsagePerTick(city),
+    energyProductionPerTick: await cityRepo.energyProductionPerTick(city)
   }
 
        return renderUnitTraining(city, units, unitTrainingQueues) + topbar({
@@ -161,8 +161,8 @@ server.post<{body: {sector: string}}, string>('/poll/map', async req => {
   const usage = {
     foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
     foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
-    energyUsagePerTick: 0,
-    energyProductionPerTick: 0
+    energyUsagePerTick: await cityRepo.energyUsagePerTick(city),
+    energyProductionPerTick: await cityRepo.energyProductionPerTick(city)
   }
 
        return renderOverworldMap(await cityRepo.findAllInSector(sector), city, sector) + topbar({
@@ -178,8 +178,8 @@ server.get<{}, string>('/poll/mailroom', async req => {
   const usage = {
     foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
     foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
-    energyUsagePerTick: 0,
-    energyProductionPerTick: 0
+    energyUsagePerTick: await cityRepo.energyUsagePerTick(city),
+    energyProductionPerTick: await cityRepo.energyProductionPerTick(city)
   }
 
        return renderMailroom(await mailRepo.listReceivedMessages(account.id)) + topbar({
index ebe84e5a4fac8d734d211d64f4034c7df27146fe..03aed1557bdc804fb2034529067b87f007eb87a8 100644 (file)
@@ -1,9 +1,20 @@
-export function coalesce(...args: any[]): any {
+import { isEmpty } from 'lodash';
+
+export function coalesce(...args) {
     let found;
     while(args.length) {
         found = args.shift();
-        if(found !== null && found !== undefined && found !== '') {
+        if(found !== null && found !== undefined && !isEmpty(found)) {
             return found;
         }
     }
-}
\ No newline at end of file
+}
+
+export function pluck<T>(arr: T[], field: string): Map<string, T> {
+  const map = new Map<string, T>();
+  arr.forEach(obj => {
+    map[obj[field]] = obj;
+  });
+
+  return map;
+}
index b019910066f1b203c74867d1e294415f0cab1483..08866f1fb4af107aee0fd11f820bb45eff2f4b57 100644 (file)
@@ -59,7 +59,10 @@ export function renderKingomOverview(city: CityWithLocation & Usage, account: Ac
        </tr>
        <tr>
                <th>Energy</th>
-               <td>${city.energy.toLocaleString()} (<span class="rate-of-change ${energyRateOfChange < 0 ? 'danger-text' : 'success-text'}">${energyRateOfChange.toLocaleString()}</span>)</td>
+               <td>
+    ${city.energy.toLocaleString()} (<span class="rate-of-change ${energyRateOfChange < 0 ? 'danger-text' : 'success-text'}">${energyRateOfChange.toLocaleString()}</span>)
+    @ 0% Battery Utilization
+    </td>
                <th>Special Defenders</th>
                <td>${city.sp_defenders.toLocaleString()}</td>
        </tr>
index 28af69a252f387b1f77610f5751163a76e51a7a5..1094f10cff35e9ee13f205eea58ba82ffa4a5dff 100644 (file)
@@ -5,7 +5,7 @@ import * as config from '../config';
 import { BuildQueue, BuildQueueRepository } from './build-queue';
 import { DateTime, Duration } from 'luxon';
 import { UnitTrainingQueue, UnitTrainingQueueRepository } from './training-queue';
-import { coalesce } from '../lib/util';
+import { coalesce, pluck } from '../lib/util';
 import { Building, BuildingRepository } from './buildings';
 import { Unit, UnitRepository } from './unit';
 import _, { random } from 'lodash';
@@ -29,6 +29,7 @@ export type City = {
     homes: number;
     farms: number;
     warehouses: number;
+    solar_panels: number;
     barracks: number;
     special_attacker_trainer: number;
     special_defender_trainer: number;
@@ -77,6 +78,7 @@ export class CityRepository extends Repository<City> {
             homes: 20,
             farms: 5,
             warehouses: 5,
+            solar_panels: 5,
             barracks: 0,
             special_attacker_trainer: 0,
             special_defender_trainer: 0,
@@ -349,12 +351,23 @@ where l.sector_id = ?`, [sector_id]);
       )
     }
 
-    async energyProductionPerTic(city: City): Promise<number> {
-      return 0;
+    async energyProductionPerTick(city: City): Promise<number> {
+      return city.solar_panels * 125;
     }
 
     async energyUsagePerTick(city: City): Promise<number> {
-      return 0;
+      const buildings = await this.buildingRepository.list();
+      const buildingsMap = pluck<Building>(buildings, 'slug');
+      const totalEnergy = _.sum([
+        city.farms * buildingsMap['farms'].energy,
+        city.barracks * buildingsMap['barracks'].energy,
+        city.special_defender_trainer * buildingsMap['special_defender_trainer'].energy,
+        city.special_attacker_trainer * buildingsMap['special_attacker_trainer'].energy,
+        city.homes * buildingsMap['homes'].energy,
+        city.warehouses * buildingsMap['warehouses'].energy,
+        city.solar_panels * buildingsMap['solar_panels'].energy
+      ]);
+      return totalEnergy;
     }
 
     async attack(attacker: CityWithLocation, attacked: CityWithLocation, army: Army): Promise<ArmyQueue> {