Advertisement
HR_Shaft

AFK Detection v1.5 for Phasor v2

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