forked from Not/srb2k-leaderboard
allow non netvar stored, cold record loading from lua
This commit is contained in:
218
lb_store.lua
Normal file
218
lb_store.lua
Normal file
@ -0,0 +1,218 @@
|
||||
-- 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 = {}
|
||||
|
||||
|
||||
-- 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
|
||||
|
||||
-- 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
|
||||
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), "\n"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
f:close()
|
||||
end
|
||||
rawset(_G, "lb_save_record", SaveRecord)
|
||||
|
||||
local function netvars(net)
|
||||
LiveStore = net($)
|
||||
end
|
||||
|
||||
addHook("NetVars", netvars)
|
||||
|
||||
local function score_t(map, name, skin, color, time, splits, flags, stat)
|
||||
return {
|
||||
["map"] = map,
|
||||
["name"] = name,
|
||||
["skin"] = skin,
|
||||
["color"] = color,
|
||||
["time"] = time,
|
||||
["splits"] = splits,
|
||||
["flags"] = flags,
|
||||
["stat"] = stat
|
||||
}
|
||||
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
|
||||
|
||||
return score_t(
|
||||
tonumber(t[1]), -- Map
|
||||
t[2], -- Name
|
||||
t[3], -- Skin
|
||||
t[4], -- Color
|
||||
tonumber(t[5]), -- Time
|
||||
splits,
|
||||
flags,
|
||||
stats
|
||||
)
|
||||
end
|
||||
rawset(_G, "lb_parse_score", parseScore)
|
||||
|
||||
-- Load the livestore
|
||||
do
|
||||
if isserver then
|
||||
local f = assert(
|
||||
io.open(LEADERBOARD_FILE, "r"),
|
||||
"Failed to open file: "..LEADERBOARD_FILE
|
||||
)
|
||||
|
||||
for l in f:lines() do
|
||||
local score = parseScore(l)
|
||||
print(score.name)
|
||||
LiveStore[score.map] = $ or {}
|
||||
table.insert(LiveStore[score.map], score)
|
||||
end
|
||||
|
||||
f:close()
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user