From 4cd868aeb302515b879ed204397b567c73d2fcd2 Mon Sep 17 00:00:00 2001 From: Chomp Date: Tue, 24 Dec 2024 18:54:12 +0000 Subject: [PATCH] Improved ability for modders to adjust active seasonal events - Active events can be set via pre-db load event --- project/src/services/SeasonalEventService.ts | 57 +++++++++++++------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/project/src/services/SeasonalEventService.ts b/project/src/services/SeasonalEventService.ts index c3bee4f7..a7c6014f 100644 --- a/project/src/services/SeasonalEventService.ts +++ b/project/src/services/SeasonalEventService.ts @@ -33,7 +33,7 @@ export class SeasonalEventService { protected christmasEventActive?: boolean = undefined; /** All events active at this point in time */ - protected currentlyActiveEvents: ISeasonalEvent[] = []; + protected currentlyActiveEvents: ISeasonalEvent[] = null; constructor( @inject("PrimaryLogger") protected logger: ILogger, @@ -50,8 +50,6 @@ export class SeasonalEventService { this.httpConfig = this.configServer.getConfig(ConfigTypes.HTTP); this.weatherConfig = this.configServer.getConfig(ConfigTypes.WEATHER); this.locationConfig = this.configServer.getConfig(ConfigTypes.LOCATION); - - this.cacheActiveEvents(); } protected get christmasEventItems(): string[] { @@ -85,6 +83,30 @@ export class SeasonalEventService { ]; } + protected getActiveEvents(): ISeasonalEvent[] { + if (this.currentlyActiveEvents === null) { + this.cacheActiveEvents(); + } + + return this.currentlyActiveEvents; + } + + /** + * Sets the currently active events + * @param activeEvents Array of active events + * @param halloweenEventActive Halloween event + * @param christmasEventActive Christmas event + */ + public setActiveEvents( + activeEvents: ISeasonalEvent[], + halloweenEventActive: boolean, + christmasEventActive: boolean, + ) { + this.currentlyActiveEvents = activeEvents; + this.halloweenEventActive = halloweenEventActive; + this.christmasEventActive = christmasEventActive; + } + /** * Get an array of christmas items found in bots inventories as loot * @returns array @@ -214,11 +236,9 @@ export class SeasonalEventService { * Handle activating seasonal events */ public enableSeasonalEvents(): void { - if (this.currentlyActiveEvents) { - const globalConfig = this.databaseService.getGlobals().config; - for (const event of this.currentlyActiveEvents) { - this.updateGlobalEvents(globalConfig, event); - } + const globalConfig = this.databaseService.getGlobals().config; + for (const event of this.getActiveEvents()) { + this.updateGlobalEvents(globalConfig, event); } } @@ -236,10 +256,11 @@ export class SeasonalEventService { /** * Store active events inside class array property `currentlyActiveEvents` + set class properties: christmasEventActive/halloweenEventActive */ - public cacheActiveEvents(): void { + protected cacheActiveEvents(): void { const currentDate = new Date(); const seasonalEvents = this.getEventDetails(); + this.currentlyActiveEvents = []; for (const event of seasonalEvents) { const eventStartDate = new Date(currentDate.getFullYear(), event.startMonth - 1, event.startDay); const eventEndDate = new Date(currentDate.getFullYear(), event.endMonth - 1, event.endDay); @@ -455,16 +476,14 @@ export class SeasonalEventService { } public givePlayerSeasonalGifts(sessionId: string): void { - if (this.currentlyActiveEvents) { - for (const event of this.currentlyActiveEvents) { - switch (event.type.toLowerCase()) { - case SeasonalEventType.CHRISTMAS.toLowerCase(): - this.giveGift(sessionId, "Christmas2022"); - break; - case SeasonalEventType.NEW_YEARS.toLowerCase(): - this.giveGift(sessionId, "NewYear2023"); - break; - } + for (const event of this.getActiveEvents()) { + switch (event.type.toLowerCase()) { + case SeasonalEventType.CHRISTMAS.toLowerCase(): + this.giveGift(sessionId, "Christmas2022"); + break; + case SeasonalEventType.NEW_YEARS.toLowerCase(): + this.giveGift(sessionId, "NewYear2023"); + break; } } }