Skip to content

Commit baec6e9

Browse files
committed
rewrite
1 parent 7b501c1 commit baec6e9

2 files changed

Lines changed: 95 additions & 100 deletions

File tree

events.js

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,62 @@
33
// Event pool array to store all events
44
const events = [
55
//Event 1: Master Death
6-
{
7-
id: "event_kill_master",
8-
description: "{master} walks into a landmine and dies.",
9-
type: "kill-master",
10-
targets: ["master"], // Needs one master as the target
11-
effect: (participants, master) => {
12-
master.status = "dead"; // Mark master as dead
13-
const servant = participants.find(p => p.type === "servant" && p.masterId === master.id);
14-
if (servant && !servant.hasIndependentAction) {
15-
servant.status = "dead"; // If the servant doesn't have Independent Action, they die too
6+
{
7+
id: "event_kill_master",
8+
description: "{master.name} walks into a landmine and dies.",
9+
valid: (participants) => {
10+
return participants.filter(p => p.type === "master" && p.status === "alive");
11+
},
12+
effects: (master, servant, participants) => {
13+
master.status = "dead";
14+
const linkedServant = participants.find(p => p.type === "servant" && p.masterId === master.id);
15+
if (linkedServant && !linkedServant.hasIndependentAction) {
16+
linkedServant.status = "dead";
17+
}
1618
}
17-
}
18-
},
19-
// Event 2: Duel
20-
{
21-
id: "event_duel",
22-
description: "{servant1} fights with {servant2} to the death.",
23-
type: "duel",
24-
targets: ["servant", "servant"], // Needs two servants as the targets
25-
effect: (participants, servant1, servant2) => {
26-
const loser = Math.random() < 0.5 ? servant1 : servant2;
27-
loser.status = "dead"; // Randomly pick a servant to die
28-
}
29-
},
30-
{
31-
id: "event_betrayal",
32-
description: "{servant1} betrays {master}. The master is left defenseless and killed.",
33-
type: "betrayal",
34-
targets: ["servant", "master"],
35-
effect: (participants, servant1, master) => {
36-
if (servant1 && master) {
37-
servant1.status = "alive"; // Servant survives
38-
master.status = "dead"; // Master is dead
19+
},
20+
21+
{
22+
id: "event_duel",
23+
description: "{servant1.name} fights with {servant2.name} to the death.",
24+
valid: (participants) => {
25+
const aliveServants = participants.filter(p => p.type === "servant" && p.status === "alive");
26+
if (aliveServants.length < 2) return [];
27+
// Generate all possible pairs
28+
const pairs = [];
29+
for (let i = 0; i < aliveServants.length; i++) {
30+
for (let j = i + 1; j < aliveServants.length; j++) {
31+
pairs.push([aliveServants[i], aliveServants[j]]);
32+
}
33+
}
34+
return pairs;
35+
},
36+
effects: (servant1, servant2, participants) => {
37+
const loser = Math.random() < 0.5 ? servant1 : servant2;
38+
loser.status = "dead";
3939
}
40-
}
41-
},
40+
},
41+
42+
{
43+
id: "event_betrayal",
44+
description: "{servant1.name} betrays {master.name}. The master is left defenseless and killed.",
45+
valid: (participants) => {
46+
const betrayers = [];
47+
const aliveServants = participants.filter(p => p.type === "servant" && p.status === "alive");
48+
aliveServants.forEach(servant => {
49+
const master = participants.find(p => p.id === servant.masterId && p.status === "alive");
50+
if (master) {
51+
betrayers.push([servant, master]);
52+
}
53+
});
54+
return betrayers;
55+
},
56+
effects: (servant1, master, participants) => {
57+
master.status = "dead";
58+
// Servant survives (no change)
59+
}
60+
},
61+
4262
{
4363
id: "poison_mushroom",
4464
description: "{master.name} eats a poisonous mushroom!",

script.js

Lines changed: 41 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ console.log(document.querySelectorAll(".master-img"));
3232
}); //this was the code for updating the master pictures
3333
window.onload = function() {
3434
console.log("Page fully loaded! Fetching Servant data...");
35-
fetchServantData();};
35+
fetchServantData();
36+
37+
console.log("Starting Day 1 events...");
38+
runDayEvents();
39+
};
3640
// now the code for the servant data:
3741
async function fetchServantData() {
3842
const url = 'https://api.atlasacademy.io/export/NA/basic_servant.json';
@@ -252,57 +256,55 @@ let currentDay = 1;
252256
let pastEvents = []; // store event IDs
253257
let participants = JSON.parse(localStorage.getItem("participants")) || [];
254258

255-
function processEventTemplate(template, master, servant) {
256-
return template
257-
.replace(/{master.name}/g, master.name)
258-
.replace(/{master.pronouns.subject}/g, master.pronouns.subject)
259-
.replace(/{master.pronouns.object}/g, master.pronouns.object)
260-
.replace(/{master.pronouns.possessive}/g, master.pronouns.possessive)
261-
.replace(/{servant.name}/g, servant ? servant.name : "no servant");
262-
}
263259

264260
function runDayEvents() {
265-
const aliveMasters = participants.filter(p => p.type === "master" && p.status === "alive");
266-
const aliveServants = participants.filter(p => p.type === "servant" && p.status === "alive");
267-
268261
console.log(`===== DAY ${currentDay} =====`);
269262

270-
aliveMasters.forEach(master => {
271-
const servant = aliveServants.find(s => s.id === master.servantId);
263+
// Loop over each event
264+
events.forEach(event => {
265+
const validPools = event.valid(participants);
272266

273-
const validEvents = events.filter(event => {
274-
const validPool = event.valid(participants, pastEvents);
275-
return validPool.includes(master);
276-
});
277-
278-
if (validEvents.length === 0) {
279-
console.log(`No valid events for ${master.name}`);
267+
if (validPools.length === 0) {
268+
console.log(`No valid targets for event ${event.id}`);
280269
return;
281270
}
282271

283-
const selectedEvent = validEvents[Math.floor(Math.random() * validEvents.length)];
272+
// Pick one random target or pair
273+
const chosen = validPools[Math.floor(Math.random() * validPools.length)];
284274

285-
// Apply effects
286-
selectedEvent.effects(master, servant, participants);
275+
if (Array.isArray(chosen)) {
276+
// Multi-target event (like servant vs servant or servant-master betrayal)
277+
event.effects(...chosen, participants);
287278

288-
// Save event id
289-
pastEvents.push(selectedEvent.id);
279+
// Generate event text using provided names
280+
const eventText = processEventTemplate(event.description, {
281+
servant1: chosen[0],
282+
servant2: chosen[1],
283+
master: chosen[1], // if second is master, works for betrayal template
284+
});
290285

291-
// Display event text
292-
const eventText = processEventTemplate(selectedEvent.description, master, servant);
293-
console.log(eventText);
286+
console.log(eventText);
287+
appendEventLog(`Day ${currentDay}: ${eventText}`);
288+
} else {
289+
// Single-target event (just a master)
290+
const master = chosen;
291+
const servant = participants.find(p => p.type === "servant" && p.masterId === master.id);
292+
293+
event.effects(master, servant, participants);
294+
295+
const eventText = processEventTemplate(event.description, {
296+
master,
297+
servant,
298+
});
294299

295-
const logDiv = document.getElementById("event-log");
296-
logDiv.innerHTML += `<p>Day ${currentDay}: ${eventText}</p>`;
300+
console.log(eventText);
301+
appendEventLog(`Day ${currentDay}: ${eventText}`);
302+
}
297303
});
298304

299305
currentDay++;
300306
}
301307

302-
// Run day 1 immediately after load
303-
window.onload = () => {
304-
runDayEvents(); // runs once for each master
305-
};
306308

307309
// Button for next days
308310
document.getElementById("next-day").addEventListener("click", () => {
@@ -327,34 +329,7 @@ function processEventTemplate(template, context) {
327329
return value;
328330
});
329331
}
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-
}
332+
const eventText = processEventTemplate(selectedEvent.description, {
333+
master: master,
334+
servant: servant
335+
});

0 commit comments

Comments
 (0)