diff --git a/data/migrations/51.lua b/data/migrations/51.lua new file mode 100644 index 00000000..7d348a36 --- /dev/null +++ b/data/migrations/51.lua @@ -0,0 +1,16 @@ +function onUpdateDatabase() + logger.info("Updating database to version 51 (Player Old Names)") + + db.query([[ + CREATE TABLE `player_oldnames` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `player_id` int(11) NOT NULL, + `former_name` varchar(255) NOT NULL DEFAULT '', + `name` varchar(255) NOT NULL, + `old_name` varchar(255) NOT NULL, + `date` int(11) NOT NULL, + PRIMARY KEY (`id`), + INDEX `player_id_index` (`player_id`) + ) + ]]) +end diff --git a/markdowns/CHANGELOG.md b/markdowns/CHANGELOG.md index 2c47d7c8..08e67c04 100644 --- a/markdowns/CHANGELOG.md +++ b/markdowns/CHANGELOG.md @@ -11,10 +11,14 @@ - Added Vibrancy imbuement. ([pennaor](https://github.com/pennaor)) - Added Soul Pit arena/animus mastery/soul core. ([FelipePaluco](https://github.com/FelipePaluco)) - Added a new command `/mute`, which will mute a player until unmuted by `/unmute`. ([Tryller](https://github.com/jprzimba)) +- Cyclopedia House Auction system. ([murilo09](https://github.com/murilo09)) +- Updated name change functionality to save old player names in a database table called `player_oldnames`. ([Tryller](https://github.com/jprzimba)) ## Added files - data/migrations/49.lua +- data/migrations/50.lua +- data/migrations/51.lua - data-global/lib/others/soulpit.lua - data-global/startup/tables/tile.lua - data-global/npc/myzzi.lua @@ -73,7 +77,6 @@ - Add new configurable featurees in `config.lua`: `chainSystemVipOnly`, `fieldOwnershipDuration`, `bedsOnlyPremium`, `loginProtectionPeriod`, `chainSystemModifyMagic`, `logPlayersStatements`. ([Tryller](https://github.com/jprzimba)) - Added a new commands for players: `!randomoutfit`, `!spellwords`. ([Tryller](https://github.com/jprzimba)) - Moved emote spells to `kv` instead of `storage`. ([Tryller](https://github.com/jprzimba)) -- Cyclopedia House Auction system. ([murilo09](https://github.com/murilo09)) - Updated npcs and spells from 13.40 updates. ([murilo09](https://github.com/murilo09)) - Added a Rook system with configurations in `config.lua`. ([Tryller](https://github.com/jprzimba)) - Added a new group `game tester` with flag `isgametester` in `groups.xml` and a new player flag `PlayerFlag_IsGameTester`. ([Tryller](https://github.com/jprzimba)) diff --git a/src/io/functions/iologindata_save_player.cpp b/src/io/functions/iologindata_save_player.cpp index dd49766d..b79c92fe 100644 --- a/src/io/functions/iologindata_save_player.cpp +++ b/src/io/functions/iologindata_save_player.cpp @@ -22,6 +22,7 @@ #include "creatures/monsters/monsters.hpp" #include "creatures/players/animus_mastery/animus_mastery.hpp" #include "game/game.hpp" +#include "game/scheduling/save_manager.hpp" #include "io/ioprey.hpp" #include "items/containers/depot/depotchest.hpp" #include "items/containers/inbox/inbox.hpp" @@ -838,3 +839,37 @@ bool IOLoginDataSave::savePlayerStatement(const std::shared_ptr &player, statementId = db.getLastInsertId(); return true; } + +bool IOLoginDataSave::savePlayerNamesAndChangeName(const std::shared_ptr &player, const std::string &newName, const std::string &oldName) { + if (!player) { + g_logger().warn("[IOLoginData::savePlayerStatement] - Player nullptr: {}", __FUNCTION__); + return false; + } + + Database &db = Database::getInstance(); + std::ostringstream query; + + const time_t now = time(nullptr); + + // find former name + std::string formerName = oldName; + auto result = db.storeQuery(fmt::format("SELECT `former_name` FROM `player_oldnames` WHERE `player_id` = {} LIMIT 1", player->getGUID())); + if (result) { + formerName = result->getString("former_name"); + } + + query << "INSERT INTO `player_oldnames` (`player_id`, `former_name`, `name`, `old_name`, `date`) VALUES (" + << player->getGUID() << ", " << db.escapeString(formerName) << ", " << db.escapeString(newName) << ", " << db.escapeString(oldName) << ", " << now << ")"; + + if (!db.executeQuery(query.str())) { + return false; + } + + if (player->isOnline()) { + player->removePlayer(true, true); + } + + player->setName(newName); + g_saveManager().savePlayer(player); + return true; +} diff --git a/src/io/functions/iologindata_save_player.hpp b/src/io/functions/iologindata_save_player.hpp index 73d6adfa..c7a9dae6 100644 --- a/src/io/functions/iologindata_save_player.hpp +++ b/src/io/functions/iologindata_save_player.hpp @@ -39,6 +39,7 @@ class IOLoginDataSave : public IOLoginData { static bool savePlayerBosstiary(const std::shared_ptr &player); static bool savePlayerStorage(const std::shared_ptr &player); static bool savePlayerStatement(const std::shared_ptr &player, const std::string &receiver, uint16_t channelId, const std::string &text, uint32_t &statementId); + static bool savePlayerNamesAndChangeName(const std::shared_ptr &player, const std::string &newName, const std::string &oldName); protected: using ItemBlockList = std::list>>; diff --git a/src/lua/functions/creatures/player/player_functions.cpp b/src/lua/functions/creatures/player/player_functions.cpp index 0713697b..d5ed543a 100644 --- a/src/lua/functions/creatures/player/player_functions.cpp +++ b/src/lua/functions/creatures/player/player_functions.cpp @@ -36,6 +36,7 @@ #include "game/scheduling/save_manager.hpp" #include "io/iobestiary.hpp" #include "io/iologindata.hpp" +#include "io/functions/iologindata_save_player.hpp" #include "io/ioprey.hpp" #include "items/containers/depot/depotchest.hpp" #include "items/containers/depot/depotlocker.hpp" @@ -4212,13 +4213,11 @@ int PlayerFunctions::luaPlayerChangeName(lua_State* L) { Lua::pushBoolean(L, false); return 0; } - if (player->isOnline()) { - player->removePlayer(true, true); - } + player->kv()->remove("namelock"); const auto newName = Lua::getString(L, 2); - player->setName(newName); - g_saveManager().savePlayer(player); + const auto oldName = player->getName(); + IOLoginDataSave::savePlayerNamesAndChangeName(player, newName, oldName); return 1; }