Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
27e596967c |
334
leaderboard.lua
334
leaderboard.lua
@ -34,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"
|
||||
local FILENAME = "leaderboard"
|
||||
local HELP_MESSAGE = "\x89Leaderboard Commands:\nretry exit findmap changelevel spba_clearcheats lb_gui rival scroll encore records"
|
||||
local FILENAME = "leaderboard.txt"
|
||||
|
||||
-- Retry / changelevel map
|
||||
local nextMap = nil
|
||||
@ -150,36 +150,15 @@ 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][tostring(map)..(cks or "")] = scoreTable
|
||||
t[mode][map] = scoreTable
|
||||
end
|
||||
|
||||
local function getST(t, map, flags)
|
||||
local mode = flags & ST_SEP
|
||||
local cks = mapChecksum(map)
|
||||
return t[mode] and t[mode][tostring(map)..(cks or "")] or nil
|
||||
return t[mode] and t[mode][map] or nil
|
||||
end
|
||||
|
||||
local function setScoreTable(map, flags, scoreTable)
|
||||
@ -243,7 +222,7 @@ local cv_spb_separate = CV_RegisterVar({
|
||||
end
|
||||
})
|
||||
|
||||
local function score_t(map, name, skin, color, time, splits, flags, stat, checksum)
|
||||
local function score_t(map, name, skin, color, time, splits, flags, stat)
|
||||
return {
|
||||
["map"] = map,
|
||||
["name"] = name,
|
||||
@ -252,8 +231,7 @@ local function score_t(map, name, skin, color, time, splits, flags, stat, checks
|
||||
["time"] = time,
|
||||
["splits"] = splits,
|
||||
["flags"] = flags,
|
||||
["stat"] = stat,
|
||||
["checksum"] = checksum
|
||||
["stat"] = stat
|
||||
}
|
||||
end
|
||||
|
||||
@ -275,47 +253,12 @@ 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 + ".txt", "r")
|
||||
local f = io.open(FILENAME, "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
|
||||
-- mapnum, name, skin, color, time, splits, flags, stat
|
||||
local t = {}
|
||||
for word in (l+"\t"):gmatch("(.-)\t") do
|
||||
table.insert(t, word)
|
||||
@ -344,41 +287,17 @@ 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(
|
||||
mapnum,
|
||||
tonumber(t[1]),
|
||||
t[2],
|
||||
t[3],
|
||||
t[4],
|
||||
tonumber(t[5]),
|
||||
spl,
|
||||
flags,
|
||||
stats,
|
||||
cks
|
||||
stats
|
||||
)
|
||||
)
|
||||
|
||||
@ -387,13 +306,8 @@ if f then
|
||||
|
||||
sortScores()
|
||||
f:close()
|
||||
|
||||
-- save scores
|
||||
if checksumWrite then
|
||||
saveScores()
|
||||
end
|
||||
else
|
||||
print("Failed to open file: ", FILENAME + ".txt")
|
||||
print("Failed to open file: ", FILENAME)
|
||||
end
|
||||
|
||||
function allowJoin(v)
|
||||
@ -469,6 +383,20 @@ 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 = ...
|
||||
|
||||
@ -481,28 +409,13 @@ 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.zonttl, map.actnum, map.levelflags)
|
||||
lvlttl = map.lvlttl + zoneAct(map)
|
||||
|
||||
if not search or lvlttl:lower():find(search:lower()) then
|
||||
-- Only care for up to TOL_MATCH (0x10)
|
||||
@ -529,10 +442,129 @@ local function findMap(player, ...)
|
||||
end
|
||||
COM_AddCommand("findmap", findMap)
|
||||
|
||||
local function mapNotExists(player, map)
|
||||
CONS_Printf(player, string.format("Map doesn't exist: %s", map:upper()))
|
||||
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
|
||||
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
|
||||
@ -547,35 +579,13 @@ local function changelevel(player, ...)
|
||||
return
|
||||
end
|
||||
|
||||
local p, q = map:upper():match("MAP(%w)(%w)$", 1)
|
||||
if not (p and q) then
|
||||
local mapnum = mapnumFromExtended(map)
|
||||
if not mapnum 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
|
||||
mapNotExists(player, map)
|
||||
CONS_Printf(player, string.format("Map doesn't exist: %s", map:upper()))
|
||||
return
|
||||
end
|
||||
|
||||
@ -698,12 +708,6 @@ 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
|
||||
|
||||
@ -717,15 +721,7 @@ local function findRival(player, ...)
|
||||
if i >= stop then break end
|
||||
i = i + 1
|
||||
|
||||
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
|
||||
local modestr = modeToString(score["rival"]["flags"])
|
||||
|
||||
if score["your"] then
|
||||
local diff = score["your"]["time"] - score["rival"]["time"]
|
||||
@ -1215,8 +1211,7 @@ local function saveTime(player)
|
||||
timeFinished,
|
||||
splits,
|
||||
Flags,
|
||||
stat_t(player.HMRs or pskin.kartspeed, player.HMRw or pskin.kartweight),
|
||||
mapChecksum(gamemap)
|
||||
stat_t(player.HMRs or pskin.kartspeed, player.HMRw or pskin.kartweight)
|
||||
)
|
||||
|
||||
-- Check if you beat your previous best
|
||||
@ -1261,7 +1256,30 @@ local function saveTime(player)
|
||||
StatTrack = true
|
||||
end
|
||||
|
||||
saveScores()
|
||||
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()
|
||||
end
|
||||
|
||||
-- DEBUGGING
|
||||
|
Reference in New Issue
Block a user