Skip to content

Commit 264c6fd

Browse files
committed
massive update
1 parent 99633cf commit 264c6fd

4 files changed

Lines changed: 235 additions & 4 deletions

File tree

eventRunner.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { events } from './events.js';
2+
3+
function processEventTemplate(template, context) {
4+
return template.replace(/\{([a-zA-Z0-9_.]+)\}/g, (_, key) => {
5+
const parts = key.split(".");
6+
let value = context;
7+
for (const part of parts) {
8+
if (value && part in value) {
9+
value = value[part];
10+
} else {
11+
return `{${key}}`; // leave if missing
12+
}
13+
}
14+
return value;
15+
});
16+
}
17+
18+
export function runRandomEvent(participants) {
19+
const aliveMasters = participants.filter(p => p.type === "master" && p.status === "alive");
20+
const aliveServants = participants.filter(p => p.type === "servant" && p.status === "alive");
21+
22+
const randomMaster = aliveMasters[Math.floor(Math.random() * aliveMasters.length)];
23+
const randomServant = participants.find(p => p.id === randomMaster.servantId);
24+
25+
const validEvents = events.filter(event => {
26+
const validPool = event.valid(participants);
27+
return validPool.includes(randomMaster);
28+
});
29+
30+
if (validEvents.length === 0) {
31+
console.warn("No valid events available.");
32+
return;
33+
}
34+
35+
const event = validEvents[Math.floor(Math.random() * validEvents.length)];
36+
37+
// Run effects
38+
event.effects(randomMaster, randomServant, participants);
39+
40+
// Generate event text
41+
const eventText = processEventTemplate(event.description, {
42+
master: randomMaster,
43+
servant: randomServant
44+
});
45+
46+
console.log(eventText);
47+
}

events.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// events.js - Contains all event definitions for the simulation
2+
3+
// Event pool array to store all events
4+
const eventPool = [
5+
//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
16+
}
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+
// Add more events here as needed...
31+
{
32+
id: "event_betrayal",
33+
description: "{servant1} betrays {master}. The master is left defenseless and killed.",
34+
type: "betrayal",
35+
targets: ["servant", "master"],
36+
effect: (participants, servant1, master) => {
37+
if (servant1 && master) {
38+
servant1.status = "alive"; // Servant survives
39+
master.status = "dead"; // Master is dead
40+
}
41+
}
42+
},
43+
44+
];
45+
46+
// Export the eventPool so it can be used in other files
47+
export { eventPool };

script.js

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { runRandomEvent } from './eventRunner.js';
2+
13
console.log("Checking if .master elements exist...");
24
console.log(document.querySelectorAll(".master"));
35
document.addEventListener("DOMContentLoaded", () => {
@@ -74,8 +76,8 @@ function populateServantDropdown(servantDataArray) {
7476
servantDataArray.forEach(servant => {
7577
const option = document.createElement("option");
7678
// Extract a shortname; if you have a better key, replace servant.name here
77-
const shortName = servant.name.split(" ")[0]; // crude fallback: take first word like "Artoria"
78-
option.value = shortName; // store shortname for simulator matching
79+
//const shortName = servant.name.split(" ")[0]; // crude fallback: take first word like "Artoria"
80+
//option.value = shortName; // store shortname for simulator matching
7981
option.textContent = servant.name; // full display name for user
8082
option.dataset.id = servant.id; // store numeric ID if needed later
8183
dropdown.appendChild(option); // <-- fix here
@@ -163,9 +165,12 @@ function saveParticipantsAndStartSimulation() {
163165
let servantContainer = teamContainer.querySelector(".servant-selection");
164166

165167
let nameInput = masterContainer.querySelector(".master-name");
168+
let genderSelect = masterContainer.querySelector(".gender-select");
166169
let pictureEl = masterContainer.querySelector(".master-img");
167170

168171
let name = nameInput ? nameInput.value.trim() || `Master ${index + 1}` : `Master ${index + 1}`;
172+
let gender = genderSelect ? genderSelect.value : "nonbinary"; // fallback to nonbinary if missing
173+
let pronouns = getPronouns(gender);
169174
let pictureUrl = pictureEl ? pictureEl.src : "";
170175

171176
let servantDropdown = servantContainer ? servantContainer.querySelector(".servant-select") : null;
@@ -191,15 +196,29 @@ function saveParticipantsAndStartSimulation() {
191196
}
192197

193198
let masterData = {
199+
id: `master${index + 1}`,
194200
name: name,
201+
gender: gender,
202+
pronouns: pronouns,
195203
picture: pictureUrl,
196204
status: "alive",
197205
type: "master",
198206
servantId: servantId,
199-
servantName: servantName
207+
208+
};
209+
210+
let servantData = {
211+
id: servantId,
212+
name: servantName,
213+
type: "servant",
214+
status: "alive",
215+
masterId: masterData.id,
216+
hasIndependentAction: servantHasIndependentAction,
217+
image: servantImage
200218
};
201219

202220
participants.push(masterData);
221+
participants.push(servantData);
203222
});
204223

205224
console.log("Saved participants:", participants);
@@ -208,6 +227,15 @@ function saveParticipantsAndStartSimulation() {
208227
window.location.href = "simulation.html";
209228
}
210229

230+
function getPronouns(gender) {
231+
if (gender === "male") {
232+
return { subject: "he", object: "him", possessive: "his" };
233+
} else if (gender === "female") {
234+
return { subject: "she", object: "her", possessive: "her" };
235+
} else {
236+
return { subject: "they", object: "them", possessive: "their" };
237+
}
238+
}
211239

212240
//code to attach the above function to the button:
213241
document.addEventListener("DOMContentLoaded", function () {
@@ -218,4 +246,66 @@ document.addEventListener("DOMContentLoaded", function () {
218246
} else {
219247
console.error("Start Simulation button not found!");
220248
}
221-
});
249+
});
250+
251+
//the code to run random events follows...?
252+
let currentDay = 1;
253+
let pastEvents = []; // store event IDs
254+
let participants = JSON.parse(localStorage.getItem("participants")) || [];
255+
256+
function processEventTemplate(template, master, servant) {
257+
return template
258+
.replace(/{master.name}/g, master.name)
259+
.replace(/{master.pronouns.subject}/g, master.pronouns.subject)
260+
.replace(/{master.pronouns.object}/g, master.pronouns.object)
261+
.replace(/{master.pronouns.possessive}/g, master.pronouns.possessive)
262+
.replace(/{servant.name}/g, servant ? servant.name : "no servant");
263+
}
264+
265+
function runDayEvents() {
266+
const aliveMasters = participants.filter(p => p.type === "master" && p.status === "alive");
267+
const aliveServants = participants.filter(p => p.type === "servant" && p.status === "alive");
268+
269+
console.log(`===== DAY ${currentDay} =====`);
270+
271+
aliveMasters.forEach(master => {
272+
const servant = aliveServants.find(s => s.id === master.servantId);
273+
274+
const validEvents = events.filter(event => {
275+
const validPool = event.valid(participants, pastEvents);
276+
return validPool.includes(master);
277+
});
278+
279+
if (validEvents.length === 0) {
280+
console.log(`No valid events for ${master.name}`);
281+
return;
282+
}
283+
284+
const selectedEvent = validEvents[Math.floor(Math.random() * validEvents.length)];
285+
286+
// Apply effects
287+
selectedEvent.effects(master, servant, participants);
288+
289+
// Save event id
290+
pastEvents.push(selectedEvent.id);
291+
292+
// Display event text
293+
const eventText = processEventTemplate(selectedEvent.description, master, servant);
294+
console.log(eventText);
295+
296+
const logDiv = document.getElementById("event-log");
297+
logDiv.innerHTML += `<p>Day ${currentDay}: ${eventText}</p>`;
298+
});
299+
300+
currentDay++;
301+
}
302+
303+
// Run day 1 immediately after load
304+
window.onload = () => {
305+
runDayEvents(); // runs once for each master
306+
};
307+
308+
// Button for next days
309+
document.getElementById("next-day").addEventListener("click", () => {
310+
runDayEvents(); // runs once for each alive master
311+
});

simulation.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ <h2 id="day-counter">Day 1</h2>
2121

2222
<button class="button" id="next-day">Proceed to Next Day</button>
2323
</main>
24+
<script type="module" src="events.js"></script>
2425
<!-- The script to collect participants follows -->
2526
<script>
2627
function loadParticipants() {
@@ -75,6 +76,52 @@ <h2 id="day-counter">Day 1</h2>
7576
console.warn("No valid participants loaded.");
7677
}
7778
});
79+
80+
//WE'RE DONE WITH THE ABOVE CODE. Now the event simulation code follows
81+
82+
import { eventPool } from './events.js'; // Adjust the path if needed
83+
function runRandomEvent() {
84+
// Ensure there are alive participants before running events
85+
const aliveMasters = participants.filter(p => p.type === "master" && p.status === "alive");
86+
const aliveServants = participants.filter(p => p.type === "servant" && p.status === "alive");
87+
88+
if (aliveMasters.length === 0) return; // No more masters, game over
89+
if (aliveServants.length === 0) return; // No more servants, game over
90+
91+
// Randomly pick an event from the event pool
92+
const event = eventPool[Math.floor(Math.random() * eventPool.length)];
93+
94+
// Select random participants for the event
95+
let selected = [];
96+
for (const targetType of event.targets) {
97+
const pool = participants.filter(p => p.type === targetType && p.status === "alive");
98+
if (pool.length === 0) return; // Skip if no eligible targets
99+
const pick = pool[Math.floor(Math.random() * pool.length)];
100+
selected.push(pick);
101+
}
102+
103+
// Apply the event's effect
104+
event.effect(participants, ...selected);
105+
106+
// Log the event description
107+
const description = event.description
108+
.replace("{master}", selected[0]?.name || "Unknown")
109+
.replace("{servant1}", selected[0]?.name || "Unknown")
110+
.replace("{servant2}", selected[1]?.name || "Unknown");
111+
112+
console.log("Event:", description);
113+
// Optionally log the event to a history
114+
logEvent(description);
115+
116+
//THE EVENT HISTORY FOR DEBUGGING
117+
let eventHistory = [];
118+
function logEvent(description) {
119+
eventHistory.push({ description, timestamp: new Date() });
120+
console.log("Logged Event:", description);
121+
}
122+
123+
}
124+
78125
</script>
79126
</body>
80127
</html>

0 commit comments

Comments
 (0)