save stats everytime

This commit is contained in:
Not 2022-04-18 20:39:58 +02:00
parent 14d2e889b5
commit 1ab596ebeb

View File

@ -12,6 +12,9 @@ local splits = {}
local PATCH = nil local PATCH = nil
local help = true local help = true
-- Tracks if stats have been written or not
local StatTrack = false
local UNCLAIMED = "Unclaimed Record" local UNCLAIMED = "Unclaimed Record"
local HELP_MESSAGE = "\x89Leaderboard Commands:\nretry exit findmap changelevel spba_clearcheats lb_gui rival scroll encore" local HELP_MESSAGE = "\x89Leaderboard Commands:\nretry exit findmap changelevel spba_clearcheats lb_gui rival scroll encore"
local FILENAME = "leaderboard.txt" local FILENAME = "leaderboard.txt"
@ -187,7 +190,7 @@ local cv_spb_separate = CV_RegisterVar({
end end
}) })
local function score_t(map, name, skin, color, time, splits, flags, restat) local function score_t(map, name, skin, color, time, splits, flags, stat)
return { return {
["map"] = map, ["map"] = map,
["name"] = name, ["name"] = name,
@ -196,11 +199,11 @@ local function score_t(map, name, skin, color, time, splits, flags, restat)
["time"] = time, ["time"] = time,
["splits"] = splits, ["splits"] = splits,
["flags"] = flags, ["flags"] = flags,
["restat"] = restat ["stat"] = stat
} }
end end
local function restat_t(speed, weight) local function stat_t(speed, weight)
if speed and weight then if speed and weight then
return { return {
["speed"] = speed, ["speed"] = speed,
@ -210,9 +213,9 @@ local function restat_t(speed, weight)
return nil return nil
end end
local function restat_str(restat) local function stat_str(stat)
if restat then if stat then
return string.format("%d%d", restat["speed"], restat["weight"]) return string.format("%d%d", stat["speed"], stat["weight"])
end end
return "0" return "0"
@ -223,7 +226,7 @@ local f = io.open(FILENAME, "r")
if f then if f then
for l in f:lines() do for l in f:lines() do
-- Leaderboard is stored in the following tab separated format -- Leaderboard is stored in the following tab separated format
-- mapnum, name, skin, color, time, splits, flags, restat -- mapnum, name, skin, color, time, splits, flags, stat
local t = {} local t = {}
for word in (l+"\t"):gmatch("(.-)\t") do for word in (l+"\t"):gmatch("(.-)\t") do
table.insert(t, word) table.insert(t, word)
@ -246,9 +249,9 @@ if f then
local stats = nil local stats = nil
if t[8] != nil then if t[8] != nil then
if #t[8] >= 2 then if #t[8] >= 2 then
local speed = string.sub(t[8], 1, 1) local speed = tonumber(string.sub(t[8], 1, 1))
local weight = string.sub(t[8], 2, 2) local weight = tonumber(string.sub(t[8], 2, 2))
stats = restat_t(speed, weight) stats = stat_t(speed, weight)
end end
end end
@ -762,15 +765,18 @@ local function drawScore(v, player, pos, x, y, gui, faceRank, score, drawPos, te
v.drawNum(x, y + 3, pos, textVFlags | VFLAGS) v.drawNum(x, y + 3, pos, textVFlags | VFLAGS)
end end
-- Restats -- Stats
local restat = score["restat"] local stat = score["stat"]
if restat then local pskin = skins[score["skin"]]
v.drawString(x + FACERANK_DIM - 2, y + 4, restat["speed"], V_HUDTRANS | VFLAGS, "small") if stat and not (
v.drawString(x + FACERANK_DIM - 2, y + 8, restat["weight"], V_HUDTRANS | VFLAGS, "small") pskin
and pskin.kartweight == stat["weight"]
and pskin.kartspeed == stat["speed"]
) then
v.drawString(x + FACERANK_DIM - 2, y + 4, stat["speed"], V_HUDTRANS | VFLAGS, "small")
v.drawString(x + FACERANK_DIM - 2, y + 8, stat["weight"], V_HUDTRANS | VFLAGS, "small")
end end
if gui == GUI_ON or (gui == GUI_SPLITS and showSplit) then if gui == GUI_ON or (gui == GUI_SPLITS and showSplit) then
local name = score["name"] local name = score["name"]
@ -961,9 +967,25 @@ function scroll_to(player)
drawState = DS_SCRLTO drawState = DS_SCRLTO
end end
-- Write skin stats to each score where there are none
local function writeStats()
for _, t in pairs(lb) do
for _, scoreTable in pairs(t) do
for _, score in ipairs(scoreTable) do
local skin = skins[score["skin"]]
if skin and not score["stat"] then
local stats = stat_t(skin.kartspeed, skin.kartweight)
score["stat"] = stats
end
end
end
end
end
local function saveTime(player) local function saveTime(player)
local scoreTable = getScoreTable(gamemap, Flags) or {} local scoreTable = getScoreTable(gamemap, Flags) or {}
local pskin = skins[player.mo.skin]
local newscore = score_t( local newscore = score_t(
gamemap, gamemap,
player.name, player.name,
@ -972,7 +994,7 @@ local function saveTime(player)
timeFinished, timeFinished,
splits, splits,
Flags, Flags,
restat_t(player.HMRs, player.HMRw) stat_t(player.HMRs or pskin.kartspeed, player.HMRw or pskin.kartweight)
) )
-- Check if you beat your previous best -- Check if you beat your previous best
@ -1006,6 +1028,11 @@ local function saveTime(player)
setScoreTable(gamemap, Flags, scoreTable) setScoreTable(gamemap, Flags, scoreTable)
if not StatTrack then
writeStats()
StatTrack = true
end
local f = assert(io.open(FILENAME, "w")) local f = assert(io.open(FILENAME, "w"))
if f == nil then if f == nil then
print("Failed to open file for writing: " + FILENAME) print("Failed to open file for writing: " + FILENAME)
@ -1023,7 +1050,7 @@ local function saveTime(player)
score["time"], "\t", score["time"], "\t",
table.concat(score["splits"], " "), "\t", table.concat(score["splits"], " "), "\t",
score["flags"], "\t", score["flags"], "\t",
restat_str(score["restat"]), "\n" stat_str(score["stat"]), "\n"
) )
end end
end end
@ -1218,6 +1245,7 @@ local function netvars(net)
splits = net($) splits = net($)
prevLap = net($) prevLap = net($)
drawState = net($) drawState = net($)
StatTrack = net($)
lb = net($) lb = net($)
end end
addHook("NetVars", netvars) addHook("NetVars", netvars)