From a6cc1b53835af0294f046e591220b26f2a74aaae Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sat, 18 Apr 2020 21:47:05 +0300 Subject: [PATCH] Change task id generation Id must be generated sequentially --- .../CheckBuildingRemainingTimeAction.ts | 4 +-- src/Action/UpgradeBuildingAction.ts | 9 ++++-- src/DashboardView/TaskList.vue | 31 +++++++++++++++---- src/DashboardView/VillageStateList.vue | 10 +++--- src/Errors.ts | 4 +-- src/Executor.ts | 4 +-- src/Storage/TaskQueue.ts | 11 ++++++- src/utils.ts | 4 +-- 8 files changed, 55 insertions(+), 22 deletions(-) diff --git a/src/Action/CheckBuildingRemainingTimeAction.ts b/src/Action/CheckBuildingRemainingTimeAction.ts index 9e8facd..415fe83 100644 --- a/src/Action/CheckBuildingRemainingTimeAction.ts +++ b/src/Action/CheckBuildingRemainingTimeAction.ts @@ -1,7 +1,7 @@ import { ActionController, registerAction } from './ActionController'; import { Args } from '../Common'; import { Task } from '../Storage/TaskQueue'; -import { BuildingQueueFullError, GrabError } from '../Errors'; +import { PostponeAllBuildingsError, GrabError } from '../Errors'; import { grabActiveVillageId, grabBuildingQueueInfo } from '../Page/VillageBlock'; import { BuildingQueueInfo } from '../Game'; @@ -10,7 +10,7 @@ export class CheckBuildingRemainingTimeAction extends ActionController { async run(args: Args, task: Task): Promise { const info = this.grabBuildingQueueInfoOrDefault(); if (info.seconds > 0) { - throw new BuildingQueueFullError( + throw new PostponeAllBuildingsError( task.id, grabActiveVillageId(), info.seconds + 1, diff --git a/src/Action/UpgradeBuildingAction.ts b/src/Action/UpgradeBuildingAction.ts index f22a49e..cf9b8fe 100644 --- a/src/Action/UpgradeBuildingAction.ts +++ b/src/Action/UpgradeBuildingAction.ts @@ -1,17 +1,22 @@ import { ActionController, registerAction } from './ActionController'; import { Args } from '../Common'; -import { GrabError, TryLaterError } from '../Errors'; +import { ActionError, GrabError, PostponeAllBuildingsError } from '../Errors'; import { Task } from '../Storage/TaskQueue'; import { clickUpgradeButton } from '../Page/BuildingPage'; @registerAction export class UpgradeBuildingAction extends ActionController { async run(args: Args, task: Task): Promise { + let villageId = args.villageId; + if (villageId === undefined) { + throw new ActionError(task.id, 'No village id'); + } + try { clickUpgradeButton(); } catch (e) { if (e instanceof GrabError) { - throw new TryLaterError(task.id, 15 * 60, 'No upgrade button, try later'); + throw new PostponeAllBuildingsError(task.id, villageId, 15 * 60, 'No upgrade button, try later'); } throw e; } diff --git a/src/DashboardView/TaskList.vue b/src/DashboardView/TaskList.vue index 9381bb4..3291372 100644 --- a/src/DashboardView/TaskList.vue +++ b/src/DashboardView/TaskList.vue @@ -4,13 +4,13 @@
- - - + + - - + +
{{ formatDate(task.ts) }}{{ task.id }} + {{ formatDate(task.ts) }}{{ task.id }} × {{ task.name }}{{ JSON.stringify(task.args) }}{{ task.name }}{{ JSON.stringify(task.args) }}
@@ -66,8 +66,8 @@ export default { } .task-item > td { border-top: 1px solid #ddd; + border-left: 1px solid #ddd; padding: 2px 4px; - max-width: 25%; } .this-village { color: blue; @@ -76,4 +76,23 @@ export default { font-weight: bold; color: red; } +.time-column, +.actions-column { + max-width: 25%; +} +.id-column { + max-width: 80px; + overflow-x: hidden; + text-overflow: ellipsis; +} +.name-column { + max-width: 80px; + overflow-x: hidden; + text-overflow: ellipsis; +} +.args-column { + white-space: nowrap; + overflow-x: hidden; + text-overflow: ellipsis; +} diff --git a/src/DashboardView/VillageStateList.vue b/src/DashboardView/VillageStateList.vue index f9f0a12..a77c3bc 100644 --- a/src/DashboardView/VillageStateList.vue +++ b/src/DashboardView/VillageStateList.vue @@ -51,11 +51,11 @@ - Необх: - - - - + Профицит: + + + + diff --git a/src/Errors.ts b/src/Errors.ts index 2090d48..ecab3e7 100644 --- a/src/Errors.ts +++ b/src/Errors.ts @@ -37,7 +37,7 @@ export class TryLaterError extends Error { } } -export class BuildingQueueFullError extends Error { +export class PostponeAllBuildingsError extends Error { readonly seconds: number; readonly villageId: number; readonly taskId: TaskId; @@ -47,6 +47,6 @@ export class BuildingQueueFullError extends Error { this.villageId = villageId; this.taskId = taskId; this.seconds = seconds; - Object.setPrototypeOf(this, BuildingQueueFullError.prototype); + Object.setPrototypeOf(this, PostponeAllBuildingsError.prototype); } } diff --git a/src/Executor.ts b/src/Executor.ts index b564cb3..e278f66 100644 --- a/src/Executor.ts +++ b/src/Executor.ts @@ -1,5 +1,5 @@ import { markPage, sleepMicro, timestamp, waitForLoad } from './utils'; -import { AbortTaskError, ActionError, BuildingQueueFullError, TryLaterError } from './Errors'; +import { AbortTaskError, ActionError, PostponeAllBuildingsError, TryLaterError } from './Errors'; import { Task } from './Storage/TaskQueue'; import { Command } from './Common'; import { TaskQueueRenderer } from './TaskQueueRenderer'; @@ -108,7 +108,7 @@ export class Executor { return; } - if (err instanceof BuildingQueueFullError) { + if (err instanceof PostponeAllBuildingsError) { this.logger.warn('BUILDING QUEUE FULL, TRY ALL AFTER', err.seconds); this.scheduler.postponeBuildingsInVillage(err.villageId, err.seconds); return; diff --git a/src/Storage/TaskQueue.ts b/src/Storage/TaskQueue.ts index 7b7133d..04232b6 100644 --- a/src/Storage/TaskQueue.ts +++ b/src/Storage/TaskQueue.ts @@ -8,8 +8,17 @@ const QUEUE_NAME = 'queue'; export type TaskId = string; +let idSequence = 1; +let lastTimestamp = null; + function uniqTaskId(): TaskId { - return uniqId(); + const ts = Math.floor(Date.now() / 1000); + if (ts === lastTimestamp) { + ++idSequence; + } else { + idSequence = 1; + } + return 'tid.' + ts + '.' + String(idSequence).padStart(4, '0') + '.' + uniqId(''); } export class Task { diff --git a/src/utils.ts b/src/utils.ts index af67a84..37d5e08 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -44,8 +44,8 @@ function generateId(count: number): string { return str; } -export function uniqId(): string { - return 'id' + generateId(6); +export function uniqId(prefix: string = 'id'): string { + return prefix + generateId(6); } export function timestamp(): number {