Beispiel #1
0
        public void SaveStarSystems(List <StarSystem> starSystems)
        {
            if (!starSystems.Any())
            {
                return;
            }

            // Update any star systems in our short term star system cache to minimize repeat deserialization
            foreach (var starSystem in starSystems)
            {
                starSystemCache.Remove(starSystem.systemname);
                starSystemCache.Add(starSystem);
            }

            // Determine whether we need to delete, insert, or update each system
            var delete = new List <StarSystem>();
            var update = new List <StarSystem>();
            var insert = new List <StarSystem>();

            var dbSystems = Instance.ReadStarSystems(starSystems);

            foreach (StarSystem system in starSystems)
            {
                DatabaseStarSystem dbSystem = dbSystems.FirstOrDefault(s =>
                                                                       s.systemAddress != null && s.systemAddress == system.systemAddress ||
                                                                       s.edsmId != null && s.edsmId == system.EDSMID ||
                                                                       s.systemName == system.systemname);

                if (dbSystem?.systemJson is null ||
                    dbSystem?.systemAddress is null && dbSystem?.edsmId is null)
                {
                    // If we're updating to schema version 2, systemAddress and edsmId will both be null.
                    // Use our delete method to purge all obsolete copies of the star system from the database,
                    // then re-add the star system.
                    delete.Add(system);
                    insert.Add(system);
                }
Beispiel #2
0
        public List <StarSystem> GetStarSystems(string[] names, bool refreshIfOutdated = true)
        {
            List <StarSystem> results = new List <StarSystem>();

            if (!File.Exists(DbFile))
            {
                return(results);
            }
            if (!names.Any())
            {
                return(results);
            }

            List <DatabaseStarSystem> systemsToUpdate = new List <DatabaseStarSystem>();
            List <DatabaseStarSystem> dataSets        = Instance.ReadStarSystems(names);

            bool needToUpdate = false;

            for (int i = 0; i < dataSets.Count; i++)
            {
                DatabaseStarSystem dbStarSystem = dataSets[i];
                if (dbStarSystem.systemJson != null)
                {
                    // Old versions of the data could have a string "No volcanism" for volcanism.  If so we remove it
                    dbStarSystem.systemJson = dbStarSystem.systemJson?.Replace(@"""No volcanism""", "null");

                    // Old versions of the data could have a string "InterstellarFactorsContact" for the facilitator station service.  If so we update it
                    dbStarSystem.systemJson = dbStarSystem.systemJson?.Replace(@"""InterstellarFactorsContact""", @"""Facilitator""");

                    if (refreshIfOutdated)
                    {
                        if (dbStarSystem.lastUpdated < DateTime.UtcNow.AddHours(-1))
                        {
                            // Data is stale or we have no record of ever updating this star system
                            needToUpdate = true;
                        }
                        else if (SCHEMA_VERSION >= 2 && (dbStarSystem.systemAddress is null || dbStarSystem.edsmId is null))
                        {
                            // Obtain data for optimized data searches starting with schema version 2
                            needToUpdate = true;
                        }
                    }

                    if (needToUpdate)
                    {
                        // We want to update this star system (don't deserialize the old result at this time)
                        systemsToUpdate.Add(dbStarSystem);
                    }
                    else
                    {
                        // Deserialize the old result
                        StarSystem result = DeserializeStarSystem(dbStarSystem.systemName, dbStarSystem.systemJson, ref needToUpdate);
                        if (result != null)
                        {
                            results.Add(result);
                        }
                        else
                        {
                            // Something went wrong... retrieve new data.
                            systemsToUpdate.Add(dbStarSystem);
                        }
                    }
                }
            }

            if (systemsToUpdate.Count > 0)
            {
                List <StarSystem> updatedSystems = dataProviderService.GetSystemsData(systemsToUpdate.Select(s => s.systemName).ToArray());
                if (updatedSystems == null)
                {
                    return(results);
                }

                // If the newly fetched star system is an empty object except (for the object name), reject it
                // Return old results when new results have been rejected
                List <string> systemsToRevert = new List <string>();
                foreach (StarSystem starSystem in updatedSystems)
                {
                    if (starSystem.systemAddress == null)
                    {
                        systemsToRevert.Add(starSystem.systemname);
                    }
                }
                updatedSystems.RemoveAll(s => systemsToRevert.Contains(s.systemname));
                foreach (string systemName in systemsToRevert)
                {
                    results.Add(GetStarSystem(systemName, false));
                }

                // Synchronize EDSM visits and comments
                updatedSystems = dataProviderService.syncFromStarMapService(updatedSystems);

                // Update properties that aren't synced from the server and that we want to preserve
                updatedSystems = PreserveUnsyncedProperties(updatedSystems, systemsToUpdate);

                // Update the `lastupdated` timestamps for the systems we have updated
                foreach (StarSystem starSystem in updatedSystems)
                {
                    starSystem.lastupdated = DateTime.UtcNow;
                }

                // Add our updated systems to our results
                results.AddRange(updatedSystems);

                // Save changes to our star systems
                Instance.updateStarSystems(updatedSystems);
            }
            return(results);
        }