Rewrite hero resource action

try to detect hero village before balancing
This commit is contained in:
2020-04-11 13:46:10 +03:00
parent 8e8b358b91
commit 2d9c3f94e7
12 changed files with 279 additions and 76 deletions

View File

@ -1,71 +1,30 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Common';
import { Task } from '../Storage/TaskQueue';
import { getNumber, trimPrefix } from '../utils';
import { AbortTaskError, ActionError } from '../Errors';
interface Resource {
type: number;
value: number;
}
const ALL = 0;
import { grabResources } from '../Page/ResourcesBlock';
import { changeHeroResource, grabCurrentHeroResource } from '../Page/HeroPage';
import { HeroAllResources } from '../Game';
@registerAction
export class BalanceHeroResourcesAction extends ActionController {
async run(args: Args, task: Task): Promise<any> {
const res = this.getResources();
const currentType = this.getCurrentHeroResource(task);
console.log('RESOURCES', res);
const resources = grabResources().asList();
const currentType = grabCurrentHeroResource();
console.log('RESOURCES', resources);
console.log('CURRENT TYPE', currentType);
const sorted = res.sort((x, y) => x.value - y.value);
const sorted = resources.sort((x, y) => x.value - y.value);
const min = sorted[0];
const max = sorted[sorted.length - 1];
const delta = max.value - min.value;
const eps = max.value / 10;
console.log('MIN', min, 'MAX', max, 'DELTA', delta, 'EPS', eps);
const resType = delta > eps ? min.type : ALL;
const resType = delta > eps ? min.type : HeroAllResources;
if (resType !== currentType) {
this.changeToHeroResource(task, resType);
changeHeroResource(resType);
}
}
private getResources(): Array<Resource> {
const res = this.state.get('resources');
const resList: Array<Resource> = [];
for (let r in res) {
const type = getNumber(r);
const value = getNumber(res[r]);
resList.push({ type, value });
}
return resList;
}
private getCurrentHeroResource(task: Task): number {
for (let type of [0, 1, 2, 3, 4]) {
const input = jQuery(`#resourceHero${type}`);
if (input.length !== 1) {
throw new ActionError(task.id, `Hero resource ${type} not found`);
}
if (input.prop('checked')) {
return type;
}
}
return 0;
}
private changeToHeroResource(task: Task, type: number) {
const input = jQuery(`#resourceHero${type}`);
if (input.length !== 1) {
throw new ActionError(task.id, `Hero resource ${type} not found`);
}
const btn = jQuery('#saveHeroAttributes');
if (btn.length !== 1) {
throw new ActionError(task.id, `Hero resource button not found`);
}
input.trigger('click');
btn.trigger('click');
}
}

View File

@ -2,7 +2,7 @@ import { ActionController, registerAction } from './ActionController';
import { Args } from '../Common';
import { Task } from '../Storage/TaskQueue';
import { BuildingQueueFullError } from '../Errors';
import { grabActiveVillageId } from '../Page/EveryPage';
import { grabActiveVillageId } from '../Page/VillageBlock';
@registerAction
export class CheckBuildingRemainingTimeAction extends ActionController {

View File

@ -0,0 +1,32 @@
import { ActionController, registerAction } from './ActionController';
import { Args } from '../Common';
import { Task } from '../Storage/TaskQueue';
import { grabVillageList } from '../Page/VillageBlock';
import { grabHeroVillage } from '../Page/HeroPage';
import { path } from '../utils';
@registerAction
export class GoToHeroVillageAction extends ActionController {
async run(args: Args, task: Task): Promise<any> {
const heroVillageId = this.getHeroVillageId();
if (heroVillageId) {
window.location.assign(path('/hero.php', { newdid: heroVillageId }));
}
}
private getHeroVillageId(): number | undefined {
const villages = grabVillageList();
const heroVillage = grabHeroVillage();
console.log('VILLAGES', villages);
console.log('HERO VILLAGE', heroVillage);
for (let village of villages) {
if (village.name === heroVillage) {
return village.id;
}
}
return undefined;
}
}