Add hero adventures task

This commit is contained in:
2020-04-02 22:22:56 +03:00
parent 711c8a414b
commit 4b1e7cb676
15 changed files with 307 additions and 33 deletions

View File

@ -0,0 +1,14 @@
import ActionController from './ActionController';
import { Args } from '../Common';
import { Task } from '../Storage/TaskQueue';
export default class ClickButtonAction extends ActionController {
static NAME = 'click_button';
async run(args: Args, task: Task): Promise<any> {
const el = jQuery(args.selector);
if (el.length === 1) {
console.log('CLICK BUTTON', el);
el.trigger('click');
}
}
}

View File

@ -0,0 +1,17 @@
import ActionController from './ActionController';
import { Args } from '../Common';
import { Task } from '../Storage/TaskQueue';
import Scheduler from '../Scheduler';
export default class CompleteTaskAction extends ActionController {
static NAME = 'complete_task';
private scheduler: Scheduler;
constructor(scheduler: Scheduler) {
super();
this.scheduler = scheduler;
}
async run(args: Args, task: Task): Promise<any> {
this.scheduler.completeTask(task.id);
}
}

View File

@ -0,0 +1,35 @@
import ActionController from './ActionController';
import { Args } from '../Common';
import { Task } from '../Storage/TaskQueue';
import { ActionError } from '../Errors';
import { GameState } from '../Storage/GameState';
export default class GrabHeroAttributesAction extends ActionController {
static NAME = 'grab_hero_attributes';
private state: GameState;
constructor(state: GameState) {
super();
this.state = state;
}
async run(args: Args, task: Task): Promise<any> {
const healthElement = jQuery(
'#attributes .attribute.health .powervalue .value'
);
if (healthElement.length !== 1) {
throw new ActionError(task.id, 'Health dom element not found');
}
const text = healthElement.text();
let normalized = text.replace(/[^0-9]/g, '');
const value = Number(normalized);
if (isNaN(value)) {
throw new ActionError(
task.id,
`Health value "${text}" (${normalized}) couldn't be converted to number`
);
}
this.state.set('hero', { health: value });
}
}

View File

@ -0,0 +1,71 @@
import ActionController from './ActionController';
import { Args } from '../Common';
import { Task } from '../Storage/TaskQueue';
import { GameState } from '../Storage/GameState';
import { trimPrefix } from '../utils';
import { AbortTaskError } from '../Errors';
const HARD = 0;
const NORMAL = 3;
interface Adventure {
el: HTMLElement;
level: number;
}
export default class SendOnAdventureAction extends ActionController {
static NAME = 'send_on_adventure';
private state: GameState;
constructor(state: GameState) {
super();
this.state = state;
}
async run(args: Args, task: Task): Promise<any> {
const adventures = this.findAdventures();
console.log('ADVENTURES', adventures);
adventures.sort((x, y) => x.level - y.level);
const easiest = adventures.shift();
const hero = this.state.get('hero') || {};
console.log('EASIEST', easiest);
console.log('HERO', hero);
if (easiest && hero.health) {
if (easiest.level === NORMAL && hero.health >= 30) {
return jQuery(easiest.el)
.find('td.goTo .gotoAdventure')
.trigger('click');
}
if (easiest.level === HARD && hero.health >= 50) {
return jQuery(easiest.el)
.find('td.goTo .gotoAdventure')
.trigger('click');
}
}
throw new AbortTaskError(task.id, 'No suitable adventure');
}
private findAdventures(): Array<Adventure> {
const adventures: Array<Adventure> = [];
jQuery('tr[id^=adventure]').each((index, el) => {
const imgClass =
jQuery(el)
.find('.difficulty img')
.attr('class')
?.toString() || '';
const level = Number(trimPrefix(imgClass, 'adventureDifficulty'));
if (!isNaN(level)) {
adventures.push({ el, level: level });
}
});
return adventures;
}
}

View File

@ -6,23 +6,15 @@ import { Task } from '../Storage/TaskQueue';
export default class UpgradeBuildingAction extends ActionController {
static NAME = 'upgrade_building';
private scheduler: Scheduler;
constructor(scheduler: Scheduler) {
super();
this.scheduler = scheduler;
}
async run(args: Args, task: Task): Promise<any> {
const btn = jQuery(
'.upgradeButtonsContainer .section1 button.green.build'
);
if (btn.length === 1) {
this.scheduler.completeTask(task.id);
btn.trigger('click');
} else {
if (btn.length !== 1) {
throw new TryLaterError(5 * 60, 'No upgrade button, try later');
}
return null;
btn.trigger('click');
}
}