From 35f010e51601a6aa42110036b323ba82546faefb Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 2 Oct 2016 23:44:41 +0200 Subject: [PATCH] refs #743, utility function for cross imulator updating --- src/blackcore/db/databaseutils.cpp | 56 ++++++++++++++++++++++++++---- src/blackcore/db/databaseutils.h | 4 +++ 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/blackcore/db/databaseutils.cpp b/src/blackcore/db/databaseutils.cpp index 5e4ba1d3c..fc7fe418e 100644 --- a/src/blackcore/db/databaseutils.cpp +++ b/src/blackcore/db/databaseutils.cpp @@ -41,8 +41,8 @@ namespace BlackCore return dbModel; } - // we try to best update by DB data here - // we have no(!) DB model, so we update each of it subobjects + // we try our best to update by DB data here + // since we have no(!) DB model, we update each of it subobjects CAircraftModel consolidatedModel(model); // copy over if (!consolidatedModel.getLivery().hasValidDbKey()) { @@ -94,10 +94,7 @@ namespace BlackCore if (modified || model.hasValidDbKey()) { c++; - if (processEvents && c % 125 == 0) - { - sApp->processEventsFor(25); - } + if (processEvents && c % 125 == 0) { sApp->processEventsFor(25); } } } CLogMessage().debug() << "Consolidated " << models.size() << " in " << timer.elapsed() << "ms"; @@ -149,5 +146,52 @@ namespace BlackCore } return c; } + + CAircraftModelList CDatabaseUtils::updateSimulatorForFsFamily(const CAircraftModelList &ownModels, int maxToStash, IProgressIndicator *progressIndicator, bool processEvents) + { + CAircraftModelList dbFsFamilyModels(sApp->getWebDataServices()->getModels().getAllFsFamilyModels()); + CAircraftModelList stashModels; + if (dbFsFamilyModels.isEmpty() || ownModels.isEmpty()) { return stashModels; } + const QSet dbKeys = dbFsFamilyModels.getModelStringSet(); + const int mexModelsCount = maxToStash >= 0 ? maxToStash : ownModels.size(); + if (mexModelsCount < 1) { return stashModels; } + + int c = 0; // counter + for (const CAircraftModel &ownModel : ownModels) + { + c++; + + // process events + if (processEvents && c % 500 == 0) + { + if (progressIndicator) + { + const int percentage = c * 100 / mexModelsCount; + progressIndicator->updateProgressIndicatorAndProcessEvents(percentage); + } + else + { + sApp->processEventsFor(10); + } + } + + // values to be skipped + if (maxToStash >= 0 && maxToStash == stashModels.size()) { break; } + if (!dbKeys.contains(ownModel.getModelString())) { continue; } + if (ownModel.matchesSimulatorFlag(CSimulatorInfo::XPLANE)) { continue; } + + // in DB + CAircraftModel dbModel = dbFsFamilyModels.findFirstByModelStringOrDefault(ownModel.getModelString()); + if (!dbModel.isLoadedFromDb()) {continue; } + if (dbModel.getSimulator() == ownModel.getSimulator()) {continue; } + + // update simulator and add + CSimulatorInfo simulator(dbModel.getSimulator()); + simulator.add(ownModel.getSimulator()); + dbModel.setSimulator(simulator); + stashModels.push_back(dbModel); + } + return stashModels; + } } // ns } // ns diff --git a/src/blackcore/db/databaseutils.h b/src/blackcore/db/databaseutils.h index 2cf465487..c0fd59cfc 100644 --- a/src/blackcore/db/databaseutils.h +++ b/src/blackcore/db/databaseutils.h @@ -13,6 +13,7 @@ #define BLACKCORE_DB_DATABASEUTILS_H #include "blackcore/blackcoreexport.h" +#include "blackcore/progress.h" #include "blackmisc/simulation/aircraftmodel.h" namespace BlackCore @@ -46,6 +47,9 @@ namespace BlackCore //! Consolidate model data with DB distributors static int consolidateModelsWithDbDistributor(BlackMisc::Simulation::CAircraftModelList &models, bool force); + + //! Create stash models if the DB models miss that simulator + static BlackMisc::Simulation::CAircraftModelList updateSimulatorForFsFamily(const BlackMisc::Simulation::CAircraftModelList &ownModels, int maxToStash = -1, BlackCore::IProgressIndicator *progressIndicator = nullptr, bool processEvents = true); }; } // ns } // ns