Advertisement
wifiboost

Untitled

Mar 13th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. if not sql.TableExists("player_job_times") then
  2. sql.Query("CREATE TABLE player_job_times (steamid TEXT, job TEXT, time INTEGER, PRIMARY KEY (steamid, job))")
  3. end
  4.  
  5. local trackedJobs = {
  6. ["Citizen"] = true,
  7. ["Hitman"] = true,
  8. ["Hacker"] = true
  9. }
  10.  
  11. local function getPlayerJobTime(ply, job)
  12. return tonumber(sql.QueryValue("SELECT time FROM player_job_times WHERE steamid = '" .. ply:SteamID64() .. "' AND job = '" .. job .. "'")) or 0
  13. end
  14.  
  15. local function setPlayerJobTime(ply, job, time)
  16. sql.Query("INSERT OR REPLACE INTO player_job_times (steamid, job, time) VALUES ('" .. ply:SteamID64() .. "', '" .. job .. "', " .. time .. ")")
  17. end
  18.  
  19. local function addPlayerJobTime(ply, job, time)
  20. local currentTime = getPlayerJobTime(ply, job)
  21. setPlayerJobTime(ply, job, currentTime + time)
  22. end
  23.  
  24. local function showJobTimesUI(ply)
  25. local jobTimes = {}
  26.  
  27. for _, job in ipairs(RPExtraTeams) do
  28. jobTimes[job.name] = {time = 0, color = job.color}
  29. end
  30.  
  31. for _, p in ipairs(player.GetAll()) do
  32. local steamid = p:SteamID64()
  33.  
  34. for _, job in ipairs(RPExtraTeams) do
  35. local time = getPlayerJobTime(p, job.name)
  36. jobTimes[job.name].time = jobTimes[job.name].time + time
  37. end
  38. end
  39.  
  40. local frame = vgui.Create("DFrame")
  41. frame:SetSize(ScrW() * 0.6, ScrH() * 0.6)
  42. frame:Center()
  43. frame:SetTitle("Job Times")
  44. frame:SetVisible(true)
  45. frame:SetDraggable(true)
  46. frame:ShowCloseButton(true)
  47. frame:MakePopup()
  48.  
  49. local jobList = vgui.Create("DListView", frame)
  50. jobList:Dock(FILL)
  51. jobList:SetMultiSelect(false)
  52. jobList:AddColumn("Job")
  53. jobList:AddColumn("Time")
  54. jobList:AddColumn("Color")
  55.  
  56. for _, job in ipairs(RPExtraTeams) do
  57. local jobTime = 0
  58. for _, p in ipairs(player.GetAll()) do
  59. local time = getPlayerJobTime(p, job.name)
  60. jobTime = jobTime + time
  61. end
  62.  
  63. if jobTime > 0 then
  64. local niceTime = string.NiceTime(jobTime)
  65. jobList:AddLine(job.name, niceTime, tostring(job.color))
  66. end
  67. end
  68. end
  69.  
  70.  
  71. concommand.Add("show_job_times", showJobTimesUI)
  72.  
  73.  
  74. hook.Add("PlayerDisconnected", "SaveJobTimes", function(ply)
  75. for job, _ in pairs(trackedJobs) do
  76. local currentTime = os.time()
  77. local lastTime = ply:GetNWInt("JobTime_" .. job, 0)
  78. local timeOnJob = currentTime - lastTime
  79.  
  80. local totalTime = tonumber(sql.QueryValue("SELECT totaltime FROM player_jobtimes WHERE steamid = '".. ply:SteamID() .."' AND job = '".. job .."'")) or 0
  81. sql.Query("REPLACE INTO player_jobtimes (steamid, job, jobtime, totaltime) VALUES ('".. ply:SteamID() .."', '".. job .."', ".. lastTime ..", ".. (totalTime + timeOnJob) ..")")
  82. end
  83. end)
  84.  
  85. hook.Add("PlayerChangedTeam", "SavePlayerJobTimes", function(ply, oldTeam, newTeam)
  86. local job = RPExtraTeams[newTeam].name
  87. local currentTime = os.time()
  88. local lastTime = ply:GetNWInt("JobTime_" .. job, currentTime)
  89.  
  90. local totalTime = tonumber(sql.QueryValue("SELECT totaltime FROM player_jobtimes WHERE steamid = '".. ply:SteamID() .."' AND job = '".. job .."'")) or 0
  91. sql.Query("REPLACE INTO player_jobtimes (steamid, job, jobtime, totaltime) VALUES ('".. ply:SteamID() .."', '".. job .."', ".. lastTime ..", ".. totalTime ..")")
  92.  
  93. ply:SetNWInt("JobTime_" .. job, currentTime)
  94. end)
  95.  
  96. hook.Add("PlayerInitialSpawn", "TrackJobTimes", function(ply)
  97. for job, _ in pairs(trackedJobs) do
  98. local lastTime = tonumber(sql.QueryValue("SELECT jobtime FROM player_jobtimes WHERE steamid = '".. ply:SteamID() .."' AND job = '".. job .."'")) or 0
  99. ply:SetNWInt("JobTime_" .. job, lastTime)
  100. end
  101. end)
  102.  
  103. hook.Add("PlayerChangedTeam", "TrackJobTimes", function(ply, oldTeam, newTeam)
  104. local job = RPExtraTeams[newTeam].name
  105. local currentTime = os.time()
  106. local lastTime = ply:GetNWInt("JobTime_" .. job, currentTime)
  107.  
  108. ply:SetNWInt("JobTime_" .. job, currentTime)
  109. end)
  110.  
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement