Small refactoring

This commit is contained in:
Anton Vakhrushev 2020-07-18 17:02:35 +03:00
parent 7d3c14a842
commit 60b1243214
3 changed files with 20 additions and 29 deletions

View File

@ -8,8 +8,8 @@ import { VillageStorage } from './Storage/VillageStorage';
import { ReceiveResourcesMode } from './Core/Village'; import { ReceiveResourcesMode } from './Core/Village';
import { ResourceType } from './Core/ResourceType'; import { ResourceType } from './Core/ResourceType';
import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask'; import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask';
import * as _ from 'underscore';
import { GARNER_ID, WAREHOUSE_ID } from './Core/Buildings'; import { GARNER_ID, WAREHOUSE_ID } from './Core/Buildings';
import { first } from './utils';
export class VillageController { export class VillageController {
private readonly villageId: number; private readonly villageId: number;
@ -195,7 +195,7 @@ export class VillageController {
// Find ready for building slots and sort them by level // Find ready for building slots and sort them by level
cropSlots.sort((s1, s2) => s1.level - s2.level); cropSlots.sort((s1, s2) => s1.level - s2.level);
const targetCropBuildId = _.first(cropSlots)?.buildId; const targetCropBuildId = first(cropSlots)?.buildId;
if (!targetCropBuildId) { if (!targetCropBuildId) {
return; return;
} }
@ -250,7 +250,7 @@ export class VillageController {
return; return;
} }
const firstSlot = _.first(storageSlots); const firstSlot = first(storageSlots);
if (firstSlot) { if (firstSlot) {
this.addTask(UpgradeBuildingTask.name, { buildId: firstSlot.buildId, buildTypeId }); this.addTask(UpgradeBuildingTask.name, { buildId: firstSlot.buildId, buildTypeId });
} }

View File

@ -26,6 +26,18 @@ export interface TaskQueueState {
finishTs: number; finishTs: number;
} }
interface VillageProductionQueueState {
queue: ProductionQueue;
tasks: ReadonlyArray<TaskState>;
isActive: boolean;
isWaiting: boolean;
currentTaskFinishTimestamp: number;
currentTaskFinishSeconds: number;
firstTask: ResourceLineState;
allTasks: ResourceLineState;
taskCount: number;
}
interface VillageWarehouseState { interface VillageWarehouseState {
resources: Resources; resources: Resources;
capacity: Resources; capacity: Resources;
@ -60,18 +72,6 @@ interface ResourceLineState {
active: boolean; active: boolean;
} }
interface VillageProductionQueueState {
queue: ProductionQueue;
tasks: ReadonlyArray<TaskState>;
isActive: boolean;
isWaiting: boolean;
currentTaskFinishTimestamp: number;
currentTaskFinishSeconds: number;
firstTask: ResourceLineState;
allTasks: ResourceLineState;
taskCount: number;
}
interface VillageOwnState { interface VillageOwnState {
/** /**
* Village id * Village id

View File

@ -1,4 +1,5 @@
import * as URLParse from 'url-parse'; import * as URLParse from 'url-parse';
import * as _ from 'underscore';
export function sleep(ms: number) { export function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
@ -67,20 +68,6 @@ export function elClassId(classes: string | undefined, prefix: string): number |
return result; return result;
} }
export function* split(n: number, from: number = 2, to: number = 6) {
let c = n;
while (c > 0) {
const next = from + Math.floor(Math.random() * (to - from));
if (next < c) {
yield next;
c -= next;
} else {
yield c;
c = 0;
}
}
}
export function toNumber(value: any): number | undefined { export function toNumber(value: any): number | undefined {
const normalized = String(value) const normalized = String(value)
.replace('\u2212', '\u002d') // minus to hyphen-minus .replace('\u2212', '\u002d') // minus to hyphen-minus
@ -119,3 +106,7 @@ export function markPage(text: string, version: string) {
'</div>' '</div>'
); );
} }
export function first<T>(items: ReadonlyArray<T>): T | undefined {
return _.first(items);
}