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 progress #2238

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local achievementStorage = 20000

local function migrateAchievementProgress(player)
for id, achievement in pairs(ACHIEVEMENTS) do
local oldStorageKey = achievementStorage + id
local progressNumber = player:getStorageValue(oldStorageKey)
if progressNumber > 0 then
player:kv():scoped("achievements"):set("progress", progressNumber)
player:setStorageValue(oldStorageKey, -1)
end
end
end

local migration = Migration("move_achievement_to_kv")

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

migration:register()
23 changes: 19 additions & 4 deletions data/scripts/lib/register_achievements.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
if ACHIEVEMENTS ~= nil then
return
end

ACHIEVEMENTS = {
[1] = { name = "Castlemania", grade = 2, points = 5, secret = true, description = "You have an eye for suspicious places and love to read other people's diaries, especially those with vampire stories in it. You're also a dedicated token collector and explorer. Respect!" },
[2] = { name = "Chorister", grade = 1, points = 1, description = "Lalalala... you now know the cult's hymn sung in Liberty Bay by heart. Not that hard, considering that it mainly consists of two notes and repetitive lyrics." },
Expand Down Expand Up @@ -637,3 +633,22 @@ function Player.getPublicAchievements(self)
end
return unlockedPublicAchievements
end

function Player.addAchievementProgress(self, achievement, totalProgress)
local foundAchievement = isNumber(achievement) and Game.getAchievementInfoById(achievement) or Game.getAchievementInfoByName(achievement)
if not foundAchievement then
logger.error("[Player.addAchievementProgress] - Invalid achievement '{}'", achievement)
return
end

local progressNumber = self:kv():scoped("achievements"):get("progress")
if progressNumber and progressNumber <= totalProgress then
if progressNumber == totalProgress then
self:addAchievement(foundAchievement.id)
self:kv():scoped("achievements"):remove("progress")
return
end

self:kv():scoped("achievements"):set("progress", progressNumber)
end
end
Loading