HR_Shaft

AFK Detection v2.1 for Phasor v2

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