@@ -158,45 +158,51 @@ function saveParticipantsAndStartSimulation() {
158158 let name = nameInput ? nameInput . value || `Master ${ index + 1 } ` : `Master ${ index + 1 } ` ;
159159 let pictureUrl = masterContainer . querySelector ( ".master-img" ) . src ;
160160
161- // Ensure servantId and servantName are properly initialized and checked
162- let servantId = "Unknown" ; // Default to Unknown
163- let servantName = "Unknown" ; // Default to Unknown
161+ let servantId = "Unknown" ; // Default
162+ let servantName = "Unknown" ; // Default
164163
165- if ( servantDropdown && servantDropdown . selectedIndex >= 0 ) {
166- const selectedOption = servantDropdown . options [ servantDropdown . selectedIndex ] ;
164+ if ( servantDropdown ) {
165+ const selectedIndex = servantDropdown . selectedIndex ;
166+ const selectedOption = servantDropdown . options [ selectedIndex ] ;
167167
168- // Safely get servantId and servantName from the selected option
169- servantId = selectedOption . value || "Unknown" ;
170- servantName = selectedOption . dataset . name || selectedOption . textContent || "Unknown" ;
171- }
168+ if ( selectedOption ) {
169+ servantId = selectedOption . value || "Unknown" ;
172170
173- // Debugging: Log details about the servant
174- console . log ( `Master ${ index + 1 } : ${ name } , Servant ID: ${ servantId } , Servant Name: ${ servantName } ` ) ;
171+ // Safely get dataset name or fallback to text
172+ servantName = selectedOption . dataset && selectedOption . dataset . name
173+ ? selectedOption . dataset . name
174+ : selectedOption . textContent || "Unknown" ;
175175
176- // Create master data object with servant data
177- let servantImageUrl = masterContainer . querySelector ( ".servant-img" ) . src ;
176+ console . log ( `Selected Servant (from option): id=${ servantId } , name=${ servantName } ` ) ;
177+ } else {
178+ // Fallback if no selected option
179+ servantId = servantDropdown . value || "Unknown" ;
180+ servantName = "Unknown (no option selected)" ;
181+ console . warn ( `No selected option found for dropdown index ${ selectedIndex } ` ) ;
182+ }
183+ } else {
184+ console . warn ( "No servant dropdown found for master container." ) ;
185+ }
178186
187+ // Create master data object with servant data
179188 let masterData = {
180189 name : name ,
181190 picture : pictureUrl ,
182191 status : "alive" ,
183192 type : "master" ,
184- servantId : servantId , // Store the servant's ID
185- servantName : servantName , // Store the servant's name
186- servantImage : servantImageUrl // NEW: Servant's image URL
193+ servantId : servantId ,
194+ servantName : servantName
187195 } ;
188196
189197 participants . push ( masterData ) ;
190198 } ) ;
191199
192200 console . log ( "Saved participants:" , participants ) ;
193-
194- // Save participants to localStorage
195201 localStorage . setItem ( "participants" , JSON . stringify ( participants ) ) ;
196202
197- // Redirect to the simulation page
198203 window . location . href = "simulation.html" ;
199204}
205+
200206//code to attach the above function to the button:
201207document . addEventListener ( "DOMContentLoaded" , function ( ) {
202208 const startButton = document . getElementById ( "simulation" ) ;
0 commit comments