diff --git a/leaderboard.lua b/leaderboard.lua index 05a5eaa..1509d8a 100644 --- a/leaderboard.lua +++ b/leaderboard.lua @@ -289,9 +289,30 @@ local function writeScore(fh, score) ) end +local function saveScores() + local f = assert(io.open(FILENAME + ".txt", "w")) + if f == nil then + print("Failed to open file for writing: " + FILENAME + ".txt") + return + end + + for _, tbl in pairs(lb) do + for _, scoreTable in pairs(tbl) do + for _, score in ipairs(scoreTable) do + writeScore(f, score) + end + end + end + + f:close() +end + -- Read the leaderboard local f = io.open(FILENAME + ".txt", "r") if f then + -- track if scores checksums have been written + local checksumWrite = false + for l in f:lines() do -- Leaderboard is stored in the following tab separated format -- mapnum, name, skin, color, time, splits, flags, stat, map_checksum @@ -326,11 +347,14 @@ if f then local mapnum = tonumber(t[1]) -- Checksums - local cks = t[9] or mapChecksum(mapnum) + local cks = t[9] - -- We have no previous recognition of this map - -- remove it if cks == nil then + checksumWrite = true + cks = mapChecksum(mapnum) + + -- We have no previous recognition of this map + -- remove it if cks == nil then local rsfh = io.open(FILENAME + "_removed_scores.txt", "a") if not rsfh then @@ -363,6 +387,11 @@ if f then sortScores() f:close() + + -- save scores + if checksumWrite then + saveScores() + end else print("Failed to open file: ", FILENAME + ".txt") end @@ -1232,21 +1261,7 @@ local function saveTime(player) StatTrack = true end - local f = assert(io.open(FILENAME + ".txt", "w")) - if f == nil then - print("Failed to open file for writing: " + FILENAME + ".txt") - return - end - - for _, tbl in pairs(lb) do - for _, scoreTable in pairs(tbl) do - for _, score in ipairs(scoreTable) do - writeScore(f, score) - end - end - end - - f:close() + saveScores() end -- DEBUGGING