Skip to content

Instantly share code, notes, and snippets.

@M4GNV5
Created February 22, 2017 01:18
Show Gist options
  • Save M4GNV5/eff3b11ce78fb1e606f04640e070b310 to your computer and use it in GitHub Desktop.
Save M4GNV5/eff3b11ce78fb1e606f04640e070b310 to your computer and use it in GitHub Desktop.
Garrys Mod Death recap
if SERVER then
util.AddNetworkString("deathrecap_show")
util.AddNetworkString("deathrecap_hide")
local recap = {}
local startTime = 0
hook.Add("EntityTakeDamage", "deathrecap_takedmg", function(target, ev)
if target:IsPlayer() then
local name = target:GetName()
recap[name] = recap[name] or {}
local attacker = ev:GetAttacker()
local inflictor = ev:GetInflictor()
if attacker:IsPlayer() then
if inflictor == attacker then
inflictor = tostring(attacker:GetActiveWeapon():GetClass())
else
inflictor = tostring(inflictor:GetClass())
end
attacker = attacker:GetName()
else
inflictor = "-"
attacker = tostring(attacker:GetClass())
end
table.insert(recap[name], {
source = attacker,
weapon = inflictor,
damage = math.Round(ev:GetDamage() * 100) / 100,
time = os.time() - startTime
})
end
end)
hook.Add("PostPlayerDeath", "deathrecap_death", function(ply)
local name = ply:GetName()
if recap[name] then
net.Start("deathrecap_show")
net.WriteTable(recap[name])
net.Send(ply)
recap[name] = nil
end
end)
hook.Add("TTTEndRound", "deathrecap_tttend", function()
local plys = player.GetAll()
for i = 1, #plys do
local name = plys[i]:GetName()
if recap[name] then
net.Start("deathrecap_show")
net.WriteTable(recap[name])
net.Send(plys[i])
end
end
end)
function resetData()
recap = {}
startTime = os.time()
net.Start("deathrecap_hide")
net.Broadcast()
end
hook.Add("TTTBeginRound", "deathrecap_tttbegin", resetData)
hook.Add("TTTPrepareRound", "deathrecap_tttprepare", resetData)
print("DeathRecap server loaded")
else
local list = nil
net.Receive("deathrecap_show", function()
if list ~= nil then
list:Remove()
end
local data = net.ReadTable()
list = vgui.Create("DListView")
list:SetSize(600, 200)
list:SetPos(ScrW() / 2 - 300, ScrH() - 200)
list:SetMultiSelect(false)
list:AddColumn("Time")
list:AddColumn("Inflictor")
list:AddColumn("Damage")
list:AddColumn("Weapon")
local i = 1
while i <= #data do
local stack, next, curr = 0, data[i + 1], data[i]
repeat
stack = stack + 1
next = data[i + stack]
until next == nil or next.source ~= curr.source or next.weapon ~= curr.weapon or next.damage ~= curr.damage
if stack > 1 then
curr.damage = tostring(stack) .. "x " .. tostring(curr.damage)
i = i + stack
else
i = i + 1
end
list:AddLine(curr.time, curr.source, curr.damage, curr.weapon)
end
end)
net.Receive("deathrecap_hide", function()
if list ~= nil then
list:Remove()
list = nil
end
end)
print("DeathRecap client loaded")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment