3 Commits

Author SHA1 Message Date
Not
db0696294b 4char padding, do proper string concat on map..checksum 2022-07-19 21:03:40 +02:00
Not
907c07a25d resave scores if checksums have been written 2022-07-19 20:31:03 +02:00
Not
00aa2060f3 use checksums to identify maps 2022-07-19 20:31:03 +02:00

View File

@ -12,6 +12,7 @@ local splits = {}
local PATCH = nil
local help = true
local EncoreInitial = nil
local cv_teamchange
local scoreTable
@ -33,8 +34,8 @@ local RedFlash = {
local StatTrack = false
local UNCLAIMED = "Unclaimed Record"
local HELP_MESSAGE = "\x89Leaderboard Commands:\nretry exit findmap changelevel spba_clearcheats lb_gui rival scroll encore records"
local FILENAME = "leaderboard.txt"
local HELP_MESSAGE = "\x89Leaderboard Commands:\nretry exit findmap changelevel spba_clearcheats lb_gui rival scroll encore"
local FILENAME = "leaderboard"
-- Retry / changelevel map
local nextMap = nil
@ -90,10 +91,6 @@ local ticsToTime
local allowJoin
---------------
-- cvars
local cv_teamchange
local cv_spbatk
local cv_gui = CV_RegisterVar({
name = "lb_gui",
defaultvalue = GUI_ON,
@ -153,15 +150,36 @@ local cv_interrupt = CV_RegisterVar({
end
})
local function djb2(message)
local digest = 5381
for c in message:gmatch(".") do
digest = (($ << 5) + $) + string.byte(c)
end
return digest
end
local function mapChecksum(mapnum)
local mh = mapheaderinfo[mapnum]
if not mh then
return nil
end
local digest = string.format("%04x", djb2(mh.lvlttl+mh.subttl+mh.zonttl))
return string.sub(digest, #digest - 3)
end
local function setST(t, map, flags, scoreTable)
local mode = flags & ST_SEP
local cks = mapChecksum(map)
t[mode] = t[mode] or {}
t[mode][map] = scoreTable
t[mode][tostring(map)..(cks or "")] = scoreTable
end
local function getST(t, map, flags)
local mode = flags & ST_SEP
return t[mode] and t[mode][map] or nil
local cks = mapChecksum(map)
return t[mode] and t[mode][tostring(map)..(cks or "")] or nil
end
local function setScoreTable(map, flags, scoreTable)
@ -225,7 +243,7 @@ local cv_spb_separate = CV_RegisterVar({
end
})
local function score_t(map, name, skin, color, time, splits, flags, stat)
local function score_t(map, name, skin, color, time, splits, flags, stat, checksum)
return {
["map"] = map,
["name"] = name,
@ -234,7 +252,8 @@ local function score_t(map, name, skin, color, time, splits, flags, stat)
["time"] = time,
["splits"] = splits,
["flags"] = flags,
["stat"] = stat
["stat"] = stat,
["checksum"] = checksum
}
end
@ -256,12 +275,47 @@ local function stat_str(stat)
return "0"
end
local function writeScore(fh, score)
fh:write(
score["map"], "\t",
score["name"], "\t",
score["skin"], "\t",
score["color"], "\t",
score["time"], "\t",
table.concat(score["splits"], " "), "\t",
score["flags"], "\t",
stat_str(score["stat"]), "\t",
score["checksum"], "\n"
)
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, "r")
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
-- mapnum, name, skin, color, time, splits, flags, stat, map_checksum
local t = {}
for word in (l+"\t"):gmatch("(.-)\t") do
table.insert(t, word)
@ -290,17 +344,41 @@ if f then
end
end
local mapnum = tonumber(t[1])
-- Checksums
local cks = t[9]
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
print("Failed to open file: ", FILENAME + "_removed_scores.txt")
continue
end
rsfh:write(l, "\n")
rsfh:close()
continue
end
end
table.insert(
scoreTable,
score_t(
tonumber(t[1]),
mapnum,
t[2],
t[3],
t[4],
tonumber(t[5]),
spl,
flags,
stats
stats,
cks
)
)
@ -309,8 +387,13 @@ if f then
sortScores()
f:close()
-- save scores
if checksumWrite then
saveScores()
end
else
print("Failed to open file: ", FILENAME)
print("Failed to open file: ", FILENAME + ".txt")
end
function allowJoin(v)
@ -386,20 +469,6 @@ local function exitlevel(player, ...)
end
COM_AddCommand("exit", exitlevel)
local function zoneAct(map)
local z = ""
if map.zonttl != "" then
z = " " + map.zonttl
elseif not(map.levelflags & LF_NOZONE) then
z = " Zone"
end
if map.actnum != "" then
z = $ + " " + map.actnum
end
return z
end
local function findMap(player, ...)
local search = ...
@ -412,13 +481,28 @@ local function findMap(player, ...)
}
local lvltype, map, lvlttl
local zoneAct = function(zone, act, levelflags)
local z = ""
if zone != "" then
z = " " + zone
elseif not(levelflags & LF_NOZONE) then
z = " Zone"
end
if act != "" then
z = $ + " " + act
end
return z
end
for i = 1, #mapheaderinfo do
map = mapheaderinfo[i]
if map == nil then
continue
end
lvlttl = map.lvlttl + zoneAct(map)
lvlttl = map.lvlttl + zoneAct(map.zonttl, map.actnum, map.levelflags)
if not search or lvlttl:lower():find(search:lower()) then
-- Only care for up to TOL_MATCH (0x10)
@ -445,129 +529,10 @@ local function findMap(player, ...)
end
COM_AddCommand("findmap", findMap)
local function mapnumFromExtended(map)
local p, q = map:upper():match("MAP(%w)(%w)$", 1)
if not (p and q) then
return nil
end
local mapnum = 0
local A = string.byte("A")
if tonumber(p) != nil then
-- Non extended map numbers
if tonumber(q) == nil then
return nil
end
mapnum = tonumber(p) * 10 + tonumber(q)
else
--Extended map numbers
p = string.byte(p) - A
local qn = tonumber(q)
if qn == nil then
qn = string.byte(q) - A + 10
end
mapnum = 36 * p + qn + 100
end
return mapnum
local function mapNotExists(player, map)
CONS_Printf(player, string.format("Map doesn't exist: %s", map:upper()))
end
local SPBModeSym = {
[F_SPBEXP] = "X",
[F_SPBBIG] = "B",
[F_SPBJUS] = "J",
}
local function modeToString(mode)
local modestr = "Time Attack"
if mode & F_SPBATK then
modestr = "SPB"
for k, v in pairs(SPBModeSym) do
if mode & k then
modestr = $ + v
end
end
end
return modestr
end
local function records(player, ...)
local mapid = ...
local mapnum = gamemap
if mapid then
mapnum = mapnumFromExtended(mapid)
if not mapnum then
CONS_Printf(player, string.format("Invalid map name: %s", mapid))
return
end
end
local map = mapheaderinfo[mapnum]
if map then
CONS_Printf(player,
string.format(
"\x83%s%8s",
map.lvlttl,
(map.menuflags & LF2_HIDEINMENU and "\x85HELL") or ""
)
)
local zoneact = zoneAct(map)
-- print the zone/act on the right hand size under the title
CONS_Printf(
player,
string.format(
string.format("\x83%%%ds%%s\x80 - \x88%%s", #map.lvlttl - #zoneact / 2 - 1),
" ",
zoneAct(map),
map.subttl
)
)
else
CONS_Printf(player, "\x85UNKNOWN MAP")
end
for mode, maps in pairs(lb) do
local maptbl = maps[mapnum]
if not maptbl then continue end
CONS_Printf(player, "")
CONS_Printf(player, modeToString(mode))
-- don't print flags for time attack
if mode then
for i, tbl in ipairs(maptbl) do
CONS_Printf(
player,
string.format(
"%2d %-21s \x89%8s \x80%s",
i,
tbl["name"],
ticsToTime(tbl["time"]),
modeToString(tbl["flags"])
)
)
end
else
for i, tbl in ipairs(maptbl) do
CONS_Printf(
player,
string.format(
"%2d %-21s \x89%8s",
i,
tbl["name"],
ticsToTime(tbl["time"])
)
)
end
end
end
end
COM_AddCommand("records", records)
local function changelevel(player, ...)
if not doyoudare(player) then
return
@ -582,13 +547,35 @@ local function changelevel(player, ...)
return
end
local mapnum = mapnumFromExtended(map)
if not mapnum then
local p, q = map:upper():match("MAP(%w)(%w)$", 1)
if not (p and q) then
CONS_Printf(player, string.format("Invalid map name: %s", map))
return
end
local mapnum = 0
local A = string.byte("A")
if tonumber(p) != nil then
-- Non extended map numbers
if tonumber(q) == nil then
mapNotExists(player, map)
return
end
mapnum = tonumber(p) * 10 + tonumber(q)
else
--Extended map numbers
p = string.byte(p) - A
local qn = tonumber(q)
if qn == nil then
qn = string.byte(q) - A + 10
end
mapnum = 36 * p + qn + 100
end
if mapheaderinfo[mapnum] == nil then
CONS_Printf(player, string.format("Map doesn't exist: %s", map:upper()))
mapNotExists(player, map)
return
end
@ -653,7 +640,7 @@ local function findRival(player, ...)
[0] = "\x89",
[-1] = "\x88"
}
local sym = {
[true] = "-",
[false] = "",
@ -711,6 +698,12 @@ local function findRival(player, ...)
return a["rival"]["map"] < b["rival"]["map"]
end
local modestrings = {
[F_SPBEXP] = "X",
[F_SPBBIG] = "B",
[F_SPBJUS] = "J",
}
for mode, tbl in pairs(scores) do
if i >= stop then break end
@ -724,7 +717,15 @@ local function findRival(player, ...)
if i >= stop then break end
i = i + 1
local modestr = modeToString(score["rival"]["flags"])
local modestr = "TA"
if score["rival"]["flags"] & F_SPBATK then
modestr = "SPB"
for k, v in pairs(modestrings) do
if score["rival"]["flags"] & k then
modestr = $ + v
end
end
end
if score["your"] then
local diff = score["your"]["time"] - score["rival"]["time"]
@ -793,7 +794,7 @@ COM_AddCommand("rival", findRival)
-- -- v[i]["flags"],
-- -- ","
-- -- )
-- --
-- --
-- --end
-- end
-- end
@ -861,7 +862,7 @@ local function marquee(text, maxwidth)
if #text <= maxwidth then
return text
end
local shift = 16
-- Creates an index range ranging from -shift to #text + shift
@ -1214,7 +1215,8 @@ local function saveTime(player)
timeFinished,
splits,
Flags,
stat_t(player.HMRs or pskin.kartspeed, player.HMRw or pskin.kartweight)
stat_t(player.HMRs or pskin.kartspeed, player.HMRw or pskin.kartweight),
mapChecksum(gamemap)
)
-- Check if you beat your previous best
@ -1259,30 +1261,7 @@ local function saveTime(player)
StatTrack = true
end
local f = assert(io.open(FILENAME, "w"))
if f == nil then
print("Failed to open file for writing: " + FILENAME)
return
end
for _, tbl in pairs(lb) do
for _, scoreTable in pairs(tbl) do
for _, score in ipairs(scoreTable) do
f:write(
score["map"], "\t",
score["name"], "\t",
score["skin"], "\t",
score["color"], "\t",
score["time"], "\t",
table.concat(score["splits"], " "), "\t",
score["flags"], "\t",
stat_str(score["stat"]), "\n"
)
end
end
end
f:close()
saveScores()
end
-- DEBUGGING
@ -1382,15 +1361,10 @@ local function think()
end
end
end
if not cv_spbatk then
cv_spbatk = CV_FindVar("spbatk")
end
-- Gamemode flags
Flags = $ & !(F_SPBATK | F_SPBEXP | F_SPBBIG | F_SPBJUS)
if server.SPBArunning
and cv_spbatk.value
and leveltime > START_TIME - (3 * TICRATE) / 2 then
if leveltime > START_TIME - (3 * TICRATE) / 2 and server.SPBArunning then
Flags = $ | F_SPBATK
if server.SPBAexpert then
Flags = $ | F_SPBEXP
@ -1413,18 +1387,10 @@ local function think()
end
end
-- make sure the spb actually spawned
if leveltime == START_TIME - 1 then
if not (server.SPBAbomb and server.SPBAbomb.valid) then
-- it didn't spawn, clear spb flags
Flags = $ & !(F_SPBATK | F_SPBEXP | F_SPBBIG | F_SPBJUS)
end
end
else
end
if not (Flags & F_SPBATK) then
hud.enable("freeplay")
end
end
scoreTable = getScoreTable(gamemap, Flags)