Advertisement
HR_Shaft

AFK Detection v2 for Phasor v2

May 3rd, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.47 KB | None | 0 0
  1. --[[ ###     AFK Detection v2      ###]]--
  2. --[[ ### for Phasor v2 by H® Shaft ###]]--
  3.  
  4. -- Original by Nugget: Re-write of Nuggets AFK 1.01, credits to him.
  5. -- Corrected some logical errors, and updated the bit offsets and action key descriptions.
  6. -- Added kick_afk_admin and hide admin features
  7. -- Players kicked by this script are logged in Phasor's game log: "H® Shaft was kicked for being AFK."
  8. -- Edited to be persistent, and to play nice with other scripts and to work efficiently as originally intended.
  9. -- V2: Added check to determine if player is in a moving/non-moving vehicle
  10.  
  11. -- edit --
  12. afk_time = 30           --| Time in seconds before a player is considered to be AFK (amount of time before warning time clock starts).
  13. afk_warn_time = 180     --| Time in seconds before a player is warned that they will be kicked for being AFK (use nil to disable).
  14. afk_kick_time = 200     --| Time in seconds before a player should be kicked for being AFK (use nil to disable).
  15. kick_afk_admin = false  --| If true, any admin who is afk can be kicked. If false, admin will be hidden instead of kicked.
  16.  
  17. -- don't edit --
  18. afk = {}
  19.  
  20. function GetRequiredVersion()
  21.     return 200
  22. end
  23.  
  24. function OnScriptLoad(processId, game, persistent)
  25.     for i=0,15 do
  26.         if getplayer(i) then
  27.             afk[i] = {}
  28.             afk[i].orientation = {}
  29.             afk[i].response = true
  30.             afk[i].time = 0
  31.             afk[i].boolean = false
  32.             afk[i].hidden = false
  33.             afk[i].timerid = registertimer(1000, "AFKTimer", i)
  34.         end
  35.     end
  36.     checkactions = registertimer(500, "CheckActions")  
  37. end
  38.  
  39. function OnServerChat(player, type, message)
  40.     local response = nil
  41.     if player and type < 4 then
  42.         afk[player].time = -1
  43.         afk[player].boolean = false        
  44.         response = true
  45.     end
  46.     return response
  47. end
  48.  
  49. function OnPlayerJoin(player)
  50.     if getplayer(player) then
  51.         afk[player] = {}   
  52.         afk[player].orientation = {}
  53.         afk[player].response = true
  54.         afk[player].time = 0
  55.         afk[player].boolean = false
  56.         afk[player].hidden = false
  57.         afk[player].timerid = registertimer(1000, "AFKTimer", player)
  58.     end
  59. end
  60.  
  61. function OnPlayerLeave(player)
  62.     if getplayer(player) then
  63.         if afk[player].timerid then
  64.             removetimer(afk[player].timerid)
  65.             afk[player].timerid = nil
  66.         end
  67.         afk[player] = nil  
  68.     end
  69. end
  70.  
  71. function OnWeaponReload(player, weapId)
  72.     if getplayer(player) then
  73.         afk[player].time = -1
  74.         afk[player].boolean = false
  75.     end
  76.     return nil
  77. end
  78.  
  79. function OnObjectCreationAttempt(mapId, parentId, player)
  80.     if player then
  81.         local tagname, tagtype = gettaginfo(mapId)
  82.         if tagtype == "proj" then
  83.             if afk[player] then
  84.                 afk[player].time = -1
  85.                 afk[player].boolean = false
  86.             end
  87.         end
  88.     end
  89.     return nil
  90. end
  91.  
  92. function OnVehicleEntry(player, vehiId, seat, mapId, voluntary)
  93.     if getplayer(player) then
  94.         afk[player].time = -1
  95.         afk[player].boolean = false
  96.     end
  97.     return nil
  98. end
  99.  
  100. function OnVehicleEject(player, voluntary)
  101.     if getplayer(player) then
  102.         afk[player].time = -1
  103.         afk[player].boolean = false
  104.     end
  105.     return nil
  106. end
  107.  
  108. function CheckActions(id, count)
  109.     for i = 0,15 do
  110.         if getplayer(i) then
  111.             local m_objectId = getplayerobjectid(i)
  112.             if m_objectId then
  113.                 local m_object = getobject(m_objectId)
  114.                 if m_object then
  115.                     local melee_key = readbit(m_object + 0x208, 7)
  116.                     local action_key = readbit(m_object + 0x208, 6)
  117.                     local flashlight_key = readbit(m_object + 0x208, 4)
  118.                     local jump_key = readbit(m_object + 0x208, 1)
  119.                     local crouch_key = readbit(m_object + 0x208, 0)
  120.                     local left_mouse = readbit(m_object + 0x209, 3)
  121.                     local right_mouse = readbit(m_object + 0x209, 4)
  122.                     local shooting = readbyte(m_object + 0x2A5)
  123.                     if melee_key or action_key or flashlight_key or jump_key or crouch_key or left_mouse or right_mouse or (shooting == 1) then
  124.                         if afk[i].hidden == true then
  125.                             afk[i].hidden = false
  126.                         end
  127.                         afk[i].time = -1
  128.                         afk[i].boolean = false
  129.                     end
  130.                 end
  131.             end
  132.         end
  133.     end
  134.     return true
  135. end
  136.  
  137. function isafk(player)
  138.     if player then
  139.         if afk[player] then
  140.             return afk[player].boolean
  141.         end
  142.     end
  143. end
  144.  
  145. if afk_warn_time then
  146.     afk_warn_message = "**WARNING** You will be kicked in " .. afk_kick_time - afk_warn_time .. " seconds for being AFK!"
  147. end
  148.  
  149. if afk_kick_time then
  150.     afk_kick_message = " is being kicked for inactivity (AFK)."
  151. end
  152.  
  153. function AFKTimer(id, count, player)
  154.     if getplayer(player) and afk[player] then
  155.         local m_objectId = getplayerobjectid(player)
  156.         if m_objectId then
  157.             local m_object = getobject(m_objectId)
  158.             if m_object then
  159.                 local x_aim = readfloat(m_object + 0x230)
  160.                 local y_aim = readfloat(m_object + 0x234)
  161.                 local z_aim = readfloat(m_object + 0x238)
  162.                 local walking = readbyte(m_object + 0x4D2)
  163.                 local bool = true
  164.                 if x_aim == (afk[player].orientation.x or x_aim) and y_aim == (afk[player].orientation.y or y_aim) and z_aim == (afk[player].orientation.z or z_aim) then
  165.                     bool = false
  166.                 elseif (walking == 0 and isinvehicle(player)) then
  167.                     bool = false
  168.                 end
  169.                 afk[player].orientation.x = x_aim
  170.                 afk[player].orientation.y = y_aim
  171.                 afk[player].orientation.z = z_aim
  172.                 afk[player].response = bool
  173.                 if isinvehicle(player) then
  174.                     local m_vehicle = getobject(getplayervehicleid(player))
  175.                     if m_vehicle then
  176.                         if readfloat(m_vehicle + 0x68) == 0 and readfloat(m_vehicle + 0x6c) == 0  then
  177.                             bool = false
  178.                         elseif readfloat(m_vehicle + 0x68) > 0 or readfloat(m_vehicle + 0x6c) > 0  then
  179.                             bool = true
  180.                             afk[player].time = afk[player].time - 1
  181.                             if afk[player].hidden == true then
  182.                                 afk[player].hidden = false
  183.                             end
  184.                         end
  185.                     end
  186.                 end
  187.                 if afk[player].response == false then
  188.                     afk[player].time = afk[player].time + 1
  189.                 else
  190.                     afk[player].time = 0
  191.                 end
  192.                 if afk[player].hidden == true then
  193.                     applycamo(player, 2)
  194.                 end
  195.                 if afk[player].time == 0 then
  196.                     afk[player].boolean = false
  197.                     afk[player].hidden = false
  198.                 elseif afk[player].time == afk_time then
  199.                     afk[player].boolean = true
  200.                 elseif afk[player].time == afk_warn_time then
  201.                     privatesay(player, afk_warn_message, false)
  202.                 elseif afk[player].time == afk_kick_time then
  203.                     if kick_afk_admin and isadmin(player) then             
  204.                         afk[player].timerid = registertimer(2000, "kickplayer", player)
  205.                     elseif not kick_afk_admin and isadmin(player) then
  206.                         afk[player].hidden = true
  207.                         privatesay(player, "**ADMIN** You are being hidden while you are AFK.", false)
  208.                         hideadmin(player)
  209.                     elseif not kick_afk_admin and not isadmin(player) then
  210.                         say(getname(player) .. afk_kick_message, false)                
  211.                         afk[player].timerid = registertimer(2000, "kickplayer", player)
  212.                     end                
  213.                 end
  214.             else
  215.                 afk[player].orientation.x = nil
  216.                 afk[player].orientation.y = nil
  217.                 afk[player].orientation.z = nil
  218.             end
  219.         end
  220.     end
  221.     return true
  222. end
  223.  
  224. function getplayervehicleid(player)
  225.     return readdword(getobject(getplayerobjectid(player)) + 0x11C)
  226. end
  227.  
  228. function kickplayer(id, count, player)
  229.     if count == 1 then
  230.         if getplayer(player) then
  231.             if kick_afk_admin and isadmin(player) then
  232.                 log_msg(1, getname(player) .. " was kicked for being AFK.")        
  233.                 svcmd("sv_kick " .. resolveplayer(player))
  234.             elseif not kick_afk_admin and isadmin(player) then
  235.                 privatesay(player, "**ADMIN** You are being hidden while you are AFK.", false)
  236.                 hideadmin(player)
  237.             elseif not kick_afk_admin and not isadmin(player) then
  238.                 log_msg(1, getname(player) .. " was kicked for being AFK.")                    
  239.                 svcmd("sv_kick " .. resolveplayer(player))
  240.             end
  241.         end
  242.     end
  243.     return false
  244. end
  245.  
  246. function hideadmin(player)
  247.     for i = 0,15 do
  248.         local m_player = getplayer(i)
  249.         if m_player then
  250.             local m_objectId = getplayerobjectid(i)
  251.             if m_objectId then
  252.                 local m_object = getobject(m_objectId)
  253.                 if m_object then
  254.                     writefloat(m_player + 0x100, readfloat(m_player + 0x100) - 1000)
  255.                     afk[i].hidden = true
  256.                 end
  257.             end
  258.         end    
  259.     end
  260. end
  261.  
  262. function OnDamageLookup(receiving, causing, tagid)
  263.     local response = nil
  264.     local tagname, tagtype = gettaginfo(tagid)
  265.     if receiving then
  266.         local r_player = objectidtoplayer(receiving)
  267.         if r_player and (afk[r_player].hidden == true) then
  268.             response = false
  269.         end
  270.     end
  271.     return response
  272. end
  273.  
  274. function OnGameEnd(stage)
  275.     if stage == 1 then
  276.         if checkactions then
  277.             removetimer(checkactions)
  278.             checkactions = nil
  279.         end    
  280.         for i=0,15 do
  281.             if getplayer(i) then
  282.                 if afk[i].timerid then
  283.                     removetimer(afk[i].timerid)
  284.                     afk[i].timerid = nil
  285.                 end
  286.             end
  287.         end
  288.     end    
  289. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement