Skip to content

Commit c842e2d

Browse files
committed
overhaul
1 parent 24621d9 commit c842e2d

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

script.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,15 @@ function populateServantDropdown(servantDataArray) {
7373
// Add each Servant to the dropdown
7474
servantDataArray.forEach(servant => {
7575
const option = document.createElement("option");
76-
option.value = servant.id; // Use Servant ID
77-
option.textContent = servant.name;
78-
option.dataset.name = servant.name;
79-
dropdown.appendChild(option); // <-- fixed here
76+
option.value = servant.id; // Use Servant ID
77+
option.textContent = servant.name; // Set name to text content
78+
option.dataset.name = servant.name; // Store name in dataset
79+
dropdown.appendChild(option); // <-- fix here
8080
});
8181
});
8282
}
8383

84+
8485
// Call the function to fetch and process Servant data
8586
fetchServantData();
8687

@@ -108,13 +109,15 @@ document.addEventListener("DOMContentLoaded", function () {
108109

109110
function findMatchingDropdownOption(dropdown, servantName) {
110111
for (let option of dropdown.options) {
111-
if (option.textContent.includes(servantName)) {
112-
return option.value;
112+
// Use trim() to remove any leading/trailing spaces
113+
if (option.textContent.trim() === servantName.trim()) {
114+
return option.value; // Return the ID of the matched servant
113115
}
114116
}
115-
return "";
117+
return ""; // Return empty if no match is found
116118
}
117119

120+
118121
function randomizeServant(index) {
119122
if (servantList.length === 0) return;
120123

@@ -124,18 +127,20 @@ document.addEventListener("DOMContentLoaded", function () {
124127
if (dropdown) {
125128
const matchedValue = findMatchingDropdownOption(dropdown, randomServant.name);
126129
if (matchedValue) {
127-
dropdown.value = matchedValue;
128-
130+
dropdown.value = matchedValue; // Set the dropdown value to the matched servant ID
129131
dropdown.selectedIndex = [...dropdown.options].findIndex(opt => opt.value === matchedValue);
132+
} else {
133+
console.warn(`Servant with name ${randomServant.name} not found in dropdown options.`);
130134
}
131135
}
132136

133137
const servantImage = document.querySelectorAll(".servant-img")[index];
134138
if (servantImage) {
135-
servantImage.src = randomServant.image;
139+
servantImage.src = randomServant.image; // Update the image source
136140
}
137141
}
138142

143+
139144
fetchServantList().then(() => {
140145
randomizeButtons.forEach((button, index) => {
141146
button.addEventListener("click", function () {
@@ -169,9 +174,8 @@ function saveParticipantsAndStartSimulation() {
169174

170175
if (selectedOption) {
171176
servantId = selectedOption.value || "Unknown";
172-
servantName = selectedOption.dataset && selectedOption.dataset.name
173-
? selectedOption.dataset.name
174-
: selectedOption.textContent || "Unknown";
177+
servantName = selectedOption.dataset.name || selectedOption.textContent || "Unknown";
178+
}
175179

176180
console.log(`Selected Servant (from option): id=${servantId}, name=${servantName}`);
177181
} else {

0 commit comments

Comments
 (0)