Skip to content

Commit

Permalink
improve: move wheel scrolls to kv (opentibiabr#2637)
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas authored May 24, 2024
1 parent c57f5a2 commit caa3546
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local promotionScrolls = {
{ oldScroll = "wheel.scroll.abridged", newScroll = "abridged" },
{ oldScroll = "wheel.scroll.basic", newScroll = "basic" },
{ oldScroll = "wheel.scroll.revised", newScroll = "revised" },
{ oldScroll = "wheel.scroll.extended", newScroll = "extended" },
{ oldScroll = "wheel.scroll.advanced", newScroll = "advanced" },
}

local function migrate(player)
for _, scrollTable in ipairs(promotionScrolls) do
local oldStorage = player:getStorageValueByName(scrollTable.oldScroll)
if oldStorage > 0 then
player:kv():scoped("wheel-of-destiny"):scoped("scrolls"):set(scrollTable.newScroll, true)
end
end
end

local migration = Migration("20241715984279_move_wheel_scrolls_from_storagename_to_kv")

function migration:onExecute()
self:forEachPlayer(migrate)
end

migration:register()
17 changes: 9 additions & 8 deletions data/scripts/actions/items/wheel_scrolls.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local promotionScrolls = {
[43946] = { storageName = "wheel.scroll.abridged", points = 3, name = "abridged promotion scroll" },
[43947] = { storageName = "wheel.scroll.basic", points = 5, name = "basic promotion scroll" },
[43948] = { storageName = "wheel.scroll.revised", points = 9, name = "revised promotion scroll" },
[43949] = { storageName = "wheel.scroll.extended", points = 13, name = "extended promotion scroll" },
[43950] = { storageName = "wheel.scroll.advanced", points = 20, name = "advanced promotion scroll" },
[43946] = { name = "abridged", points = 3, itemName = "abridged promotion scroll" },
[43947] = { name = "basic", points = 5, itemName = "basic promotion scroll" },
[43948] = { name = "revised", points = 9, itemName = "revised promotion scroll" },
[43949] = { name = "extended", points = 13, itemName = "extended promotion scroll" },
[43950] = { name = "advanced", points = 20, itemName = "advanced promotion scroll" },
}

local scroll = Action()
Expand All @@ -15,13 +15,14 @@ function scroll.onUse(player, item, fromPosition, target, toPosition, isHotkey)
end

local scrollData = promotionScrolls[item:getId()]
if player:getStorageValueByName(scrollData.storageName) == 1 then
local scrollKV = player:kv():scoped("wheel-of-destiny"):scoped("scrolls")
if scrollKV:get(scrollData.name) then
player:sendTextMessage(MESSAGE_LOOK, "You have already deciphered this scroll.")
return true
end

player:setStorageValueByName(scrollData.storageName, 1)
player:sendTextMessage(MESSAGE_LOOK, "You have gained " .. scrollData.points .. " promotion points for the Wheel of Destiny by deciphering the " .. scrollData.name .. ".")
scrollKV:set(scrollData.name, true)
player:sendTextMessage(MESSAGE_LOOK, "You have gained " .. scrollData.points .. " promotion points for the Wheel of Destiny by deciphering the " .. scrollData.itemName .. ".")
item:remove(1)
return true
end
Expand Down
2 changes: 1 addition & 1 deletion data/scripts/lib/register_migrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function Migration:register()
return
end
if not self:_validateName() then
error("Invalid migration name: " .. self.name .. ". Migration names must be in the format: <timestamp>_<description>. Example: 20231128213149_add_new_monsters")
logger.error("Invalid migration name: " .. self.name .. ". Migration names must be in the format: <timestamp>_<description>. Example: 20231128213149_add_new_monsters")
end

table.insert(Migration.registry, self)
Expand Down
34 changes: 21 additions & 13 deletions src/creatures/players/wheel/player_wheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ namespace {

struct PromotionScroll {
uint16_t itemId;
std::string storageKey;
std::string name;
uint8_t extraPoints;
};

std::vector<PromotionScroll> WheelOfDestinyPromotionScrolls = {
{ 43946, "wheel.scroll.abridged", 3 },
{ 43947, "wheel.scroll.basic", 5 },
{ 43948, "wheel.scroll.revised", 9 },
{ 43949, "wheel.scroll.extended", 13 },
{ 43950, "wheel.scroll.advanced", 20 },
{ 43946, "abridged", 3 },
{ 43947, "basic", 5 },
{ 43948, "revised", 9 },
{ 43949, "extended", 13 },
{ 43950, "advanced", 20 },
};
} // namespace

Expand Down Expand Up @@ -744,18 +744,21 @@ int PlayerWheel::getSpellAdditionalDuration(const std::string &spellName) const
}

void PlayerWheel::addPromotionScrolls(NetworkMessage &msg) const {
uint16_t count = 0;
std::vector<uint16_t> unlockedScrolls;

for (const auto &scroll : WheelOfDestinyPromotionScrolls) {
auto storageValue = m_player.getStorageValueByName(scroll.storageKey);
if (storageValue > 0) {
count++;
const auto &scrollKv = m_player.kv()->scoped("wheel-of-destiny")->scoped("scrolls");
if (!scrollKv) {
continue;
}

auto scrollOpt = scrollKv->get(scroll.name);
if (scrollOpt && scrollOpt->get<bool>()) {
unlockedScrolls.push_back(scroll.itemId);
}
}

msg.add<uint16_t>(count);
msg.add<uint16_t>(unlockedScrolls.size());
for (const auto &itemId : unlockedScrolls) {
msg.add<uint16_t>(itemId);
}
Expand Down Expand Up @@ -1239,8 +1242,13 @@ uint16_t PlayerWheel::getExtraPoints() const {

uint16_t totalBonus = 0;
for (const auto &scroll : WheelOfDestinyPromotionScrolls) {
auto storageValue = m_player.getStorageValueByName(scroll.storageKey);
if (storageValue > 0) {
const auto &scrollKv = m_player.kv()->scoped("wheel-of-destiny")->scoped("scrolls");
if (!scrollKv) {
continue;
}

auto scrollKV = scrollKv->get(scroll.name);
if (scrollKV && scrollKV->get<bool>()) {
totalBonus += scroll.extraPoints;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lua/functions/creatures/player/player_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,7 @@ int PlayerFunctions::luaPlayerGetStorageValueByName(lua_State* L) {
return 0;
}

g_logger().warn("The function 'player:getStorageValueByName' is deprecated and will be removed in future versions, please use KV system");
auto name = getString(L, 2);
lua_pushnumber(L, player->getStorageValueByName(name));
return 1;
Expand All @@ -1781,6 +1782,7 @@ int PlayerFunctions::luaPlayerSetStorageValueByName(lua_State* L) {
return 0;
}

g_logger().warn("The function 'player:setStorageValueByName' is deprecated and will be removed in future versions, please use KV system");
auto storageName = getString(L, 2);
int32_t value = getNumber<int32_t>(L, 3);

Expand Down

0 comments on commit caa3546

Please sign in to comment.