Add parse location utility

This commit is contained in:
Anton Vakhrushev 2020-04-18 12:38:51 +03:00
parent 7fa3d67df4
commit 8001510f0a
5 changed files with 15 additions and 10 deletions

View File

@ -1,5 +1,4 @@
import * as URLParse from 'url-parse'; import { getNumber, parseLocation, uniqId, waitForLoad } from './utils';
import { getNumber, uniqId, waitForLoad } from './utils';
import { Scheduler } from './Scheduler'; import { Scheduler } from './Scheduler';
import { BuildPage } from './Page/BuildPage'; import { BuildPage } from './Page/BuildPage';
import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask'; import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask';
@ -38,12 +37,13 @@ export class ControlPanel {
async run() { async run() {
await waitForLoad(); await waitForLoad();
const p = new URLParse(window.location.href, true); const p = parseLocation();
this.logger.log('PARSED LOCATION', p); this.logger.log('PARSED LOCATION', p);
const villageId = grabActiveVillageId(); const villageId = grabActiveVillageId();
this.grabbers.grab(); this.grabbers.grab();
setInterval(() => this.grabbers.grab(), 2000);
const scheduler = this.scheduler; const scheduler = this.scheduler;
const quickActions: QuickAction[] = []; const quickActions: QuickAction[] = [];

View File

@ -1,4 +1,4 @@
import * as URLParse from 'url-parse'; import { parseLocation } from './utils';
const SESSION_KEY = 'travian_automation_mode'; const SESSION_KEY = 'travian_automation_mode';
const SESSION_VALUE = 'command_execution'; const SESSION_VALUE = 'command_execution';
@ -14,7 +14,7 @@ export class ModeDetector {
} }
private isAutoByLocation(): boolean { private isAutoByLocation(): boolean {
const p = new URLParse(window.location.href, true); const p = parseLocation();
return p.query[MODE_PARAMETER_NAME] !== undefined; return p.query[MODE_PARAMETER_NAME] !== undefined;
} }

View File

@ -1,7 +1,6 @@
import { Coordinates, Resources, Village, VillageList } from '../Game'; import { Coordinates, Resources, Village, VillageList } from '../Game';
import { GrabError } from '../Errors'; import { GrabError } from '../Errors';
import * as URLParse from 'url-parse'; import { getNumber, parseLocation } from '../utils';
import { getNumber } from '../utils';
function getVillageListItems() { function getVillageListItems() {
const $elements = jQuery('#sidebarBoxVillagelist ul li a'); const $elements = jQuery('#sidebarBoxVillagelist ul li a');
@ -36,7 +35,7 @@ export function grabActiveVillageId(): number {
function grabVillageInfo($el): Village { function grabVillageInfo($el): Village {
const href = $el.attr('href'); const href = $el.attr('href');
const parsedHref = new URLParse(href || '', true); const parsedHref = parseLocation(href || '');
const id = getNumber(parsedHref.query.newdid); const id = getNumber(parsedHref.query.newdid);
const name = $el.find('.name').text(); const name = $el.find('.name').text();
const active = $el.hasClass('active'); const active = $el.hasClass('active');

View File

@ -1,11 +1,11 @@
import * as URLParse from 'url-parse';
import { StateGrabber } from './StateGrabber'; import { StateGrabber } from './StateGrabber';
import { grabActiveVillageId, grabResourcesPerformance } from '../Page/VillageBlock'; import { grabActiveVillageId, grabResourcesPerformance } from '../Page/VillageBlock';
import { VillageState } from './VillageState'; import { VillageState } from './VillageState';
import { parseLocation } from '../utils';
export class ResourcePerformanceGrabber extends StateGrabber { export class ResourcePerformanceGrabber extends StateGrabber {
grab(): void { grab(): void {
const p = new URLParse(window.location.href, true); const p = parseLocation();
if (p.pathname !== '/dorf1.php') { if (p.pathname !== '/dorf1.php') {
return; return;
} }

View File

@ -1,3 +1,5 @@
import * as URLParse from 'url-parse';
export function sleep(ms: number) { export function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
@ -88,6 +90,10 @@ export function getNumber(value: any, def: number = 0): number {
return converted === undefined ? def : converted; return converted === undefined ? def : converted;
} }
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 } = {}) { export function path(p: string, query: { [key: string]: string | number | undefined } = {}) {
let parts: string[] = []; let parts: string[] = [];
for (let k in query) { for (let k in query) {