From 6d9872f4e54e96fd3bf82e3318b992f5a980e04b Mon Sep 17 00:00:00 2001 From: onlypuppy7 <35199523+onlypuppy7@users.noreply.github.com> Date: Wed, 4 Dec 2024 01:12:25 +0000 Subject: [PATCH] event trigger on event, event date less stupid --- .../christmasevent/{shared.js => client.js} | 10 +- plugins_default/christmasevent/index.js | 15 ++- src/shell/events.js | 114 ++++++++++++------ 3 files changed, 92 insertions(+), 47 deletions(-) rename plugins_default/christmasevent/{shared.js => client.js} (97%) diff --git a/plugins_default/christmasevent/shared.js b/plugins_default/christmasevent/client.js similarity index 97% rename from plugins_default/christmasevent/shared.js rename to plugins_default/christmasevent/client.js index 1d37f42f..3f979c08 100644 --- a/plugins_default/christmasevent/shared.js +++ b/plugins_default/christmasevent/client.js @@ -5,7 +5,7 @@ import { iterateXYZ } from "#loading"; import { plugins } from '#plugins'; // -export var isChristmas = true; +var isChristmas = events.currentArray.includes("december"); export const ChristmasEvent = { registerListeners: function (pluginManager) { @@ -14,19 +14,11 @@ export const ChristmasEvent = { this.plugins = pluginManager; if (isChristmas) { - this.plugins.on('game:roomInitGameOptions', this.roomInitGameOptions.bind(this)); - this.plugins.on('game:createMapCellsMapLoaded', this.createMapCellsMapLoaded.bind(this)); this.plugins.on('game:createMapCells', this.createMapCells.bind(this)); }; }, - async roomInitGameOptions(data) { - var ctx = data.this - - ctx.gameOptions.weather.snowStormEnabled = true; - }, - async createMapCells(data) { const replacements = { // "nature.tree-01.aabb": "nature.tree-01-snow.aabb", diff --git a/plugins_default/christmasevent/index.js b/plugins_default/christmasevent/index.js index 2e7ae05e..b62edc2a 100644 --- a/plugins_default/christmasevent/index.js +++ b/plugins_default/christmasevent/index.js @@ -2,7 +2,6 @@ import fs from 'node:fs'; import path from 'node:path'; //plugin: samplecommand -import { ChristmasEvent } from './shared.js'; // export const PluginMeta = { @@ -24,18 +23,18 @@ export class Plugin { pluginInstance = this; - ChristmasEvent.registerListeners(this.plugins); - this.plugins.on('client:pluginSourceInsertion', this.pluginSourceInsertion.bind(this)); this.plugins.on('client:prepareBabylon', this.prepareBabylon.bind(this)); this.plugins.on('game:prepareBabylon', this.prepareBabylon.bind(this)); + + this.plugins.on('game:roomInitGameOptions', this.roomInitGameOptions.bind(this)); }; pluginSourceInsertion(data) { data.pluginInsertion.files.push({ insertBefore: '\nconsole.log("inserting before... (ChristmasEvent)");', - filepath: path.join(this.thisDir, 'shared.js'), + filepath: path.join(this.thisDir, 'client.js'), insertAfter: '\nconsole.log("inserting after... (ChristmasEvent)!");', position: 'before' }); @@ -58,4 +57,12 @@ export class Plugin { }; }; }; + + async roomInitGameOptions(data) { + if (events.currentArray.includes("december")) { + var ctx = data.this + + ctx.gameOptions.weather.snowStormEnabled = true; + }; + }; }; \ No newline at end of file diff --git a/src/shell/events.js b/src/shell/events.js index b69db8eb..9ffee53b 100644 --- a/src/shell/events.js +++ b/src/shell/events.js @@ -21,122 +21,122 @@ import { plugins } from '#plugins'; export const defaultEvents = [{ name: 'spring', - start: "W12-1", + start: "03-20", duration: "13w", data: {}, }, { name: 'summer', - start: "W25-1", + start: "06-20", duration: "13w", data: {}, }, { name: 'autumn', - start: "W38-1", + start: "09-22", duration: "13w", data: {}, }, { name: 'winter', - start: "W51-1", + start: "12-21", duration: "13w", data: {}, }, { name: 'christmas', - start: "W50-7", - duration: "1w", + start: "12-10", + duration: "4w", data: {}, }, { name: 'new-year', - start: "W50-7", + start: "12-31", duration: "2w", data: {}, }, { name: 'easter', - start: "W12-4", //annoying holiday that moves around + start: "03-24", //annoying holiday that moves around duration: "4w", data: {}, }, { name: 'halloween', - start: "W42-7", - duration: "1w", + start: "10-15", + duration: "3w", data: {}, }, { name: 'valentines', - start: "W06-4", + start: "02-14", duration: "1w", data: {}, }, { name: 'april-fools', - start: "W13-2", + start: "04-01", duration: "1w", data: {}, }, { name: 'thanksgiving', - start: "W47-4", - duration: "1w", + start: "11-22", + duration: "2w", data: {}, }, { name: 'independence-day', - start: "W27-7", + start: "07-04", duration: "1w", data: {}, }, { name:'january', - start: "W00-1", + start: "01-01", duration: "31d", data: {}, }, { name:'february', - start: "W04-4", + start: "02-01", duration: "28d", data: {}, }, { name:'march', - start: "W08-4", + start: "03-01", duration: "31d", data: {}, }, { name:'april', - start: "W13-2", + start: "04-01", duration: "30d", data: {}, }, { name:'may', - start: "W17-4", + start: "05-01", duration: "31d", data: {}, }, { name:'june', - start: "W21-7", + start: "06-01", duration: "30d", data: {}, }, { name:'july', - start: "W26-2", + start: "07-01", duration: "31d", data: {}, }, { name:'august', - start: "W30-4", + start: "08-01", duration: "31d", data: {}, }, { name:'september', - start: "W35-1", + start: "09-01", duration: "30d", data: {}, }, { name:'october', - start: "W39-3", + start: "10-01", duration: "31d", data: {}, }, { name:'november', - start: "W43-6", + start: "11-01", duration: "30d", data: {}, }, { name:'december', - start: "W47-7", + start: "12-01", duration: "31d", data: {}, }, @@ -144,6 +144,7 @@ export const defaultEvents = [{ export class EventManager { constructor() { + this.printed = false; }; async init() { @@ -159,26 +160,71 @@ export class EventManager { this.currentArray = []; for (const event of this.events) { - const start = this.weekDayToUnix(event.start); - const duration = this.parseHumanToMs(event.duration); + const start = this.parseDate(event.start, time); + const duration = this.parseHumanToMs(event.duration, time); const end = start + duration; - // console.log(event, time, start, duration, end); + if (!this.printed) devlog(event, time, start, duration, end); if (start <= time && time <= end) { - devlog('event', event.name, 'is happening now'); + if (!this.printed) log.bgCyan('event', event.name, 'is happening now'); this.current.push(event); this.currentArray.push(event.name); }; }; + this.printed = true; + return { current: this.current, currentArray: this.currentArray, }; }; - weekDayToUnix(weekDay) { + isActive(event) { + return this.getEventsAtTime().currentArray.includes(event); + }; + + parseDate(dateStr, timeNow = Date.now()) { + if (dateStr.match(/^\d{4}-\d{2}-\d{2}$/) || dateStr.match(/^\d{2}-\d{2}$/)) { + return this.dateToUnix(dateStr, timeNow); + } else if (dateStr.match(/^W\d{2}-\d$/)) { + return this.weekDayToUnix(dateStr, timeNow); + } else { + throw new Error('Invalid format. Use YYYY-MM-DD or MM-DD or "W"WW-DD.'); + }; + }; + + dateToUnix(dateStr, timeNow = Date.now()) { + const currentYear = new Date().getFullYear(); + let year, month, day, match; + + if (dateStr.match(/^\d{4}-\d{2}-\d{2}$/)) { + match = dateStr.match(/^(\d{4})-(\d{2})-(\d{2})$/); + year = Number(match[1]); + month = Number(match[2]) - 1; + day = Number(match[3]); + } else if (dateStr.match(/^\d{2}-\d{2}$/)) { + match = dateStr.match(/^(\d{2})-(\d{2})$/); + year = currentYear; + month = Number(match[1]) - 1; + day = Number(match[2]); + } else { + throw new Error('Invalid format. Use YYYY-MM-DD or MM-DD.'); + }; + + const targetDate = new Date(year, month, day); + let time = targetDate.getTime(); + + if (timeNow <= time) { + targetDate.setFullYear(targetDate.getFullYear() - 1); + time = targetDate.getTime(); + }; + + return time; + }; + + weekDayToUnix(weekDay, timeNow = Date.now()) { const currentYear = new Date().getFullYear(); let year; let match; @@ -208,7 +254,7 @@ export class EventManager { let time = Math.floor(targetDate.getTime()); - if (Date.now() <= time) { + if (timeNow <= time) { //take away a year time -= 1e3*60*60*24*365; }; @@ -216,7 +262,7 @@ export class EventManager { return time; }; - parseHumanToMs(input) { //takes inputs like 2w3d4h5min6s7ms (extreme example, more likely just 1w) and converts it to ms + parseHumanToMs(input, time = Date.now()) { //takes inputs like 2w3d4h5min6s7ms (extreme example, more likely just 1w) and converts it to ms const timeUnits = { w: 1e3*60*60*24*7, d: 1e3*60*60*24,