Skip to content

Commit

Permalink
fix: achievement migration and highscore categories (#2260)
Browse files Browse the repository at this point in the history
Fix initialization of vector and map from category and others small
things.
  • Loading branch information
dudantas authored Feb 20, 2024
1 parent b8b5d80 commit 38c51e0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 29 deletions.
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("20241708275600_move_achievement_to_kv")

function migration:onExecute()
self:forEachPlayer(migrateAchievementProgress)
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

0 comments on commit 38c51e0

Please sign in to comment.