Improve resource scan

Walk only unique paths
This commit is contained in:
2020-05-04 11:04:29 +03:00
parent d350603c48
commit 789c5aba3e
20 changed files with 113 additions and 58 deletions

View File

@ -1,10 +1,10 @@
import { ActionController, registerAction } from './ActionController';
import { grabVillageList } from '../Page/VillageBlock';
import { grabHeroVillage } from '../Page/HeroPage';
import { path } from '../utils';
import { HeroStorage } from '../Storage/HeroStorage';
import { Args } from '../Queue/Args';
import { Task } from '../Queue/TaskProvider';
import { path } from '../Helpers/Path';
@registerAction
export class GoToHeroVillageAction extends ActionController {

View File

@ -16,11 +16,11 @@ import { ResourcesToLevel } from './Task/ResourcesToLevel';
import { ConsoleLogger, Logger } from './Logger';
import { DataStorage } from './DataStorage';
import { getBuildingPageAttributes, isBuildingPage } from './Page/PageDetectors';
import { debounce } from 'debounce';
import { ExecutionStorage } from './Storage/ExecutionStorage';
import { createVillageStates, VillageState } from './VillageState';
import { Task } from './Queue/TaskProvider';
import { Action } from './Queue/ActionQueue';
import * as _ from 'underscore';
interface QuickAction {
label: string;
@ -110,7 +110,7 @@ export class ControlPanel {
}, 3000);
DataStorage.onChange(
debounce(() => {
_.debounce(() => {
state.refresh();
}, 500)
);

View File

@ -197,10 +197,10 @@
</template>
<script>
import { path } from '../utils';
import ResourceBalance from './ResourceBalance';
import VillageResource from './VillageResource';
import { COLLECTION_POINT_ID, HORSE_STABLE_ID, MARKET_ID, QUARTERS_ID, WAREHOUSE_ID } from '../Core/Buildings';
import { path } from '../Helpers/Path';
export default {
components: {

34
src/Helpers/Path.ts Normal file
View File

@ -0,0 +1,34 @@
export interface PathQuery {
[key: string]: string | number | undefined;
}
export interface PathDefinition {
name: string;
query: PathQuery;
}
export type PathList = Array<PathDefinition>;
export function path(base: string, query: PathQuery = {}): string {
let parts: string[] = [];
for (let name of Object.keys(query)) {
if (query[name] !== undefined) {
parts.push(`${name}=${query[name]}`);
}
}
return base + (parts.length ? '?' + parts.join('&') : '');
}
export function uniqPaths(paths: PathList): PathList {
const keys: { [k: string]: boolean | undefined } = {};
const result: PathList = [];
for (let path of paths) {
const pathKey = JSON.stringify(path);
if (keys[pathKey]) {
continue;
}
keys[pathKey] = true;
result.push(path);
}
return result;
}

View File

@ -30,7 +30,7 @@ export function getBuildingPageAttributes(): BuildingPageAttributes {
buildTypeId: getNumber(elClassId($buildEl.attr('class'), 'gid')),
level: getNumber(elClassId($buildEl.attr('class'), 'level')),
buildId: getNumber(p.query.id) || undefined,
categoryId: getNumber(p.query.category, 1),
categoryId: getNumber(p.query.category) || 1,
sheetId: getNumber(p.query.s) || undefined,
tabId: getNumber(p.query.t) || undefined,
};

View File

@ -1,8 +1,8 @@
import { ActionDefinition } from './TaskController';
import { grabVillageList } from '../Page/VillageBlock';
import { GoToPageAction } from '../Action/GoToPageAction';
import { path } from '../utils';
import { MARKET_ID } from '../Core/Buildings';
import { path } from '../Helpers/Path';
export function scanAllVillagesBundle(): Array<ActionDefinition> {
const actions: Array<ActionDefinition> = [];

View File

@ -2,11 +2,11 @@ import { TaskController, registerTask } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction';
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
import { BalanceHeroResourcesAction } from '../Action/BalanceHeroResourcesAction';
import { path } from '../utils';
import { GoToHeroVillageAction } from '../Action/GoToHeroVillageAction';
import { Action } from '../Queue/ActionQueue';
import { Args } from '../Queue/Args';
import { Task } from '../Queue/TaskProvider';
import { path } from '../Helpers/Path';
@registerTask
export class BalanceHeroResourcesTask extends TaskController {

View File

@ -1,8 +1,8 @@
import { BuildBuildingAction } from '../Action/BuildBuildingAction';
import { GoToPageAction } from '../Action/GoToPageAction';
import { path } from '../utils';
import { TaskController, registerTask, ActionDefinition } from './TaskController';
import { Task } from '../Queue/TaskProvider';
import { path } from '../Helpers/Path';
@registerTask
export class BuildBuildingTask extends TaskController {

View File

@ -1,8 +1,8 @@
import { TaskController, registerTask, ActionDefinition } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction';
import { path } from '../utils';
import { Task } from '../Queue/TaskProvider';
import { ForgeImprovementAction } from '../Action/ForgeImprovementAction';
import { path } from '../Helpers/Path';
@registerTask
export class ForgeImprovementTask extends TaskController {

View File

@ -1,8 +1,8 @@
import { TaskController, registerTask, ActionDefinition } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction';
import { path } from '../utils';
import { UpgradeResourceToLevel } from '../Action/UpgradeResourceToLevel';
import { Task } from '../Queue/TaskProvider';
import { path } from '../Helpers/Path';
@registerTask
export class ResourcesToLevel extends TaskController {

View File

@ -3,10 +3,10 @@ import { GoToPageAction } from '../Action/GoToPageAction';
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
import { SendOnAdventureAction } from '../Action/SendOnAdventureAction';
import { ClickButtonAction } from '../Action/ClickButtonAction';
import { path } from '../utils';
import { Action } from '../Queue/ActionQueue';
import { Args } from '../Queue/Args';
import { Task } from '../Queue/TaskProvider';
import { path } from '../Helpers/Path';
@registerTask
export class SendOnAdventureTask extends TaskController {

View File

@ -1,12 +1,12 @@
import { TaskController, registerTask, ActionDefinition } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction';
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
import { path } from '../utils';
import { SendResourcesAction } from '../Action/SendResourcesAction';
import { ClickButtonAction } from '../Action/ClickButtonAction';
import { scanAllVillagesBundle } from './ActionBundles';
import { Args } from '../Queue/Args';
import { Task } from '../Queue/TaskProvider';
import { path } from '../Helpers/Path';
@registerTask
export class SendResourcesTask extends TaskController {

View File

@ -2,10 +2,10 @@ import { registerTask, TaskController } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction';
import { CompleteTaskAction } from '../Action/CompleteTaskAction';
import { TrainTrooperAction } from '../Action/TrainTrooperAction';
import { path } from '../utils';
import { Action } from '../Queue/ActionQueue';
import { Args } from '../Queue/Args';
import { Task } from '../Queue/TaskProvider';
import { path } from '../Helpers/Path';
@registerTask
export class TrainTroopTask extends TaskController {

View File

@ -1,33 +1,36 @@
import { TaskController, registerTask, ActionDefinition } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction';
import { path } from '../utils';
import { UpgradeBuildingTask } from './UpgradeBuildingTask';
import { ImmutableTaskList, Task } from '../Queue/TaskProvider';
import { ForgeImprovementTask } from './ForgeImprovementTask';
import { path, PathList, uniqPaths } from '../Helpers/Path';
@registerTask
export class UpdateResourceContracts extends TaskController {
defineActions(task: Task): Array<ActionDefinition> {
const tasks = this.scheduler.getTaskItems();
return [...this.walkUpgradeTasks(tasks), ...this.walkImprovementTask(tasks)];
const paths = [...this.walkUpgradeTasks(tasks), ...this.walkImprovementTask(tasks)];
const uniq = uniqPaths(paths);
return uniq.map(p => [GoToPageAction.name, { path: path(p.name, p.query) }]);
}
private walkUpgradeTasks(tasks: ImmutableTaskList): Array<ActionDefinition> {
private walkUpgradeTasks(tasks: ImmutableTaskList): PathList {
return tasks
.filter(t => t.name === UpgradeBuildingTask.name && t.args.villageId && t.args.buildId)
.map(t => [
GoToPageAction.name,
{ path: path('/build.php', { newdid: t.args.villageId, id: t.args.buildId }) },
]);
.map(t => ({
name: '/build.php',
query: { newdid: t.args.villageId, id: t.args.buildId },
}));
}
private walkImprovementTask(tasks: ImmutableTaskList): Array<ActionDefinition> {
private walkImprovementTask(tasks: ImmutableTaskList): PathList {
return tasks
.filter(t => t.name === ForgeImprovementTask.name && t.args.villageId && t.args.buildId)
.map(t => [
GoToPageAction.name,
{ path: path('/build.php', { newdid: t.args.villageId, id: t.args.buildId }) },
]);
.map(t => ({
name: '/build.php',
query: { newdid: t.args.villageId, id: t.args.buildId },
}));
}
}

View File

@ -1,8 +1,8 @@
import { UpgradeBuildingAction } from '../Action/UpgradeBuildingAction';
import { TaskController, registerTask, ActionDefinition } from './TaskController';
import { GoToPageAction } from '../Action/GoToPageAction';
import { path } from '../utils';
import { Task } from '../Queue/TaskProvider';
import { path } from '../Helpers/Path';
@registerTask
export class UpgradeBuildingTask extends TaskController {

View File

@ -92,25 +92,11 @@ export function parseLocation(location?: string | undefined) {
return new URLParse(location || window.location.href, true);
}
export function path(p: string, query: { [key: string]: string | number | undefined } = {}) {
let parts: string[] = [];
for (let k in query) {
if (query[k] !== undefined) {
parts.push(`${k}=${query[k]}`);
}
}
return p + (parts.length ? '?' + parts.join('&') : '');
}
export function notify(msg: string): void {
const n = new Notification(msg);
setTimeout(() => n && n.close(), 4000);
}
export interface NowTimeGenerator {
(): number;
}
export function markPage(text: string, version: string) {
jQuery('body').append(
'<div style="' +