@@ -17,6 +17,25 @@ const ESTIMATED_TOTAL_ITEMS = 7500;
1717const ITEMS_PER_REQUEST = 500 ;
1818let validItemsProcessed = 0 ;
1919
20+ const loadDefaultData = async ( ) => {
21+ try {
22+ const response = await fetch ( "../public/assets/defaultData/defaultData.json" ) ;
23+ if ( ! response . ok ) throw new Error ( "Could not load defaultData.json" ) ;
24+ const data = await response . json ( ) ;
25+
26+ const existing = getCachedData ( CIVILIZATIONS_CACHE_KEY ) ;
27+ if ( ! existing ) {
28+ console . log ( "Loading default data into the cache..." ) ;
29+ setCachedData ( CIVILIZATIONS_CACHE_KEY , data ) ;
30+ }
31+
32+ return data ;
33+ } catch ( error ) {
34+ console . error ( "Error loading default data:" , error ) ;
35+ return null ;
36+ }
37+ } ;
38+
2039const updateProgressBar = ( current , total , startTime ) => {
2140 const progressContainer = document . getElementById ( 'progressBarContainer' ) ;
2241 const progressFill = document . getElementById ( 'progressBarFill' ) ;
@@ -445,6 +464,7 @@ const displayRandomGlyphs = () => {
445464} ;
446465
447466const coords2Glyphs = ( coordinates ) => {
467+ if ( ! coordinates || typeof coordinates !== 'string' ) return '' ;
448468 const parts = coordinates . split ( ':' ) ;
449469 if ( parts . length !== 4 ) return '' ;
450470 const [ xStr , yStr , zStr ] = parts ;
@@ -497,12 +517,64 @@ const showNotification = (message, type) => {
497517 } , 3000 ) ;
498518} ;
499519
520+ function mergeCivilizationData ( oldData , newData ) {
521+ const merged = JSON . parse ( JSON . stringify ( oldData ) ) ;
522+
523+ for ( const galaxy of newData . galaxies ) {
524+ if ( ! merged . galaxies . includes ( galaxy ) ) {
525+ merged . galaxies . push ( galaxy ) ;
526+ }
527+ }
528+
529+ for ( const [ galaxyName , galaxyInfo ] of Object . entries ( newData . data ) ) {
530+ if ( ! merged . data [ galaxyName ] ) {
531+ merged . data [ galaxyName ] = galaxyInfo ;
532+ continue ;
533+ }
534+
535+ const oldGalaxy = merged . data [ galaxyName ] ;
536+
537+ for ( const civ of galaxyInfo . civilizations ) {
538+ if ( ! oldGalaxy . civilizations . includes ( civ ) ) {
539+ oldGalaxy . civilizations . push ( civ ) ;
540+ }
541+ }
542+
543+ for ( const civ of galaxyInfo . civilizations ) {
544+ if ( ! oldGalaxy . regions [ civ ] ) {
545+ oldGalaxy . regions [ civ ] = galaxyInfo . regions [ civ ] ;
546+ continue ;
547+ }
548+
549+ const oldRegions = oldGalaxy . regions [ civ ] ;
550+ const newRegions = galaxyInfo . regions [ civ ] ;
551+
552+ for ( const r of newRegions ) {
553+ if ( ! oldRegions . some ( or => or . name === r . name ) ) {
554+ oldRegions . push ( r ) ;
555+ }
556+ }
557+ }
558+ }
559+
560+ return merged ;
561+ }
562+
500563const forceRefreshCivilizations = async ( ) => {
501- resetOffsetCache ( ) ;
502- clearPartialCache ( ) ;
503- localStorage . removeItem ( CIVILIZATIONS_CACHE_KEY ) ;
504- console . log ( 'Cache cleared, forcing full refresh' ) ;
505- return await fetchAllCivilizationsAndRegions ( ) ;
564+ console . log ( "🔄 Forzando actualización y merge de datos…" ) ;
565+ showNotification ( i18next . t ( "updatingInfo" ) , "info" ) ;
566+
567+ const defaultData = await loadDefaultData ( ) ;
568+
569+ const freshData = await fetchAllCivilizationsAndRegions ( true ) ;
570+ const merged = mergeCivilizationData ( defaultData , freshData ) ;
571+
572+ setCachedData ( CIVILIZATIONS_CACHE_KEY , merged ) ;
573+
574+ console . log ( "✅ Merge completado. JSON actualizado sin borrar datos." ) ;
575+ showNotification ( i18next . t ( "updatingFinish" ) , "success" ) ;
576+
577+ return merged ;
506578} ;
507579
508580window . forceRefreshCivilizations = forceRefreshCivilizations ;
@@ -685,7 +757,13 @@ const onRegionChange = (select) => {
685757} ;
686758
687759const initializeRegionSelection = async ( ) => {
760+ await loadDefaultData ( ) ;
761+
688762 await populateGalaxySelect ( ) ;
763+
764+ fetchAllCivilizationsAndRegions ( )
765+ . then ( ( ) => console . log ( "Data updated in the background" ) )
766+ . catch ( err => console . error ( "Error refreshing data:" , err ) ) ;
689767} ;
690768
691769window . displayRandomGlyphs = displayRandomGlyphs ;
@@ -754,7 +832,9 @@ i18next.init(
754832 loadingData : "Loading data..." ,
755833 estimatedTime : "~{{minutes}}m {{seconds}}s remaining" ,
756834 estimatedTimeSeconds : "~{{seconds}}s remaining" ,
757- complete : "Complete!"
835+ complete : "Complete!" ,
836+ updatingFinish : "The regions update is complete." ,
837+ updatingInfo : "The region update may take more than 5 minutes, please be patient."
758838 } ,
759839 } ,
760840 es : {
@@ -786,7 +866,9 @@ i18next.init(
786866 loadingData : "Cargando datos..." ,
787867 estimatedTime : "~{{minutes}}m {{seconds}}s restantes" ,
788868 estimatedTimeSeconds : "~{{seconds}}s restantes" ,
789- complete : "¡Completado!"
869+ complete : "¡Completado!" ,
870+ updatingFinish : "Se ha terminado de actualizar las regiones." ,
871+ updatingInfo : "La actualización de regiones puede tardar más de 5 minutos, sea paciente por favor."
790872 } ,
791873 } ,
792874 fr : {
@@ -814,7 +896,9 @@ i18next.init(
814896 loadingData : "Chargement des données..." ,
815897 estimatedTime : "~{{minutes}}m {{seconds}}s restantes" ,
816898 estimatedTimeSeconds : "~{{seconds}}s restantes" ,
817- complete : "Terminé!"
899+ complete : "Terminé!" ,
900+ updatingFinish : "La mise à jour des régions est terminée." ,
901+ updatingInfo : "La mise à jour de la région peut prendre plus de 5 minutes. Veuillez patienter."
818902 } ,
819903 } ,
820904 de : {
@@ -846,7 +930,9 @@ i18next.init(
846930 loadingData : "Daten werden geladen..." ,
847931 estimatedTime : "~{{minutes}}m {{seconds}}s verbleibend" ,
848932 estimatedTimeSeconds : "~{{seconds}}s verbleibend" ,
849- complete : "Fertig!"
933+ complete : "Fertig!" ,
934+ updatingFinish : "Die Aktualisierung der Regionen ist abgeschlossen." ,
935+ updatingInfo : "Die Aktualisierung der Region kann mehr als 5 Minuten dauern. Bitte haben Sie Geduld."
850936 } ,
851937 } ,
852938 eu : {
@@ -878,7 +964,9 @@ i18next.init(
878964 loadingData : "Datuak kargatzen..." ,
879965 estimatedTime : "~{{minutes}}m {{seconds}}s geratzen" ,
880966 estimatedTimeSeconds : "~{{seconds}}s geratzen" ,
881- complete : "Osatuta!"
967+ complete : "Osatuta!" ,
968+ updatingFinish : "Eskualdeak gaurkotzen amaitu da." ,
969+ updatingInfo : "Eskualdeak eguneratzeko 5 minutu baino gehiago behar dira, pazientzia izan mesedez."
882970 } ,
883971 } ,
884972 } ,
0 commit comments