@@ -81,7 +81,59 @@ function populateServantDropdown(servantDataArray) {
8181}
8282// Call the function to fetch and process Servant data
8383fetchServantData ( ) ;
84- //now the code for the servant randomizing
84+ // now the code for the servant data:
85+ async function fetchServantData ( ) {
86+ const url = 'https://api.atlasacademy.io/export/NA/basic_servant.json' ;
87+
88+ try {
89+ console . log ( "Fetching Servant List..." ) ;
90+ const response = await fetch ( url ) ;
91+ const servantsList = await response . json ( ) ;
92+
93+ if ( ! Array . isArray ( servantsList ) ) {
94+ console . error ( "Invalid response: Expected an array but got" , servantsList ) ;
95+ return ;
96+ }
97+
98+ console . log ( "Servants List Fetched:" , servantsList ) ;
99+
100+ // Prepare an array to store only necessary data
101+ const servantDataArray = servantsList . map ( servant => ( {
102+ id : servant . id ,
103+ name : servant . name ,
104+ image : servant . face , // This contains their default portrait
105+ hasIndependentAction : false // Placeholder, since this isn't in basic_servant.json
106+ } ) ) ;
107+
108+ console . log ( "Final Servant Data Array:" , servantDataArray ) ;
109+ populateServantDropdown ( servantDataArray ) ;
110+ } catch ( error ) {
111+ console . error ( "Error fetching Servant data:" , error ) ;
112+ }
113+ }
114+
115+ // Example function to populate a dropdown with Servant names
116+ function populateServantDropdown ( servantDataArray ) {
117+ // Select ALL servant dropdowns using the class
118+ document . querySelectorAll ( ".servant-select" ) . forEach ( ( dropdown ) => {
119+ // Clear existing options except the default "Choose a Servant"
120+ dropdown . innerHTML = '<option value="">Choose a Servant</option>' ;
121+
122+ // Add each Servant to the dropdown
123+ servantDataArray . forEach ( servant => {
124+ const option = document . createElement ( "option" ) ;
125+ option . value = servant . id ; // Use Servant ID
126+ option . textContent = servant . name ;
127+ option . dataset . name = servant . name ;
128+ dropdown . appendChild ( option ) ; // <-- fixed here
129+ } ) ;
130+ } ) ;
131+ }
132+
133+ // Call the function to fetch and process Servant data
134+ fetchServantData ( ) ;
135+
136+ // now the code for the servant randomizing
85137document . addEventListener ( "DOMContentLoaded" , function ( ) {
86138 const servantDropdowns = document . querySelectorAll ( ".servant-select" ) ;
87139 const randomizeButtons = document . querySelectorAll ( ".randomize-servant" ) ;
@@ -122,6 +174,14 @@ document.addEventListener("DOMContentLoaded", function () {
122174 const matchedValue = findMatchingDropdownOption ( dropdown , randomServant . name ) ;
123175 if ( matchedValue ) {
124176 dropdown . value = matchedValue ;
177+
178+ // ✅ NEW: also set selectedIndex properly
179+ for ( let i = 0 ; i < dropdown . options . length ; i ++ ) {
180+ if ( dropdown . options [ i ] . value === matchedValue ) {
181+ dropdown . selectedIndex = i ;
182+ break ;
183+ }
184+ }
125185 }
126186 }
127187
0 commit comments