Skip to content

Instantly share code, notes, and snippets.

@x4fx77x4f
Last active February 12, 2022 23:53
Show Gist options
  • Save x4fx77x4f/f1f9e51fb720e47033009862387c8ab9 to your computer and use it in GitHub Desktop.
Save x4fx77x4f/f1f9e51fb720e47033009862387c8ab9 to your computer and use it in GitHub Desktop.
if CLIENT and player() ~= owner() then
return
end
local function test(desc, cond, err)
print((cond and "pass" or "FAIL")..(desc and ": "..desc or ""))
if not cond and err then print(err) end
end
local function test_eq(desc, a, b)
if a == b then
print("pass: "..desc)
else
print(string.format("FAIL: %s: expected %q, got %q", desc, tostring(b), tostring(a)))
end
end
print("LOOK AT THE DOOR NOW")
timer.simple(2, function()
local me = player()
local this = me:getEyeTrace().Entity
print("testing...")
if SERVER then
test_eq("darkrp.doorIndexToEnt", darkrp.doorIndexToEnt(0), nil)
test_eq("darkrp.doorIndexToEnt", darkrp.doorIndexToEnt(2282), this)
test_eq("darkrp.jailPosCount", darkrp.jailPosCount(), 0)
test("darkrp.payPlayer", pcall(darkrp.payPlayer, me, me, 50))
test("Player:keysUnOwnAll", pcall(me.keysUnOwnAll, me))
test_eq("Player:teamBanTimeLeft", me:teamBanTimeLeft(), nil)
else
test("darkrp.openF1Menu", pcall(darkrp.openF1Menu))
test("darkrp.openF4Menu", pcall(darkrp.openF4Menu))
test("darkrp.openKeysMenu", pcall(darkrp.openKeysMenu))
test("darkrp.openPocketMenu", pcall(darkrp.openPocketMenu))
test("darkrp.toggleF4Menu", pcall(darkrp.toggleF4Menu))
test_eq("Player:isInRoom", me:isInRoom(me), true)
end
test_eq("darkrp.formatMoney", darkrp.formatMoney(1337), "$1,337")
test("darkrp.getAvailableVehicles", pcall(darkrp.getAvailableVehicles))
test("darkrp.getCategories", pcall(darkrp.getCategories))
test("darkrp.getFoodItems", pcall(darkrp.getFoodItems))
test_eq("darkrp.getLaws", darkrp.getLaws()[1], "Do not attack other citizens except in self-defence.")
test_eq("Player:canAfford", me:canAfford(1000), true)
test_eq("Player:canKeysLock", me:canKeysLock(this), nil)
test_eq("Player:canKeysUnlock", me:canKeysUnlock(this), nil)
print("Ensure this equals your money (excluding cost of door): "..me:getDarkRPVar('money'))
test_eq("Player:getJobTable", me:getJobTable().name, "Medic")
test_eq("Player:getPocketItems", me:getPocketItems()[1].model, "models/weapons/w_pist_deagle.mdl")
test_eq("Player:getWantedReason", me:getWantedReason(), nil)
test_eq("Player:hasDarkRPPrivilege", me:hasDarkRPPrivilege("AdminChat"), false) -- differs between client and server??
test_eq("Player:isArrested", me:isArrested(), nil)
test_eq("Player:isChief", me:isChief(), nil)
test_eq("Player:isCook", me:isCook(), nil)
test_eq("Player:isCP", me:isCP(), false)
test_eq("Player:isHitman", me:isHitman(), nil)
test_eq("Player:isMayor", me:isMayor(), nil)
test_eq("Player:isMedic", me:isMedic(), true)
test_eq("Player:isWanted", me:isWanted(), nil)
print("LOOK AT THE CASH NOW")
timer.simple(2, function()
local this2 = me:getEyeTrace().Entity
if SERVER then
test_eq("Entity:doorIndex", this:doorIndex(), 2282)
test_eq("Entity:isLocked", this:isLocked(), false)
end
test_eq("Entity:getDoorOwner", this:getDoorOwner(), me)
test_eq("Entity:getKeysTitle", this:getKeysTitle(), nil)
test_eq("Entity:isDoor", this:isDoor(), true)
test_eq("Entity:isMoneyBag", this2:isMoneyBag(), true)
test_eq("Entity:getAmount", this2:getAmount(), 50)
print("LOOK AT THE CHEQUE NOW")
timer.simple(2, function()
this2 = me:getEyeTrace().Entity
test_eq("Entity:isMoneyBag", this2:isMoneyBag(), false)
test_eq("Entity:getAmount", this2:getAmount(), 100)
print("done.")
end)
end)
end)
assert(darkrp)
local function func(desc)
hook.add(desc, '', function(...)
print((SERVER and "server" or "client")..": "..desc..": ")
printTable({...})
return true
end)
end
func("addLaw")
func("removeLaw")
func("resetLaws")
func("lockdownEnded")
func("lockdownStarted")
func("moneyPrinterCatchFire")
func("moneyPrinterPrinted")
func("moneyPrinterPrintMoney")
func("lockpickStarted")
func("onLockpickCompleted")
print("OK")
--@shared
printTable(darkrp.getCustomShipments())
print("LOOK AT THE SHIPMENT NOW")
timer.simple(1, function()
local this = player():getEyeTrace().Entity
print("Entity:getAmount", this:getAmount())
print("Entity:getCount", this:getCount())
print("Entity:getShipmentContentsIndex", this:getShipmentContentsIndex())
print("Entity:getShipmentContents", this:getShipmentContents())
end)
--@server
assert(not pcall(darkrp.requestMoney, nil, ""))
assert(not pcall(darkrp.requestMoney, entity(0), 100))
assert(not pcall(darkrp.requestMoney, owner(), 100, entity(0)))
darkrp.requestMoney(owner(), 100)
--@server
local me = owner()
assert(not pcall(me.requestMoney, me, nil, ""))
assert(not pcall(me.requestMoney, me, nil, 100, ""))
assert(not pcall(me.requestMoney, me, nil, 100, nil, ""))
assert(not pcall(me.requestMoney, me, nil, 100, nil, nil, ""))
assert(not pcall(me.requestMoney, me, owner(), 100, entity(0)))
local function dewit()
print("about to do it", darkrp.moneyRequestsLeft(), darkrp.canMakeMoneyRequest())
--me:requestMoney("foo", 100)
me:requestMoney(nil, 100, function(...)
print("callbackSuccess", ...)
end, function(...)
print("callbackFailure", ...)
end)
end
dewit()
--@shared
if SERVER then
hook.add('starfallUsed', '', function(activator, used)
if activator ~= owner() then return end
print("doing it")
activator:requestMoney("foo", 100)
net.start('')
net.writeFloat(timer.curtime())
net.writeFloat(darkrp.moneyRequestsLeft())
net.send(owner())
end)
timer.create('', 0.25, 0, function()
net.start('')
net.writeFloat(timer.curtime())
net.writeFloat(darkrp.moneyRequestsLeft())
net.send(owner())
end)
return
end
local ymin = 0
local ymax = 1
local graph = {}
--[[
timer.create('', 0.25, 0, function()
table.insert(graph, {
x = timer.curtime(),
y = darkrp.moneyRequestsLeft(),
--y = 1
})
end)
--]]
net.receive('', function()
local x = net.readFloat()
local y = net.readFloat()
table.insert(graph, {
x = x,
y = y,
})
end)
hook.add('render', '', function()
local sw, sh = render.getResolution()
local gx, gy, gw, gh = 64, 64, sw, sh
gw = gw-gx-32
gh = gh-gy-32
local now = timer.curtime()
local xmin = now-10
local xmax = now
local lx, ly
for k, point in ipairs(graph) do
local x, y = point.x, point.y
if x >= xmin and x <= xmax then
local rx = (x-xmin)/(xmax-xmin)*gw+gx
local ry = (y-ymin)/(ymax-ymin)*gh+gy
if lx and ly then
render.setRGBA(191, 127, 0, 255)
render.drawLine(
(lx-xmin)/(xmax-xmin)*gw+gx,
(ly-ymin)/(ymax-ymin)*gh+gy,
rx, ry
)
end
render.setRGBA(191, 127, 0, 63)
render.drawRect(rx-2, ry-2, 4, 4)
lx, ly = x, y
end
end
render.setFont("DebugFixed")
render.setRGBA(255, 255, 255, 15)
render.drawLine(gx, gy, gw+gx, gy)
render.drawLine(gx, gy, gx, gh+gy)
render.drawLine(gx+gw, gy, gw+gx, gy+gh)
render.drawLine(gx, gy+gh, gw+gx, gy+gh)
render.drawSimpleText(0, gy, tostring(ymin), TEXT_ALIGN.LEFT, TEXT_ALIGN.CENTER)
render.drawSimpleText(0, gy+gh, tostring(ymax), TEXT_ALIGN.LEFT, TEXT_ALIGN.CENTER)
render.drawSimpleText(gx, 0, string.format("%.02f", xmin), TEXT_ALIGN.CENTER, TEXT_ALIGN.TOP)
render.drawSimpleText(gx+gw, 0, string.format("%.02f", xmax), TEXT_ALIGN.CENTER, TEXT_ALIGN.TOP)
render.setRGBA(255, 255, 255, 255)
local latest = graph[#graph] or {}
render.drawText(8, 8, string.format("curtime: %.02f\n%d entries\nlatest: %.02f / %.02f", now, #graph, latest.x or -1, latest.y or -1))
end)
--@shared
if SERVER then
hook.add('starfallUsed', '', function(activator, used)
if activator ~= owner() then return end
print("doing it")
activator:giveMoney(100)
net.start('')
net.writeFloat(timer.curtime())
net.writeFloat(darkrp.moneyGivingsLeft())
net.send(owner())
end)
timer.create('', 0.25, 0, function()
net.start('')
net.writeFloat(timer.curtime())
net.writeFloat(darkrp.moneyGivingsLeft())
net.send(owner())
end)
return
end
local ymin = 0
local ymax = 2
local graph = {}
net.receive('', function()
local x = net.readFloat()
local y = net.readFloat()
table.insert(graph, {
x = x,
y = y,
})
end)
hook.add('render', '', function()
local sw, sh = render.getResolution()
local gx, gy, gw, gh = 64, 64, sw, sh
gw = gw-gx-32
gh = gh-gy-32
local now = timer.curtime()
local xmin = now-10
local xmax = now
local lx, ly
for k, point in ipairs(graph) do
local x, y = point.x, point.y
if x >= xmin and x <= xmax then
local rx = (x-xmin)/(xmax-xmin)*gw+gx
local ry = (y-ymin)/(ymax-ymin)*gh+gy
if lx and ly then
render.setRGBA(0, 127, 191, 255)
render.drawLine(
(lx-xmin)/(xmax-xmin)*gw+gx,
(ly-ymin)/(ymax-ymin)*gh+gy,
rx, ry
)
end
render.setRGBA(0, 127, 191, 63)
render.drawRect(rx-2, ry-2, 4, 4)
lx, ly = x, y
end
end
render.setFont("DebugFixed")
render.setRGBA(255, 255, 255, 15)
render.drawLine(gx, gy, gw+gx, gy)
render.drawLine(gx, gy, gx, gh+gy)
render.drawLine(gx+gw, gy, gw+gx, gy+gh)
render.drawLine(gx, gy+gh, gw+gx, gy+gh)
render.drawSimpleText(0, gy, tostring(ymin), TEXT_ALIGN.LEFT, TEXT_ALIGN.CENTER)
render.drawSimpleText(0, gy+gh, tostring(ymax), TEXT_ALIGN.LEFT, TEXT_ALIGN.CENTER)
render.drawSimpleText(gx, 0, string.format("%.02f", xmin), TEXT_ALIGN.CENTER, TEXT_ALIGN.TOP)
render.drawSimpleText(gx+gw, 0, string.format("%.02f", xmax), TEXT_ALIGN.CENTER, TEXT_ALIGN.TOP)
render.setRGBA(255, 255, 255, 255)
local latest = graph[#graph] or {}
render.drawText(8, 8, string.format("curtime: %.02f\n%d entries\nlatest: %.02f / %.02f", now, #graph, latest.x or -1, latest.y or -1))
end)
print(darkrp.isDarkRPVarBlacklisted("money") or "falsy")
print(owner():getDarkRPVar("rpname") or "falsy")
print(owner():getMoney("rpname") or "falsy")
print(darkrp.isDarkRPVarBlacklisted("money") or "falsy")
--@shared
if SERVER then
hook.add('starfallUsed', '', function(activator, used)
if activator ~= owner() then return end
print("doing it")
if not darkrp.canMakeMoneyRequest(activator) then return end
activator:requestMoney("foo", 100)
end)
local gettime = timer.systime
local cmmr = darkrp.canMakeMoneyRequest
local target = owner()
timer.create('', 0.25, 0, function()
local usefultime
for i=1, 1000 do
local starttime = gettime()
cmmr(target)
local endtime = gettime()
local time = endtime-starttime
usefultime = usefultime and (usefultime+time)/2 or time
--usefultime = usefultime and math.min(usefultime, time) or time
--usefultime = usefultime and math.max(usefultime, time) or time
end
net.start('')
net.writeFloat(timer.curtime())
net.writeFloat(usefultime*1000*1000)
net.send(owner())
end)
return
end
local ymin = 0
local ymax = 100
local graph = {}
net.receive('', function()
local x = net.readFloat()
local y = net.readFloat()
table.insert(graph, {
x = x,
y = y,
})
end)
hook.add('render', '', function()
local sw, sh = render.getResolution()
local gx, gy, gw, gh = 64, 64, sw, sh
gw = gw-gx-32
gh = gh-gy-32
local now = timer.curtime()
local xmin = now-10
local xmax = now
local lx, ly
for k, point in ipairs(graph) do
local x, y = point.x, point.y
if x >= xmin and x <= xmax then
local rx = (x-xmin)/(xmax-xmin)*gw+gx
local ry = (y-ymin)/(ymax-ymin)*gh+gy
if lx and ly then
render.setRGBA(255, 0, 0, 255)
render.drawLine(
(lx-xmin)/(xmax-xmin)*gw+gx,
(ly-ymin)/(ymax-ymin)*gh+gy,
rx, ry
)
end
render.setRGBA(255, 0, 0, 63)
render.drawRect(rx-1, ry-1, 3, 3)
lx, ly = x, y
end
end
render.setFont("DebugFixed")
render.setRGBA(255, 255, 255, 15)
render.drawLine(gx, gy, gw+gx, gy)
render.drawLine(gx, gy, gx, gh+gy)
render.drawLine(gx+gw, gy, gw+gx, gy+gh)
render.drawLine(gx, gy+gh, gw+gx, gy+gh)
render.drawSimpleText(0, gy, tostring(ymin), TEXT_ALIGN.LEFT, TEXT_ALIGN.CENTER)
render.drawSimpleText(0, gy+gh, tostring(ymax), TEXT_ALIGN.LEFT, TEXT_ALIGN.CENTER)
render.drawSimpleText(gx, 0, string.format("%.02f", xmin), TEXT_ALIGN.CENTER, TEXT_ALIGN.TOP)
render.drawSimpleText(gx+gw, 0, string.format("%.02f", xmax), TEXT_ALIGN.CENTER, TEXT_ALIGN.TOP)
render.setRGBA(255, 255, 255, 255)
local latest = graph[#graph] or {}
render.drawText(8, 8, string.format("curtime: %.02f\n%d entries\nlatest: %.02f / %.02f", now, #graph, latest.x or -1, latest.y or -1))
end)
--@server
--@superuser
local target = entity(1)
for i=1, 1000 do
target:requestMoney(nil, 100, nil, nil, target)
end
jit.on()
local getTime = SysTime
local j, l = 1000, 1000
local function test(name, func, ...)
local function testbed(...)
local dummy = 1+1
return func(...)
end
local averageTime
for i=1, j do
local totalTime = 0
for k=1, l do
local startTime = getTime()
testbed(...)
local endTime = getTime()
local time = endTime-startTime
totalTime = totalTime+time
end
averageTime = averageTime and (averageTime+totalTime)/2 or totalTime
end
print(string.format("It took an average %.05f microseconds to iterate %q %s times, of the %s times we tested.", averageTime*1000*1000, name, string.Comma(l), string.Comma(j)))
end
local whitelist = {
["nil"] = true,
["boolean"] = true,
["number"] = true,
["string"] = true,
}
local select = select
local type = type
local ipairs = ipairs
local pairs = pairs
local function funcSelect(...)
for k=1, select("#", ...) do
local v = select(k, ...)
if not whitelist[type(v)] then
return false
end
end
return true
end
local function funcIpairs(...)
for k, v in ipairs({...}) do
if not whitelist[type(v)] then
return false
end
end
return true
end
local function funcPairs(...)
for k, v in pairs({...}) do
if not whitelist[type(v)] then
return false
end
end
return true
end
local args = {"foo", 1337, true, false}
local startTime = getTime()
test("select", funcSelect, unpack(args))
test("ipairs", funcIpairs, unpack(args))
test("pairs", funcPairs, unpack(args))
local endTime = getTime()
print(string.format("All tests combined took %.02f seconds to run.", endTime-startTime))
jit.off()
local getTime = SysTime
local j, l = 1000, 1000
local function test(name, func, ...)
local function testbed(...)
local dummy = 1+1
return func(...)
end
local averageTime
for i=1, j do
local totalTime = 0
for k=1, l do
local startTime = getTime()
testbed(...)
local endTime = getTime()
local time = endTime-startTime
totalTime = totalTime+time
end
averageTime = averageTime and (averageTime+totalTime)/2 or totalTime
end
print(string.format("It took an average %.05f microseconds to iterate %q %s times, of the %s times we tested.", averageTime*1000*1000, name, string.Comma(l), string.Comma(j)))
end
local whitelist = {
["nil"] = true,
["boolean"] = true,
["number"] = true,
["string"] = true,
}
local select = select
local type = type
local ipairs = ipairs
local pairs = pairs
local unpack = unpack
local function funcA(...)
-- Will cause values after an unsafe type to be lost.
-- Should be safe regardless of Lua version, but will likely also be slower.
local args = {...}
local j
for k, v in ipairs(args) do
if not whitelist[type(v)] then
args[k] = nil
j = k
break
end
end
return unpack(args, 1, j)
end
local function funcB(...)
-- Will cause values after an unsafe type to be lost.
-- Fails dangerously on some versions of Lua, but will likely also be faster.
local args = {...}
for k, v in ipairs(args) do
if not whitelist[type(v)] then
args[k] = nil
break
end
end
return unpack(args)
end
local function funcC(...)
-- Will cause values after an unsafe type to be lost.
-- Fails dangerously if assumption about table order is wrong, but will likely also be even faster.
local args = {...}
for k, v in pairs(args) do
if not whitelist[type(v)] then
args[k] = nil
break
end
end
return unpack(args)
end
local function funcD(a, ...)
-- Will preserve all values. Probably slow (correction: surprisingly fast!).
if not whitelist[type(a)] then
a = nil
end
if select("#", ...) == 0 then
return a
else
return a, funcD(...)
end
end
local function funcE(...)
-- Will change the order of the values by removing unsafe ones. Probably slow (correction: about tied with funcD).
local args = {...}
local i, j = 1, #args
if j == 0 then
return
end
while i <= j do
if not whitelist[type(args[i])] then
table.remove(args, i)
j = j-1
else
i = i+1
end
end
return unpack(args)
end
local function funcF(...)
-- Will change the order of the values by removing unsafe ones. Probably fast (correction: about tied with funcD and funcE).
local args = {...}
for i=#args, 1, -1 do
if not whitelist[type(args[i])] then
table.remove(args, i)
end
end
return unpack(args)
end
local args = {"foo", 1337, true, false}
--[[
table.insert(args, 4, {})
PrintTable({funcF(unpack(args))})
do return end
--]]
local startTime = getTime()
test("A", funcA, unpack(args))
test("B", funcB, unpack(args))
test("C", funcC, unpack(args))
test("D", funcD, unpack(args))
test("E", funcE, unpack(args))
test("F", funcF, unpack(args))
local endTime = getTime()
print(string.format("All tests combined took %.02f seconds to run.", endTime-startTime))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment