Advertisement
Guest User

L4D2 Smart Server Restarter

a guest
Mar 31st, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <sourcemod>
  2.  
  3. #define MONITORING_INTERVAL     2400.0  //server restarts every 40 minutes
  4. #define SHUTDOWN_DELAY          6.0 // When server's empty, restart after this many seconds
  5.  
  6. Handle g_hTimer_PendingShutdown;
  7. Handle g_hTimer_EnableServerImmunity;
  8. bool g_bMapChange;
  9.  
  10. public Plugin:myinfo =
  11. {
  12.     name = "L4D2 Smart Server Restarter",
  13.     author = "Visor, moified by drixevel (Keith Warren)",
  14.     description = "Restarts the server after the game is finished",
  15.     version = "1.3.3.7"
  16. };
  17.  
  18. public void OnPluginStart()
  19. {
  20.     CreateTimer(MONITORING_INTERVAL, CheckForPlayers, _, TIMER_REPEAT);
  21. }
  22.  
  23. public Action CheckForPlayers(Handle timer)
  24. {
  25.     ExecuteShutdownTimer();
  26. }
  27.  
  28. public void OnClientDisconnect_Post(int client)
  29. {
  30.     ExecuteShutdownTimer();
  31. }
  32.  
  33. void ExecuteShutdownTimer()
  34. {
  35.     // Check if there are no players on the servers
  36.     if (GetRealClientCount() == 0 && g_hTimer_PendingShutdown == null)
  37.     {
  38.         // No players; let's wait and see if anyone joins within the specified interval
  39.         g_hTimer_PendingShutdown = CreateTimer(SHUTDOWN_DELAY, ShutdownTimer);
  40.     }
  41. }
  42.  
  43. public Action ShutdownTimer(Handle timer)
  44. {
  45.     // No one has joined within the allocated time? Shutdown the server
  46.     if (GetRealClientCount() == 0 && g_hTimer_EnableServerImmunity == null)
  47.     {
  48.         ServerCommand("quit");
  49.     }
  50.     else // Someone did join. Go back to monitoring...
  51.     {
  52.         g_hTimer_PendingShutdown = null;
  53.         return Plugin_Stop;
  54.     }
  55. }
  56.  
  57. int GetRealClientCount()
  58. {
  59.     int count;
  60.     for (int i = 1; i <= MaxClients; i++)
  61.     {
  62.         if (IsClientInGame(i) && !IsFakeClient(i))
  63.         {
  64.             count++;
  65.         }
  66.     }
  67.    
  68.     return count;
  69. }
  70.  
  71. /* ---------- boolean values----------*/
  72.  
  73. public void OnMapEnd()
  74. {
  75.     KillTimerSafe(g_hTimer_EnableServerImmunity);
  76.     g_hTimer_EnableServerImmunity = CreateTimer(120.0, DisableServerShutdownImmunity);
  77.     g_bMapChange = true;
  78. }
  79.  
  80. public void OnMapStart()
  81. {
  82.     g_bMapChange = false;
  83. }
  84.  
  85. public Action DisableServerShutdownImmunity(Handle timer)
  86. {
  87.     g_hTimer_EnableServerImmunity = null;
  88. }
  89.  
  90. public void OnClientPutInServer(int client)
  91. {
  92.     if (!g_bMapChange)
  93.     {
  94.         KillTimerSafe(g_hTimer_EnableServerImmunity);
  95.     }
  96. }
  97.  
  98. void KillTimerSafe(Handle& timer)
  99. {
  100.     if (timer != null)
  101.     {
  102.         KillTimer(timer);
  103.         timer = null;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement