Refactor hero resource balance action

This commit is contained in:
2020-04-19 10:38:48 +03:00
parent a6cc1b5383
commit 84ac56d052
42 changed files with 182 additions and 76 deletions

View File

@ -0,0 +1,44 @@
import { StateGrabber } from './StateGrabber';
import {
grabActiveVillageId,
grabBuildingQueueInfo,
grabResourcesPerformance,
grabVillageList,
} from '../Page/VillageBlock';
import { VillageState } from '../State/VillageState';
import { parseLocation } from '../utils';
import { GrabError } from '../Errors';
import { BuildingQueueInfo } from '../Game';
import { HeroState } from '../State/HeroState';
import { grabHeroAttributes, grabHeroVillage } from '../Page/HeroPage';
export class HeroPageGrabber extends StateGrabber {
grab(): void {
const p = parseLocation();
if (p.pathname !== '/hero.php') {
return;
}
const state = new HeroState();
state.storeAttributes(grabHeroAttributes());
const villageId = this.getHeroVillageId();
if (villageId) {
state.storeVillageId(villageId);
}
}
private getHeroVillageId(): number | undefined {
const villages = grabVillageList();
const heroVillage = grabHeroVillage();
for (let village of villages) {
if (village.name === heroVillage) {
return village.id;
}
}
return undefined;
}
}

View File

@ -0,0 +1,13 @@
import { StateGrabber } from './StateGrabber';
import { grabActiveVillageId } from '../Page/VillageBlock';
import { grabResources, grabResourceStorage } from '../Page/ResourcesBlock';
import { VillageState } from '../State/VillageState';
export class ResourceGrabber extends StateGrabber {
grab(): void {
const villageId = grabActiveVillageId();
const state = new VillageState(villageId);
state.storeResources(grabResources());
state.storeResourceStorage(grabResourceStorage());
}
}

View File

@ -0,0 +1,3 @@
export abstract class StateGrabber {
abstract grab(): void;
}

View File

@ -0,0 +1,21 @@
import { StateGrabber } from './StateGrabber';
import { ResourceGrabber } from './ResourceGrabber';
import { VillageOverviewPageGrabber } from './VillageOverviewPageGrabber';
import { HeroPageGrabber } from './HeroPageGrabber';
export class StateGrabberManager {
private readonly grabbers: Array<StateGrabber> = [];
constructor() {
this.grabbers = [];
this.grabbers.push(new ResourceGrabber());
this.grabbers.push(new VillageOverviewPageGrabber());
this.grabbers.push(new HeroPageGrabber());
}
grab() {
for (let grabber of this.grabbers) {
grabber.grab();
}
}
}

View File

@ -0,0 +1,31 @@
import { StateGrabber } from './StateGrabber';
import { grabActiveVillageId, grabBuildingQueueInfo, grabResourcesPerformance } from '../Page/VillageBlock';
import { VillageState } from '../State/VillageState';
import { parseLocation } from '../utils';
import { GrabError } from '../Errors';
import { BuildingQueueInfo } from '../Game';
export class VillageOverviewPageGrabber extends StateGrabber {
grab(): void {
const p = parseLocation();
if (p.pathname !== '/dorf1.php') {
return;
}
const villageId = grabActiveVillageId();
const state = new VillageState(villageId);
state.storeResourcesPerformance(grabResourcesPerformance());
state.storeBuildingQueueInfo(this.grabBuildingQueueInfoOrDefault());
}
private grabBuildingQueueInfoOrDefault() {
try {
return grabBuildingQueueInfo();
} catch (e) {
if (e instanceof GrabError) {
return new BuildingQueueInfo(0);
}
throw e;
}
}
}