@@ -209,7 +209,7 @@ function addServerDropdown(serverName, inactive) {
209209 if ( inactive == true ) {
210210 dropdown . toggleClass ( "inactiveServer" ) ;
211211 }
212- updateServerInfoMCSpecialization ( ) ;
212+ updateServerInfoSpecializations ( ) ;
213213}
214214
215215function addDropdownNoDupe ( name , inactive ) {
@@ -233,35 +233,59 @@ function checkAllServers() {
233233 var servers = $ ( ".CentralMenuDropdown" ) . toArray ( ) ;
234234 //finish this, make sure serverInfo
235235}
236- function updateServerInfoMCSpecialization ( ) {
236+ /**
237+ * Specialization base class.
238+ * Extend this for each server specialization.
239+ */
240+ class ServerSpecialization {
241+ updateUI ( dropdownElement , server ) {
242+ // Default: do nothing
243+ }
244+ }
245+
246+ /**
247+ * Minecraft specialization class.
248+ */
249+ class MinecraftSpecialization extends ServerSpecialization {
250+ updateUI ( dropdownElement , server ) {
251+ const serverNameElem = dropdownElement . querySelector ( ".serverName" ) ;
252+ if ( serverNameElem ) {
253+ if ( server . active ) {
254+ let playerCount = server . specialized_info ?. player_count ?? 0 ;
255+ let maxPlayers = server . specialized_info ?. max_players ?? 0 ;
256+ let isReady = server . specialized_info ?. ready ?? false ;
257+ let statusText = isReady ? "ready to join" : "starting" ;
258+ serverNameElem . textContent = `${ server . name } (${ playerCount } / ${ maxPlayers } ) Status: ${ statusText } ` ;
259+ } else {
260+ serverNameElem . textContent = `${ server . name } (inactive)` ;
261+ }
262+ }
263+ }
264+ }
265+
266+ /**
267+ * Specialization registry.
268+ * Maps config string to specialization class instance.
269+ */
270+ const specializationRegistry = {
271+ Minecraft : new MinecraftSpecialization ( ) ,
272+ // Add more: Rust: new RustSpecialization(), etc.
273+ } ;
274+
275+ /**
276+ * Generic update function for all specializations.
277+ * Calls the appropriate specialization's updateUI for each server.
278+ */
279+ function updateServerInfoSpecializations ( ) {
237280 try {
238281 const serverInfo = window . serverInfoObj ;
282+ if ( ! serverInfo || ! serverInfo . servers ) return ;
239283 serverInfo . servers . forEach ( ( server ) => {
240- if ( server . specialization === "Minecraft" ) {
241- // Check if the specialization is Minecraft
242- const serverElement = $ ( `.${ server . name } dropdown` ) . find (
243- ".serverName" ,
244- ) [ 0 ] ;
245- if ( serverElement ) {
246- if ( server . active ) {
247- // Expect specialized_info to be an object with player_count, max_players, ready
248- let playerCount = 0 ;
249- let maxPlayers = 0 ;
250- let isReady = false ;
251- if (
252- server . specialized_info &&
253- typeof server . specialized_info === "object"
254- ) {
255- playerCount = server . specialized_info . player_count ?? 0 ;
256- maxPlayers = server . specialized_info . max_players ?? 0 ;
257- isReady = server . specialized_info . ready ?? false ;
258- }
259- let statusText = isReady ? "ready to join" : "starting" ;
260- serverElement . textContent = `${ server . name } (${ playerCount } / ${ maxPlayers } ) Status: ${ statusText } ` ;
261- } else {
262- serverElement . textContent = `${ server . name } (inactive)` ;
263- }
264- }
284+ const dropdownElement = document . querySelector ( `.${ server . name } dropdown` ) ;
285+ if ( ! dropdownElement ) return ;
286+ const specialization = specializationRegistry [ server . specialization ] ;
287+ if ( specialization ) {
288+ specialization . updateUI ( dropdownElement , server ) ;
265289 }
266290 } ) ;
267291 } catch ( e ) { }
@@ -296,7 +320,7 @@ $(document).ready(function () {
296320 hotReloadWhenReady ( ) ;
297321 } ;
298322 // Set interval to update server info every quarter second
299- setInterval ( updateServerInfoMCSpecialization , 250 ) ;
323+ setInterval ( updateServerInfoSpecializations , 250 ) ;
300324
301325 socket . onclose = function ( ) {
302326 hotReloadWhenReady ( ) ;
0 commit comments