Franc1sco

For TUSK3N

Jan 3rd, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. #include <sourcemod>
  2. #include <sdktools>
  3. #define PLUGIN_VERSION "1.0"
  4.  
  5. new Float:g_fAFKTime[MAXPLAYERS + 1] = {0.0, ...};
  6. new Float:g_fEyeAngles[MAXPLAYERS + 1][3];
  7. new Float:g_fMapPosition[MAXPLAYERS + 1][3];
  8. new bool:g_AFK[MAXPLAYERS + 1] = {false, ...};
  9. new Handle:g_AFKTimer[MAXPLAYERS + 1];
  10. new Handle:g_MaxAFKTime = INVALID_HANDLE;
  11. new bool:g_bKickOnFull[MAXPLAYERS + 1] = {false, ...};
  12. new Handle:g_MaxPlayers = INVALID_HANDLE;
  13. new Handle:g_KickAFKPlayers_Timer;
  14.  
  15. public Plugin:myinfo =
  16. {
  17. name = "AFK Manager",
  18. author = "Darkness",
  19. description = "Manages AFK players",
  20. version = PLUGIN_VERSION,
  21. url = "https://forums.alliedmods.net/showthread.php?t=254430"
  22. }
  23.  
  24. public OnPluginStart()
  25. {
  26. HookEvent("player_spawn", Event_PlayerSpawn);
  27. HookEvent("round_end", Event_RoundEnd);
  28. HookEvent("player_death", Event_PlayerDeath);
  29. HookEvent("round_end", Event_RoundEnd);
  30. AddCommandListener(Command_Say, "say");
  31. AddCommandListener(Command_Say, "say_team");
  32. g_MaxAFKTime = CreateConVar("maxafktime", "45.0", "Maximum amount of time in seconds that a player can be AFK.");
  33. g_MaxPlayers = CreateConVar("maxplayers", "32", "When player count reaches this number - afk players will be kicked.");
  34. g_KickAFKPlayers_Timer = CreateTimer(30.0, KickAFKSpec, INVALID_HANDLE, TIMER_REPEAT);
  35. AutoExecConfig(true, "afkmanager");
  36. }
  37.  
  38. public Action:Event_RoundEnd(Handle:event, const String:name[], bool:dontbroadcast)
  39. {
  40. // HERE YOU GO
  41. }
  42.  
  43. public Action:Event_PlayerDeath(Handle:event, const String:name[], bool:dontbroadcast)
  44. {
  45. new client = GetClientOfUserId(GetEventInt(event, "userid"));
  46.  
  47. if (IsValidClient(client))
  48. {
  49. g_fAFKTime[client] = 0.0;
  50. if (g_AFKTimer[client] != INVALID_HANDLE)
  51. {
  52. KillTimer(g_AFKTimer[client])
  53. g_AFKTimer[client] = INVALID_HANDLE;
  54. }
  55. }
  56. }
  57.  
  58. public OnMapEnd()
  59. {
  60. if (g_KickAFKPlayers_Timer != INVALID_HANDLE)
  61. {
  62. KillTimer(g_KickAFKPlayers_Timer);
  63. g_KickAFKPlayers_Timer = INVALID_HANDLE;
  64. }
  65. for (new i = 1; i <= MaxClients; i++)
  66. {
  67. if (IsValidClient(i))
  68. {
  69. if (g_AFKTimer[i] != INVALID_HANDLE)
  70. {
  71. KillTimer(g_AFKTimer[i]);
  72. g_AFKTimer[i] = INVALID_HANDLE;
  73. }
  74. }
  75. }
  76. }
  77.  
  78. public Action:KickAFKSpec(Handle:timer)
  79. {
  80. if (GetClientCount(false) >= GetConVarInt(g_MaxPlayers))
  81. {
  82. for (new i = 1; i <= MaxClients; i++)
  83. {
  84. if (IsValidClient(i))
  85. {
  86. if (g_bKickOnFull[i])
  87. {
  88. KickClient(i, "You have been kicked for being AFK too long on a full server.");
  89. }
  90. }
  91. }
  92. }
  93. }
  94.  
  95. public Action:Command_Say(client, const String:command[], args)
  96. {
  97. g_fAFKTime[client] = 0.0;
  98. g_AFK[client] = false;
  99. g_bKickOnFull[client] = false;
  100. }
  101.  
  102. public OnClientPutInServer(client)
  103. {
  104. g_fAFKTime[client] = 0.0;
  105. g_bKickOnFull[client] = false;
  106. }
  107.  
  108. public OnClientDisconnect(client)
  109. {
  110. g_fAFKTime[client] = 0.0;
  111. g_bKickOnFull[client] = false;
  112. if (g_AFKTimer[client] != INVALID_HANDLE)
  113. {
  114. KillTimer(g_AFKTimer[client]);
  115. g_AFKTimer[client] = INVALID_HANDLE;
  116. }
  117. }
  118.  
  119. public Action:Event_PlayerSpawn(Handle:event, const String:name[], bool:dontbroadcast)
  120. {
  121. new client = GetClientOfUserId(GetEventInt(event, "userid"));
  122.  
  123. if (IsValidClient(client))
  124. {
  125. if (IsPlayerAlive(client))
  126. {
  127. GetClientEyeAngles(client, g_fEyeAngles[client]);
  128. GetClientAbsOrigin(client, g_fMapPosition[client]);
  129. g_fAFKTime[client] = 0.0;
  130. if (g_AFKTimer[client] != INVALID_HANDLE)
  131. {
  132. KillTimer(g_AFKTimer[client])
  133. g_AFKTimer[client] = CreateTimer(1.0, CheckAFK, client, TIMER_REPEAT);
  134. }
  135. if (g_AFKTimer[client] == INVALID_HANDLE)
  136. {
  137. g_AFKTimer[client] = CreateTimer(1.0, CheckAFK, client, TIMER_REPEAT);
  138. }
  139. }
  140. }
  141. }
  142.  
  143. public Action:CheckAFK(Handle:timer, any:client)
  144. {
  145. if (IsValidClient(client))
  146. {
  147. decl Float:f_Map_Location[3];
  148. f_Map_Location = g_fMapPosition[client];
  149.  
  150. decl Float:f_Eye_Angles[3];
  151. f_Eye_Angles = g_fEyeAngles[client];
  152.  
  153. GetClientEyeAngles(client, g_fEyeAngles[client]);
  154. GetClientAbsOrigin(client, g_fMapPosition[client]);
  155.  
  156. if ((f_Map_Location[0] == g_fMapPosition[client][0]) &&
  157. (f_Map_Location[1] == g_fMapPosition[client][1]) &&
  158. (f_Map_Location[2] == g_fMapPosition[client][2]))
  159. {
  160. if ((f_Eye_Angles[0] == g_fEyeAngles[client][0]) &&
  161. (f_Eye_Angles[1] == g_fEyeAngles[client][1]) &&
  162. (f_Eye_Angles[2] == g_fEyeAngles[client][2]))
  163. {
  164. g_AFK[client] = true;
  165. }
  166. else
  167. {
  168. g_AFK[client] = false;
  169. g_fAFKTime[client] = 0.0;
  170. g_bKickOnFull[client] = false;
  171. }
  172. }
  173. else
  174. {
  175. g_AFK[client] = false;
  176. g_fAFKTime[client] = 0.0;
  177. g_bKickOnFull[client] = false;
  178. }
  179.  
  180. if (g_AFK[client])
  181. {
  182. g_fAFKTime[client]++;
  183. }
  184. if (g_fAFKTime[client] >= GetConVarFloat(g_MaxAFKTime))
  185. {
  186. MoveToSpectate(client);
  187. }
  188. }
  189. return Plugin_Continue;
  190. }
  191.  
  192. MoveToSpectate(client)
  193. {
  194. if (IsValidClient(client))
  195. {
  196. if (IsPlayerAlive(client))
  197. {
  198. if (GetClientTeam(client) > 1)
  199. {
  200. PrintToChatAll("[\x04SG JB\x01] %N has been moved to \x08Spectator\x01 for being AFK too long!", client);
  201. ForcePlayerSuicide(client);
  202. ChangeClientTeam(client, 1);
  203. g_bKickOnFull[client] = true;
  204. }
  205. }
  206. }
  207. }
  208.  
  209. stock bool:IsValidClient(client)
  210. {
  211. if (client < 1 || client > MaxClients)
  212. {
  213. return false;
  214. }
  215. if (!IsClientConnected(client))
  216. {
  217. return false;
  218. }
  219. if (!IsClientInGame(client))
  220. {
  221. return false;
  222. }
  223.  
  224. return true;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment