2022-10-07 01:35:24 +02:00
|
|
|
-- This file handles the storage and related netvars of the leaderboard
|
|
|
|
|
|
|
|
---- Imported functions ----
|
|
|
|
|
|
|
|
-- lb_common.lua
|
|
|
|
local stat_t = lb_stat_t
|
|
|
|
local lbComp = lb_comp
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
local LEADERBOARD_FILE = "leaderboard.txt"
|
|
|
|
|
|
|
|
-- ColdStore are records loaded from lua addons
|
|
|
|
-- this table should never be modified outside of the AddColdStore function
|
|
|
|
local ColdStore = {}
|
|
|
|
|
|
|
|
-- Livestore are new records nad records loaded from leaderboard.txt file
|
|
|
|
local LiveStore = {}
|
|
|
|
|
|
|
|
|
2022-10-07 17:59:27 +02:00
|
|
|
-- GLOBAL
|
|
|
|
-- Returns a list of all maps with records
|
|
|
|
local function MapList()
|
|
|
|
local maps = {}
|
|
|
|
for map in pairs(ColdStore) do
|
|
|
|
maps[map] = true
|
|
|
|
end
|
|
|
|
for map in pairs(LiveStore) do
|
|
|
|
maps[map] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
local maplist = {}
|
|
|
|
for map in pairs(maps) do
|
|
|
|
table.insert(maplist, map)
|
|
|
|
end
|
|
|
|
table.sort(maplist)
|
|
|
|
|
|
|
|
return maplist
|
|
|
|
end
|
|
|
|
rawset(_G, "lb_map_list", MapList)
|
|
|
|
|
2022-10-07 01:35:24 +02:00
|
|
|
-- GLOBAL
|
|
|
|
-- Function for adding records from lua
|
|
|
|
local function AddColdStore(record)
|
|
|
|
ColdStore[record.map] = $ or {}
|
|
|
|
table.insert(ColdStore[record.map], record)
|
|
|
|
end
|
|
|
|
rawset(_G, "lb_add_coldstore_record", AddColdStore)
|
|
|
|
|
|
|
|
|
|
|
|
-- Insert mode separated records from the flat sourceTable into dest
|
|
|
|
local function insertRecords(dest, sourceTable, modeSep)
|
|
|
|
if not sourceTable then return end
|
|
|
|
|
|
|
|
local mode = nil
|
|
|
|
for _, record in ipairs(sourceTable) do
|
|
|
|
mode = record.flags & modeSep
|
|
|
|
dest[mode] = $ or {}
|
|
|
|
table.insert(dest[mode], record)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- GLOBAL
|
|
|
|
-- Construct the leaderboard table of the supplied mapid
|
|
|
|
-- combines the ColdStore and LiveStore records
|
|
|
|
local function GetMapRecords(map, modeSep)
|
|
|
|
local mapRecords = {}
|
|
|
|
|
|
|
|
-- Insert ColdStore records
|
|
|
|
insertRecords(mapRecords, ColdStore[map], modeSep)
|
|
|
|
|
|
|
|
-- Insert LiveStore records
|
|
|
|
insertRecords(mapRecords, LiveStore[map], modeSep)
|
|
|
|
|
|
|
|
-- Sort records
|
|
|
|
for _, records in pairs(mapRecords) do
|
|
|
|
table.sort(records, lbComp)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Remove duplicate entries
|
|
|
|
for _, records in pairs(mapRecords) do
|
|
|
|
local players = {}
|
|
|
|
local i = 1
|
|
|
|
while i <= #records do
|
|
|
|
if players[records[i].name] then
|
|
|
|
table.remove(records, i)
|
|
|
|
else
|
|
|
|
players[records[i].name] = true
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return mapRecords
|
|
|
|
end
|
|
|
|
rawset(_G, "lb_get_map_records", GetMapRecords)
|
|
|
|
|
|
|
|
local function insertOrReplaceRecord(map, score, modeSep)
|
|
|
|
LiveStore[map] = $ or {}
|
|
|
|
|
|
|
|
for i, record in ipairs(LiveStore[map]) do
|
|
|
|
-- Replace the record
|
|
|
|
if record.name == score.name
|
|
|
|
and (record.flags & modeSep) == (score.flags & modeSep) then
|
|
|
|
LiveStore[map][i] = score
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(LiveStore[map], score)
|
|
|
|
|
|
|
|
-- TODO: remove excess records
|
|
|
|
end
|
|
|
|
|
|
|
|
local MSK_SPEED = 0xF0
|
|
|
|
local MSK_WEIGHT = 0xF
|
|
|
|
local function stat_str(stat)
|
|
|
|
if stat then
|
|
|
|
return string.format("%d%d", (stat & MSK_SPEED) >> 4, stat & MSK_WEIGHT)
|
|
|
|
end
|
|
|
|
|
|
|
|
return "0"
|
|
|
|
end
|
|
|
|
|
2022-11-14 22:17:30 +01:00
|
|
|
local function djb2(message)
|
|
|
|
local digest = 5381
|
|
|
|
for c in message:gmatch(".") do
|
|
|
|
digest = (($ << 5) + $) + string.byte(c)
|
|
|
|
end
|
|
|
|
|
|
|
|
return digest
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Produce a checksum by using the maps title, subtitle and zone
|
|
|
|
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
|
|
|
|
|
2022-10-07 01:35:24 +02:00
|
|
|
-- GLOBAL
|
|
|
|
-- Save a record to the LiveStore and write to disk
|
|
|
|
-- SaveRecord will replace the record holders previous record but it will not compare any record times
|
|
|
|
local function SaveRecord(score, map, modeSep)
|
|
|
|
insertOrReplaceRecord(map, score, modeSep)
|
|
|
|
|
|
|
|
print("Saving score")
|
|
|
|
|
|
|
|
if not isserver then return end
|
|
|
|
|
|
|
|
local f = assert(
|
|
|
|
io.open(LEADERBOARD_FILE, "w"),
|
|
|
|
"Failed to open file for writing: "..LEADERBOARD_FILE
|
|
|
|
)
|
|
|
|
|
|
|
|
f:setvbuf("line")
|
|
|
|
|
|
|
|
for mapid, records in pairs(LiveStore) do
|
|
|
|
for _, record in ipairs(records) do
|
2022-11-14 22:17:30 +01:00
|
|
|
-- Insert checksum if missing
|
|
|
|
if (not record.checksum) or record.checksum == "" then
|
|
|
|
record.checksum = mapChecksum(mapid)
|
|
|
|
end
|
|
|
|
|
2022-10-07 01:35:24 +02:00
|
|
|
f:write(
|
|
|
|
mapid, "\t",
|
|
|
|
record.name, "\t",
|
|
|
|
record.skin, "\t",
|
|
|
|
record.color, "\t",
|
|
|
|
record.time, "\t",
|
|
|
|
table.concat(record.splits, " "), "\t",
|
|
|
|
record.flags, "\t",
|
2022-11-14 22:17:30 +01:00
|
|
|
stat_str(record.stat), "\t",
|
|
|
|
record.checksum or "", "\n"
|
2022-10-07 01:35:24 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
rawset(_G, "lb_save_record", SaveRecord)
|
|
|
|
|
|
|
|
local function netvars(net)
|
|
|
|
LiveStore = net($)
|
|
|
|
end
|
|
|
|
|
|
|
|
addHook("NetVars", netvars)
|
|
|
|
|
2022-11-14 22:17:30 +01:00
|
|
|
local function score_t(map, name, skin, color, time, splits, flags, stat, checksum)
|
2022-10-07 01:35:24 +02:00
|
|
|
return {
|
|
|
|
["map"] = map,
|
|
|
|
["name"] = name,
|
|
|
|
["skin"] = skin,
|
|
|
|
["color"] = color,
|
|
|
|
["time"] = time,
|
|
|
|
["splits"] = splits,
|
|
|
|
["flags"] = flags,
|
2022-11-14 22:17:30 +01:00
|
|
|
["stat"] = stat,
|
|
|
|
["checksum"] = checksum
|
2022-10-07 01:35:24 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local function parseScore(str)
|
|
|
|
-- Leaderboard is stored in the following tab separated format
|
|
|
|
-- mapnum, name, skin, color, time, splits, flags, stat
|
|
|
|
local t = {}
|
|
|
|
for word in (str.."\t"):gmatch("(.-)\t") do
|
|
|
|
table.insert(t, word)
|
|
|
|
end
|
|
|
|
|
|
|
|
local splits = {}
|
|
|
|
if t[6] != nil then
|
|
|
|
for str in t[6]:gmatch("([^ ]+)") do
|
|
|
|
table.insert(splits, tonumber(str))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local flags = 0
|
|
|
|
if t[7] != nil then
|
|
|
|
flags = tonumber(t[7])
|
|
|
|
end
|
|
|
|
|
|
|
|
local stats = nil
|
|
|
|
if t[8] != nil then
|
|
|
|
if #t[8] >= 2 then
|
|
|
|
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
|
|
|
|
|
2022-11-14 22:17:30 +01:00
|
|
|
local checksum = t[9]
|
|
|
|
|
2022-10-07 01:35:24 +02:00
|
|
|
return score_t(
|
|
|
|
tonumber(t[1]), -- Map
|
|
|
|
t[2], -- Name
|
|
|
|
t[3], -- Skin
|
|
|
|
t[4], -- Color
|
|
|
|
tonumber(t[5]), -- Time
|
|
|
|
splits,
|
|
|
|
flags,
|
2022-11-14 22:17:30 +01:00
|
|
|
stats,
|
|
|
|
checksum
|
2022-10-07 01:35:24 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
rawset(_G, "lb_parse_score", parseScore)
|
|
|
|
|
|
|
|
-- Load the livestore
|
2022-11-14 22:17:30 +01:00
|
|
|
if isserver then
|
|
|
|
local f = assert(
|
|
|
|
io.open(LEADERBOARD_FILE, "r"),
|
|
|
|
"Failed to open file: "..LEADERBOARD_FILE
|
|
|
|
)
|
2022-10-07 01:35:24 +02:00
|
|
|
|
2022-11-14 22:17:30 +01:00
|
|
|
for l in f:lines() do
|
|
|
|
local score = parseScore(l)
|
|
|
|
LiveStore[score.map] = $ or {}
|
|
|
|
table.insert(LiveStore[score.map], score)
|
2022-10-07 01:35:24 +02:00
|
|
|
end
|
2022-11-14 22:17:30 +01:00
|
|
|
|
|
|
|
f:close()
|
2022-10-07 01:35:24 +02:00
|
|
|
end
|