DarkflameServer/dMasterServer/ObjectIDManager.cpp

72 lines
1.8 KiB
C++
Raw Permalink Normal View History

#include "ObjectIDManager.h"
// Custom Classes
#include "Database.h"
#include "dLogger.h"
// Static Variables
2022-07-28 13:39:57 +00:00
ObjectIDManager* ObjectIDManager::m_Address = nullptr;
//! Initializes the manager
2022-07-28 13:39:57 +00:00
void ObjectIDManager::Initialize(dLogger* logger) {
this->mLogger = logger;
this->currentPersistentID = 1;
2022-07-28 13:39:57 +00:00
try {
sql::PreparedStatement* stmt = Database::CreatePreppedStmt(
"SELECT last_object_id FROM object_id_tracker");
2022-07-28 13:39:57 +00:00
sql::ResultSet* result = stmt->executeQuery();
auto next = result->next();
2022-07-28 13:39:57 +00:00
if (!next) {
sql::PreparedStatement* insertStmt = Database::CreatePreppedStmt(
"INSERT INTO object_id_tracker VALUES (1)");
2022-07-28 13:39:57 +00:00
insertStmt->execute();
2022-07-28 13:39:57 +00:00
delete insertStmt;
2022-07-28 13:39:57 +00:00
return;
}
2022-07-28 13:39:57 +00:00
while (next) {
this->currentPersistentID =
result->getInt(1) > 0 ? result->getInt(1) : 1;
next = result->next();
}
2022-07-28 13:39:57 +00:00
delete result;
delete stmt;
} catch (sql::SQLException& e) {
mLogger->Log("ObjectIDManager", "Unable to fetch max persistent object "
"ID in use. Defaulting to 1.");
mLogger->Log("ObjectIDManager", "SQL error: %s", e.what());
this->currentPersistentID = 1;
}
}
//! Generates a new persistent ID
uint32_t ObjectIDManager::GeneratePersistentID(void) {
2022-07-28 13:39:57 +00:00
uint32_t toReturn = ++this->currentPersistentID;
2022-07-28 13:39:57 +00:00
// So we peroidically save our ObjID to the database:
// if (toReturn % 25 == 0) { // TEMP: DISABLED FOR DEBUG / DEVELOPMENT!
2022-07-28 13:39:57 +00:00
sql::PreparedStatement* stmt = Database::CreatePreppedStmt(
"UPDATE object_id_tracker SET last_object_id=?");
stmt->setUInt(1, toReturn);
stmt->execute();
delete stmt;
// }
2022-07-28 13:39:57 +00:00
return toReturn;
}
void ObjectIDManager::SaveToDatabase() {
2022-07-28 13:39:57 +00:00
sql::PreparedStatement* stmt = Database::CreatePreppedStmt(
"UPDATE object_id_tracker SET last_object_id=?");
stmt->setUInt(1, currentPersistentID);
stmt->execute();
delete stmt;
}