bugfix: correct unit name / time remaining on training queues
authorxangelo <git@xangelo.ca>
Fri, 27 May 2022 17:44:06 +0000 (13:44 -0400)
committerxangelo <git@xangelo.ca>
Fri, 27 May 2022 17:44:06 +0000 (13:44 -0400)
src/render/unit-training.ts
src/repository/city.ts
src/repository/training-queue.ts

index 686b57b16c86bed51b6ba7fc0b74eff256828836..a177dec3cbec67caf3a1a7d64a99d425ab085f56 100644 (file)
@@ -1,9 +1,10 @@
 import _ from "lodash";
 import { CityWithLocation } from "../repository/city";
-import { UnitTrainingQueue } from "../repository/training-queue";
+import { UnitTrainingQueueWithName } from "../repository/training-queue";
 import { Unit } from "../repository/unit";
+import { DateTime } from "luxon";
 
-function progressBar(current, max): string {
+function progressBar(current: number, max: number): string {
     const percent = Math.ceil((current/max) * 100);
     return `
     <div class="progress-bar unit-training" style="background: background: var(--green-bg);
@@ -13,7 +14,7 @@ function progressBar(current, max): string {
     `;
 }
 
-export function renderUnitTraining(city: CityWithLocation, units: Unit[], trainingQueues: UnitTrainingQueue[]): string {
+export function renderUnitTraining(city: CityWithLocation, units: Unit[], trainingQueues: UnitTrainingQueueWithName[]): string {
     const unit = _.keyBy(units, 'slug');
     let html = `
     <div hx-trigger="reload-unit-training, every 600s" hx-get="/poll/unit-training">
@@ -101,16 +102,22 @@ export function renderUnitTraining(city: CityWithLocation, units: Unit[], traini
     ${trainingQueues.sort((a, b) => {
         return a.due - b.due;
     }).map(queue => {
-        const now = Date.now() - queue.created;
-        const duration = queue.due - queue.created;
+      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.unit_type}</td>
-        <td>${queue.amount}</td>
-        <td>${progressBar(now, duration)}</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>
index f49b1db9d09226a8ac8fa46536867c28be588e82..4becd98836c653ba22eabf986d8e416601199ee3 100644 (file)
@@ -4,7 +4,7 @@ import {Repository} from './base';
 import * as config from '../config';
 import { BuildQueue, BuildQueueRepository } from './build-queue';
 import { DateTime, Duration } from 'luxon';
-import { UnitTrainingQueue, UnitTrainingQueueRepository } from './training-queue';
+import { UnitTrainingQueue, UnitTrainingQueueRepository, UnitTrainingQueueWithName } from './training-queue';
 import { coalesce, pluck } from '../lib/util';
 import { Building, BuildingRepository } from './buildings';
 import { Unit, UnitRepository } from './unit';
@@ -429,7 +429,7 @@ where l.sector_id = ?`, [sector_id]);
         return this.buildQueue.list(owner);
     }
 
-    async getUnitTrainingQueues(owner: string): Promise<UnitTrainingQueue[]> {
+    async getUnitTrainingQueues(owner: string): Promise<UnitTrainingQueueWithName[]> {
         return this.unitTrainigQueue.list(owner);
     }
 }
index 5d2b58cc1dc871b6d8d898582bb9c486a1913dd4..9f30ca794d25593935aa8c6779e87b5c6673daa8 100644 (file)
@@ -18,6 +18,10 @@ export type UnitTrainingQueue = {
     due: number;
 };
 
+export type UnitTrainingQueueWithName = {
+  display: string;
+} & UnitTrainingQueue;
+
 export const FriendlyUnitNames = new Map<Unit, string>();
 FriendlyUnitNames.set(Unit.SOLDIERS, 'Soldiers');
 FriendlyUnitNames.set(Unit.ATTACKERS, 'Attackers');
@@ -44,7 +48,7 @@ export class UnitTrainingQueueRepository extends Repository<UnitTrainingQueue> {
         return data;
     }
 
-    list(owner: string): Promise<UnitTrainingQueue[]> {
-        return this.FindAll({owner}).orderBy('due');
+    list(owner: string): Promise<UnitTrainingQueueWithName[]> {
+      return this.db.raw(`select q.*, u.display from unit_training_queue q join units u on u.slug = q.unit_type where owner = ? order by due`, owner);
     }
-}
\ No newline at end of file
+}