Improve resource scan

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

30
package-lock.json generated
View File

@ -336,12 +336,6 @@
"integrity": "sha512-KlPPdikagvL6ELjWsljbyDIPzNCeliYkqRpI+zea99vBBbCIA5JNshZAwQKTON139c87y9qvTFVgkFd14rtS4g==",
"dev": true
},
"@types/debounce": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@types/debounce/-/debounce-1.2.0.tgz",
"integrity": "sha512-bWG5wapaWgbss9E238T0R6bfo5Fh3OkeoSt245CM7JJwVwpw6MEBCbIxLq5z8KzsE3uJhzcIuQkyiZmzV3M/Dw==",
"dev": true
},
"@types/jquery": {
"version": "3.3.34",
"resolved": "https://registry.npmjs.org/@types/jquery/-/jquery-3.3.34.tgz",
@ -369,6 +363,12 @@
"integrity": "sha512-7EJYyKTL7tFR8+gDbB6Wwz/arpGa0Mywk1TJbNzKzHtzbwVmY4HR9WqS5VV7dsBUKQmPNr192jHr/VpBluj/hg==",
"dev": true
},
"@types/underscore": {
"version": "1.9.4",
"resolved": "https://registry.npmjs.org/@types/underscore/-/underscore-1.9.4.tgz",
"integrity": "sha512-CjHWEMECc2/UxOZh0kpiz3lEyX2Px3rQS9HzD20lxMvx571ivOBQKeLnqEjxUY0BMgp6WJWo/pQLRBwMW5v4WQ==",
"dev": true
},
"@types/url-parse": {
"version": "1.4.3",
"resolved": "https://registry.npmjs.org/@types/url-parse/-/url-parse-1.4.3.tgz",
@ -2088,12 +2088,6 @@
"integrity": "sha1-sgOOhG3DO6pXlhKNCAS0VbjB4h0=",
"dev": true
},
"debounce": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/debounce/-/debounce-1.2.0.tgz",
"integrity": "sha512-mYtLl1xfZLi1m4RtQYlZgJUNQjl4ZxVnHzIR8nLLgi4q1YT8o/WM+MK/f8yfcc9s5Ir5zRaPZyZU6xs1Syoocg==",
"dev": true
},
"debug": {
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
@ -4470,9 +4464,9 @@
}
},
"yargs-parser": {
"version": "18.1.1",
"resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.1.tgz",
"integrity": "sha512-KRHEsOM16IX7XuLnMOqImcPNbLVXMNHYAoFc3BKR8Ortl5gzDbtXvvEoGx9imk5E+X1VeNKNlcHr8B8vi+7ipA==",
"version": "18.1.3",
"resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.3.tgz",
"integrity": "sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==",
"dev": true,
"requires": {
"camelcase": "^5.0.0",
@ -6043,6 +6037,12 @@
"integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==",
"dev": true
},
"underscore": {
"version": "1.10.2",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.10.2.tgz",
"integrity": "sha512-N4P+Q/BuyuEKFJ43B9gYuOj4TQUHXX+j2FqguVOpjkssLUUrnJofCcBccJSCoeturDoZU6GorDTHSvUDlSQbTg==",
"dev": true
},
"union-value": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/union-value/-/union-value-1.0.1.tgz",

View File

@ -23,15 +23,14 @@
"devDependencies": {
"@types/chai": "^4.2.11",
"@types/dateformat": "^3.0.1",
"@types/debounce": "^1.2.0",
"@types/jquery": "^3.3.34",
"@types/mocha": "^7.0.2",
"@types/node": "^13.9.4",
"@types/underscore": "^1.9.4",
"@types/url-parse": "^1.4.3",
"chai": "^4.2.0",
"css-loader": "^3.5.2",
"dateformat": "^3.0.3",
"debounce": "^1.2.0",
"jquery": "^3.4.1",
"mocha": "^7.1.1",
"mocha-junit-reporter": "^1.23.3",
@ -42,6 +41,7 @@
"ts-loader": "^6.2.2",
"ts-node": "^8.8.1",
"typescript": "^3.8.3",
"underscore": "^1.10.2",
"url-parse": "^1.4.7",
"vue": "^2.6.11",
"vue-loader": "^15.9.1",

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="' +

32
tests/Helpers/PathTest.ts Normal file
View File

@ -0,0 +1,32 @@
import { it, describe } from 'mocha';
import { expect } from 'chai';
import { path, uniqPaths } from '../../src/Helpers/Path';
describe('Path', function() {
it('Can build path with empty query', function() {
const p = path('/info.php');
expect(p).to.be.equals('/info.php');
});
it('Can build path with query', function() {
const p = path('/info.php', { foo: 'bar' });
expect(p).to.be.equals('/info.php?foo=bar');
});
it('Can build path with complex query', function() {
const p = path('/info.php', { a: 'x', b: 'y', c: 5 });
expect(p).to.be.equals('/info.php?a=x&b=y&c=5');
});
it('Can build path with query (undefined part)', function() {
const p = path('/info.php', { foo: 'bar', foobar: undefined });
expect(p).to.be.equals('/info.php?foo=bar');
});
it('Can keep uniq paths', function() {
const p1 = { name: '/info.php', query: { a: 'b' } };
const p2 = { name: '/info.php', query: { a: 'b' } };
const up = uniqPaths([p1, p2]);
expect(up).to.has.lengthOf(1);
});
});

View File

@ -9,7 +9,7 @@
"strictNullChecks": true,
"strictPropertyInitialization": true,
"target": "es2018",
"types": ["node", "url-parse", "jquery", "mocha", "chai", "debounce"]
"types": ["node", "url-parse", "jquery", "underscore", "mocha", "chai"]
},
"include": [
"./src/**/*"