Advertisement
wifiboost

Untitled

Apr 10th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1. -- admin_request_sv.lua
  2. util.AddNetworkString("AdminRequest")
  3. util.AddNetworkString("AdminRequestAction")
  4. util.AddNetworkString("AdminRequestClaim")
  5. util.AddNetworkString("AdminRequestClaimData")
  6. util.AddNetworkString("RequestUpdatedAdminRequests")
  7. util.AddNetworkString("UpdatedAdminRequests")
  8. util.AddNetworkString("AdminRequestClose")
  9. local userRequestTracker = {}
  10. local function loadAdminClaimData()
  11. if file.Exists("admin_claim_data.txt", "DATA") then
  12. local jsonData = file.Read("admin_claim_data.txt", "DATA")
  13. adminClaimData = util.JSONToTable(jsonData)
  14.  
  15. -- Ensure 'lastConnected' and 'timeOnline' fields are initialized for existing records
  16. for adminID, data in pairs(adminClaimData) do
  17. if not data.lastConnected then
  18. data.lastConnected = os.date("%Y-%m-%d")
  19. end
  20. if not data.timeOnline then
  21. data.timeOnline = 0
  22. end
  23. if not data.sessionStart then
  24. data.sessionStart = os.time()
  25. end
  26. end
  27. else
  28. adminClaimData = {}
  29. end
  30. end
  31. loadAdminClaimData()
  32.  
  33.  
  34. local adminRequests = {}
  35. local function GetAdmins()
  36. local adminGroups = {
  37. "trialmod", "moderator", "seniormod", "admin", "headadmin", "senioradmin", "council", "superadmin", "manager"
  38. }
  39. local admins = {}
  40. for _, ply in ipairs(player.GetAll()) do
  41. for _, group in ipairs(adminGroups) do
  42. if ply:IsUserGroup(group) then
  43. table.insert(admins, ply)
  44. break
  45. end
  46. end
  47. end
  48. return admins
  49. end
  50. local function IsAdminGroup(ply)
  51. local adminGroups = {
  52. "trialmod", "moderator", "seniormod", "admin", "headadmin", "senioradmin", "council", "superadmin", "manager"
  53. }
  54. for _, group in ipairs(adminGroups) do
  55. if ply:IsUserGroup(group) then
  56. return true
  57. end
  58. end
  59.  
  60. return false
  61. end
  62. hook.Add("PlayerInitialSpawn", "AdminTimeOnlineStart", function(ply)
  63. if not IsAdminGroup(ply) then return end
  64.  
  65. local adminID = ply:SteamID64()
  66. if not adminClaimData[adminID] then
  67. adminClaimData[adminID] = {
  68. name = ply:Nick(),
  69. totalCount = 0,
  70. weeklyCount = 0,
  71. week = os.date("%U"),
  72. lastConnected = os.date("%Y-%m-%d"),
  73. timeOnline = 0,
  74. sessionStart = os.time()
  75. }
  76. else
  77. adminClaimData[adminID].sessionStart = os.time()
  78. end
  79. end)
  80.  
  81.  
  82. hook.Add("PlayerSay", "AdminRequestSystem", function(ply, text, teamChat)
  83. if string.sub(text, 1, 1) == "@" then
  84. -- Check if user already has an open request
  85. local currentPlayerSteamID64 = ply:SteamID64()
  86. if userRequestTracker[currentPlayerSteamID64] then
  87. ply:ChatPrint("You already have an open request.")
  88. return ""
  89. end
  90.  
  91. -- Proceed with creating the new request
  92. local requestData = {
  93. steamID64 = currentPlayerSteamID64,
  94. name = ply:Nick(),
  95. message = string.sub(text, 2),
  96. requestTime = CurTime()
  97. }
  98. table.insert(adminRequests, requestData) -- Save the request data to the adminRequests table
  99. userRequestTracker[currentPlayerSteamID64] = requestData
  100.  
  101. net.Start("AdminRequest")
  102. net.WriteTable(requestData)
  103. net.Send(GetAdmins()) -- GetAdmins will now check for the specified user groups
  104. return "" -- Hide the message from the chat
  105. end
  106. end)
  107. local function getUpdatedAdminRequests()
  108. -- Return the contents of the adminRequests table
  109. return adminRequests
  110. end
  111. -- Handle admin actions
  112. local playerPreviousPositions = {}
  113. net.Receive("AdminRequestAction", function(len, ply)
  114. if not IsAdminGroup(ply) then return end
  115. local action = net.ReadString()
  116. local target = net.ReadEntity()
  117. if action == "goto" then
  118. playerPreviousPositions[ply:SteamID64()] = ply:GetPos()
  119. ply:SetPos(target:GetPos() + Vector(0, 0, 50))
  120. elseif action == "bring" then
  121. playerPreviousPositions[target:SteamID64()] = target:GetPos()
  122. target:SetPos(ply:GetPos() + ply:GetForward() * 100 + Vector(0, 0, 50))
  123. elseif action == "return" then
  124. local prevPos = playerPreviousPositions[target:SteamID64()]
  125. if prevPos then
  126. target:SetPos(prevPos)
  127. playerPreviousPositions[target:SteamID64()] = nil
  128. end
  129. end
  130. end)
  131. local function saveAdminClaimData()
  132. local jsonData = util.TableToJSON(adminClaimData)
  133. file.Write("admin_claim_data.txt", jsonData)
  134. end
  135. local function GetCurrentTimeOnline(adminID)
  136. local data = adminClaimData[adminID]
  137. if data and data.sessionStart then
  138. return data.timeOnline + (os.time() - data.sessionStart)
  139. end
  140. return data.timeOnline
  141. end
  142. -- Periodically update claim data
  143. local function updateClaimData()
  144. local currentTime = os.time()
  145. for adminID, data in pairs(adminClaimData) do
  146. if data.sessionStart then
  147. data.timeOnline = GetCurrentTimeOnline(adminID)
  148. data.sessionStart = currentTime
  149. end
  150. end
  151. saveAdminClaimData()
  152. end
  153. hook.Add("PlayerDisconnected", "AdminTimeOnlineEnd", function(ply)
  154. if not IsAdminGroup(ply) then return end
  155.  
  156. local adminID = ply:SteamID64()
  157. local data = adminClaimData[adminID]
  158. if data and data.sessionStart then
  159. data.timeOnline = data.timeOnline + (os.time() - data.sessionStart)
  160. data.sessionStart = nil
  161. end
  162. saveAdminClaimData()
  163. end)
  164. timer.Create("UpdateClaimDataTimer", 2, 0, function() updateClaimData() end)
  165. net.Receive("AdminRequestClaim", function(len, ply)
  166. if not IsAdminGroup(ply) then return end
  167.  
  168. local requestData = net.ReadTable() -- Add this line
  169. UpdateAdminClaim(ply)
  170. local adminID = ply:SteamID64()
  171. local currentDate = os.date("*t")
  172. local currentWeek = os.date("%U")
  173.  
  174. if not adminClaimData[adminID] then
  175. adminClaimData[adminID] = {
  176. name = ply:Nick(),
  177. totalCount = 0,
  178. weeklyCount = 0,
  179. week = os.date("%U"),
  180. lastConnected = currentDate,
  181. timeOnline = 0,
  182. sessionStart = os.time()
  183. }
  184. end
  185. local data = adminClaimData[adminID]
  186. if data.week ~= os.date("%U") then
  187. data.weeklyCount = 0
  188. data.week = os.date("%U")
  189. end
  190. data.lastConnected = currentDate
  191. data.timeOnline = GetCurrentTimeOnline(adminID) -- Update time online using the new function
  192. data.sessionStart = os.time()
  193. data.totalCount = data.totalCount + 1
  194. data.weeklyCount = data.weeklyCount + 1
  195. saveAdminClaimData()
  196. userRequestTracker[requestData.steamID64] = nil
  197. end)
  198. -- Handle claimed cases
  199. local function markCaseAsClaimed(caseIndex, adminID)
  200. adminRequests[caseIndex].claimedBy = adminID
  201. end
  202. net.Receive("RequestUpdatedAdminRequests", function(len, ply)
  203. -- Get updated admin requests here
  204. local updatedRequests = getUpdatedAdminRequests()
  205. net.Start("UpdatedAdminRequests")
  206. net.WriteTable(updatedRequests)
  207. net.Send(ply)
  208. end)
  209. net.Receive("AdminRequestClose", function(len, ply)
  210. if not IsAdminGroup(ply) then return end
  211. local requestData = net.ReadTable()
  212. local index = -1
  213. for i, request in ipairs(adminRequests) do
  214. if request.steamID64 == requestData.steamID64 and request.requestTime == requestData.requestTime then
  215. index = i
  216. break
  217. end
  218. end
  219. if index > 0 then
  220. table.remove(adminRequests, index)
  221. end
  222. end)
  223. concommand.Add("show_claim_data", function(ply, cmd, args)
  224. if not IsAdminGroup(ply) then return end
  225. -- Send the claim data to the player who requested it
  226. saveAdminClaimData()
  227. net.Start("AdminRequestClaimData")
  228. net.WriteTable(adminClaimData)
  229. net.Send(ply)
  230. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement