Skip to content

Commit

Permalink
event trigger on event, event date less stupid
Browse files Browse the repository at this point in the history
  • Loading branch information
onlypuppy7 committed Dec 4, 2024
1 parent 744a675 commit 6d9872f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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",
Expand Down
15 changes: 11 additions & 4 deletions plugins_default/christmasevent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import fs from 'node:fs';
import path from 'node:path';
//plugin: samplecommand
import { ChristmasEvent } from './shared.js';
//

export const PluginMeta = {
Expand All @@ -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'
});
Expand All @@ -58,4 +57,12 @@ export class Plugin {
};
};
};

async roomInitGameOptions(data) {
if (events.currentArray.includes("december")) {
var ctx = data.this

ctx.gameOptions.weather.snowStormEnabled = true;
};
};
};
114 changes: 80 additions & 34 deletions src/shell/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,129 +21,130 @@ 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: {},
},
];

export class EventManager {
constructor() {
this.printed = false;
};

async init() {
Expand All @@ -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;
Expand Down Expand Up @@ -208,15 +254,15 @@ 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;
};

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,
Expand Down

0 comments on commit 6d9872f

Please sign in to comment.