Skip to content

Commit 7b501c1

Browse files
committed
breh
1 parent 665e30b commit 7b501c1

2 files changed

Lines changed: 51 additions & 48 deletions

File tree

eventRunner.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

script.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { runRandomEvent } from './eventRunner.js';
1+
import { events } from './events.js';
22
console.log("Checking if .master elements exist...");
33
console.log(document.querySelectorAll(".master"));
44
document.addEventListener("DOMContentLoaded", () => {
@@ -308,3 +308,53 @@ window.onload = () => {
308308
document.getElementById("next-day").addEventListener("click", () => {
309309
runDayEvents(); // runs once for each alive master
310310
});
311+
312+
313+
314+
// eventRunner follows
315+
316+
function processEventTemplate(template, context) {
317+
return template.replace(/\{([a-zA-Z0-9_.]+)\}/g, (_, key) => {
318+
const parts = key.split(".");
319+
let value = context;
320+
for (const part of parts) {
321+
if (value && part in value) {
322+
value = value[part];
323+
} else {
324+
return `{${key}}`; // leave if missing
325+
}
326+
}
327+
return value;
328+
});
329+
}
330+
331+
function runRandomEvent(participants) {
332+
const aliveMasters = participants.filter(p => p.type === "master" && p.status === "alive");
333+
const aliveServants = participants.filter(p => p.type === "servant" && p.status === "alive");
334+
335+
const randomMaster = aliveMasters[Math.floor(Math.random() * aliveMasters.length)];
336+
const randomServant = participants.find(p => p.id === randomMaster.servantId);
337+
338+
const validEvents = events.filter(event => {
339+
const validPool = event.valid(participants);
340+
return validPool.includes(randomMaster);
341+
});
342+
343+
if (validEvents.length === 0) {
344+
console.warn("No valid events available.");
345+
return;
346+
}
347+
348+
const event = validEvents[Math.floor(Math.random() * validEvents.length)];
349+
350+
// Run effects
351+
event.effects(randomMaster, randomServant, participants);
352+
353+
// Generate event text
354+
const eventText = processEventTemplate(event.description, {
355+
master: randomMaster,
356+
servant: randomServant
357+
});
358+
359+
console.log(eventText);
360+
}

0 commit comments

Comments
 (0)