Skip to content

Store world save variables at characters db #2825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions sql/migrations/20241108102245_characters.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
DROP PROCEDURE IF EXISTS add_migration;
DELIMITER ??
CREATE PROCEDURE `add_migration`()
BEGIN
DECLARE v INT DEFAULT 1;
SET v = (SELECT COUNT(*) FROM `migrations` WHERE `id`='20241108102245');
IF v = 0 THEN
INSERT INTO `migrations` VALUES ('20241108102245');
-- Add your query below.

DROP TABLE IF EXISTS `world_persistent_variables`;
CREATE TABLE `world_persistent_variables` (
`index` int(10) unsigned NOT NULL DEFAULT 0,
`value` int(10) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`index`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Wareffort, Stranglethorn Vale Fishing , ...';

-- End of migration.
END IF;
END??
DELIMITER ;
CALL add_migration();
DROP PROCEDURE IF EXISTS add_migration;
19 changes: 19 additions & 0 deletions sql/migrations/20241108102245_world.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
DROP PROCEDURE IF EXISTS add_migration;
DELIMITER ??
CREATE PROCEDURE `add_migration`()
BEGIN
DECLARE v INT DEFAULT 1;
SET v = (SELECT COUNT(*) FROM `migrations` WHERE `id`='20241108102245');
IF v = 0 THEN
INSERT INTO `migrations` VALUES ('20241108102245');
-- Add your query below.

-- Moved to characters db (world_persistent_variables)
DROP TABLE IF EXISTS `variables`;

-- End of migration.
END IF;
END??
DELIMITER ;
CALL add_migration();
DROP PROCEDURE IF EXISTS add_migration;
2 changes: 1 addition & 1 deletion src/game/Commands/ServerCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1875,7 +1875,7 @@ bool ChatHandler::HandleReloadPetitions(char*)
bool ChatHandler::HandleReloadVariablesCommand(char*)
{
sObjectMgr.LoadSavedVariable();
SendSysMessage("Table `variables` has been reloaded.");
SendSysMessage("Table `world_persistent_variables` has been reloaded.");
return true;
}

Expand Down
28 changes: 7 additions & 21 deletions src/game/ObjectMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,10 @@ void ObjectMgr::_SaveVariable(SavedVariable const& toSave)
{
// Must do this in a transaction, else if worker threads > 1 we could do one before the other
// when order is important...
WorldDatabase.BeginTransaction();
WorldDatabase.PExecute("DELETE FROM `variables` WHERE `index` = %u", toSave.uiIndex);
WorldDatabase.PExecute("INSERT INTO `variables` (`index`, `value`) VALUES (%u, %u)", toSave.uiIndex, toSave.uiValue);
WorldDatabase.CommitTransaction();
CharacterDatabase.BeginTransaction();
CharacterDatabase.PExecute("DELETE FROM `world_persistent_variables` WHERE `index` = %u", toSave.uiIndex);
CharacterDatabase.PExecute("INSERT INTO `world_persistent_variables` (`index`, `value`) VALUES (%u, %u)", toSave.uiIndex, toSave.uiValue);
CharacterDatabase.CommitTransaction();
}

void ObjectMgr::InitSavedVariable(uint32 index, uint32 value)
Expand Down Expand Up @@ -587,20 +587,6 @@ void ObjectMgr::SetSavedVariable(uint32 index, uint32 value, bool autoSave)
_SaveVariable(variable);
}

void ObjectMgr::LoadVariable(uint32 index, uint32* variable, uint32 defaultValue, uint32 maxValue, uint32 minValue)
{
bool inIndex = false;
(*variable) = GetSavedVariable(index, defaultValue, &inIndex);
uint32 originalValue = (*variable);
if (maxValue != 0 && (*variable) > maxValue)
(*variable) = defaultValue;
if ((*variable) < minValue)
(*variable) = defaultValue;
if (!inIndex)
_InsertVariable(index, (*variable), true);
if (originalValue != (*variable))
SetSavedVariable(index, (*variable), true);
}
void ObjectMgr::SaveVariables()
{
SavedVariablesVector::iterator it;
Expand All @@ -615,7 +601,7 @@ void ObjectMgr::LoadSavedVariable()
{
m_SavedVariables.clear();

std::unique_ptr<QueryResult> result(WorldDatabase.Query("SELECT `index`, `value` FROM `variables`"));
std::unique_ptr<QueryResult> result(CharacterDatabase.Query("SELECT `index`, `value` FROM `world_persistent_variables`"));

uint32 total_count = 0;

Expand All @@ -625,7 +611,7 @@ void ObjectMgr::LoadSavedVariable()
bar.step();

sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "");
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, ">> Loaded %u saved variables", total_count);
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, ">> Loaded %u world persistent variables", total_count);
return;
}

Expand All @@ -641,7 +627,7 @@ void ObjectMgr::LoadSavedVariable()
while (result->NextRow());

sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "");
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, ">> Loaded %u saved variables", total_count);
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, ">> Loaded %u world persistent variables", total_count);
}

// Caching player data
Expand Down
1 change: 0 additions & 1 deletion src/game/ObjectMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,6 @@ class ObjectMgr
void InitSavedVariable(uint32 index, uint32 value);
uint32 GetSavedVariable(uint32 index, uint32 defaultValue = 0, bool* exist = nullptr);
void SetSavedVariable(uint32 index, uint32 value, bool SaveToDb = false);
void LoadVariable(uint32 index, uint32* variable, uint32 defaultValue, uint32 maxValue=0, uint32 minValue=0);

void LoadSavedVariable();
void SaveVariables();
Expand Down
4 changes: 2 additions & 2 deletions src/game/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1331,8 +1331,8 @@ void World::SetInitialWorldSettings()
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "");
sInstanceStatistics.LoadFromDB();

// Chargements des variables (necessaire pour le OutdoorJcJ)
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "Loading saved variables ...");
// Loads world persistent variables (Wareffort, Stranglethorn Vale Fishing , ...)
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "Loading world_persistent_variables...");
sObjectMgr.LoadSavedVariable();


Expand Down