Village state refactoring

This commit is contained in:
Anton Vakhrushev 2020-05-10 11:08:14 +03:00
parent c5de5ca901
commit b16a121d37
4 changed files with 25 additions and 20 deletions

View File

@ -24,7 +24,7 @@ export class BalanceHeroResourcesAction extends ActionController {
thisVillageState.required.balance, thisVillageState.required.balance,
thisVillageState.commitments, thisVillageState.commitments,
thisVillageState.totalRequired.balance, thisVillageState.totalRequired.balance,
thisVillageState.resources.sub(thisVillageState.storage), thisVillageState.resources.sub(thisVillageState.storage.capacity),
]; ];
console.log('Requirements'); console.log('Requirements');

View File

@ -60,7 +60,7 @@ export class SendResourcesAction extends ActionController {
} }
private getRecipientRequirements(recipientState: VillageState): Resources { private getRecipientRequirements(recipientState: VillageState): Resources {
const maxPossibleToStore = recipientState.storage.sub(recipientState.performance); const maxPossibleToStore = recipientState.storage.capacity.sub(recipientState.performance);
const currentResources = recipientState.resources; const currentResources = recipientState.resources;
const incomingResources = recipientState.incomingResources; const incomingResources = recipientState.incomingResources;
const requirementResources = recipientState.required.resources; const requirementResources = recipientState.required.resources;

View File

@ -12,7 +12,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<template v-for="villageState in shared.villageStates"> <template v-for="villageState in villageStates">
<tr class="normal-line top-line"> <tr class="normal-line top-line">
<td :class="{ active: villageState.village.active }" :title="villageState.id"> <td :class="{ active: villageState.village.active }" :title="villageState.id">
{{ villageState.village.name }} {{ villageState.village.name }}
@ -20,28 +20,28 @@
<td class="right"> <td class="right">
<filling <filling
:value="villageState.resources.lumber" :value="villageState.resources.lumber"
:max="villageState.storage.lumber" :max="villageState.storage.capacity.lumber"
:speed="villageState.performance.lumber" :speed="villageState.performance.lumber"
></filling> ></filling>
</td> </td>
<td class="right"> <td class="right">
<filling <filling
:value="villageState.resources.clay" :value="villageState.resources.clay"
:max="villageState.storage.clay" :max="villageState.storage.capacity.clay"
:speed="villageState.performance.clay" :speed="villageState.performance.clay"
></filling> ></filling>
</td> </td>
<td class="right"> <td class="right">
<filling <filling
:value="villageState.resources.iron" :value="villageState.resources.iron"
:max="villageState.storage.iron" :max="villageState.storage.capacity.iron"
:speed="villageState.performance.iron" :speed="villageState.performance.iron"
></filling> ></filling>
</td> </td>
<td class="right"> <td class="right">
<filling <filling
:value="villageState.resources.crop" :value="villageState.resources.crop"
:max="villageState.storage.crop" :max="villageState.storage.capacity.crop"
:speed="villageState.performance.crop" :speed="villageState.performance.crop"
></filling> ></filling>
</td> </td>
@ -167,7 +167,7 @@
<td></td> <td></td>
<td class="right" colspan="5"> <td class="right" colspan="5">
<a <a
v-for="s in shared.villageStates" v-for="s in villageStates"
v-if="s.id !== villageState.id" v-if="s.id !== villageState.id"
class="village-quick-link" class="village-quick-link"
:class="{ active: villageState.shipment.includes(s.id) }" :class="{ active: villageState.shipment.includes(s.id) }"
@ -218,6 +218,7 @@ export default {
data() { data() {
return { return {
shared: this.$root.$data, shared: this.$root.$data,
villageStates: this.$root.$data.villageStates,
activeVillageState: this.$root.$data.activeVillageState, activeVillageState: this.$root.$data.activeVillageState,
}; };
}, },
@ -226,8 +227,8 @@ export default {
return path(name, args); return path(name, args);
}, },
storageTime(villageState) { storageTime(villageState) {
const toZero = villageState.storageBalance.timeToZero; const toZero = villageState.storage.timeToZero;
const toFull = villageState.storageBalance.timeToFull; const toFull = villageState.storage.timeToFull;
return this.renderGatheringTime(toFull.never ? toZero : toFull); return this.renderGatheringTime(toFull.never ? toZero : toFull);
}, },
marketPath(fromVillage, toVillage) { marketPath(fromVillage, toVillage) {

View File

@ -6,9 +6,9 @@ import { calcGatheringTimings, GatheringTime } from './Core/GatheringTimings';
import { VillageRepositoryInterface } from './VillageRepository'; import { VillageRepositoryInterface } from './VillageRepository';
import { VillageNotFound } from './Errors'; import { VillageNotFound } from './Errors';
interface StorageBalance { interface VillageStorageState {
resources: Resources; resources: Resources;
storage: Resources; capacity: Resources;
balance: Resources; balance: Resources;
performance: Resources; performance: Resources;
timeToZero: GatheringTime; timeToZero: GatheringTime;
@ -21,7 +21,7 @@ interface RequiredResources {
*/ */
resources: Resources; resources: Resources;
/** /**
* Balance resources (current - required), may be negative * Balance resources (current - required, may be negative)
*/ */
balance: Resources; balance: Resources;
/** /**
@ -44,9 +44,14 @@ interface VillageOwnState {
*/ */
resources: Resources; resources: Resources;
performance: Resources; performance: Resources;
storage: Resources; storage: VillageStorageState;
storageBalance: StorageBalance; /**
* Required resources for nearest task
*/
required: RequiredResources; required: RequiredResources;
/**
* Required resources for all tasks
*/
totalRequired: RequiredResources; totalRequired: RequiredResources;
incomingResources: Resources; incomingResources: Resources;
buildRemainingSeconds: number; buildRemainingSeconds: number;
@ -58,7 +63,7 @@ interface VillageOwnStateDictionary {
export interface VillageState extends VillageOwnState { export interface VillageState extends VillageOwnState {
/** /**
* Resource commitments of this village to other * Resource commitments of this village to other (may be negative)
*/ */
commitments: Resources; commitments: Resources;
/** /**
@ -75,10 +80,10 @@ function calcResourceBalance(resources: Resources, current: Resources, performan
}; };
} }
function calcStorageBalance(resources: Resources, storage: Resources, performance: Resources): StorageBalance { function calcStorageBalance(resources: Resources, storage: Resources, performance: Resources): VillageStorageState {
return { return {
resources, resources,
storage, capacity: storage,
performance, performance,
balance: storage.sub(resources), balance: storage.sub(resources),
timeToZero: timeToFastestResource(resources, Resources.zero(), performance), timeToZero: timeToFastestResource(resources, Resources.zero(), performance),
@ -114,8 +119,7 @@ function createVillageOwnState(village: Village, scheduler: Scheduler): VillageO
village, village,
resources, resources,
performance, performance,
storage: Resources.fromStorage(resourceStorage), storage: calcStorageBalance(resources, Resources.fromStorage(resourceStorage), performance),
storageBalance: calcStorageBalance(resources, Resources.fromStorage(resourceStorage), performance),
required: calcResourceBalance(requiredResources, resources, performance), required: calcResourceBalance(requiredResources, resources, performance),
totalRequired: calcResourceBalance(totalRequiredResources, resources, performance), totalRequired: calcResourceBalance(totalRequiredResources, resources, performance),
buildRemainingSeconds: buildQueueInfo.seconds, buildRemainingSeconds: buildQueueInfo.seconds,