From 718b618770f55dcc0c1c4ef5903495c2f6e6bc66 Mon Sep 17 00:00:00 2001 From: xangelo Date: Wed, 25 May 2022 09:47:35 -0400 Subject: [PATCH] add solar_panels and calculate energy gen/use --- src/api.ts | 20 ++++++++++---------- src/lib/util.ts | 17 ++++++++++++++--- src/render/kingdom-overview.ts | 5 ++++- src/repository/city.ts | 21 +++++++++++++++++---- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/api.ts b/src/api.ts index f59ec0e..54f4482 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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({ diff --git a/src/lib/util.ts b/src/lib/util.ts index ebe84e5..03aed15 100644 --- a/src/lib/util.ts +++ b/src/lib/util.ts @@ -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(arr: T[], field: string): Map { + const map = new Map(); + arr.forEach(obj => { + map[obj[field]] = obj; + }); + + return map; +} diff --git a/src/render/kingdom-overview.ts b/src/render/kingdom-overview.ts index b019910..08866f1 100644 --- a/src/render/kingdom-overview.ts +++ b/src/render/kingdom-overview.ts @@ -59,7 +59,10 @@ export function renderKingomOverview(city: CityWithLocation & Usage, account: Ac Energy - ${city.energy.toLocaleString()} (${energyRateOfChange.toLocaleString()}) + + ${city.energy.toLocaleString()} (${energyRateOfChange.toLocaleString()}) + @ 0% Battery Utilization + Special Defenders ${city.sp_defenders.toLocaleString()} diff --git a/src/repository/city.ts b/src/repository/city.ts index 28af69a..1094f10 100644 --- a/src/repository/city.ts +++ b/src/repository/city.ts @@ -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 { 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 { - return 0; + async energyProductionPerTick(city: City): Promise { + return city.solar_panels * 125; } async energyUsagePerTick(city: City): Promise { - return 0; + const buildings = await this.buildingRepository.list(); + const buildingsMap = pluck(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 { -- 2.25.1