Add uniq task scheduler

This commit is contained in:
Anton Vakhrushev 2020-04-04 15:50:56 +03:00
parent d85490958b
commit 153e31463c
2 changed files with 13 additions and 21 deletions

View File

@ -3,7 +3,7 @@ import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask';
import { AbortTaskError, ActionError, BuildingQueueFullError, TryLaterError } from './Errors'; import { AbortTaskError, ActionError, BuildingQueueFullError, TryLaterError } from './Errors';
import { Task, TaskId, TaskList, TaskQueue } from './Storage/TaskQueue'; import { Task, TaskId, TaskList, TaskQueue } from './Storage/TaskQueue';
import { ActionQueue } from './Storage/ActionQueue'; import { ActionQueue } from './Storage/ActionQueue';
import { Command } from './Common'; import { Args, Command } from './Common';
import { TaskQueueRenderer } from './TaskQueueRenderer'; import { TaskQueueRenderer } from './TaskQueueRenderer';
import { createAction } from './Action/ActionController'; import { createAction } from './Action/ActionController';
import { createTask } from './Task/TaskController'; import { createTask } from './Task/TaskController';
@ -31,11 +31,8 @@ export class Scheduler {
this.renderTaskQueue(); this.renderTaskQueue();
setInterval(() => this.renderTaskQueue(), 5 * 1000); setInterval(() => this.renderTaskQueue(), 5 * 1000);
this.scheduleHeroAdventure(); this.scheduleUniqTask(3600, SendOnAdventureTask.name);
setInterval(() => this.scheduleHeroAdventure(), 3600 * 1000); this.scheduleUniqTask(1200, BalanceHeroResourcesTask.name);
this.scheduleResGrab();
setInterval(() => this.scheduleResGrab(), 600 * 1000);
while (true) { while (true) {
await this.doTaskProcessingStep(); await this.doTaskProcessingStep();
@ -47,16 +44,14 @@ export class Scheduler {
new TaskQueueRenderer().render(this.taskQueue.seeItems()); new TaskQueueRenderer().render(this.taskQueue.seeItems());
} }
private scheduleHeroAdventure() { private scheduleUniqTask(seconds: number, name: string, args: Args = {}) {
if (!this.taskQueue.hasNamed(SendOnAdventureTask.name)) { const taskScheduler = () => {
this.taskQueue.push(new Command(SendOnAdventureTask.name, {}), timestamp()); if (!this.taskQueue.hasNamed(name)) {
} this.taskQueue.push(new Command(name, args), timestamp());
}
private scheduleResGrab() {
if (!this.taskQueue.hasNamed(BalanceHeroResourcesTask.name)) {
this.taskQueue.push(new Command(BalanceHeroResourcesTask.name, {}), timestamp());
} }
};
taskScheduler();
setInterval(taskScheduler, seconds * 1000);
} }
private async doTaskProcessingStep() { private async doTaskProcessingStep() {

View File

@ -27,10 +27,6 @@ export class Task {
export type TaskList = Array<Task>; export type TaskList = Array<Task>;
export class TaskQueue { export class TaskQueue {
private static normalize(items: TaskList): TaskList {
return items.sort((x, y) => x.ts - y.ts);
}
push(cmd: Command, ts: number): Task { push(cmd: Command, ts: number): Task {
const id = uniqTaskId(); const id = uniqTaskId();
const task = new Task(id, ts, cmd); const task = new Task(id, ts, cmd);
@ -111,7 +107,8 @@ export class TaskQueue {
} }
private flushItems(items: TaskList): void { private flushItems(items: TaskList): void {
localStorage.setItem(QUEUE_NAME, JSON.stringify(TaskQueue.normalize(items))); const normalized = items.sort((x, y) => x.ts - y.ts);
localStorage.setItem(QUEUE_NAME, JSON.stringify(normalized));
} }
private log(...args) { private log(...args) {