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

30
src/State/HeroState.ts Normal file
View File

@ -0,0 +1,30 @@
import { DataStorage } from '../DataStorage';
import { HeroAttributes } from '../Game';
const VILLAGE_ID_KEY = 'village_id';
const ATTRIBUTES_KEY = 'attr';
export class HeroState {
private storage: DataStorage;
constructor() {
this.storage = new DataStorage('hero.v1');
}
storeVillageId(villageId: number) {
this.storage.set(VILLAGE_ID_KEY, villageId);
}
getVillageId(): number | undefined {
return (this.storage.get(VILLAGE_ID_KEY) as number) || undefined;
}
storeAttributes(attributes: HeroAttributes) {
this.storage.set(ATTRIBUTES_KEY, attributes);
}
getAttributes(): HeroAttributes {
let plain = this.storage.get(ATTRIBUTES_KEY);
let res = new HeroAttributes(0);
return Object.assign(res, plain) as HeroAttributes;
}
}

View File

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

View File

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

View File

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

View File

@ -1,31 +0,0 @@
import { StateGrabber } from './StateGrabber';
import { grabActiveVillageId, grabBuildingQueueInfo, grabResourcesPerformance } from '../Page/VillageBlock';
import { VillageState } from './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;
}
}
}

View File

@ -1,4 +1,4 @@
import { DataStorage } from '../Storage/DataStorage';
import { DataStorage } from '../DataStorage';
import { BuildingQueueInfo, Resources, ResourceStorage } from '../Game';
const RESOURCES_KEY = 'res';