From ed54ffb9b589bd35842561fd6cd13779e836b8eb Mon Sep 17 00:00:00 2001 From: xangelo Date: Fri, 27 May 2022 13:44:06 -0400 Subject: [PATCH] bugfix: correct unit name / time remaining on training queues --- src/render/unit-training.ts | 31 +++++++++++++++++++------------ src/repository/city.ts | 4 ++-- src/repository/training-queue.ts | 10 +++++++--- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/render/unit-training.ts b/src/render/unit-training.ts index 686b57b..a177dec 100644 --- a/src/render/unit-training.ts +++ b/src/render/unit-training.ts @@ -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 `
@@ -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 ` - - ${queue.unit_type} - ${queue.amount} - ${progressBar(now, duration)} - - `; + return ` + + ${queue.display} + ${queue.amount} + + + ${progressBar(now, duration)}
+
+ + + `; }).join("\n")}
diff --git a/src/repository/city.ts b/src/repository/city.ts index f49b1db..4becd98 100644 --- a/src/repository/city.ts +++ b/src/repository/city.ts @@ -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 { + async getUnitTrainingQueues(owner: string): Promise { return this.unitTrainigQueue.list(owner); } } diff --git a/src/repository/training-queue.ts b/src/repository/training-queue.ts index 5d2b58c..9f30ca7 100644 --- a/src/repository/training-queue.ts +++ b/src/repository/training-queue.ts @@ -18,6 +18,10 @@ export type UnitTrainingQueue = { due: number; }; +export type UnitTrainingQueueWithName = { + display: string; +} & UnitTrainingQueue; + export const FriendlyUnitNames = new Map(); FriendlyUnitNames.set(Unit.SOLDIERS, 'Soldiers'); FriendlyUnitNames.set(Unit.ATTACKERS, 'Attackers'); @@ -44,7 +48,7 @@ export class UnitTrainingQueueRepository extends Repository { return data; } - list(owner: string): Promise { - return this.FindAll({owner}).orderBy('due'); + list(owner: string): Promise { + 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 +} -- 2.25.1