save stats everytime

This commit is contained in:
Not 2022-04-18 20:39:58 +02:00
parent 14d2e889b5
commit 1ab596ebeb
1 changed files with 47 additions and 19 deletions

View File

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