map identity by checksum

This commit is contained in:
Not
2022-11-18 01:35:59 +01:00
parent 68e44e534b
commit cf74af8945
5 changed files with 91 additions and 69 deletions

View File

@ -5,6 +5,7 @@
-- lb_common.lua
local stat_t = lb_stat_t
local lbComp = lb_comp
local mapChecksum = lb_map_checksum
----------------------------
@ -24,18 +25,26 @@ local parseScore
-- Returns a list of all maps with records
local function MapList()
local maps = {}
for map in pairs(ColdStore) do
maps[map] = true
for mapid, checksums in pairs(ColdStore) do
maps[mapid] = $ or {}
for checksum in pairs(checksums) do
maps[mapid][checksum] = true
end
end
for map in pairs(LiveStore) do
maps[map] = true
for mapid, checksums in pairs(LiveStore) do
maps[mapid] = $ or {}
for checksum in pairs(checksums) do
maps[mapid][checksum] = true
end
end
local maplist = {}
for map in pairs(maps) do
table.insert(maplist, map)
for mapid, checksums in pairs(maps) do
for checksum in pairs(checksums) do
table.insert(maplist, {["id"] = mapid, ["checksum"] = checksum})
end
end
table.sort(maplist)
table.sort(maplist, function(a, b) return a.id < b.id end)
return maplist
end
@ -45,7 +54,9 @@ rawset(_G, "lb_map_list", MapList)
-- Function for adding a single record from lua
local function AddColdStore(record)
ColdStore[record.map] = $ or {}
table.insert(ColdStore[record.map], record)
ColdStore[record.map][record.checksum] = $ or {}
table.insert(ColdStore[record.map][record.checksum], record)
end
rawset(_G, "lb_add_coldstore_record", AddColdStore)
@ -57,11 +68,12 @@ end
rawset(_G, "lb_add_coldstore_record_string", AddColdStoreString)
-- Insert mode separated records from the flat sourceTable into dest
local function insertRecords(dest, sourceTable, modeSep)
local function insertRecords(dest, sourceTable, checksum, modeSep)
if not sourceTable then return end
if not sourceTable[checksum] then return end
local mode = nil
for _, record in ipairs(sourceTable) do
for _, record in ipairs(sourceTable[checksum]) do
mode = record.flags & modeSep
dest[mode] = $ or {}
table.insert(dest[mode], record)
@ -71,14 +83,14 @@ end
-- GLOBAL
-- Construct the leaderboard table of the supplied mapid
-- combines the ColdStore and LiveStore records
local function GetMapRecords(map, modeSep)
local function GetMapRecords(map, checksum, modeSep)
local mapRecords = {}
-- Insert ColdStore records
insertRecords(mapRecords, ColdStore[map], modeSep)
insertRecords(mapRecords, ColdStore[map], checksum, modeSep)
-- Insert LiveStore records
insertRecords(mapRecords, LiveStore[map], modeSep)
insertRecords(mapRecords, LiveStore[map], checksum, modeSep)
-- Sort records
for _, records in pairs(mapRecords) do
@ -104,18 +116,20 @@ end
rawset(_G, "lb_get_map_records", GetMapRecords)
local function insertOrReplaceRecord(map, score, modeSep)
local checksum = mapChecksum(map)
LiveStore[map] = $ or {}
LiveStore[map][checksum] = $ or {}
for i, record in ipairs(LiveStore[map]) do
for i, record in ipairs(LiveStore[map][checksum]) do
-- Replace the record
if record.name == score.name
and (record.flags & modeSep) == (score.flags & modeSep) then
LiveStore[map][i] = score
LiveStore[map][checksum][i] = score
return
end
end
table.insert(LiveStore[map], score)
table.insert(LiveStore[map][checksum], score)
-- TODO: remove excess records
end
@ -130,26 +144,6 @@ local function stat_str(stat)
return "0"
end
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
-- 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
@ -167,24 +161,26 @@ local function SaveRecord(score, map, modeSep)
f:setvbuf("line")
for mapid, records in pairs(LiveStore) do
for _, record in ipairs(records) do
-- Insert checksum if missing
if (not record.checksum) or record.checksum == "" then
record.checksum = mapChecksum(mapid)
end
for mapid, checksums in pairs(LiveStore) do
for checksum, records in pairs(checksums) do
for _, record in ipairs(records) do
-- Insert checksum if missing
if (not record.checksum) or record.checksum == "" then
record.checksum = mapChecksum(mapid)
end
f:write(
mapid, "\t",
record.name, "\t",
record.skin, "\t",
record.color, "\t",
record.time, "\t",
table.concat(record.splits, " "), "\t",
record.flags, "\t",
stat_str(record.stat), "\t",
record.checksum or "", "\n"
)
f:write(
mapid, "\t",
record.name, "\t",
record.skin, "\t",
record.color, "\t",
record.time, "\t",
table.concat(record.splits, " "), "\t",
record.flags, "\t",
stat_str(record.stat), "\t",
record.checksum or "", "\n"
)
end
end
end
@ -267,7 +263,8 @@ if isserver then
for l in f:lines() do
local score = parseScore(l)
LiveStore[score.map] = $ or {}
table.insert(LiveStore[score.map], score)
LiveStore[score.map][score.checksum] = $ or {}
table.insert(LiveStore[score.map][score.checksum], score)
end
f:close()