Rename state to storage
This commit is contained in:
parent
c1a63875a8
commit
3d54294043
@ -2,7 +2,7 @@ import { ActionController, registerAction } from './ActionController';
|
|||||||
import { grabVillageResources, grabVillageResourceStorage } from '../Page/ResourcesBlock';
|
import { grabVillageResources, grabVillageResourceStorage } from '../Page/ResourcesBlock';
|
||||||
import { changeHeroResource, grabCurrentHeroResource } from '../Page/HeroPage';
|
import { changeHeroResource, grabCurrentHeroResource } from '../Page/HeroPage';
|
||||||
import { grabActiveVillageId } from '../Page/VillageBlock';
|
import { grabActiveVillageId } from '../Page/VillageBlock';
|
||||||
import { HeroState } from '../State/HeroState';
|
import { HeroStorage } from '../Storage/HeroStorage';
|
||||||
import { calcHeroResource } from '../Core/HeroBalance';
|
import { calcHeroResource } from '../Core/HeroBalance';
|
||||||
import { HeroAllResources } from '../Core/Hero';
|
import { HeroAllResources } from '../Core/Hero';
|
||||||
import { Args } from '../Queue/Args';
|
import { Args } from '../Queue/Args';
|
||||||
@ -12,7 +12,7 @@ import { Task } from '../Queue/TaskProvider';
|
|||||||
export class BalanceHeroResourcesAction extends ActionController {
|
export class BalanceHeroResourcesAction extends ActionController {
|
||||||
async run(args: Args, task: Task): Promise<any> {
|
async run(args: Args, task: Task): Promise<any> {
|
||||||
const activeVillageId = grabActiveVillageId();
|
const activeVillageId = grabActiveVillageId();
|
||||||
const heroVillageId = new HeroState().getVillageId();
|
const heroVillageId = new HeroStorage().getVillageId();
|
||||||
|
|
||||||
if (heroVillageId === undefined || activeVillageId !== heroVillageId) {
|
if (heroVillageId === undefined || activeVillageId !== heroVillageId) {
|
||||||
changeHeroResource(HeroAllResources);
|
changeHeroResource(HeroAllResources);
|
||||||
|
@ -2,7 +2,7 @@ import { ActionController, registerAction } from './ActionController';
|
|||||||
import { grabVillageList } from '../Page/VillageBlock';
|
import { grabVillageList } from '../Page/VillageBlock';
|
||||||
import { grabHeroVillage } from '../Page/HeroPage';
|
import { grabHeroVillage } from '../Page/HeroPage';
|
||||||
import { path } from '../utils';
|
import { path } from '../utils';
|
||||||
import { HeroState } from '../State/HeroState';
|
import { HeroStorage } from '../Storage/HeroStorage';
|
||||||
import { Args } from '../Queue/Args';
|
import { Args } from '../Queue/Args';
|
||||||
import { Task } from '../Queue/TaskProvider';
|
import { Task } from '../Queue/TaskProvider';
|
||||||
|
|
||||||
@ -25,6 +25,6 @@ export class GoToHeroVillageAction extends ActionController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new HeroState().getVillageId();
|
return new HeroStorage().getVillageId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ import { grabVillageResources } from '../Page/ResourcesBlock';
|
|||||||
import { grabActiveVillageId, grabVillageList } from '../Page/VillageBlock';
|
import { grabActiveVillageId, grabVillageList } from '../Page/VillageBlock';
|
||||||
import { SendResourcesTask } from '../Task/SendResourcesTask';
|
import { SendResourcesTask } from '../Task/SendResourcesTask';
|
||||||
import { aroundMinutes, timestamp } from '../utils';
|
import { aroundMinutes, timestamp } from '../utils';
|
||||||
import { VillageState } from '../State/VillageState';
|
import { VillageStorage } from '../Storage/VillageStorage';
|
||||||
import { Args } from '../Queue/Args';
|
import { Args } from '../Queue/Args';
|
||||||
import { Task } from '../Queue/TaskProvider';
|
import { Task } from '../Queue/TaskProvider';
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ export class SendResourcesAction extends ActionController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private getRecipientRequirements(villageId: number): Resources {
|
private getRecipientRequirements(villageId: number): Resources {
|
||||||
const state = new VillageState(villageId);
|
const state = new VillageStorage(villageId);
|
||||||
const resources = state.getResources();
|
const resources = state.getResources();
|
||||||
const incoming = state.getIncomingMerchants().reduce((m, i) => m.add(i.resources), Resources.zero());
|
const incoming = state.getIncomingMerchants().reduce((m, i) => m.add(i.resources), Resources.zero());
|
||||||
const requirements = this.scheduler.getVillageRequiredResources(villageId);
|
const requirements = this.scheduler.getVillageRequiredResources(villageId);
|
||||||
|
@ -13,14 +13,14 @@ import Vue from 'vue';
|
|||||||
import DashboardApp from './DashboardView/Dashboard.vue';
|
import DashboardApp from './DashboardView/Dashboard.vue';
|
||||||
import { ResourcesToLevel } from './Task/ResourcesToLevel';
|
import { ResourcesToLevel } from './Task/ResourcesToLevel';
|
||||||
import { ConsoleLogger, Logger } from './Logger';
|
import { ConsoleLogger, Logger } from './Logger';
|
||||||
import { VillageState } from './State/VillageState';
|
import { VillageStorage } from './Storage/VillageStorage';
|
||||||
import { Resources } from './Core/Resources';
|
import { Resources } from './Core/Resources';
|
||||||
import { Coordinates, Village } from './Core/Village';
|
import { Coordinates, Village } from './Core/Village';
|
||||||
import { calcGatheringTimings } from './Core/GatheringTimings';
|
import { calcGatheringTimings } from './Core/GatheringTimings';
|
||||||
import { DataStorage } from './DataStorage';
|
import { DataStorage } from './DataStorage';
|
||||||
import { getBuildingPageAttributes, isBuildingPage } from './Page/PageDetectors';
|
import { getBuildingPageAttributes, isBuildingPage } from './Page/PageDetectors';
|
||||||
import { debounce } from 'debounce';
|
import { debounce } from 'debounce';
|
||||||
import { ExecutionState } from './State/ExecutionState';
|
import { ExecutionStorage } from './Storage/ExecutionStorage';
|
||||||
import { ResourceStorage } from './Core/ResourceStorage';
|
import { ResourceStorage } from './Core/ResourceStorage';
|
||||||
|
|
||||||
interface QuickAction {
|
interface QuickAction {
|
||||||
@ -50,7 +50,7 @@ export class ControlPanel {
|
|||||||
const scheduler = this.scheduler;
|
const scheduler = this.scheduler;
|
||||||
const quickActions: QuickAction[] = [];
|
const quickActions: QuickAction[] = [];
|
||||||
|
|
||||||
const executionState = new ExecutionState();
|
const executionState = new ExecutionStorage();
|
||||||
|
|
||||||
const state: any = {
|
const state: any = {
|
||||||
name: 'Dashboard',
|
name: 'Dashboard',
|
||||||
@ -77,7 +77,7 @@ export class ControlPanel {
|
|||||||
|
|
||||||
refreshVillages() {
|
refreshVillages() {
|
||||||
this.villages = grabVillageList().map(village => {
|
this.villages = grabVillageList().map(village => {
|
||||||
return new VillageController(village, new VillageState(village.id), scheduler);
|
return new VillageController(village, new VillageStorage(village.id), scheduler);
|
||||||
});
|
});
|
||||||
for (let village of this.villages) {
|
for (let village of this.villages) {
|
||||||
if (village.active) {
|
if (village.active) {
|
||||||
@ -183,7 +183,7 @@ class VillageController {
|
|||||||
public readonly granary: number;
|
public readonly granary: number;
|
||||||
public readonly buildRemainingSeconds: number;
|
public readonly buildRemainingSeconds: number;
|
||||||
|
|
||||||
constructor(village: Village, state: VillageState, scheduler: Scheduler) {
|
constructor(village: Village, state: VillageStorage, scheduler: Scheduler) {
|
||||||
const resources = state.getResources();
|
const resources = state.getResources();
|
||||||
const storage = state.getResourceStorage();
|
const storage = state.getResourceStorage();
|
||||||
const performance = state.getResourcesPerformance();
|
const performance = state.getResourcesPerformance();
|
||||||
@ -228,7 +228,7 @@ class VillageController {
|
|||||||
return timings.hours * 3600;
|
return timings.hours * 3600;
|
||||||
}
|
}
|
||||||
|
|
||||||
private calcIncomingResources(state: VillageState): Resources {
|
private calcIncomingResources(state: VillageStorage): Resources {
|
||||||
return state.getIncomingMerchants().reduce((m, i) => m.add(i.resources), new Resources(0, 0, 0, 0));
|
return state.getIncomingMerchants().reduce((m, i) => m.add(i.resources), new Resources(0, 0, 0, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { ConsoleLogger, Logger, NullLogger } from './Logger';
|
import { Logger, NullLogger } from './Logger';
|
||||||
|
|
||||||
const NAMESPACE = 'travian:v1';
|
const NAMESPACE = 'travian:v1';
|
||||||
|
|
||||||
@ -48,7 +48,6 @@ export class DataStorage {
|
|||||||
|
|
||||||
constructor(name: string) {
|
constructor(name: string) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
// this.logger = new ConsoleLogger(this.constructor.name);
|
|
||||||
this.logger = new NullLogger();
|
this.logger = new NullLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,11 +88,6 @@ export class DataStorage {
|
|||||||
return (plain as Array<any>).map(mapper);
|
return (plain as Array<any>).map(mapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
has(key: string): boolean {
|
|
||||||
const fullKey = join(NAMESPACE, this.name, key);
|
|
||||||
return storage.getItem(fullKey) !== null;
|
|
||||||
}
|
|
||||||
|
|
||||||
set(key: string, value: any) {
|
set(key: string, value: any) {
|
||||||
const fullKey = join(NAMESPACE, this.name, key);
|
const fullKey = join(NAMESPACE, this.name, key);
|
||||||
let serialized = JSON.stringify(value);
|
let serialized = JSON.stringify(value);
|
||||||
|
@ -7,7 +7,7 @@ import { ConsoleLogger, Logger } from './Logger';
|
|||||||
import { GrabberManager } from './Grabber/GrabberManager';
|
import { GrabberManager } from './Grabber/GrabberManager';
|
||||||
import { Scheduler } from './Scheduler';
|
import { Scheduler } from './Scheduler';
|
||||||
import { Statistics } from './Statistics';
|
import { Statistics } from './Statistics';
|
||||||
import { ExecutionState } from './State/ExecutionState';
|
import { ExecutionStorage } from './Storage/ExecutionStorage';
|
||||||
import { Action } from './Queue/ActionQueue';
|
import { Action } from './Queue/ActionQueue';
|
||||||
import { Task } from './Queue/TaskProvider';
|
import { Task } from './Queue/TaskProvider';
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ export class Executor {
|
|||||||
private readonly scheduler: Scheduler;
|
private readonly scheduler: Scheduler;
|
||||||
private grabbers: GrabberManager;
|
private grabbers: GrabberManager;
|
||||||
private statistics: Statistics;
|
private statistics: Statistics;
|
||||||
private executionState: ExecutionState;
|
private executionState: ExecutionStorage;
|
||||||
private logger: Logger;
|
private logger: Logger;
|
||||||
|
|
||||||
constructor(version: string, scheduler: Scheduler) {
|
constructor(version: string, scheduler: Scheduler) {
|
||||||
@ -28,7 +28,7 @@ export class Executor {
|
|||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
this.grabbers = new GrabberManager();
|
this.grabbers = new GrabberManager();
|
||||||
this.statistics = new Statistics();
|
this.statistics = new Statistics();
|
||||||
this.executionState = new ExecutionState();
|
this.executionState = new ExecutionStorage();
|
||||||
this.logger = new ConsoleLogger(this.constructor.name);
|
this.logger = new ConsoleLogger(this.constructor.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Grabber } from './Grabber';
|
import { Grabber } from './Grabber';
|
||||||
import { grabVillageList } from '../Page/VillageBlock';
|
import { grabVillageList } from '../Page/VillageBlock';
|
||||||
import { HeroState } from '../State/HeroState';
|
import { HeroStorage } from '../Storage/HeroStorage';
|
||||||
import { grabHeroAttributes, grabHeroVillage } from '../Page/HeroPage';
|
import { grabHeroAttributes, grabHeroVillage } from '../Page/HeroPage';
|
||||||
import { isHeroPage } from '../Page/PageDetectors';
|
import { isHeroPage } from '../Page/PageDetectors';
|
||||||
|
|
||||||
@ -10,7 +10,7 @@ export class HeroPageGrabber extends Grabber {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const state = new HeroState();
|
const state = new HeroStorage();
|
||||||
|
|
||||||
state.storeAttributes(grabHeroAttributes());
|
state.storeAttributes(grabHeroAttributes());
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Grabber } from './Grabber';
|
import { Grabber } from './Grabber';
|
||||||
import { grabActiveVillageId } from '../Page/VillageBlock';
|
import { grabActiveVillageId } from '../Page/VillageBlock';
|
||||||
import { VillageState } from '../State/VillageState';
|
import { VillageStorage } from '../Storage/VillageStorage';
|
||||||
import { grabIncomingMerchants } from '../Page/BuildingPage';
|
import { grabIncomingMerchants } from '../Page/BuildingPage';
|
||||||
import { isMarketSendResourcesPage } from '../Page/PageDetectors';
|
import { isMarketSendResourcesPage } from '../Page/PageDetectors';
|
||||||
|
|
||||||
@ -11,7 +11,7 @@ export class MarketPageGrabber extends Grabber {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const villageId = grabActiveVillageId();
|
const villageId = grabActiveVillageId();
|
||||||
const state = new VillageState(villageId);
|
const state = new VillageStorage(villageId);
|
||||||
state.storeIncomingMerchants(grabIncomingMerchants());
|
state.storeIncomingMerchants(grabIncomingMerchants());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Grabber } from './Grabber';
|
import { Grabber } from './Grabber';
|
||||||
import { grabActiveVillageId, grabBuildingQueueInfo, grabResourcesPerformance } from '../Page/VillageBlock';
|
import { grabActiveVillageId, grabBuildingQueueInfo, grabResourcesPerformance } from '../Page/VillageBlock';
|
||||||
import { VillageState } from '../State/VillageState';
|
import { VillageStorage } from '../Storage/VillageStorage';
|
||||||
import { parseLocation } from '../utils';
|
import { parseLocation } from '../utils';
|
||||||
import { GrabError } from '../Errors';
|
import { GrabError } from '../Errors';
|
||||||
import { BuildingQueueInfo } from '../Game';
|
import { BuildingQueueInfo } from '../Game';
|
||||||
@ -13,7 +13,7 @@ export class VillageOverviewPageGrabber extends Grabber {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const villageId = grabActiveVillageId();
|
const villageId = grabActiveVillageId();
|
||||||
const state = new VillageState(villageId);
|
const state = new VillageStorage(villageId);
|
||||||
state.storeResourcesPerformance(grabResourcesPerformance());
|
state.storeResourcesPerformance(grabResourcesPerformance());
|
||||||
state.storeBuildingQueueInfo(this.grabBuildingQueueInfoOrDefault());
|
state.storeBuildingQueueInfo(this.grabBuildingQueueInfoOrDefault());
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
import { Grabber } from './Grabber';
|
import { Grabber } from './Grabber';
|
||||||
import { grabActiveVillageId } from '../Page/VillageBlock';
|
import { grabActiveVillageId } from '../Page/VillageBlock';
|
||||||
import { grabVillageResources, grabVillageResourceStorage } from '../Page/ResourcesBlock';
|
import { grabVillageResources, grabVillageResourceStorage } from '../Page/ResourcesBlock';
|
||||||
import { VillageState } from '../State/VillageState';
|
import { VillageStorage } from '../Storage/VillageStorage';
|
||||||
|
|
||||||
export class VillageResourceGrabber extends Grabber {
|
export class VillageResourceGrabber extends Grabber {
|
||||||
grab(): void {
|
grab(): void {
|
||||||
const villageId = grabActiveVillageId();
|
const villageId = grabActiveVillageId();
|
||||||
const state = new VillageState(villageId);
|
const state = new VillageStorage(villageId);
|
||||||
state.storeResources(grabVillageResources());
|
state.storeResources(grabVillageResources());
|
||||||
state.storeResourceStorage(grabVillageResourceStorage());
|
state.storeResourceStorage(grabVillageResourceStorage());
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { StatisticsState } from './State/StatisticsState';
|
import { StatisticsStorage } from './Storage/StatisticsStorage';
|
||||||
import * as dateFormat from 'dateformat';
|
import * as dateFormat from 'dateformat';
|
||||||
|
|
||||||
export interface ActionStatistics {
|
export interface ActionStatistics {
|
||||||
@ -6,10 +6,10 @@ export interface ActionStatistics {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Statistics {
|
export class Statistics {
|
||||||
private state: StatisticsState;
|
private state: StatisticsStorage;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.state = new StatisticsState();
|
this.state = new StatisticsStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
incrementAction(): void {
|
incrementAction(): void {
|
||||||
|
@ -5,7 +5,7 @@ const NAMESPACE = 'execution.v1';
|
|||||||
|
|
||||||
const SETTINGS_KEY = 'settings';
|
const SETTINGS_KEY = 'settings';
|
||||||
|
|
||||||
export class ExecutionState {
|
export class ExecutionStorage {
|
||||||
private storage: DataStorage;
|
private storage: DataStorage;
|
||||||
constructor() {
|
constructor() {
|
||||||
this.storage = new DataStorage(NAMESPACE);
|
this.storage = new DataStorage(NAMESPACE);
|
@ -4,7 +4,7 @@ import { HeroAttributes } from '../Core/Hero';
|
|||||||
const VILLAGE_ID_KEY = 'village_id';
|
const VILLAGE_ID_KEY = 'village_id';
|
||||||
const ATTRIBUTES_KEY = 'attr';
|
const ATTRIBUTES_KEY = 'attr';
|
||||||
|
|
||||||
export class HeroState {
|
export class HeroStorage {
|
||||||
private storage: DataStorage;
|
private storage: DataStorage;
|
||||||
constructor() {
|
constructor() {
|
||||||
this.storage = new DataStorage('hero.v1');
|
this.storage = new DataStorage('hero.v1');
|
@ -5,7 +5,7 @@ const NAMESPACE = 'statistics.v1';
|
|||||||
|
|
||||||
const ACTION_STATISTICS_KEY = 'actions';
|
const ACTION_STATISTICS_KEY = 'actions';
|
||||||
|
|
||||||
export class StatisticsState {
|
export class StatisticsStorage {
|
||||||
private storage: DataStorage;
|
private storage: DataStorage;
|
||||||
constructor() {
|
constructor() {
|
||||||
this.storage = new DataStorage(NAMESPACE);
|
this.storage = new DataStorage(NAMESPACE);
|
@ -14,7 +14,7 @@ const ResourceOptions = {
|
|||||||
factory: () => new Resources(0, 0, 0, 0),
|
factory: () => new Resources(0, 0, 0, 0),
|
||||||
};
|
};
|
||||||
|
|
||||||
export class VillageState {
|
export class VillageStorage {
|
||||||
private storage: DataStorage;
|
private storage: DataStorage;
|
||||||
constructor(villageId: number) {
|
constructor(villageId: number) {
|
||||||
this.storage = new DataStorage(`village.${villageId}`);
|
this.storage = new DataStorage(`village.${villageId}`);
|
Loading…
Reference in New Issue
Block a user