Resource fixes!
[browser-rts.git] / src / api.ts
index aba677b1f72ad8c1e6f6dc59a2ca8471481441e5..f59ec0e3a83a173840346cd289829b236c4942c6 100644 (file)
@@ -15,7 +15,9 @@ import { launchOffensive, listOperations, renderOverworldMap } from './render/ma
 import { createBullBoard } from '@bull-board/api';
 import { BullAdapter } from '@bull-board/api/bullAdapter';
 import _ from 'lodash';
-import { renderMailroom, renderMessage } from './render/mail';
+import { renderCost } from './render/costs';
+import {renderMailroom, renderMessage} from './render/mail';
+import {topbar} from './render/topbar';
 
 const server = new HttpServer(config.API_PORT);
 
@@ -95,7 +97,17 @@ server.get<{}, string>('/poll/overview', async req => {
        const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
        const city = await cityRepo.getUsersCity(account.id);
 
-       return renderKingomOverview(city, account);
+  const usage = {
+    foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
+    foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
+    energyUsagePerTick: 0,
+    energyProductionPerTick: 0
+  }
+
+       return renderKingomOverview({
+    ...city,
+    ...usage
+  }, account) + topbar({...city, ...usage});
 });
 
 server.get<{}, string>('/poll/construction', async req => {
@@ -104,7 +116,13 @@ server.get<{}, string>('/poll/construction', async req => {
        const buildings = await cityRepo.buildingRepository.list();
 
        const buildQueues = await cityRepo.getBuildQueues(account.id);
-       return renderLandDevelopment(city, buildings, buildQueues);
+  const usage = {
+    foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
+    foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
+    energyUsagePerTick: 0,
+    energyProductionPerTick: 0
+  }
+       return renderLandDevelopment(city, buildings, buildQueues) + topbar({...city, ...usage});
 });
 
 server.get<{}, string>('/poll/unit-training', async req => {
@@ -113,8 +131,17 @@ server.get<{}, string>('/poll/unit-training', async req => {
 
        const unitTrainingQueues = await cityRepo.getUnitTrainingQueues(account.id);
        const units = await cityRepo.unitRepository.list();
+  const usage = {
+    foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
+    foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
+    energyUsagePerTick: 0,
+    energyProductionPerTick: 0
+  }
 
-       return renderUnitTraining(city, units, unitTrainingQueues);
+       return renderUnitTraining(city, units, unitTrainingQueues) + topbar({
+    ...city,
+    ...usage
+  });
 });
 
 server.post<{body: {sector: string}}, string>('/poll/map', async req => {
@@ -131,15 +158,34 @@ server.post<{body: {sector: string}}, string>('/poll/map', async req => {
     }
   }
 
-  console.log('Checking cities in sector', sector);
+  const usage = {
+    foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
+    foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
+    energyUsagePerTick: 0,
+    energyProductionPerTick: 0
+  }
 
-       return renderOverworldMap(await cityRepo.findAllInSector(sector), city, sector);
+       return renderOverworldMap(await cityRepo.findAllInSector(sector), city, sector) + topbar({
+    ...city,
+    ...usage
+  });
 });
 
 server.get<{}, string>('/poll/mailroom', async req => {
        const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
+       const city = await cityRepo.getUsersCity(account.id);
 
-       return renderMailroom(await mailRepo.listReceivedMessages(account.id));
+  const usage = {
+    foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
+    foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
+    energyUsagePerTick: 0,
+    energyProductionPerTick: 0
+  }
+
+       return renderMailroom(await mailRepo.listReceivedMessages(account.id)) + topbar({
+    ...city,
+    ...usage
+  });
 });
 
 
@@ -150,7 +196,6 @@ server.post<{
        }
 }, string>('/cost/construction', async req => {
        const amount = parseInt(req.body.amount.trim(), 10);
-       console.log('checking amount', amount);
 
        if(isNaN(amount) || amount < 1) {
                return '';
@@ -161,13 +206,15 @@ server.post<{
                throw new NotFoundError(`Invalid building type ${req.body.building_type}`, ERROR_CODE.INVALID_BUILDING);
        }
 
-       return JSON.stringify({
-               gold: building.gold * amount,
-               ore: building.ore * amount,
-               logs: building.logs * amount,
+  const cost = {
+               credits: building.credits * amount,
+               alloys: building.alloys * amount,
+               energy: building.energy * amount,
                land: building.land * amount,
                time: building.time
-       });
+  };
+
+  return renderCost(cost);
 });
 
 server.post<{
@@ -177,19 +224,24 @@ server.post<{
        }
 }, string>('/cost/training', async req => {
        const amount = parseInt(req.body.amount, 10);
-       const unit = await cityRepo.unitRepository.findBySlug(req.body.type);
 
+       if(isNaN(amount) || amount < 1) {
+               return '';
+       }
+
+       const unit = await cityRepo.unitRepository.findBySlug(req.body.type);
        if(!unit) {
                throw new NotFoundError(`Invalid unit type ${req.body.type}`, ERROR_CODE.INVALID_UNIT);
        }
 
-       return JSON.stringify({
+       return renderCost({
                population: unit.population * amount,
                soldiers: unit.soldiers * amount,
                attackers: unit.attackers * amount,
                defenders: unit.defenders * amount,
-               gold: unit.gold * amount,
-               bushels: unit.bushels * amount
+               credits: unit.credits * amount,
+               food: unit.food * amount,
+    time: unit.time * amount
        });
 });
 
@@ -199,17 +251,17 @@ server.post<{
                building_type: string,
        }
 }, void>('/build', async req => {
-       const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
-       const city = await cityRepo.getUsersCity(account.id);
+  const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
+  const city = await cityRepo.getUsersCity(account.id);
 
-       const amount = parseInt(req.body.amount, 10);
-       const building = await cityRepo.buildingRepository.findBySlug(req.body.building_type);
+  const amount = parseInt(req.body.amount, 10);
+  const building = await cityRepo.buildingRepository.findBySlug(req.body.building_type);
 
-       if(!building) {
-               throw new NotFoundError(`Invalid building type ${req.body.building_type}`, ERROR_CODE.INVALID_BUILDING);
-       }
+  if(!building) {
+    throw new NotFoundError(`Invalid building type ${req.body.building_type}`, ERROR_CODE.INVALID_BUILDING);
+  }
 
-       const queueData = await cityRepo.buildBuilding(building, amount ,city);
+  const queueData = await cityRepo.buildBuilding(building, amount, city);
 
        construction.trigger(queueData, { delay: queueData.due });
 }, 'reload-construction-queue');