Skip to content
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

fix: achievement migration and highscore categories #2260

Merged
merged 10 commits into from
Feb 21, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local achievementProgressStorage = 20000
local achievementStorage = 30000

local function migrateAchievementProgress(player)
for id, achievement in pairs(ACHIEVEMENTS) do
local oldStorageKey = achievementProgressStorage + id
local progressNumber = player:getStorageValue(oldStorageKey)
if progressNumber > 0 then
local achievScopeName = tostring(achievement.name .. "-progress")
player:kv():scoped(achievScopeName, progressNumber)
player:setStorageValue(oldStorageKey, -1)
end
local oldAchievement = player:getStorageValue(achievementStorage + id)
if oldAchievement > 0 then
player:addAchievement(achievement.name)
player:setStorageValue(achievementStorage + id, -1)
end
end
local points = 0
local list = player:getAchievements()
if #list > 0 then
for i = 1, #list do
local a = Game.getAchievementInfoById(list[i])
if a.points > 0 then
points = points + a.points
end
end
end

player:addAchievementPoints(points)
end

local migration = Migration("20241708000535_move_achievement_to_kv")

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

migration:register()
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local oldAutolootStorage = 30063
local flasksStorage = "talkaction.potions.flask"

local function migrate(player)
local isAutoLoot = player:getStorageValue(oldAutolootStorage)
if isAutoLoot > 0 then
player:setFeature(Features.AutoLoot, 1)
player:setStorageValue(oldAutolootStorage, -1)
end

local getOldFlasksStorage = player:getStorageValueByName(flasksStorage)
if getOldFlasksStorage > 0 then
player:kv():set("talkaction.potions.flask", true)
player:setStorageValueByName(flasksStorage, -1)
end
end

local migration = Migration("20241708485868_move_some_storages_to_kv")

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

migration:register()

This file was deleted.

13 changes: 8 additions & 5 deletions data/scripts/lib/register_achievements.lua
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ ACHIEVEMENTS = {
[482] = { name = "Dream Catcher", grade = 1, points = 3, description = "You are the slayer of the ancient nightmare beast and prevented the nightmare to spread its madness." },
[483] = { name = "Champion of Summer", grade = 1, points = 2, secret = true, description = "You have vanquished numerous arena champions in the name of the Summer Court." },
[484] = { name = "Champion of Winter", grade = 1, points = 2, secret = true, description = "You have vanquished numerous arena champions in the name of the Winter Court." },
[485] = { name = "Allow Cookies?", grade = 2, points = 6, description = "With a perfectly harmless smile, you tricked all the funny guys into eating your exploding cookies. Next time you pull this prank, consider wearing a Boy Scout outfit to make it even better." },
[486] = { name = "Bewitcher", grade = 2, points = 5, secret = true, description = "You literally put everything in that cauldron except lilac and gooseberries." },
[487] = { name = "Gryphon Rider", grade = 1, points = 3, description = "Unmasking spies, killing demons, discovering omens, solving puzzles and fighting ogres, manticores and feral sphinxes. - Nobody said it was easy to become a gryphon rider." },
[488] = { name = "Sculptor Apprentice", grade = 1, points = 2, secret = true, description = "Granted, you didn't carve those lifelike animal figurines yourself. But helping a medusa to find proper objects and even watching her using her petrifying gaze is almost as rewarding." },
Expand Down Expand Up @@ -644,14 +645,16 @@ function Player.addAchievementProgress(self, achievement, totalProgress)
local achievScope = self:kv():scoped("achievements")
local achievScopeName = tostring(foundAchievement.name .. "-progress")
local progressNumber = achievScope:get(achievScopeName) or 0
local newProgress = progressNumber + 1
if newProgress > totalProgress then
return
end

if progressNumber + 1 == totalProgress then
if newProgress == totalProgress then
self:addAchievement(foundAchievement.id)
logger.debug("[Player.addAchievementProgress] - Achievement '{}' completed", foundAchievement.name)
achievScope:remove(achievScopeName)
return
end

logger.debug("[Player.addAchievementProgress] - Achievement '{}' progress updated to '{}', total progress '{}'", foundAchievement.name, progressNumber + 1, totalProgress)
achievScope:set(achievScopeName, progressNumber + 1)
logger.debug("[Player.addAchievementProgress] - Achievement '{}' progress updated to '{}', total progress '{}'", foundAchievement.name, newProgress, totalProgress)
achievScope:set(achievScopeName, newProgress)
end
4 changes: 2 additions & 2 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ Game::Game() {
// Create instance of IOWheel to Game class
m_IOWheel = std::make_unique<IOWheel>();

std::unordered_map<uint8_t, std::string> m_highscoreCategoriesNames = {
m_highscoreCategoriesNames = {
{ static_cast<uint8_t>(HighscoreCategories_t::ACHIEVEMENTS), "Achievement Points" },
{ static_cast<uint8_t>(HighscoreCategories_t::AXE_FIGHTING), "Axe Fighting" },
{ static_cast<uint8_t>(HighscoreCategories_t::CHARMS), "Charm Points" },
Expand All @@ -224,7 +224,7 @@ Game::Game() {
{ static_cast<uint8_t>(HighscoreCategories_t::SWORD_FIGHTING), "Sword Fighting" },
};

std::vector<HighscoreCategory> m_highscoreCategories = {
m_highscoreCategories = {
HighscoreCategory("Experience Points", static_cast<uint8_t>(HighscoreCategories_t::EXPERIENCE)),
HighscoreCategory("Fist Fighting", static_cast<uint8_t>(HighscoreCategories_t::FIST_FIGHTING)),
HighscoreCategory("Club Fighting", static_cast<uint8_t>(HighscoreCategories_t::CLUB_FIGHTING)),
Expand Down
4 changes: 2 additions & 2 deletions src/lua/functions/creatures/player/player_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4237,7 +4237,7 @@ int PlayerFunctions::luaPlayerRemoveAchievement(lua_State* L) {
}

int PlayerFunctions::luaPlayerGetAchievementPoints(lua_State* L) {
// player:getAchievementsPoints()
// player:getAchievementPoints()
const auto &player = getUserdataShared<Player>(L, 1);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
Expand All @@ -4249,7 +4249,7 @@ int PlayerFunctions::luaPlayerGetAchievementPoints(lua_State* L) {
}

int PlayerFunctions::luaPlayerAddAchievementPoints(lua_State* L) {
// player:addAchievementsPoints(amount)
// player:addAchievementPoints(amount)
const auto &player = getUserdataShared<Player>(L, 1);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
Expand Down
2 changes: 2 additions & 0 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2161,7 +2161,9 @@ void ProtocolGame::sendHighscores(const std::vector<HighscoreCharacter> &charact
uint8_t selectedCategory = 0;
const auto &highscoreCategories = g_game().getHighscoreCategories();
msg.addByte(highscoreCategories.size()); // Category Count
g_logger().debug("[ProtocolGame::sendHighscores] - Category Count: {}", highscoreCategories.size());
for (const HighscoreCategory &category : highscoreCategories) {
g_logger().debug("[ProtocolGame::sendHighscores] - Category: {} - Name: {}", category.m_id, category.m_name);
msg.addByte(category.m_id); // Category Id
msg.addString(category.m_name, "ProtocolGame::sendHighscores - category.name"); // Category Name
if (category.m_id == categoryId) {
Expand Down
Loading