Simplify warehouse capacity store

This commit is contained in:
Anton Vakhrushev 2020-10-10 11:46:28 +03:00
parent 5b85f3fe18
commit 9531c7850a
6 changed files with 21 additions and 45 deletions

View File

@ -1,9 +0,0 @@
export class ResourceStorage {
readonly warehouse: number;
readonly granary: number;
constructor(warehouse: number, granary: number) {
this.warehouse = warehouse;
this.granary = granary;
}
}

View File

@ -1,5 +1,4 @@
import { ResourceList, ResourceMapping, ResourceType } from './ResourceType'; import { ResourceList, ResourceMapping, ResourceType } from './ResourceType';
import { ResourceStorage } from './ResourceStorage';
export interface ResourcesInterface { export interface ResourcesInterface {
lumber: number; lumber: number;
@ -25,15 +24,6 @@ export class Resources implements ResourcesInterface {
return new Resources(obj.lumber, obj.clay, obj.iron, obj.crop); return new Resources(obj.lumber, obj.clay, obj.iron, obj.crop);
} }
static fromStorage(storage: ResourceStorage): Resources {
return new Resources(
storage.warehouse,
storage.warehouse,
storage.warehouse,
storage.granary
);
}
static zero(): Resources { static zero(): Resources {
return new Resources(0, 0, 0, 0); return new Resources(0, 0, 0, 0);
} }

View File

@ -1,9 +1,9 @@
import { Grabber } from './Grabber'; import { Grabber } from './Grabber';
import { grabVillageResources, grabVillageResourceStorage } from '../Page/ResourcesBlock'; import { grabVillageResources, grabVillageWarehouseCapacity } from '../Page/ResourcesBlock';
export class VillageResourceGrabber extends Grabber { export class VillageResourceGrabber extends Grabber {
grab(): void { grab(): void {
this.storage.storeResources(grabVillageResources()); this.storage.storeResources(grabVillageResources());
this.storage.storeResourceStorage(grabVillageResourceStorage()); this.storage.storeWarehouseCapacity(grabVillageWarehouseCapacity());
} }
} }

View File

@ -1,7 +1,6 @@
import { GrabError } from '../Errors'; import { GrabError } from '../Errors';
import { Resources } from '../Core/Resources'; import { Resources } from '../Core/Resources';
import { ResourceType } from '../Core/ResourceType'; import { ResourceType } from '../Core/ResourceType';
import { ResourceStorage } from '../Core/ResourceStorage';
import { getNumber } from '../Helpers/Convert'; import { getNumber } from '../Helpers/Convert';
export function grabVillageResources(): Resources { export function grabVillageResources(): Resources {
@ -9,14 +8,13 @@ export function grabVillageResources(): Resources {
const clay = grabResource(ResourceType.Clay); const clay = grabResource(ResourceType.Clay);
const iron = grabResource(ResourceType.Iron); const iron = grabResource(ResourceType.Iron);
const crop = grabResource(ResourceType.Crop); const crop = grabResource(ResourceType.Crop);
return new Resources(lumber, clay, iron, crop); return new Resources(lumber, clay, iron, crop);
} }
export function grabVillageResourceStorage() { export function grabVillageWarehouseCapacity(): Resources {
const warehouse = grabCapacity('warehouse'); const warehouse = grabCapacity('warehouse');
const granary = grabCapacity('granary'); const granary = grabCapacity('granary');
return new ResourceStorage(warehouse, granary); return new Resources(warehouse, warehouse, warehouse, granary);
} }
function findStockBarElement() { function findStockBarElement() {

View File

@ -1,6 +1,5 @@
import { DataStorage } from './DataStorage'; import { DataStorage } from './DataStorage';
import { Resources, ResourcesInterface } from '../Core/Resources'; import { Resources, ResourcesInterface } from '../Core/Resources';
import { ResourceStorage } from '../Core/ResourceStorage';
import { IncomingMerchant, MerchantsInfo } from '../Core/Market'; import { IncomingMerchant, MerchantsInfo } from '../Core/Market';
import { VillageSettings, VillageSettingsDefaults } from '../Core/Village'; import { VillageSettings, VillageSettingsDefaults } from '../Core/Village';
import { ProductionQueue } from '../Core/ProductionQueue'; import { ProductionQueue } from '../Core/ProductionQueue';
@ -15,7 +14,7 @@ import { Task } from '../Queue/Task';
import { uniqTaskId } from '../Queue/TaskId'; import { uniqTaskId } from '../Queue/TaskId';
const RESOURCES_KEY = 'resources'; const RESOURCES_KEY = 'resources';
const CAPACITY_KEY = 'capacity'; const WAREHOUSE_CAPACITY_KEY = 'warehouse_capacity';
const PERFORMANCE_KEY = 'performance'; const PERFORMANCE_KEY = 'performance';
const INCOMING_MERCHANTS_KEY = 'incoming_merchants'; const INCOMING_MERCHANTS_KEY = 'incoming_merchants';
const MERCHANTS_INFO_KEY = 'merchants_info'; const MERCHANTS_INFO_KEY = 'merchants_info';
@ -35,7 +34,7 @@ export class VillageStorage {
this.storage = new DataStorage(`village.${villageId}`); this.storage = new DataStorage(`village.${villageId}`);
} }
storeResources(resources: Resources) { storeResources(resources: ResourcesInterface) {
this.storage.set(RESOURCES_KEY, resources); this.storage.set(RESOURCES_KEY, resources);
} }
@ -43,14 +42,12 @@ export class VillageStorage {
return this.storage.getTyped(RESOURCES_KEY, ResourceOptions); return this.storage.getTyped(RESOURCES_KEY, ResourceOptions);
} }
storeResourceStorage(storage: ResourceStorage) { storeWarehouseCapacity(warehouse: ResourcesInterface) {
this.storage.set(CAPACITY_KEY, storage); this.storage.set(WAREHOUSE_CAPACITY_KEY, warehouse);
} }
getResourceStorage(): ResourceStorage { getWarehouseCapacity(): Resources {
let plain = this.storage.get(CAPACITY_KEY); return this.storage.getTyped(WAREHOUSE_CAPACITY_KEY, ResourceOptions);
let res = new ResourceStorage(0, 0);
return Object.assign(res, plain) as ResourceStorage;
} }
storeResourcesPerformance(resources: Resources) { storeResourcesPerformance(resources: Resources) {

View File

@ -123,24 +123,24 @@ function makeResourceState(
}; };
} }
function makeStorageState( function makeWarehouseState(
resources: Resources, resources: Resources,
storage: Resources, capacity: Resources,
performance: Resources performance: Resources
): VillageWarehouseState { ): VillageWarehouseState {
// @fixme Если у героя большая добыча ресурсов, а склад маленький, то значения получаются тож маленькими // @fixme Если у героя большая добыча ресурсов, а склад маленький, то значения получаются тож маленькими
// @fixme с одной деревней это не прокатывает, и даже не построить склад // @fixme с одной деревней это не прокатывает, и даже не построить склад
// const optimumFullness = storage.sub(performance.scale(3)); // const optimumFullness = capacity.sub(performance.scale(3));
// const criticalFullness = storage.sub(performance.scale(1)); // const criticalFullness = capacity.sub(performance.scale(1));
const optimumFullness = storage.scale(0.9); const optimumFullness = capacity.scale(0.9);
const criticalFullness = storage.scale(0.98); const criticalFullness = capacity.scale(0.98);
return { return {
resources, resources,
capacity: storage, capacity,
performance, performance,
balance: storage.sub(resources), balance: capacity.sub(resources),
timeToZero: timeToFastestResource(resources, Resources.zero(), performance), timeToZero: timeToFastestResource(resources, Resources.zero(), performance),
timeToFull: timeToFastestResource(resources, storage, performance), timeToFull: timeToFastestResource(resources, capacity, performance),
optimumFullness: optimumFullness, optimumFullness: optimumFullness,
criticalFullness: criticalFullness, criticalFullness: criticalFullness,
isOverflowing: criticalFullness.anyLower(resources), isOverflowing: criticalFullness.anyLower(resources),
@ -281,9 +281,9 @@ function createVillageOwnState(
): VillageOwnState { ): VillageOwnState {
const settings = storage.getSettings(); const settings = storage.getSettings();
const resources = storage.getResources(); const resources = storage.getResources();
const storageResources = Resources.fromStorage(storage.getResourceStorage()); const capacity = storage.getWarehouseCapacity();
const performance = storage.getResourcesPerformance(); const performance = storage.getResourcesPerformance();
const storageState = makeStorageState(resources, storageResources, performance); const storageState = makeWarehouseState(resources, capacity, performance);
const tasks = taskCollection const tasks = taskCollection
.getTasks() .getTasks()
.map((t) => makeTaskState(t, storageState.optimumFullness)); .map((t) => makeTaskState(t, storageState.optimumFullness));