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);
`;
}
-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">
${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>
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';
return this.buildQueue.list(owner);
}
- async getUnitTrainingQueues(owner: string): Promise<UnitTrainingQueue[]> {
+ async getUnitTrainingQueues(owner: string): Promise<UnitTrainingQueueWithName[]> {
return this.unitTrainigQueue.list(owner);
}
}
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');
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
+}