-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
282 lines (231 loc) · 10.2 KB
/
script.js
File metadata and controls
282 lines (231 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
let participants = [];
import { events } from './events.js';
console.log("Checking if .master elements exist...");
console.log(document.querySelectorAll(".master"));
document.addEventListener("DOMContentLoaded", () => {
console.log("Checking all master-picture inputs...");
console.log(document.querySelectorAll(".master-picture"));
console.log("Checking all master image elements...");
console.log(document.querySelectorAll(".master-img"));
document.querySelectorAll(".master").forEach((masterContainer, index) => {
const inputField = masterContainer.querySelector(".master-picture");
const imageElement = masterContainer.querySelector(".master-img");
console.log(`Initializing Master ${index + 1}`);
if (!inputField || !imageElement) {
console.error(`Master container ${index + 1} is missing required elements.`);
return;
}
inputField.addEventListener("input", () => {
const imageUrl = inputField.value.trim();
console.log(`Master ${index + 1} input detected: ${imageUrl}`);
if (imageUrl.startsWith("http://") || imageUrl.startsWith("https://")) {
imageElement.src = imageUrl;
console.log(`Master ${index + 1} image updated to: ${imageUrl}`);
} else {
console.warn(`Master ${index + 1} invalid URL: ${imageUrl}`);
}
});
});
}); //this was the code for updating the master pictures
document.addEventListener("DOMContentLoaded", () => {
if (typeof runDayEvents === "function") {
runDayEvents();
} else {
console.error("runDayEvents() is not defined yet.");
}
});
// now the code for the servant data:
async function fetchServantData() {
const url = 'https://api.atlasacademy.io/export/NA/basic_servant.json';
try {
console.log("Fetching Servant List...");
const response = await fetch(url);
const servantsList = await response.json();
if (!Array.isArray(servantsList)) {
console.error("Invalid response: Expected an array but got", servantsList);
return;
}
console.log("Servants List Fetched:", servantsList);
// Prepare an array to store only necessary data
const servantDataArray = servantsList.map(servant => ({
id: servant.id,
name: servant.name,
image: servant.face, // This contains their default portrait
hasIndependentAction: servant.hasIndependentAction || false
}));
console.log("Final Servant Data Array:", servantDataArray);
populateServantDropdown(servantDataArray);
} catch (error) {
console.error("Error fetching Servant data:", error);
}
}
// Example function to populate a dropdown with Servant names
function populateServantDropdown(servantDataArray) {
// Select ALL servant dropdowns using the class
document.querySelectorAll(".servant-select").forEach((dropdown) => {
// Clear existing options except the default "Choose a Servant"
dropdown.innerHTML = '<option value="">Choose a Servant</option>';
// Add each Servant to the dropdown
servantDataArray.forEach(servant => {
const option = document.createElement("option");
option.textContent = servant.name; // full display name for user
option.dataset.id = servant.id; // store numeric ID if needed later
option.dataset.image = servant.image; // get the servantimage
dropdown.appendChild(option); // <-- fix here
});
});
}
// Call the function to fetch and process Servant data
fetchServantData();
// now the code for the servant randomizing
document.addEventListener("DOMContentLoaded", function () {
const servantDropdowns = document.querySelectorAll(".servant-select");
const randomizeButtons = document.querySelectorAll(".randomize-servant");
let servantList = [];
async function fetchServantList() {
const url = "https://api.atlasacademy.io/export/NA/basic_servant.json";
try {
const response = await fetch(url);
const servants = await response.json();
servantList = servants.map(servant => ({
id: servant.id,
name: servant.name,
image: servant.image
}));
} catch (error) {
console.error("Failed to fetch Servants:", error);
servantList = [];
}
}
function findMatchingDropdownOption(dropdown, servantName) {
for (let option of dropdown.options) {
// Use trim() to remove any leading/trailing spaces
if (option.textContent.trim() === servantName.trim()) {
return option.value; // Return the ID of the matched servant
}
}
return ""; // Return empty if no match is found
}
function randomizeServant(index) {
if (servantList.length === 0) return;
const randomServant = servantList[Math.floor(Math.random() * servantList.length)];
const dropdown = servantDropdowns[index];
if (dropdown) {
const matchedValue = findMatchingDropdownOption(dropdown, randomServant.name);
if (matchedValue) {
dropdown.value = matchedValue; // Set the dropdown value to the matched servant ID
dropdown.selectedIndex = [...dropdown.options].findIndex(opt => opt.value === matchedValue);
} else {
console.warn(`Servant with name ${randomServant.name} not found in dropdown options.`);
}
}
}
fetchServantList().then(() => {
randomizeButtons.forEach((button, index) => {
button.addEventListener("click", function () {
randomizeServant(index);
});
});
});
});
//simulation button function:
function saveParticipantsAndStartSimulation() {
let teamContainers = document.querySelectorAll(".form-container");
participants = [];
teamContainers.forEach((teamContainer, index) => {
let masterContainer = teamContainer.querySelector(".master");
let servantContainer = teamContainer.querySelector(".servant-selection");
let nameInput = masterContainer.querySelector(".master-name");
let genderSelect = masterContainer.querySelector(".gender-select");
let pictureEl = masterContainer.querySelector(".master-img");
let name = nameInput ? nameInput.value.trim() || `Master ${index + 1}` : `Master ${index + 1}`;
let gender = genderSelect ? genderSelect.value : "nonbinary"; // fallback to nonbinary if missing
let pronouns = getPronouns(gender);
let servantHasIndependentAction = false; // default unless set elsewhere
let pictureUrl = pictureEl ? pictureEl.src : "";
let servantDropdown = servantContainer ? servantContainer.querySelector(".servant-select") : null;
let servantId = "Unknown";
let servantName = "Unknown";
let servantImage;
if (servantDropdown) {
const selectedIndex = servantDropdown.selectedIndex;
const selectedOption = servantDropdown.options[selectedIndex];
if (selectedOption) {
servantId = selectedOption.dataset.id || "Unknown"; // numeric ID if needed
servantName = selectedOption.value || "Unknown"; // shortname the simulator expects
console.log(`Team ${index + 1} - Selected Servant: id=${servantId}, name=${servantName}`);
} else {
console.warn(`Team ${index + 1} - No selected option in servant dropdown`);
}
} else {
console.warn(`Team ${index + 1} - No servant dropdown found`);
}
let masterData = {
id: `master${index + 1}`,
name: name,
gender: gender,
pronouns: pronouns,
picture: pictureUrl,
status: "alive",
type: "master",
servantId: servantId,
};
let servantData = {
id: servantId,
name: servantName,
type: "servant",
status: "alive",
masterId: masterData.id,
hasIndependentAction: servantHasIndependentAction,
image: servantImage //don't gd touch this if servant images work
};
participants.push(masterData);
participants.push(servantData);
});
console.log("Saved participants:", participants);
localStorage.setItem("participants", JSON.stringify(participants));
window.location.href = "simulation.html";
}
function getPronouns(gender) {
if (gender === "male") {
return { subject: "he", object: "him", possessive: "his" };
} else if (gender === "female") {
return { subject: "she", object: "her", possessive: "her" };
} else {
return { subject: "they", object: "them", possessive: "their" };
}
}
//code to attach the above function to the button:
document.addEventListener("DOMContentLoaded", function () {
const startButton = document.getElementById("simulation");
if (startButton) {
startButton.addEventListener("click", saveParticipantsAndStartSimulation);
} else {
console.error("Start Simulation button not found!");
}
});
// ----------------------------
// Run Daily Events
// ----------------------------
function runDayEvents() {
console.log("===== DAY 1 =====");
if (!participants || participants.length === 0) {
participants = JSON.parse(localStorage.getItem("participants")) || [];
}
const aliveMasters = participants.filter(p => p.type === "master" && p.status === "alive");
if (aliveMasters.length === 0) {
console.log("No masters remain alive. Simulation ends.");
return;
}
// Example Day 1 event
const randomEvent = events[Math.floor(Math.random() * events.length)];
const targetMaster = aliveMasters[Math.floor(Math.random() * aliveMasters.length)];
if (randomEvent && targetMaster) {
const description = randomEvent.description.replace("{master}", targetMaster.name);
console.log(description);
// Apply event effect if defined
if (typeof randomEvent.effects === "function") {
randomEvent.effects(participants, targetMaster);
}
}
}