diff --git a/src/Scheduler.ts b/src/Scheduler.ts index a888843..b363b0e 100644 --- a/src/Scheduler.ts +++ b/src/Scheduler.ts @@ -19,6 +19,8 @@ export class Scheduler { this.actionQueue = new ActionQueue(); this.logger = new ConsoleLogger(this.constructor.name); + // this.taskQueue.push(GrabVillageState.name, {}, timestamp()); + this.scheduleUniqTask(3600, SendOnAdventureTask.name); this.scheduleUniqTask(1200, BalanceHeroResourcesTask.name); this.scheduleUniqTask(180, GrabVillageState.name); diff --git a/src/Storage/ActionQueue.ts b/src/Storage/ActionQueue.ts index cd584ee..9972e5c 100644 --- a/src/Storage/ActionQueue.ts +++ b/src/Storage/ActionQueue.ts @@ -1,64 +1,57 @@ import { Command } from '../Common'; import { ConsoleLogger, Logger } from '../Logger'; +import { DataStorage } from './DataStorage'; -const QUEUE_NAME = 'action_queue:v2'; +const NAMESPACE = 'actions.v1'; +const QUEUE_NAME = 'queue'; -class State { - items: Array; - constructor(items: Array) { - this.items = items; - } - - pop(): Command | undefined { - return this.items.shift(); - } - - push(cmd: Command) { - this.items.push(cmd); - } -} +export type ActionList = Array; export class ActionQueue { + private storage: DataStorage; private readonly logger: Logger; constructor() { + this.storage = new DataStorage(NAMESPACE); this.logger = new ConsoleLogger(this.constructor.name); } pop(): Command | undefined { - const state = this.getState(); - const first = state.pop(); - this.flushState(state); + const commands = this.getCommands(); + const first = commands.shift(); + this.flushState(commands); return first; } push(cmd: Command): void { - const state = this.getState(); - state.push(cmd); - this.flushState(state); + const commands = this.getCommands(); + commands.push(cmd); + this.flushState(commands); } - assign(items: Array): void { - this.flushState(new State(items)); + assign(commands: ActionList): void { + this.flushState(commands); } clear(): void { - this.flushState(new State([])); + this.flushState([]); } - private getState(): State { - const serialized = localStorage.getItem(QUEUE_NAME); - if (serialized === null) { - return new State([]); + private getCommands(): ActionList { + const serialized = this.storage.get(QUEUE_NAME); + if (!Array.isArray(serialized)) { + return []; } - let parsed = JSON.parse(serialized) as State; - this.logger.log('STATE', parsed); + const items = serialized as Array<{ [key: string]: any }>; - return new State(parsed.items); + return items.map(i => { + const command = new Command('', {}); + return Object.assign(command, i); + }); } - private flushState(state: State): void { - localStorage.setItem(QUEUE_NAME, JSON.stringify(state)); + private flushState(commands: ActionList): void { + this.storage.set(QUEUE_NAME, commands); } } diff --git a/src/Storage/DataStorage.ts b/src/Storage/DataStorage.ts index 659ae2b..71aa409 100644 --- a/src/Storage/DataStorage.ts +++ b/src/Storage/DataStorage.ts @@ -18,9 +18,9 @@ export class DataStorage { get(key: string): any { const fullKey = join(NAMESPACE, this.name, key); - this.logger.log('GET', key); try { const serialized = localStorage.getItem(fullKey); + this.logger.log('GET', fullKey, serialized); return JSON.parse(serialized || '"null"'); } catch (e) { if (e instanceof SyntaxError) { diff --git a/src/Storage/TaskQueue.ts b/src/Storage/TaskQueue.ts index c9fdd2d..f29944e 100644 --- a/src/Storage/TaskQueue.ts +++ b/src/Storage/TaskQueue.ts @@ -1,8 +1,10 @@ import { Args } from '../Common'; import { uniqId } from '../utils'; import { ConsoleLogger, Logger } from '../Logger'; +import { DataStorage } from './DataStorage'; -const QUEUE_NAME = 'task_queue:v4'; +const NAMESPACE = 'tasks:v1'; +const QUEUE_NAME = 'queue'; export type TaskId = string; @@ -31,8 +33,10 @@ export type TaskList = Array; export class TaskQueue { private readonly logger: Logger; + private storage: DataStorage; constructor() { + this.storage = new DataStorage(NAMESPACE); this.logger = new ConsoleLogger(this.constructor.name); } @@ -111,17 +115,21 @@ export class TaskQueue { } private getItems(): TaskList { - const serialized = localStorage.getItem(QUEUE_NAME); - const storedItems = serialized !== null ? (JSON.parse(serialized) as Array<{ [key: string]: any }>) : []; - const items: TaskList = []; - storedItems.forEach(obj => { - items.push(new Task(obj.id || uniqId(), +obj.ts, obj.name, obj.args)); + const serialized = this.storage.get(QUEUE_NAME); + if (!Array.isArray(serialized)) { + return []; + } + + const storedItems = serialized as Array<{ [key: string]: any }>; + + return storedItems.map(i => { + const task = new Task(uniqId(), 0, '', {}); + return Object.assign(task, i); }); - return items; } private flushItems(items: TaskList): void { const normalized = items.sort((x, y) => x.ts - y.ts); - localStorage.setItem(QUEUE_NAME, JSON.stringify(normalized)); + this.storage.set(QUEUE_NAME, normalized); } }