diff --git a/src/VillageController.ts b/src/VillageController.ts index fee9b34..ae1e719 100644 --- a/src/VillageController.ts +++ b/src/VillageController.ts @@ -8,8 +8,8 @@ import { VillageStorage } from './Storage/VillageStorage'; import { ReceiveResourcesMode } from './Core/Village'; import { ResourceType } from './Core/ResourceType'; import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask'; -import * as _ from 'underscore'; import { GARNER_ID, WAREHOUSE_ID } from './Core/Buildings'; +import { first } from './utils'; export class VillageController { private readonly villageId: number; @@ -195,7 +195,7 @@ export class VillageController { // Find ready for building slots and sort them by level cropSlots.sort((s1, s2) => s1.level - s2.level); - const targetCropBuildId = _.first(cropSlots)?.buildId; + const targetCropBuildId = first(cropSlots)?.buildId; if (!targetCropBuildId) { return; } @@ -250,7 +250,7 @@ export class VillageController { return; } - const firstSlot = _.first(storageSlots); + const firstSlot = first(storageSlots); if (firstSlot) { this.addTask(UpgradeBuildingTask.name, { buildId: firstSlot.buildId, buildTypeId }); } diff --git a/src/VillageState.ts b/src/VillageState.ts index c941023..896e4c7 100644 --- a/src/VillageState.ts +++ b/src/VillageState.ts @@ -26,6 +26,18 @@ export interface TaskQueueState { finishTs: number; } +interface VillageProductionQueueState { + queue: ProductionQueue; + tasks: ReadonlyArray; + isActive: boolean; + isWaiting: boolean; + currentTaskFinishTimestamp: number; + currentTaskFinishSeconds: number; + firstTask: ResourceLineState; + allTasks: ResourceLineState; + taskCount: number; +} + interface VillageWarehouseState { resources: Resources; capacity: Resources; @@ -60,18 +72,6 @@ interface ResourceLineState { active: boolean; } -interface VillageProductionQueueState { - queue: ProductionQueue; - tasks: ReadonlyArray; - isActive: boolean; - isWaiting: boolean; - currentTaskFinishTimestamp: number; - currentTaskFinishSeconds: number; - firstTask: ResourceLineState; - allTasks: ResourceLineState; - taskCount: number; -} - interface VillageOwnState { /** * Village id diff --git a/src/utils.ts b/src/utils.ts index 461e854..96cfc24 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,5 @@ import * as URLParse from 'url-parse'; +import * as _ from 'underscore'; export function sleep(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); @@ -67,20 +68,6 @@ export function elClassId(classes: string | undefined, prefix: string): number | 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 { const normalized = String(value) .replace('\u2212', '\u002d') // minus to hyphen-minus @@ -119,3 +106,7 @@ export function markPage(text: string, version: string) { '' ); } + +export function first(items: ReadonlyArray): T | undefined { + return _.first(items); +}