@@ -118,28 +118,28 @@ document.addEventListener("DOMContentLoaded", function () {
118118 }
119119
120120
121- function randomizeServant ( index ) {
122- if ( servantList . length === 0 ) return ;
123-
124- const randomServant = servantList [ Math . floor ( Math . random ( ) * servantList . length ) ] ;
125- const dropdown = servantDropdowns [ index ] ;
126-
127- if ( dropdown ) {
128- const matchedValue = findMatchingDropdownOption ( dropdown , randomServant . name ) ;
129- if ( matchedValue ) {
130- dropdown . value = matchedValue ; // Set the dropdown value to the matched servant ID
131- dropdown . selectedIndex = [ ...dropdown . options ] . findIndex ( opt => opt . value === matchedValue ) ;
132- } else {
133- console . warn ( `Servant with name ${ randomServant . name } not found in dropdown options.` ) ;
134- }
135- }
136-
137- const servantImage = document . querySelectorAll ( ".servant-img" ) [ index ] ;
138- if ( servantImage ) {
139- servantImage . src = randomServant . image ; // Update the image source
121+ function randomizeServant ( index ) {
122+ if ( servantList . length === 0 ) return ;
123+
124+ const randomServant = servantList [ Math . floor ( Math . random ( ) * servantList . length ) ] ;
125+ const dropdown = servantDropdowns [ index ] ;
126+
127+ if ( dropdown ) {
128+ const matchedValue = findMatchingDropdownOption ( dropdown , randomServant . name ) ;
129+ if ( matchedValue ) {
130+ dropdown . value = matchedValue ; // Set the dropdown value to the matched servant ID
131+ dropdown . selectedIndex = [ ...dropdown . options ] . findIndex ( opt => opt . value === matchedValue ) ;
132+ } else {
133+ console . warn ( `Servant with name ${ randomServant . name } not found in dropdown options.` ) ;
140134 }
141135 }
142136
137+ const servantImage = document . querySelectorAll ( ".servant-img" ) [ index ] ;
138+ if ( servantImage ) {
139+ servantImage . src = randomServant . image ; // Update the image source
140+ }
141+ }
142+
143143
144144 fetchServantList ( ) . then ( ( ) => {
145145 randomizeButtons . forEach ( ( button , index ) => {
@@ -158,35 +158,38 @@ function saveParticipantsAndStartSimulation() {
158158
159159 masterContainers . forEach ( ( masterContainer , index ) => {
160160 let nameInput = masterContainer . querySelector ( ".master-name" ) ;
161- console . log ( masterContainer . innerHTML ) ; // Log the contents of the master container
162- // Updated to find the servant dropdown inside the servant-selection div
163- let servantDropdown = masterContainer . querySelector ( ".servant-selection .servant-select" ) ;
161+ console . log ( masterContainer . innerHTML ) ;
162+
163+ // LOOK FOR THE NEXT SIBLING .servant-selection
164+ let servantSelection = masterContainer . nextElementSibling . querySelector ( ".servant-selection" ) ;
165+ let servantDropdown = servantSelection ? servantSelection . querySelector ( ".servant-select" ) : null ;
166+
164167 let pictureEl = masterContainer . querySelector ( ".master-img" ) ;
165-
168+
166169 let name = nameInput ? nameInput . value || `Master ${ index + 1 } ` : `Master ${ index + 1 } ` ;
167170 let pictureUrl = pictureEl ? pictureEl . src : "" ;
168-
171+
169172 let servantId = "Unknown" ;
170173 let servantName = "Unknown" ;
171-
174+
172175 if ( servantDropdown ) {
173176 const selectedIndex = servantDropdown . selectedIndex ;
174177 const selectedOption = servantDropdown . options [ selectedIndex ] ;
175-
178+
176179 if ( selectedOption ) {
177180 servantId = selectedOption . value || "Unknown" ;
178181 servantName = selectedOption . dataset && selectedOption . dataset . name
179182 ? selectedOption . dataset . name
180183 : selectedOption . textContent || "Unknown" ;
181-
184+
182185 console . log ( `Selected Servant (from option): id=${ servantId } , name=${ servantName } ` ) ;
183186 } else {
184187 console . warn ( `No selected option found in dropdown index ${ index } ` ) ;
185188 }
186189 } else {
187190 console . warn ( `No servant dropdown found for master container index ${ index } ` ) ;
188191 }
189-
192+
190193 let masterData = {
191194 name : name ,
192195 picture : pictureUrl ,
@@ -195,9 +198,10 @@ function saveParticipantsAndStartSimulation() {
195198 servantId : servantId ,
196199 servantName : servantName
197200 } ;
198-
201+
199202 participants . push ( masterData ) ;
200203 } ) ;
204+
201205
202206 console . log ( "Saved participants:" , participants ) ;
203207 localStorage . setItem ( "participants" , JSON . stringify ( participants ) ) ;
@@ -206,7 +210,6 @@ function saveParticipantsAndStartSimulation() {
206210}
207211
208212
209-
210213//code to attach the above function to the button:
211214document . addEventListener ( "DOMContentLoaded" , function ( ) {
212215 const startButton = document . getElementById ( "simulation" ) ;
0 commit comments