Add state grabbers

This commit is contained in:
2020-04-16 17:53:04 +03:00
parent fc75b007c8
commit c3428afabb
7 changed files with 57 additions and 10 deletions

View File

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

View File

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

View File

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

19
src/State/VillageState.ts Normal file
View File

@ -0,0 +1,19 @@
import { DataStorage } from '../Storage/DataStorage';
import { Resources } from '../Game';
export class VillageState {
private storage: DataStorage;
constructor(villageId: number) {
this.storage = new DataStorage(`village.${villageId}`);
}
storeResources(resources: Resources) {
this.storage.set('res', resources);
}
getResources(): Resources {
let plain = this.storage.get('res');
let res = new Resources(0, 0, 0, 0, 0, 0);
return Object.assign(res, plain) as Resources;
}
}