2024-09-01 20:18:40 +01:00
|
|
|
import BattleScene from "#app/battle-scene";
|
|
|
|
import { TextStyle, addTextObject } from "#app/ui/text";
|
2024-09-30 18:11:45 +01:00
|
|
|
import { nil } from "#app/utils";
|
2024-09-01 20:18:40 +01:00
|
|
|
import i18next from "i18next";
|
2024-06-14 17:42:37 +01:00
|
|
|
|
|
|
|
export enum EventType {
|
2024-09-01 20:18:40 +01:00
|
|
|
SHINY,
|
|
|
|
GENERIC
|
2024-06-14 17:42:37 +01:00
|
|
|
}
|
|
|
|
|
2024-09-01 20:18:40 +01:00
|
|
|
interface EventBanner {
|
|
|
|
bannerKey?: string;
|
|
|
|
xPosition?: number;
|
|
|
|
yPosition?: number;
|
|
|
|
scale?: number;
|
|
|
|
availableLangs?: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TimedEvent extends EventBanner {
|
|
|
|
name: string;
|
|
|
|
eventType: EventType;
|
|
|
|
shinyMultiplier?: number;
|
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
2024-06-14 17:42:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const timedEvents: TimedEvent[] = [
|
|
|
|
{
|
2024-09-08 04:17:02 +01:00
|
|
|
name: "Egg Skip Update",
|
2024-09-01 20:18:40 +01:00
|
|
|
eventType: EventType.GENERIC,
|
2024-09-08 04:17:02 +01:00
|
|
|
startDate: new Date(Date.UTC(2024, 8, 8, 0)),
|
|
|
|
endDate: new Date(Date.UTC(2024, 8, 12, 0)),
|
|
|
|
bannerKey: "egg-update",
|
2024-09-01 20:18:40 +01:00
|
|
|
xPosition: 19,
|
2024-09-08 04:17:02 +01:00
|
|
|
yPosition: 120,
|
|
|
|
scale: 0.21,
|
2024-09-01 23:33:10 +01:00
|
|
|
availableLangs: ["en", "de", "it", "fr", "ja", "ko", "es", "pt-BR", "zh-CN"]
|
2024-08-17 05:33:31 +01:00
|
|
|
}
|
2024-06-14 17:42:37 +01: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
getShinyMultiplier(): number {
|
|
|
|
let multiplier = 1;
|
|
|
|
const shinyEvents = timedEvents.filter((te) => te.eventType === EventType.SHINY && this.isActive(te));
|
|
|
|
shinyEvents.forEach((se) => {
|
2024-09-30 18:11:45 +01:00
|
|
|
multiplier *= se.shinyMultiplier ?? 1;
|
2024-06-14 17:42:37 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return multiplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
getEventBannerFilename(): string {
|
2024-09-30 18:11:45 +01:00
|
|
|
return timedEvents.find((te: TimedEvent) => this.isActive(te))?.bannerKey ?? "";
|
2024-06-14 17:42:37 +01:00
|
|
|
}
|
|
|
|
}
|
2024-06-23 17:09:23 +01:00
|
|
|
|
|
|
|
export class TimedEventDisplay extends Phaser.GameObjects.Container {
|
2024-09-30 18:11:45 +01:00
|
|
|
private event: TimedEvent | nil;
|
2024-06-23 17:09:23 +01:00
|
|
|
private eventTimerText: Phaser.GameObjects.Text;
|
|
|
|
private banner: Phaser.GameObjects.Image;
|
|
|
|
private bannerShadow: Phaser.GameObjects.Rectangle;
|
2024-08-07 17:23:12 +01:00
|
|
|
private eventTimer: NodeJS.Timeout | null;
|
2024-06-23 17:09:23 +01:00
|
|
|
|
2024-08-07 17:23:12 +01:00
|
|
|
constructor(scene: BattleScene, x: number, y: number, event?: TimedEvent) {
|
2024-06-23 17:09:23 +01:00
|
|
|
super(scene, x, y);
|
2024-09-30 18:11:45 +01:00
|
|
|
this.event = event;
|
2024-06-23 17:09:23 +01:00
|
|
|
this.setVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
setup() {
|
2024-09-01 20:18:40 +01: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 20:18:40 +01:00
|
|
|
if (lang && this.event.availableLangs && this.event.availableLangs.length > 0) {
|
|
|
|
if (this.event.availableLangs.includes(lang)) {
|
2024-09-08 04:17:02 +01:00
|
|
|
key += "_"+lang;
|
2024-09-01 20:18:40 +01:00
|
|
|
} else {
|
2024-09-08 04:17:02 +01:00
|
|
|
key += "_en";
|
2024-09-01 20:18:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(this.event.bannerKey);
|
2024-09-01 21:11:23 +01:00
|
|
|
this.banner = new Phaser.GameObjects.Image(this.scene, this.event.xPosition ?? 29, this.event.yPosition ?? 64, key);
|
2024-09-01 20:18:40 +01:00
|
|
|
this.banner.setName("img-event-banner");
|
|
|
|
this.banner.setOrigin(0.08, -0.35);
|
|
|
|
this.banner.setScale(this.event.scale ?? 0.18);
|
|
|
|
if (this.event.eventType !== EventType.GENERIC) {
|
|
|
|
this.eventTimerText = addTextObject(
|
|
|
|
this.scene,
|
|
|
|
this.banner.x + 8,
|
|
|
|
this.banner.y + 100,
|
|
|
|
this.timeToGo(this.event.endDate),
|
|
|
|
TextStyle.WINDOW
|
|
|
|
);
|
|
|
|
this.eventTimerText.setName("text-event-timer");
|
|
|
|
this.eventTimerText.setScale(0.15);
|
|
|
|
this.eventTimerText.setOrigin(0, 0);
|
|
|
|
|
|
|
|
this.add(this.eventTimerText);
|
|
|
|
}
|
|
|
|
this.add(this.banner);
|
|
|
|
}
|
2024-06-23 17:09:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
show() {
|
|
|
|
this.setVisible(true);
|
|
|
|
this.updateCountdown();
|
|
|
|
|
|
|
|
this.eventTimer = setInterval(() => {
|
|
|
|
this.updateCountdown();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
this.setVisible(false);
|
2024-08-07 17:23:12 +01:00
|
|
|
this.eventTimer && clearInterval(this.eventTimer);
|
2024-06-23 17:09:23 +01:00
|
|
|
this.eventTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private timeToGo(date: Date) {
|
|
|
|
|
|
|
|
// Utility to add leading zero
|
|
|
|
function z(n) {
|
|
|
|
return (n < 10? "0" : "") + n;
|
|
|
|
}
|
|
|
|
const now = new Date();
|
|
|
|
let diff = Math.abs(date.getTime() - now.getTime());
|
|
|
|
|
|
|
|
// Allow for previous times
|
|
|
|
diff = Math.abs(diff);
|
|
|
|
|
|
|
|
// Get time components
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Return formatted string
|
|
|
|
return "Event Ends in : " + z(days) + "d " + z(hours) + "h " + z(mins) + "m " + z(secs)+ "s";
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCountdown() {
|
2024-09-01 20:18:40 +01:00
|
|
|
if (this.event && this.event.eventType !== EventType.GENERIC) {
|
|
|
|
this.eventTimerText.setText(this.timeToGo(this.event.endDate));
|
|
|
|
}
|
2024-06-23 17:09:23 +01:00
|
|
|
}
|
|
|
|
}
|