Add ability to check building queue
This commit is contained in:
parent
cd2bc8d7fb
commit
473e3572e3
21
src/Action/CheckBuildingRemainingTimeAction.ts
Normal file
21
src/Action/CheckBuildingRemainingTimeAction.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import ActionController from './ActionController';
|
||||||
|
import { Args } from '../Common';
|
||||||
|
import { Task } from '../Storage/TaskQueue';
|
||||||
|
import { TryLaterError } from '../Errors';
|
||||||
|
|
||||||
|
export default class CheckBuildingRemainingTimeAction extends ActionController {
|
||||||
|
static NAME = 'check_building_remaining_time';
|
||||||
|
|
||||||
|
async run(args: Args, task: Task): Promise<any> {
|
||||||
|
const timer = jQuery('.buildDuration .timer');
|
||||||
|
if (timer.length === 1) {
|
||||||
|
const remainingSeconds = Number(timer.attr('value'));
|
||||||
|
if (remainingSeconds > 0) {
|
||||||
|
throw new TryLaterError(
|
||||||
|
remainingSeconds + 1,
|
||||||
|
'Building queue is full'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,9 +2,9 @@ import ActionController from './ActionController';
|
|||||||
import { Args } from '../Common';
|
import { Args } from '../Common';
|
||||||
import { Task } from '../Storage/TaskQueue';
|
import { Task } from '../Storage/TaskQueue';
|
||||||
|
|
||||||
export default class GoToResourceFieldsAction extends ActionController {
|
export default class GoToPageAction extends ActionController {
|
||||||
static NAME = 'go_to_resource_fields';
|
static NAME = 'go_to_page';
|
||||||
async run(args: Args, task: Task): Promise<any> {
|
async run(args: Args, task: Task): Promise<any> {
|
||||||
window.location.assign('/dorf1.php');
|
window.location.assign(args.path);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,15 +0,0 @@
|
|||||||
import ActionController from './ActionController';
|
|
||||||
import { Args } from '../Common';
|
|
||||||
import { Task } from '../Storage/TaskQueue';
|
|
||||||
|
|
||||||
export default class StoreRemainingBuildTimeAction extends ActionController {
|
|
||||||
static NAME = 'store_remaining_build_time';
|
|
||||||
|
|
||||||
async run(args: Args, task: Task): Promise<any> {
|
|
||||||
const timer = jQuery('.buildDuration .timer');
|
|
||||||
// if (timer.length === 1) {
|
|
||||||
// const remainingSeconds = +timer.val();
|
|
||||||
// }
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
1
src/GameState.ts
Normal file
1
src/GameState.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default class GameState {}
|
@ -1,4 +1,4 @@
|
|||||||
import { markPage, sleepLong, sleepShort, timestamp } from './utils';
|
import { markPage, sleepShort, timestamp } from './utils';
|
||||||
import UpgradeBuildingTask from './Task/UpgradeBuildingTask';
|
import UpgradeBuildingTask from './Task/UpgradeBuildingTask';
|
||||||
import GoToBuildingAction from './Action/GoToBuildingAction';
|
import GoToBuildingAction from './Action/GoToBuildingAction';
|
||||||
import UpgradeBuildingAction from './Action/UpgradeBuildingAction';
|
import UpgradeBuildingAction from './Action/UpgradeBuildingAction';
|
||||||
@ -9,41 +9,38 @@ import { Args, Command } from './Common';
|
|||||||
import TaskQueueRenderer from './TaskQueueRenderer';
|
import TaskQueueRenderer from './TaskQueueRenderer';
|
||||||
import ActionController from './Action/ActionController';
|
import ActionController from './Action/ActionController';
|
||||||
import TaskController from './Task/TaskController';
|
import TaskController from './Task/TaskController';
|
||||||
|
import GoToPageAction from './Action/GoToPageAction';
|
||||||
enum SleepType {
|
import CheckBuildingRemainingTimeAction from './Action/CheckBuildingRemainingTimeAction';
|
||||||
Long,
|
|
||||||
Short,
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class Scheduler {
|
export default class Scheduler {
|
||||||
private readonly version: string;
|
private readonly version: string;
|
||||||
private taskQueue: TaskQueue;
|
private taskQueue: TaskQueue;
|
||||||
private actionQueue: ActionQueue;
|
private actionQueue: ActionQueue;
|
||||||
private sleepType: SleepType;
|
|
||||||
|
|
||||||
constructor(version: string) {
|
constructor(version: string) {
|
||||||
this.version = version;
|
this.version = version;
|
||||||
this.taskQueue = new TaskQueue();
|
this.taskQueue = new TaskQueue();
|
||||||
this.actionQueue = new ActionQueue();
|
this.actionQueue = new ActionQueue();
|
||||||
this.sleepType = SleepType.Short;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async run() {
|
async run() {
|
||||||
await sleepShort();
|
await sleepShort();
|
||||||
markPage('Executor', this.version);
|
markPage('Executor', this.version);
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => this.renderTaskQueue(), 5000);
|
||||||
this.log('RENDER TASK QUEUE');
|
|
||||||
new TaskQueueRenderer().render(this.taskQueue.seeItems());
|
|
||||||
}, 1000);
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
await this.doLoopStep();
|
await this.doLoopStep();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private renderTaskQueue() {
|
||||||
|
this.log('RENDER TASK QUEUE');
|
||||||
|
new TaskQueueRenderer().render(this.taskQueue.seeItems());
|
||||||
|
}
|
||||||
|
|
||||||
private async doLoopStep() {
|
private async doLoopStep() {
|
||||||
await this.sleep();
|
await sleepShort();
|
||||||
const currentTs = timestamp();
|
const currentTs = timestamp();
|
||||||
const taskCommand = this.taskQueue.get(currentTs);
|
const taskCommand = this.taskQueue.get(currentTs);
|
||||||
|
|
||||||
@ -68,33 +65,22 @@ export default class Scheduler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async processTaskCommand(task: Task) {
|
||||||
|
const taskController = this.createTaskControllerByName(task.cmd.name);
|
||||||
|
this.log('PROCESS TASK CONTROLLER', taskController, task);
|
||||||
|
if (taskController) {
|
||||||
|
taskController.run(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async processActionCommand(cmd: Command, task: Task) {
|
private async processActionCommand(cmd: Command, task: Task) {
|
||||||
const actionController = this.createActionControllerByName(cmd.name);
|
const actionController = this.createActionControllerByName(cmd.name);
|
||||||
this.log('PROCESS ACTION CTR', actionController);
|
this.log('PROCESS ACTION CONTROLLER', cmd.name, actionController);
|
||||||
if (actionController) {
|
if (actionController) {
|
||||||
await this.runAction(actionController, cmd.args, task);
|
await this.runAction(actionController, cmd.args, task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async processTaskCommand(task: Task) {
|
|
||||||
const taskController = this.createTaskControllerByName(task.cmd.name);
|
|
||||||
this.log('PROCESS TASK CTR', taskController, task);
|
|
||||||
taskController?.run(task);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async sleep() {
|
|
||||||
if (this.sleepType === SleepType.Long) {
|
|
||||||
await sleepLong();
|
|
||||||
} else {
|
|
||||||
await sleepShort();
|
|
||||||
}
|
|
||||||
this.sleepType = SleepType.Short;
|
|
||||||
}
|
|
||||||
|
|
||||||
private nextSleepLong() {
|
|
||||||
this.sleepType = SleepType.Long;
|
|
||||||
}
|
|
||||||
|
|
||||||
getTaskItems(): TaskList {
|
getTaskItems(): TaskList {
|
||||||
return this.taskQueue.seeItems();
|
return this.taskQueue.seeItems();
|
||||||
}
|
}
|
||||||
@ -119,7 +105,7 @@ export default class Scheduler {
|
|||||||
case UpgradeBuildingTask.NAME:
|
case UpgradeBuildingTask.NAME:
|
||||||
return new UpgradeBuildingTask(this);
|
return new UpgradeBuildingTask(this);
|
||||||
}
|
}
|
||||||
this.log('UNKNOWN TASK', taskName);
|
this.logError('TASK NOT FOUND', taskName);
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,7 +114,6 @@ export default class Scheduler {
|
|||||||
if (actionItem === undefined) {
|
if (actionItem === undefined) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
this.log('UNKNOWN ACTION', actionItem.name);
|
|
||||||
return actionItem;
|
return actionItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,6 +126,13 @@ export default class Scheduler {
|
|||||||
if (actonName === UpgradeBuildingAction.NAME) {
|
if (actonName === UpgradeBuildingAction.NAME) {
|
||||||
return new UpgradeBuildingAction(this);
|
return new UpgradeBuildingAction(this);
|
||||||
}
|
}
|
||||||
|
if (actonName === GoToPageAction.NAME) {
|
||||||
|
return new GoToPageAction();
|
||||||
|
}
|
||||||
|
if (actonName === CheckBuildingRemainingTimeAction.NAME) {
|
||||||
|
return new CheckBuildingRemainingTimeAction();
|
||||||
|
}
|
||||||
|
this.logError('ACTION NOT FOUND', actonName);
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,7 +145,6 @@ export default class Scheduler {
|
|||||||
console.warn('TRY', task.id, 'AFTER', e.seconds);
|
console.warn('TRY', task.id, 'AFTER', e.seconds);
|
||||||
this.actionQueue.clear();
|
this.actionQueue.clear();
|
||||||
this.taskQueue.postpone(task.id, timestamp() + e.seconds);
|
this.taskQueue.postpone(task.id, timestamp() + e.seconds);
|
||||||
this.nextSleepLong();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -161,4 +152,8 @@ export default class Scheduler {
|
|||||||
private log(...args) {
|
private log(...args) {
|
||||||
console.log('SCHEDULER:', ...args);
|
console.log('SCHEDULER:', ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private logError(...args) {
|
||||||
|
console.error(...args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ import UpgradeBuildingAction from '../Action/UpgradeBuildingAction';
|
|||||||
import { Command } from '../Common';
|
import { Command } from '../Common';
|
||||||
import { Task } from '../Storage/TaskQueue';
|
import { Task } from '../Storage/TaskQueue';
|
||||||
import TaskController from './TaskController';
|
import TaskController from './TaskController';
|
||||||
|
import GoToPageAction from '../Action/GoToPageAction';
|
||||||
|
import CheckBuildingRemainingTimeAction from '../Action/CheckBuildingRemainingTimeAction';
|
||||||
|
|
||||||
export default class UpgradeBuildingTask extends TaskController {
|
export default class UpgradeBuildingTask extends TaskController {
|
||||||
static NAME = 'upgrade_building';
|
static NAME = 'upgrade_building';
|
||||||
@ -18,6 +20,8 @@ export default class UpgradeBuildingTask extends TaskController {
|
|||||||
console.log('RUN', UpgradeBuildingTask.NAME, 'with', task);
|
console.log('RUN', UpgradeBuildingTask.NAME, 'with', task);
|
||||||
const args = { ...task.cmd.args, taskId: task.id };
|
const args = { ...task.cmd.args, taskId: task.id };
|
||||||
this.scheduler.scheduleActions([
|
this.scheduler.scheduleActions([
|
||||||
|
new Command(GoToPageAction.NAME, { ...args, path: '/dorf1.php' }),
|
||||||
|
new Command(CheckBuildingRemainingTimeAction.NAME, args),
|
||||||
new Command(GoToBuildingAction.NAME, args),
|
new Command(GoToBuildingAction.NAME, args),
|
||||||
new Command(UpgradeBuildingAction.NAME, args),
|
new Command(UpgradeBuildingAction.NAME, args),
|
||||||
]);
|
]);
|
||||||
|
@ -8,13 +8,13 @@ export function sleep(ms: number) {
|
|||||||
|
|
||||||
export async function sleepShort() {
|
export async function sleepShort() {
|
||||||
let ms = 3000 + Math.random() * 1000;
|
let ms = 3000 + Math.random() * 1000;
|
||||||
console.log('SLEEP SHORT', Math.round(ms));
|
console.log('SLEEP SHORT', Math.round(ms / 1000));
|
||||||
return await sleep(ms);
|
return await sleep(ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function sleepLong() {
|
export async function sleepLong() {
|
||||||
let ms = 120_000 + Math.random() * 300_000;
|
let ms = 120_000 + Math.random() * 300_000;
|
||||||
console.log('SLEEP LONG', Math.round(ms));
|
console.log('SLEEP LONG', Math.round(ms / 1000));
|
||||||
return await sleep(ms);
|
return await sleep(ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user