From fe44cb11801f5b2e4480cc913cae8453f78392f2 Mon Sep 17 00:00:00 2001 From: Linventif Date: Tue, 27 Feb 2024 02:30:55 +0100 Subject: [PATCH] refactor: log to players --- lua/gmod_integration/server/sv_logs.lua | 197 ------------------- lua/gmod_integration/server/sv_players.lua | 208 +++++++++++++++------ 2 files changed, 151 insertions(+), 254 deletions(-) delete mode 100644 lua/gmod_integration/server/sv_logs.lua diff --git a/lua/gmod_integration/server/sv_logs.lua b/lua/gmod_integration/server/sv_logs.lua deleted file mode 100644 index 4c18820..0000000 --- a/lua/gmod_integration/server/sv_logs.lua +++ /dev/null @@ -1,197 +0,0 @@ -// -// Functions -// - -local function logDisable() - return !gmInte.config.sendLog -end - -local function validLogAndPlayers(players) - if (logDisable()) then return false end - for _, ply in pairs(players) do - // return if not valid, player or bot and bots logs are disabled - if (!IsValid(ply)) then return false end - if (!ply:IsPlayer()) then return false end - if (!ply:IsBot() && !gmInte.config.logBotActions) then return false end - end - return true -end - -// -// Posts -// - -function gmInte.postLogPlayerSay(ply, text, teamChat) - if (!validLogAndPlayers({ply})) then return end - - gmInte.http.post("/logs/playerSay", - { - ["ply"] = gmInte.getPlayerFormat(ply), - ["text"] = text, - ["teamChat"] = teamChat - } - ) -end - -function gmInte.postLogPlayerDeath(ply, inflictor, attacker) - if (!validLogAndPlayers({ply, attacker})) then return end - - gmInte.http.post("/logs/playerDeath", - { - ["ply"] = gmInte.getPlayerFormat(ply), - ["inflictor"] = gmInte.getEntityFormat(inflictor), - ["attacker"] = gmInte.getPlayerFormat(attacker) - } - ) -end - -function gmInte.postLogPlayerInitialSpawn(ply) - if (!validLogAndPlayers({ply})) then return end - - gmInte.http.post("/logs/playerInitialSpawn", - { - ["ply"] = gmInte.getPlayerFormat(ply) - } - ) -end - -function gmInte.postLogPlayerHurt(ply, attacker, healthRemaining, damageTaken) - if (!validLogAndPlayers({ply, attacker})) then return end - - // Wait a second to see if the player is going to be hurt again - ply.gmodInteLastHurt = ply.gmodInteLastHurt || {} - local locCurTime = CurTime() - ply.gmodInteLastHurt[attacker:SteamID64()] = locCurTime - - timer.Simple(1, function() - if (ply.gmodInteLastHurt[attacker:SteamID64()] != locCurTime) then - ply.gmodInteTotalDamage = ply.gmodInteTotalDamage || 0 - ply.gmodInteTotalDamage = ply.gmodInteTotalDamage + damageTaken - return - end - - gmInte.http.post("/logs/playerHurt", - { - ["ply"] = gmInte.getPlayerFormat(ply), - ["attacker"] = gmInte.getPlayerFormat(attacker), - ["healthRemaining"] = healthRemaining, - ["damageTaken"] = ply.gmodInteTotalDamage - } - ) - end) -end - -function gmInte.postLogPlayerSpawnedSomething(object, ply, ent, model) - if (!validLogAndPlayers({ply})) then return end - - gmInte.http.post("/logs/playerSpawnedSomething", - { - ["object"] = object, - ["ply"] = gmInte.getPlayerFormat(ply), - ["ent"] = gmInte.getEntityFormat(ent), - ["model"] = model || "" - } - ) -end - -function gmInte.postLogPlayerSpawn(ply) - if (!validLogAndPlayers({ply})) then return end - - gmInte.http.post("/logs/playerSpawn", - { - ["ply"] = gmInte.getPlayerFormat(ply) - } - ) -end - -function gmInte.postLogPlayerDisconnect(ply) - if (!validLogAndPlayers({ply})) then return end - - gmInte.http.post("/logs/playerDisconnect", - { - ["ply"] = gmInte.getPlayerFormat(ply) - } - ) -end - -function gmInte.postLogPlayerConnect(data) - if (logDisable() || data.bot) then return end - - gmInte.http.post("/logs/playerConnect", - { - ["steamID64"] = util.SteamIDTo64(data.networkid), - ["steamID"] = data.networkid, - ["name"] = data.name, - ["ip"] = data.address - } - ) -end - -function gmInte.postLogPlayerGivet(ply, class, swep) - if (!validLogAndPlayers({ply})) then return end - - gmInte.http.post("/logs/playerGive", - { - ["ply"] = gmInte.getPlayerFormat(ply), - ["class"] = class, - ["swep"] = swep - } - ) -end - -// -// Hooks -// - -gameevent.Listen("player_connect") - -// Base - Player -hook.Add("PlayerSay", "gmInte:Log:PlayerSay", function(ply, text, teamChat) - gmInte.postLogPlayerSay(ply, text, teamChat) -end) -hook.Add("PlayerSpawn", "gmInte:Log:PlayerSpawn", function(ply) - gmInte.postLogPlayerSpawn(ply) -end) -hook.Add("player_connect", "gmInte:Log:PlayerConnect", function(data) - gmInte.postLogPlayerConnect(data) -end) -hook.Add("PlayerInitialSpawn", "gmInte:Log:PlayerInitialSpawn", function(ply) - gmInte.postLogPlayerInitialSpawn(ply) -end) -hook.Add("PlayerDisconnected", "gmInte:Log:PlayerDisconnected", function(ply) - gmInte.postLogPlayerDisconnect(ply) -end) -hook.Add("PlayerGiveSWEP", "gmInte:Log:PlayerSWEPs", function( ply, class, swep ) - gmInte.postLogPlayerGivet(ply, class, swep) -end) - -// Base - Player Combat -hook.Add("PlayerDeath", "gmInte:Log:PlayerDeath", function(ply, inflictor, attacker) - gmInte.postLogPlayerDeath(ply, inflictor, attacker) -end) -hook.Add("PlayerHurt", "gmInte:Log:PlayerHurt", function(ply, attacker, healthRemaining, damageTaken) - gmInte.postLogPlayerHurt(ply, attacker, healthRemaining, damageTaken) -end) - -// Base - Spawnables -hook.Add("PlayerSpawnedProp", "gmInte:Log:PlayerSpawnedProp", function(ply, model, ent) - gmInte.postLogPlayerSpawnedSomething("SENT", ply, ent, model) -end) -hook.Add("PlayerSpawnedSENT", "gmInte:Log:PlayerSpawnedSENT", function(ply, ent) - gmInte.postLogPlayerSpawnedSomething("SENT", ply, ent) -end) -hook.Add("PlayerSpawnedNPC", "gmInte:Log:PlayerSpawnedNPC", function(ply, ent) - gmInte.postLogPlayerSpawnedSomething("NPC", ply, ent) -end) -hook.Add("PlayerSpawnedVehicle", "gmInte:Log:PlayerSpawnedVehicle", function(ply, ent) - gmInte.postLogPlayerSpawnedSomething("Vehicle", ply, ent) -end) -hook.Add("PlayerSpawnedEffect", "gmInte:Log:PlayerSpawnedEffect", function(ply, model, ent) - gmInte.postLogPlayerSpawnedSomething("Effect", ply, ent, model) -end) -hook.Add("PlayerSpawnedRagdoll", "gmInte:Log:PlayerSpawnedRagdoll", function(ply, model, ent) - gmInte.postLogPlayerSpawnedSomething("Ragdoll", ply, ent, model) -end) -hook.Add("PlayerSpawnedSWEP", "gmInte:Log:PlayerSpawnedSWEP", function(ply, ent) - gmInte.postLogPlayerSpawnedSomething("SWEP", ply, ent) -end) \ No newline at end of file diff --git a/lua/gmod_integration/server/sv_players.lua b/lua/gmod_integration/server/sv_players.lua index bf8debc..e5d5358 100644 --- a/lua/gmod_integration/server/sv_players.lua +++ b/lua/gmod_integration/server/sv_players.lua @@ -2,59 +2,8 @@ // Methods // -function gmInte.verifyPlayer(ply) - if (!gmInte.plyValid(ply)) then return end - gmInte.http.get("/players/" .. ply:SteamID64(), function(code, data) - if (!gmInte.config.forcePlayerLink) then return end - - if (data && data.steamID64) then - if (ply.gmIntVerified) then return end - gmInte.SendNet("chatColorMessage", { - [1] = { - ["text"] = "You have been verified", - ["color"] = Color(255, 255, 255) - } - }, ply) - ply:Freeze(false) - ply.gmIntVerified = true - else - gmInte.SendNet("chatColorMessage", { - [1] = { - ["text"] = "You are not verified", - ["color"] = Color(255, 0, 0) - } - }, ply) - ply:Freeze(true) - gmInte.SendNet("openVerifPopup", nil, ply) - end - end) -end - -// Generate a unique token that allow player to update data link to this server (ex: screnshot, report bug, etc.) -function gmInte.getClientOneTimeToken(ply, callback) - gmInte.http.get("/players/" .. ply:SteamID64() .. "/single-token", function(code, data) - callback(data.token) - end) -end - -function gmInte.createClientToken(ply, callback) - gmInte.http.get("/players/" .. ply:SteamID64() .. "/tokens", function(code, data) - callback(data) - end) -end - -function gmInte.revokeClientToken(ply, callback) - gmInte.http.delete("/players/" .. ply:SteamID64() .. "/tokens", function(code, data) - callback(data) - end) -end - -function gmInte.playerConnect(data) - gmInte.http.post("/players/" .. util.SteamIDTo64(data.networkid) .. "/connect", data) -end - -function gmInte.userFinishConnect(ply) - if (!gmInte.plyValid(ply)) then return end +hook.Add("gmInte:PlayerReady", "gmInte:Player:Ready", function(ply) + if (!ply:IsValid() || !ply:IsPlayer(ply)) then return end // Initialize Time ply.gmIntTimeConnect = math.Round(RealTime()) @@ -62,14 +11,23 @@ function gmInte.userFinishConnect(ply) // Send Public Config gmInte.publicGetConfig(ply) - gmInte.http.post("/players/" .. ply:SteamID64() .. "/finish-connect", gmInte.getPlayerFormat(ply)) + // Send Player Ready + gmInte.playerReady(ply) +end) - if (!gmInte.config.forcePlayerLink) then return end - gmInte.verifyPlayer(ply) +function gmInte.playerReady(ply) + if (!ply:IsValid() || !ply:IsPlayer(ply)) then return end + + gmInte.http.post("/players/" .. ply:SteamID64() .. "/ready", gmInte.getPlayerFormat(ply)) +end + +function gmInte.playerConnect(data) + data.steamID64 = util.SteamIDTo64(data.networkid) + gmInte.http.post("/players/" .. util.SteamIDTo64(data.networkid) .. "/connect", data) end function gmInte.playerDisconnected(ply) - if (!gmInte.plyValid(ply)) then return end + if (!ply:IsValid() || !ply:IsPlayer(ply)) then return end gmInte.http.post("/players/" .. ply:SteamID64() .. "/disconnect", { @@ -78,6 +36,94 @@ function gmInte.playerDisconnected(ply) ) end +function gmInte.playerSpawn(ply) + if (!ply:IsValid() || !ply:IsPlayer(ply)) then return end + + gmInte.http.post("/players/" .. ply:SteamID64() .. "/spawn", + { + ["player"] = gmInte.getPlayerFormat(ply) + } + ) +end + +function gmInte.postLogPlayerDeath(ply, inflictor, attacker) + if (!ply:IsValid() || !ply:IsPlayer(ply)) then return end + if (!attacker:IsValid() || !attacker:IsPlayer(attacker)) then return end + if (!inflictor:IsValid()) then return end + + gmInte.http.post("/logs/playerDeath", + { + ["player"] = gmInte.getPlayerFormat(ply), + ["inflictor"] = gmInte.getEntityFormat(inflictor), + ["attacker"] = gmInte.getPlayerFormat(attacker) + } + ) +end + +function gmInte.postLogPlayerInitialSpawn(ply) + if (!ply:IsValid() || !ply:IsPlayer(ply)) then return end + + gmInte.http.post("/logs/playerInitialSpawn", + { + ["ply"] = gmInte.getPlayerFormat(ply) + } + ) +end + +function gmInte.postLogPlayerHurt(ply, attacker, healthRemaining, damageTaken) + if (!ply:IsValid() || !ply:IsPlayer(ply)) then return end + if (!attacker:IsValid() || !attacker:IsPlayer(attacker)) then return end + + // Wait a second to see if the player is going to be hurt again + ply.gmodInteLastHurt = ply.gmodInteLastHurt || {} + local locCurTime = CurTime() + ply.gmodInteLastHurt[attacker:SteamID64()] = locCurTime + + timer.Simple(1, function() + if (ply.gmodInteLastHurt[attacker:SteamID64()] != locCurTime) then + ply.gmodInteTotalDamage = ply.gmodInteTotalDamage || 0 + ply.gmodInteTotalDamage = ply.gmodInteTotalDamage + damageTaken + return + end + + gmInte.http.post("/logs/playerHurt", + { + ["victim"] = gmInte.getPlayerFormat(ply), + ["attacker"] = gmInte.getPlayerFormat(attacker), + ["healthRemaining"] = healthRemaining, + ["damageTaken"] = ply.gmodInteTotalDamage + } + ) + end) +end + +function gmInte.postLogPlayerSpawnedSomething(object, ply, ent, model) + if (!ply:IsValid() || !ply:IsPlayer(ply)) then return end + if (!ent:IsValid()) then return end + + gmInte.http.post("/logs/playerSpawnedSomething", + { + ["object"] = object, + ["player"] = gmInte.getPlayerFormat(ply), + ["entity"] = gmInte.getEntityFormat(ent), + ["model"] = model || "" + } + ) +end + +function gmInte.postLogPlayerGivet(ply, class, swep) + if (!ply:IsValid() || !ply:IsPlayer(ply)) then return end + + gmInte.http.post("/logs/playerGive", + { + ["player"] = gmInte.getPlayerFormat(ply), + ["class"] = class, + ["swep"] = swep + } + ) +end + + // // Hooks // @@ -95,4 +141,52 @@ end) hook.Add("PlayerDisconnected", "gmInte:Player:Disconnect", function(ply) gmInte.playerDisconnected(ply) +end) + +hook.Add("PlayerSpawn", "gmInte:Player:Spawn", function(ply) + gmInte.playerSpawn(ply) +end) + +hook.Add("PlayerInitialSpawn", "gmInte:Player:InitialSpawn", function(ply) + gmInte.postLogPlayerInitialSpawn(ply) +end) + +hook.Add("PlayerGiveSWEP", "gmInte:Player:SWEPs", function( ply, class, swep ) + gmInte.postLogPlayerGivet(ply, class, swep) +end) + +hook.Add("PlayerDeath", "gmInte:Player:Death", function(ply, inflictor, attacker) + gmInte.postLogPlayerDeath(ply, inflictor, attacker) +end) + +hook.Add("PlayerHurt", "gmInte:Player:Hurt", function(ply, attacker, healthRemaining, damageTaken) + gmInte.postLogPlayerHurt(ply, attacker, healthRemaining, damageTaken) +end) + +hook.Add("PlayerSpawnedProp", "gmInte:Player:SpawnedProp", function(ply, model, ent) + gmInte.postLogPlayerSpawnedSomething("SENT", ply, ent, model) +end) + +hook.Add("PlayerSpawnedSENT", "gmInte:Player:SpawnedSENT", function(ply, ent) + gmInte.postLogPlayerSpawnedSomething("SENT", ply, ent) +end) + +hook.Add("PlayerSpawnedNPC", "gmInte:Player:SpawnedNPC", function(ply, ent) + gmInte.postLogPlayerSpawnedSomething("NPC", ply, ent) +end) + +hook.Add("PlayerSpawnedVehicle", "gmInte:Player:SpawnedVehicle", function(ply, ent) + gmInte.postLogPlayerSpawnedSomething("Vehicle", ply, ent) +end) + +hook.Add("PlayerSpawnedEffect", "gmInte:Player:SpawnedEffect", function(ply, model, ent) + gmInte.postLogPlayerSpawnedSomething("Effect", ply, ent, model) +end) + +hook.Add("PlayerSpawnedRagdoll", "gmInte:Player:SpawnedRagdoll", function(ply, model, ent) + gmInte.postLogPlayerSpawnedSomething("Ragdoll", ply, ent, model) +end) + +hook.Add("PlayerSpawnedSWEP", "gmInte:Player:SpawnedSWEP", function(ply, ent) + gmInte.postLogPlayerSpawnedSomething("SWEP", ply, ent) end) \ No newline at end of file