2025-01-12 15:33:05 -08:00
|
|
|
import { globalScene } from "#app/global-scene";
|
2024-09-01 12:18:40 -07:00
|
|
|
import { TextStyle, addTextObject } from "#app/ui/text";
|
2025-01-12 15:33:05 -08:00
|
|
|
import type { nil } from "#app/utils";
|
2024-09-01 12:18:40 -07:00
|
|
|
import i18next from "i18next";
|
2024-12-20 17:11:06 -05:00
|
|
|
import { Species } from "#enums/species";
|
2025-01-12 15:33:05 -08:00
|
|
|
import type { WeatherPoolEntry } from "#app/data/weather";
|
2024-12-20 17:11:06 -05:00
|
|
|
import { WeatherType } from "#enums/weather-type";
|
2024-06-14 12:42:37 -04:00
|
|
|
|
|
|
|
export enum EventType {
|
2024-09-01 12:18:40 -07:00
|
|
|
SHINY,
|
2024-10-26 17:14:17 +01:00
|
|
|
NO_TIMER_DISPLAY
|
2024-06-14 12:42:37 -04:00
|
|
|
}
|
|
|
|
|
2024-09-01 12:18:40 -07:00
|
|
|
interface EventBanner {
|
|
|
|
bannerKey?: string;
|
2024-10-26 17:14:17 +01:00
|
|
|
xOffset?: number;
|
|
|
|
yOffset?: number;
|
2024-09-01 12:18:40 -07:00
|
|
|
scale?: number;
|
|
|
|
availableLangs?: string[];
|
|
|
|
}
|
|
|
|
|
2024-12-20 17:11:06 -05:00
|
|
|
interface EventEncounter {
|
|
|
|
species: Species;
|
|
|
|
allowEvolution?: boolean;
|
|
|
|
}
|
|
|
|
|
2024-09-01 12:18:40 -07:00
|
|
|
interface TimedEvent extends EventBanner {
|
|
|
|
name: string;
|
|
|
|
eventType: EventType;
|
|
|
|
shinyMultiplier?: number;
|
2024-10-26 17:14:17 +01:00
|
|
|
friendshipMultiplier?: number;
|
2024-09-01 12:18:40 -07:00
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
2024-12-20 17:11:06 -05:00
|
|
|
uncommonBreedEncounters?: EventEncounter[];
|
|
|
|
delibirdyBuff?: string[];
|
|
|
|
weather?: WeatherPoolEntry[];
|
2024-06-14 12:42:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const timedEvents: TimedEvent[] = [
|
|
|
|
{
|
2024-12-20 17:11:06 -05:00
|
|
|
name: "Winter Holiday Update",
|
2024-10-26 17:14:17 +01:00
|
|
|
eventType: EventType.SHINY,
|
|
|
|
shinyMultiplier: 2,
|
2024-12-20 17:11:06 -05:00
|
|
|
friendshipMultiplier: 1,
|
|
|
|
startDate: new Date(Date.UTC(2024, 11, 21, 0)),
|
|
|
|
endDate: new Date(Date.UTC(2025, 0, 4, 0)),
|
|
|
|
bannerKey: "winter_holidays2024-event-",
|
2024-09-07 23:17:02 -04:00
|
|
|
scale: 0.21,
|
2024-12-20 17:11:06 -05:00
|
|
|
availableLangs: [ "en", "de", "it", "fr", "ja", "ko", "es-ES", "pt-BR", "zh-CN" ],
|
|
|
|
uncommonBreedEncounters: [
|
|
|
|
{ species: Species.GIMMIGHOUL },
|
|
|
|
{ species: Species.DELIBIRD },
|
2024-12-20 18:53:42 -05:00
|
|
|
{ species: Species.STANTLER, allowEvolution: true },
|
2024-12-20 17:11:06 -05:00
|
|
|
{ species: Species.CYNDAQUIL, allowEvolution: true },
|
|
|
|
{ species: Species.PIPLUP, allowEvolution: true },
|
|
|
|
{ species: Species.CHESPIN, allowEvolution: true },
|
|
|
|
{ species: Species.BALTOY, allowEvolution: true },
|
|
|
|
{ species: Species.SNOVER, allowEvolution: true },
|
|
|
|
{ species: Species.CHINGLING, allowEvolution: true },
|
|
|
|
{ species: Species.LITWICK, allowEvolution: true },
|
|
|
|
{ species: Species.CUBCHOO, allowEvolution: true },
|
|
|
|
{ species: Species.SWIRLIX, allowEvolution: true },
|
|
|
|
{ species: Species.AMAURA, allowEvolution: true },
|
|
|
|
{ species: Species.MUDBRAY, allowEvolution: true },
|
|
|
|
{ species: Species.ROLYCOLY, allowEvolution: true },
|
|
|
|
{ species: Species.MILCERY, allowEvolution: true },
|
|
|
|
{ species: Species.SMOLIV, allowEvolution: true },
|
|
|
|
{ species: Species.ALOLA_VULPIX, allowEvolution: true },
|
|
|
|
{ species: Species.GALAR_DARUMAKA, allowEvolution: true },
|
|
|
|
{ species: Species.IRON_BUNDLE }
|
|
|
|
],
|
|
|
|
delibirdyBuff: [ "CATCHING_CHARM", "SHINY_CHARM", "ABILITY_CHARM", "EXP_CHARM", "SUPER_EXP_CHARM", "HEALING_CHARM" ],
|
|
|
|
weather: [{ weatherType: WeatherType.SNOW, weight: 1 }]
|
2024-08-17 05:33:31 +01:00
|
|
|
}
|
2024-06-14 12:42:37 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
export class TimedEventManager {
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
isActive(event: TimedEvent) {
|
|
|
|
return (
|
|
|
|
event.startDate < new Date() &&
|
|
|
|
new Date() < event.endDate
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
activeEvent(): TimedEvent | undefined {
|
|
|
|
return timedEvents.find((te: TimedEvent) => this.isActive(te));
|
|
|
|
}
|
|
|
|
|
|
|
|
isEventActive(): boolean {
|
|
|
|
return timedEvents.some((te: TimedEvent) => this.isActive(te));
|
|
|
|
}
|
|
|
|
|
|
|
|
activeEventHasBanner(): boolean {
|
|
|
|
const activeEvents = timedEvents.filter((te) => this.isActive(te) && te.hasOwnProperty("bannerFilename"));
|
|
|
|
return activeEvents.length > 0;
|
|
|
|
}
|
|
|
|
|
2024-10-26 17:14:17 +01:00
|
|
|
getFriendshipMultiplier(): number {
|
|
|
|
let multiplier = 1;
|
|
|
|
const friendshipEvents = timedEvents.filter((te) => this.isActive(te));
|
|
|
|
friendshipEvents.forEach((fe) => {
|
|
|
|
multiplier *= fe.friendshipMultiplier ?? 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
return multiplier;
|
|
|
|
}
|
|
|
|
|
2024-06-14 12:42:37 -04:00
|
|
|
getShinyMultiplier(): number {
|
|
|
|
let multiplier = 1;
|
|
|
|
const shinyEvents = timedEvents.filter((te) => te.eventType === EventType.SHINY && this.isActive(te));
|
|
|
|
shinyEvents.forEach((se) => {
|
2024-09-30 10:11:45 -07:00
|
|
|
multiplier *= se.shinyMultiplier ?? 1;
|
2024-06-14 12:42:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return multiplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
getEventBannerFilename(): string {
|
2024-09-30 10:11:45 -07:00
|
|
|
return timedEvents.find((te: TimedEvent) => this.isActive(te))?.bannerKey ?? "";
|
2024-06-14 12:42:37 -04:00
|
|
|
}
|
|
|
|
}
|
2024-06-23 12:09:23 -04:00
|
|
|
|
|
|
|
export class TimedEventDisplay extends Phaser.GameObjects.Container {
|
2024-09-30 10:11:45 -07:00
|
|
|
private event: TimedEvent | nil;
|
2024-06-23 12:09:23 -04:00
|
|
|
private eventTimerText: Phaser.GameObjects.Text;
|
|
|
|
private banner: Phaser.GameObjects.Image;
|
2024-10-26 17:14:17 +01:00
|
|
|
private availableWidth: number;
|
2024-08-07 09:23:12 -07:00
|
|
|
private eventTimer: NodeJS.Timeout | null;
|
2024-06-23 12:09:23 -04:00
|
|
|
|
2025-01-12 15:33:05 -08:00
|
|
|
constructor(x: number, y: number, event?: TimedEvent) {
|
|
|
|
super(globalScene, x, y);
|
|
|
|
this.availableWidth = globalScene.scaledCanvas.width;
|
2024-09-30 10:11:45 -07:00
|
|
|
this.event = event;
|
2024-06-23 12:09:23 -04:00
|
|
|
this.setVisible(false);
|
|
|
|
}
|
|
|
|
|
2024-10-26 17:14:17 +01:00
|
|
|
/**
|
|
|
|
* Set the width that can be used to display the event timer and banner. By default
|
|
|
|
* these elements get centered horizontally in that space, in the bottom left of the screen
|
|
|
|
*/
|
|
|
|
setWidth(width: number) {
|
|
|
|
if (width !== this.availableWidth) {
|
|
|
|
this.availableWidth = width;
|
|
|
|
const xPosition = this.availableWidth / 2 + (this.event?.xOffset ?? 0);
|
|
|
|
if (this.banner) {
|
|
|
|
this.banner.x = xPosition;
|
|
|
|
}
|
|
|
|
if (this.eventTimerText) {
|
|
|
|
this.eventTimerText.x = xPosition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 12:09:23 -04:00
|
|
|
setup() {
|
2024-09-01 12:18:40 -07:00
|
|
|
const lang = i18next.resolvedLanguage;
|
|
|
|
if (this.event && this.event.bannerKey) {
|
2024-09-01 21:16:20 +01:00
|
|
|
let key = this.event.bannerKey;
|
2024-09-01 12:18:40 -07:00
|
|
|
if (lang && this.event.availableLangs && this.event.availableLangs.length > 0) {
|
|
|
|
if (this.event.availableLangs.includes(lang)) {
|
2024-10-26 17:14:17 +01:00
|
|
|
key += lang;
|
2024-09-01 12:18:40 -07:00
|
|
|
} else {
|
2024-10-26 17:14:17 +01:00
|
|
|
key += "en";
|
2024-09-01 12:18:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(this.event.bannerKey);
|
2024-10-26 17:14:17 +01:00
|
|
|
const padding = 5;
|
|
|
|
const showTimer = this.event.eventType !== EventType.NO_TIMER_DISPLAY;
|
2025-01-12 15:33:05 -08:00
|
|
|
const yPosition = globalScene.game.canvas.height / 6 - padding - (showTimer ? 10 : 0) - (this.event.yOffset ?? 0);
|
|
|
|
this.banner = new Phaser.GameObjects.Image(globalScene, this.availableWidth / 2, yPosition - padding, key);
|
2024-09-01 12:18:40 -07:00
|
|
|
this.banner.setName("img-event-banner");
|
2024-10-26 17:14:17 +01:00
|
|
|
this.banner.setOrigin(0.5, 1);
|
2024-09-01 12:18:40 -07:00
|
|
|
this.banner.setScale(this.event.scale ?? 0.18);
|
2024-10-26 17:14:17 +01:00
|
|
|
if (showTimer) {
|
2024-09-01 12:18:40 -07:00
|
|
|
this.eventTimerText = addTextObject(
|
2024-10-26 17:14:17 +01:00
|
|
|
this.banner.x,
|
|
|
|
this.banner.y + 2,
|
2024-09-01 12:18:40 -07:00
|
|
|
this.timeToGo(this.event.endDate),
|
|
|
|
TextStyle.WINDOW
|
|
|
|
);
|
|
|
|
this.eventTimerText.setName("text-event-timer");
|
|
|
|
this.eventTimerText.setScale(0.15);
|
2024-10-26 17:14:17 +01:00
|
|
|
this.eventTimerText.setOrigin(0.5, 0);
|
2024-09-01 12:18:40 -07:00
|
|
|
|
|
|
|
this.add(this.eventTimerText);
|
|
|
|
}
|
|
|
|
this.add(this.banner);
|
|
|
|
}
|
2024-06-23 12:09:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
show() {
|
|
|
|
this.setVisible(true);
|
|
|
|
this.updateCountdown();
|
|
|
|
|
|
|
|
this.eventTimer = setInterval(() => {
|
|
|
|
this.updateCountdown();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
this.setVisible(false);
|
2024-08-07 09:23:12 -07:00
|
|
|
this.eventTimer && clearInterval(this.eventTimer);
|
2024-06-23 12:09:23 -04:00
|
|
|
this.eventTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private timeToGo(date: Date) {
|
|
|
|
|
|
|
|
// Utility to add leading zero
|
|
|
|
function z(n) {
|
2024-10-04 13:08:31 +08:00
|
|
|
return (n < 10 ? "0" : "") + n;
|
2024-06-23 12:09:23 -04:00
|
|
|
}
|
|
|
|
const now = new Date();
|
|
|
|
let diff = Math.abs(date.getTime() - now.getTime());
|
|
|
|
|
|
|
|
// Allow for previous times
|
|
|
|
diff = Math.abs(diff);
|
|
|
|
|
|
|
|
// Get time components
|
2024-10-04 13:08:31 +08:00
|
|
|
const days = diff / 8.64e7 | 0;
|
|
|
|
const hours = diff % 8.64e7 / 3.6e6 | 0;
|
|
|
|
const mins = diff % 3.6e6 / 6e4 | 0;
|
|
|
|
const secs = Math.round(diff % 6e4 / 1e3);
|
2024-06-23 12:09:23 -04:00
|
|
|
|
|
|
|
// Return formatted string
|
2024-12-07 02:49:09 +01:00
|
|
|
return i18next.t("menu:eventTimer", { days: z(days), hours: z(hours), mins: z(mins), secs: z(secs) });
|
2024-06-23 12:09:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
updateCountdown() {
|
2024-10-26 17:14:17 +01:00
|
|
|
if (this.event && this.event.eventType !== EventType.NO_TIMER_DISPLAY) {
|
2024-09-01 12:18:40 -07:00
|
|
|
this.eventTimerText.setText(this.timeToGo(this.event.endDate));
|
|
|
|
}
|
2024-06-23 12:09:23 -04:00
|
|
|
}
|
|
|
|
}
|