display the total number of queues availabe to the user
authorxangelo <git@xangelo.ca>
Fri, 3 Jun 2022 19:40:37 +0000 (15:40 -0400)
committerxangelo <git@xangelo.ca>
Fri, 3 Jun 2022 19:40:37 +0000 (15:40 -0400)
src/render/land-development.ts
src/render/unit-training.ts

index 9cd4eb7418c2f601412311bcd19176a168f4b767..e3643a9fc8732bc28d0b1cafcb15217ba43b07b3 100644 (file)
@@ -3,7 +3,16 @@ import { CityWithLocation } from "../repository/city";
 import { Building } from '../repository/buildings';
 import _ from "lodash";
 
-function progressBar(current, max): string {
+const emptyBuilding: BuildQueue = {
+  id: '',
+  owner: '',
+  amount: 0,
+  created: 0,
+  due: 0,
+  building_type: 'empty',
+};
+
+function progressBar(current: number, max: number): string {
     const percent = Math.ceil((current/max) * 100);
     return `
     <div class="progress-bar construction" style="background: background: var(--green-bg);
@@ -15,6 +24,10 @@ function progressBar(current, max): string {
 
 export function renderLandDevelopment(city: CityWithLocation, buildings: Building[], buildQueues: BuildQueue[]): string {
     const freeSpace = city.totalSpace - city.usedSpace;
+    const sortedBuildQueues = buildQueues.sort((a, b) => {
+        return a.due - b.due;
+    });
+
     let html = `
     <div hx-trigger="reload-construction-queue, every 600s" hx-get="/poll/construction">
     <h2 data-augmented-ui="tl-clip bl-clip none">Construction</h2>
@@ -47,19 +60,33 @@ export function renderLandDevelopment(city: CityWithLocation, buildings: Buildin
     `;
 
     const quickFindBuilding = _.keyBy(buildings, 'slug');
+    const finalBuildQueues = sortedBuildQueues;
+
+    if (finalBuildQueues.length < city.max_construction_queue) {
+      while(finalBuildQueues.length < city.max_construction_queue) {
+        finalBuildQueues.push(emptyBuilding);
+      }
+    }
+
 
     const queues = `
     <hr>
     <h2 data-augmented-ui="tl-clip bl-clip none">Build Queues</h2>
     <table>
     <tr>
-    <th>Building</th>
-    <th>Amount Expected</th>
-    <th>Progress</th>
+    <th align="left">Building</th>
+    <th align="left">Amount Expected</th>
+    <th colspan="2">Progress</th>
     </tr>
-    ${buildQueues.sort((a, b) => {
-        return a.due - b.due;
-    }).map(queue => {
+    ${sortedBuildQueues.map(queue => {
+      if(queue.building_type === 'empty') {
+        return `
+        <tr>
+        <td colspan="4">You have sufficient capacity to build something.</td>
+        </tr>
+        `
+      }
+      else {
         const now = Date.now() - queue.created;
         const duration = queue.due - queue.created;
 
@@ -70,6 +97,7 @@ export function renderLandDevelopment(city: CityWithLocation, buildings: Buildin
         <td>${progressBar(now, duration)}</td>
         </tr>
         `;
+      }
     }).join("\n")}
     </table>
     </div>
index 5742d9453bf4704df05134fe07f70100e2f8a909..a1bd4b8756bef6a275f1c07b9609229bf510ce90 100644 (file)
@@ -4,6 +4,16 @@ import { UnitTrainingQueueWithName } from "../repository/training-queue";
 import { Unit } from "../repository/unit";
 import { DateTime } from "luxon";
 
+const emptyQueue: UnitTrainingQueueWithName = {
+  display: '',
+  id: '',
+  owner: '',
+  amount: 0,
+  created: 0,
+  due: 0,
+  unit_type: 'empty'
+}
+
 function progressBar(current: number, max: number): string {
     const percent = Math.ceil((current/max) * 100);
     return `
@@ -16,6 +26,9 @@ function progressBar(current: number, max: number): string {
 
 export function renderUnitTraining(city: CityWithLocation, units: Unit[], trainingQueues: UnitTrainingQueueWithName[]): string {
     const unit = _.keyBy(units, 'slug');
+    const sortedTrainingQueue = trainingQueues.sort((a, b) => {
+        return a.due - b.due;
+    });
     let html = `
     <div hx-trigger="reload-unit-training, every 600s" hx-get="/poll/unit-training">
     <h2 data-augmented-ui="tl-clip bl-clip none">Unit Training</h2>
@@ -90,34 +103,46 @@ export function renderUnitTraining(city: CityWithLocation, units: Unit[], traini
     </table>
     `;
 
+    const finalTrainingQueue = sortedTrainingQueue;
+    if(finalTrainingQueue.length < city.max_training_queue) {
+      while(finalTrainingQueue.length < city.max_construction_queue) {
+        finalTrainingQueue.push(emptyQueue);
+      }
+    }
+
     const queues = `
     <hr>
     <h2 data-augmented-ui="tl-clip bl-clip none">Training Queues</h2>
     <table>
     <tr>
-    <th>Unit Type</th>
-    <th>Amount Expected</th>
+    <th align="left">Unit Type</th>
+    <th align="left">Amount Expected</th>
     <th>Progress</th>
     </tr>
-    ${trainingQueues.sort((a, b) => {
-        return a.due - b.due;
-    }).map(queue => {
-      const created = DateTime.fromMillis(queue.created);
-      const due = DateTime.fromMillis(queue.due);
-      const now = Date.now() - queue.created;
-      const duration = queue.due - queue.created;
+    ${sortedTrainingQueue.map(queue => {
+      if(queue.unit_type === 'empty') {
+        return `<tr>
+        <td colspan="4">You have sufficient queue capacity to train more units.</td>
+        </tr>`;
+      }
+      else {
+        const created = DateTime.fromMillis(queue.created);
+        const due = DateTime.fromMillis(queue.due);
+        const now = Date.now() - queue.created;
+        const duration = queue.due - queue.created;
 
-      return `
-      <tr>
-      <td>${queue.display}</td>
-      <td>${queue.amount}</td>
-      <td>
-      <span title="${Math.ceil(due.diff(created).as('minutes')).toLocaleString()} minutes remaining">
-      ${progressBar(now, duration)}<br>
-      </span>
-      </td>
-      </tr>
-      `;
+        return `
+        <tr>
+        <td>${queue.display}</td>
+        <td>${queue.amount}</td>
+        <td>
+        <span title="${Math.ceil(due.diff(created).as('minutes')).toLocaleString()} minutes remaining">
+        ${progressBar(now, duration)}<br>
+        </span>
+        </td>
+        </tr>
+        `;
+      }
     }).join("\n")}
     </table>
     </div>