srb2k-leaderboard/leaderboard.lua

658 lines
13 KiB
Lua
Raw Permalink Normal View History

2022-03-28 12:48:16 +02:00
-- Leaderboards written by Not
local FILENAME = "leaderboard.txt"
local lb = {}
local timeFinished = 0
2022-03-28 13:12:43 +02:00
local disable = false
2022-03-28 12:48:16 +02:00
local prevLap = 0
local splits = {}
2022-03-28 13:08:08 +02:00
local PATCH = nil
2022-03-28 13:11:00 +02:00
local help = true
2022-03-28 13:08:08 +02:00
2022-03-28 13:13:42 +02:00
-- Retry / changelevel map
local nextMap = nil
2022-03-28 13:08:08 +02:00
local Flags = 0
-- SPB flags with the least significance first
local F_SPBATK = 0x1
local F_SPBJUS = 0x2
local F_SPBBIG = 0x4
local F_SPBEXP = 0x8
local clearcheats = false
2022-03-28 13:13:21 +02:00
local START_TIME = 6 * TICRATE + (3 * TICRATE / 4)
local AFK_TIMEOUT = TICRATE * 5
local AFK_BALANCE = TICRATE * 60
local PREVENT_JOIN_TIME = START_TIME + TICRATE * 5
2022-03-28 13:08:08 +02:00
-- patch caching function
local cachePatches
local function lbID(map, flags)
local id = tostring(map)
if flags & F_SPBATK then
id = id + "S"
end
return id
end
local function score_t(map, name, skin, color, time, splits, flags)
return {
["map"] = map,
["name"] = name,
["skin"] = skin,
["color"] = color,
["time"] = time,
["splits"] = splits,
["flags"] = flags
}
end
2022-03-28 12:48:16 +02:00
-- Read the leaderboard
local f = io.open(FILENAME, "r")
if f then
for l in f:lines() do
2022-03-28 13:08:08 +02:00
-- Leaderboard is stored in the following tab separated format
-- name, skin, color, time, splits, flags
2022-03-28 12:48:16 +02:00
local t = {}
for word in (l+"\t"):gmatch("(.-)\t") do
table.insert(t, word)
end
2022-03-28 13:08:08 +02:00
local flags = 0
if t[7] != nil then
flags = tonumber(t[7])
end
local lbt = lb[lbID(t[1], flags)]
if lbt == nil then
lbt = {}
2022-03-28 12:48:16 +02:00
end
2022-03-28 13:08:08 +02:00
2022-03-28 12:48:16 +02:00
local spl = {}
if t[6] != nil then
for str in t[6]:gmatch("([^ ]+)") do
table.insert(spl, tonumber(str))
end
end
2022-03-28 13:08:08 +02:00
table.insert(
lbt,
score_t(
t[1],
t[2],
t[3],
t[4],
tonumber(t[5]),
spl,
flags
)
)
lb[lbID(t[1], flags)] = lbt
2022-03-28 12:48:16 +02:00
end
f:close()
else
print("Failed to open file: ", FILENAME)
end
2022-03-28 13:13:21 +02:00
local function allowJoin(v)
local y
if v then
y = "yes"
2022-03-28 13:13:42 +02:00
hud.enable("freeplay")
2022-03-28 13:13:21 +02:00
else
y = "no"
2022-03-28 13:13:42 +02:00
hud.disable("freeplay")
2022-03-28 13:13:21 +02:00
end
COM_BufInsertText(server, "allowteamchange " + y)
end
2022-03-28 13:11:21 +02:00
local function ingame()
local n = 0
2022-03-28 12:48:16 +02:00
for p in players.iterate do
if p.valid and not p.spectator then
2022-03-28 13:11:21 +02:00
n = $ + 1
2022-03-28 12:48:16 +02:00
end
end
2022-03-28 13:11:21 +02:00
return n
end
2022-03-28 12:48:16 +02:00
2022-03-28 13:11:21 +02:00
local function initLeaderboard(player)
2022-03-28 13:13:21 +02:00
if disable and leveltime < START_TIME then
2022-03-28 13:12:43 +02:00
disable = ingame() > 1
else
disable = disable or ingame() > 1
end
2022-03-28 13:13:21 +02:00
player.afkTimeout = leveltime
2022-03-28 12:48:16 +02:00
end
addHook("PlayerSpawn", initLeaderboard)
2022-03-28 13:10:13 +02:00
local function doyoudare(player)
2022-03-28 13:12:43 +02:00
if ingame() > 1 or player.spectator then
2022-03-28 13:08:08 +02:00
CONS_Printf(player, "How dare you")
2022-03-28 13:10:13 +02:00
return false
2022-03-28 12:48:16 +02:00
end
2022-03-28 13:10:13 +02:00
return true
end
2022-03-28 12:48:16 +02:00
2022-03-28 13:10:13 +02:00
local function retry(player, ...)
if doyoudare(player) then
2022-03-28 13:13:00 +02:00
-- Prevents bind crash
2022-03-28 13:13:42 +02:00
if leveltime < 20 then
2022-03-28 13:13:00 +02:00
return
end
2022-03-28 13:13:42 +02:00
nextMap = G_BuildMapName(gamemap)
2022-03-28 13:10:13 +02:00
end
2022-03-28 12:48:16 +02:00
end
COM_AddCommand("retry", retry)
2022-03-28 13:08:08 +02:00
local function exitlevel(player, ...)
2022-03-28 13:10:13 +02:00
if doyoudare(player) then
G_ExitLevel()
2022-03-28 13:08:08 +02:00
end
end
COM_AddCommand("exit", exitlevel)
2022-03-28 13:10:13 +02:00
local function mapNotExists(player, map)
CONS_Printf(player, string.format("Map doesn't exist: %s", map:upper()))
end
local ALPH = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local function changelevel(player, ...)
if not doyoudare(player) then
return
end
2022-03-28 13:13:42 +02:00
if leveltime < 20 then
2022-03-28 13:13:00 +02:00
return
end
2022-03-28 13:10:13 +02:00
local map = ...
2022-03-28 13:11:00 +02:00
if map == nil then
CONS_Printf(player, "Usage: changelevel MAPXX")
return
end
2022-03-28 13:10:13 +02:00
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
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 = ALPH:find(p) - 1
2022-03-28 13:10:43 +02:00
q = (tonumber(q) or ALPH:find(q) + 9)
2022-03-28 13:10:13 +02:00
mapnum = 36 * p + q + 100
end
if mapheaderinfo[mapnum] == nil then
mapNotExists(player, map)
return
end
2022-03-28 13:13:42 +02:00
nextMap = G_BuildMapName(mapnum)
2022-03-28 13:10:13 +02:00
end
COM_AddCommand("changelevel", changelevel)
2022-03-28 13:09:23 +02:00
local function clearcheats(player)
if not player.spectator then
2022-03-28 13:08:08 +02:00
clearcheats = true
CONS_Printf(player, "SPB Attack cheats will be cleared on next round")
end
2022-03-28 13:09:23 +02:00
end
COM_AddCommand("spba_clearcheats", clearcheats)
2022-03-28 12:48:16 +02:00
--DEBUGGING
--local function printTable(tb)
-- for k, v in pairs(tb) do
-- for i = 1, #v do
-- print("TABLE: " + k, tb[k])
-- if v[i] != nil then
2022-03-28 13:08:08 +02:00
-- print(
-- v[i]["name"],
-- v[i]["skin"],
-- v[i]["color"],
-- v[i]["time"],
-- table.concat(v[i]["splits"]),
-- v[i]["flags"],
-- ","
-- )
--
2022-03-28 12:48:16 +02:00
-- end
-- end
-- end
--end
addHook("MapLoad", function()
timeFinished = 0
splits = {}
prevLap = 0
2022-03-28 13:13:21 +02:00
allowJoin(true)
2022-03-28 13:08:08 +02:00
--printTable(lb)
2022-03-28 12:48:16 +02:00
end
)
local function ticsToTime(tics)
return string.format(
"%d:%02d:%02d",
G_TicsToMinutes(tics),
G_TicsToSeconds(tics),
G_TicsToCentiseconds(tics)
)
end
2022-03-28 13:08:08 +02:00
-- Item patches have the amazing property of being displaced 12x 13y pixels
local iXoffset = 13 * FRACUNIT
local iYoffset = 12 * FRACUNIT
local function drawitem(v, x, y, scale, itempatch, vflags)
v.drawScaled(
x * FRACUNIT - FixedMul(iXoffset, scale),
y * FRACUNIT - FixedMul(iYoffset, scale),
scale,
itempatch,
vflags
)
end
2022-03-28 13:09:52 +02:00
local function marquee(text, maxwidth)
if #text <= maxwidth then
return text
end
local shift = 16
local pos = ((leveltime / 16) % (#text - maxwidth + shift * 2)) + 1 - shift
pos = min(max(pos, 1), #text - maxwidth + 1)
return text:sub(pos, pos + maxwidth - 1)
end
2022-03-28 13:04:34 +02:00
local bodium = {V_YELLOWMAP, V_GRAYMAP, V_BROWNMAP, 0}
2022-03-28 12:48:16 +02:00
local splitColor = {[true]=V_SKYMAP, [false]=V_REDMAP}
local splitSymbol = {[true]="-", [false]="+"}
local showSplit = 0
2022-03-28 13:12:16 +02:00
local VFLAGS = V_SNAPTOLEFT
2022-03-28 13:04:34 +02:00
local function drawScoreboard(v, player)
2022-03-28 12:48:16 +02:00
if disable then return end
2022-03-28 13:04:34 +02:00
if player != displayplayers[0] then return end
2022-03-28 13:08:08 +02:00
cachePatches(v)
2022-03-28 13:04:34 +02:00
2022-03-28 13:08:08 +02:00
local m = lb[lbID(gamemap, Flags)]
2022-03-28 12:48:16 +02:00
if m == nil then
return
end
2022-03-28 13:08:08 +02:00
for i, score in ipairs(m) do
local name = score["name"]
local skin = skins[score["skin"]]
2022-03-28 12:48:16 +02:00
if skin == nil then
skin = skins["sonic"]
end
2022-03-28 13:04:34 +02:00
local skinPatch = PATCH["FACERANK"][skin.name]
2022-03-28 12:48:16 +02:00
2022-03-28 13:08:08 +02:00
-- | OFFSET | + | PADDING | * |INDEX|
2022-03-28 13:04:34 +02:00
local h = ((200 / 4) + 4) + (skinPatch.height + 4) * (i - 1)
2022-03-28 13:08:08 +02:00
2022-03-28 13:12:16 +02:00
v.draw(4, h, skinPatch, V_HUDTRANS | VFLAGS, v.getColormap("sonic", score["color"]))
2022-03-28 13:04:34 +02:00
if player.name == name then
2022-03-28 13:12:16 +02:00
v.draw(4, h, PATCH["CHILI"][(leveltime / 4) % 8], V_HUDTRANS | VFLAGS)
2022-03-28 13:04:34 +02:00
end
2022-03-28 13:08:08 +02:00
-- SPB
if score["flags"] & F_SPBATK then
local scale = FRACUNIT / 4
drawitem(
v,
4 - 2,
h - 2,
scale,
PATCH["SPB"],
2022-03-28 13:12:16 +02:00
V_HUDTRANS | VFLAGS
2022-03-28 13:08:08 +02:00
)
if score["flags"] & F_SPBEXP then
drawitem(
v,
skinPatch.width,
h - 2,
scale,
PATCH["INV"][(leveltime / 4) % 6],
2022-03-28 13:12:16 +02:00
V_HUDTRANS | VFLAGS
2022-03-28 13:08:08 +02:00
)
end
if score["flags"] & F_SPBBIG then
drawitem(
v,
4 - 2,
h + skinPatch.height - 4,
scale,
PATCH["BIG"],
2022-03-28 13:12:16 +02:00
V_HUDTRANS | VFLAGS
2022-03-28 13:08:08 +02:00
)
end
if score["flags"] & F_SPBJUS then
drawitem(
v,
skinPatch.width,
h + skinPatch.height - 4,
scale,
PATCH["HYUD"],
2022-03-28 13:12:16 +02:00
V_HUDTRANS | VFLAGS
2022-03-28 13:08:08 +02:00
)
end
end
2022-03-28 13:04:34 +02:00
-- Shorten long names
local stralign = "left"
local MAXWIDTH = 70
local px = 6
local py = 0
if v.stringWidth(name) > MAXWIDTH then
stralign = "thin"
py = -1
if v.stringWidth(name, 0, "thin") > MAXWIDTH then
stralign = "small"
py = 2
if v.stringWidth(name, 0, "small") > MAXWIDTH then
2022-03-28 13:09:52 +02:00
name = marquee(name, 15)
2022-03-28 13:04:34 +02:00
end
end
end
2022-03-28 13:12:16 +02:00
v.drawString(
px + skinPatch.width,
h + py,
name,
V_HUDTRANSHALF | V_ALLOWLOWERCASE | VFLAGS,
stralign
)
2022-03-28 12:48:16 +02:00
-- Draw splits
2022-03-28 13:08:08 +02:00
if showSplit > 0 and score["splits"][prevLap] != nil then
local split = splits[prevLap] - score["splits"][prevLap]
2022-03-28 13:12:16 +02:00
v.drawString(
px + skinPatch.width,
h + 8,
splitSymbol[split < 0] + ticsToTime(abs(split)),
V_HUDTRANSHALF | splitColor[split < 0] | VFLAGS
)
2022-03-28 12:48:16 +02:00
else
2022-03-28 13:12:16 +02:00
v.drawString(
px + skinPatch.width,
h + 8,
ticsToTime(score["time"]),
V_HUDTRANSHALF | bodium[min(i, 4)] | VFLAGS
)
2022-03-28 12:48:16 +02:00
end
end
end
hud.add(drawScoreboard, "game")
2022-03-28 13:08:08 +02:00
function cachePatches(v)
if PATCH == nil then
PATCH = {}
PATCH["CHILI"] = {}
for i = 1, 8 do
PATCH["CHILI"][i-1] = v.cachePatch("K_CHILI" + i)
end
PATCH["FACERANK"] = {}
for skin in skins.iterate do
PATCH["FACERANK"][skin.name] = v.cachePatch(skin.facerank)
end
PATCH["SPB"] = v.cachePatch("K_ISSPB")
PATCH["INV"] = {}
for i = 1, 6 do
PATCH["INV"][i - 1] = v.cachePatch("K_ISINV" + i)
end
PATCH["BIG"] = v.cachePatch("K_ISGROW")
PATCH["HYUD"] = v.cachePatch("K_ISHYUD")
end
end
-- True if a is better than b
2022-03-28 12:48:16 +02:00
local function lbComp(a, b)
2022-03-28 13:08:08 +02:00
-- Calculates the difficulty, harder has higher priority
-- if s is positive then a is harder
-- if s is negative then b is harder
-- if s is 0 then compare time
local s = (a["flags"] & (F_SPBEXP | F_SPBBIG)) - (b["flags"] & (F_SPBEXP | F_SPBBIG))
return s > 0 or not(s < 0 or a["time"] > b["time"])
2022-03-28 12:48:16 +02:00
end
local function saveTime(player)
2022-03-28 13:08:08 +02:00
local m = lb[lbID(gamemap, Flags)]
2022-03-28 12:48:16 +02:00
if m == nil then
m = {}
end
2022-03-28 13:08:08 +02:00
local newscore = score_t(
gamemap,
player.name,
player.mo.skin,
2022-03-28 13:09:23 +02:00
player.skincolor,
2022-03-28 13:08:08 +02:00
timeFinished,
splits,
Flags
)
2022-03-28 12:48:16 +02:00
2022-03-28 13:08:08 +02:00
-- Check if you beat your previous best
2022-03-28 12:48:16 +02:00
for i = 1, #m do
2022-03-28 13:08:08 +02:00
if m[i]["name"] == player.name then
if lbComp(newscore, m[i]) then
2022-03-28 12:48:16 +02:00
table.remove(m, i)
S_StartSound(nil, 130)
break
else
-- You suck lol
S_StartSound(nil, 201)
return
end
end
end
print("Saving score")
2022-03-28 13:08:08 +02:00
table.insert(
m,
newscore
)
2022-03-28 12:48:16 +02:00
table.sort(m, lbComp)
while #m > 5 do
table.remove(m)
end
2022-03-28 13:08:08 +02:00
lb[lbID(gamemap, Flags)] = m
2022-03-28 12:48:16 +02:00
local f = assert(io.open(FILENAME, "w"))
if f == nil then
print("Failed to open file for writing: " + FILENAME)
return
end
for k, v in pairs(lb) do
for i = 1, #v do
local s = v[i]
2022-03-28 13:08:08 +02:00
f:write(
s["map"], "\t",
s["name"], "\t",
s["skin"], "\t",
s["color"], "\t",
s["time"], "\t",
table.concat(s["splits"], " "), "\t",
s["flags"], "\n"
)
2022-03-28 12:48:16 +02:00
end
end
f:close()
end
-- DEBUGGING
--local function saveLeaderboard(player, ...)
-- timeFinished = player.realtime
-- splits = {1000, 2000, 3000}
-- saveTime(player)
--end
--COM_AddCommand("save", saveLeaderboard)
local function regLap(player)
if player.laps > prevLap and timeFinished == 0 then
prevLap = player.laps
table.insert(splits, player.realtime)
showSplit = 5 * TICRATE
end
end
2022-03-28 13:13:21 +02:00
local function getGamer()
for p in players.iterate do
if p.valid and not p.spectator then
return p
end
end
end
2022-03-28 13:08:08 +02:00
local function think()
2022-03-28 13:13:42 +02:00
if nextMap then
COM_BufInsertText(server, "map " + nextMap)
nextMap = nil
end
2022-03-28 13:13:21 +02:00
2022-03-28 12:48:16 +02:00
if disable then
2022-03-28 13:13:21 +02:00
if ingame() > 1 then
for p in players.iterate do
if p.valid and not p.spectator and not p.exiting then
if p.cmd.buttons then
p.afkTimeout = leveltime
end
if p.afkTimeout + AFK_BALANCE < leveltime then
p.spectator = true
chatprint("\x89" + p.name + " was moved to the other team for game balance", true)
end
end
end
else
for p in players.iterate do
if p.valid and not p.spectator then
p.afkTimeout = leveltime
end
end
end
2022-03-28 13:11:21 +02:00
help = true
2022-03-28 12:48:16 +02:00
return
end
if showSplit > 0 then
showSplit = showSplit - 1
end
2022-03-28 13:13:21 +02:00
local p = getGamer()
if leveltime < START_TIME then
2022-03-28 13:11:00 +02:00
-- Help message
2022-03-28 13:13:21 +02:00
if leveltime == START_TIME - TICRATE * 3 then
2022-03-28 13:11:21 +02:00
if ingame() == 1 then
if help then
help = false
chatprint("\x89Leaderboard Commands:\nretry exit findmap changelevel spba_clearcheats", true)
end
else
help = true
end
2022-03-28 13:11:00 +02:00
end
2022-03-28 13:11:51 +02:00
-- Autospec
if leveltime == 1 then
2022-03-28 13:13:21 +02:00
if p then
for s in players.iterate do
if s.valid and s.spectator then
COM_BufInsertText(s, string.format("view \"%d\"", #p))
2022-03-28 13:11:51 +02:00
end
end
end
end
-- Gamemode flags
2022-03-28 13:08:08 +02:00
Flags = $ & !(F_SPBATK | F_SPBEXP | F_SPBBIG | F_SPBJUS)
2022-03-28 13:13:21 +02:00
if leveltime > START_TIME - (3 * TICRATE) / 2 and server.SPBArunning then
2022-03-28 13:08:08 +02:00
Flags = $ | F_SPBATK
if server.SPBAexpert then
Flags = $ | F_SPBEXP
end
if clearcheats then
clearcheats = false
for p in players.iterate do
p.SPBAKARTBIG = false
p.SPBAjustice = false
p.SPBAshutup = false
end
end
for p in players.iterate do
2022-03-28 13:09:23 +02:00
if not p.spectator then
if p.SPBAKARTBIG then
Flags = $ | F_SPBBIG
end
if p.SPBAjustice then
Flags = $ | F_SPBJUS
end
2022-03-28 13:08:08 +02:00
end
end
end
end
2022-03-28 13:13:21 +02:00
local cv_teamchange = CV_FindVar("allowteamchange")
if p then
if p.cmd.buttons then
p.afkTimeout = leveltime
end
if leveltime > PREVENT_JOIN_TIME and p.afkTimeout + AFK_TIMEOUT > leveltime then
if cv_teamchange.value then
allowJoin(false)
end
elseif p.afkTimeout + AFK_TIMEOUT < leveltime then
if not cv_teamchange.value then
allowJoin(true)
end
end
2022-03-28 12:48:16 +02:00
if p.laps >= mapheaderinfo[gamemap].numlaps and timeFinished == 0 then
timeFinished = p.realtime
saveTime(p)
end
regLap(p)
2022-03-28 13:13:21 +02:00
elseif cv_teamchange.value == 0 then
allowJoin(true)
2022-03-28 12:48:16 +02:00
end
end
2022-03-28 13:08:08 +02:00
addHook("ThinkFrame", think)
2022-03-28 12:48:16 +02:00
2022-03-28 13:13:42 +02:00
local function interThink()
if nextMap then
COM_BufInsertText(server, "map " + nextMap)
nextMap = nil
end
2022-03-28 13:13:21 +02:00
if not CV_FindVar("allowteamchange").value then
allowJoin(true)
end
end
2022-03-28 13:13:42 +02:00
addHook("IntermissionThinker", interThink)
2022-03-28 13:13:21 +02:00
2022-03-28 12:48:16 +02:00
local function netvars(net)
lb = net($)
end
addHook("NetVars", netvars)