From 591065c25c161667b8a71b13c5145da13c69f4dc Mon Sep 17 00:00:00 2001 From: dogan Date: Fri, 17 Jul 2020 15:35:13 +0200 Subject: [PATCH] RestartManager: last polish + safe restart command + remove UptimeRestarts --- RestartManager/scripting/RestartManager.sp | 155 +++++++++++-- UptimeRestarts/scripting/UptimeRestarts.sp | 247 --------------------- 2 files changed, 132 insertions(+), 270 deletions(-) delete mode 100644 UptimeRestarts/scripting/UptimeRestarts.sp diff --git a/RestartManager/scripting/RestartManager.sp b/RestartManager/scripting/RestartManager.sp index b35cc042..0128c37a 100644 --- a/RestartManager/scripting/RestartManager.sp +++ b/RestartManager/scripting/RestartManager.sp @@ -1,6 +1,7 @@ #include #include #include +#include #pragma semicolon 1 #pragma newdecls required @@ -20,10 +21,10 @@ public Plugin myinfo = name = "RestartManager", author = "Dogan + Neon", description = "Display Server Uptime and do controlled Restarts", - version = "1.0.0", + version = "2.0.0", url = "" }; -// TODO: SAFE command for admins to restart!!! + //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- @@ -31,6 +32,7 @@ public void OnPluginStart() { RegAdminCmd("uptime", Command_Uptime, ADMFLAG_GENERIC, "Displays server Uptime"); RegAdminCmd("sm_uptime", Command_Uptime, ADMFLAG_GENERIC, "Displays server Uptime"); + RegAdminCmd("sm_forcerestart", Command_ForceRestart, ADMFLAG_RCON, "Force-restarts the server"); g_cvarDefaultMap = CreateConVar("sm_defaultmap", "ze_atix_panic_b3t", "default map of the server"); g_cvarMaxUptime = CreateConVar("sm_maxuptime", "68", "Uptime in hours after which the server should be restarted", FCVAR_NONE); @@ -44,6 +46,18 @@ public void OnPluginStart() RegServerCmd("changelevel", BlockMapSwitch); CreateTimer(60.0, CheckForRestart, _, TIMER_REPEAT); + CreateTimer(20.0, ForceRestartMessage, _, TIMER_REPEAT); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public Action Command_ForceRestart(int client, int args) +{ + ReplyToCommand(client, "[SM] Confirm the force-restart please!"); + OpenAdminPanel(client); + + return Plugin_Handled; } //---------------------------------------------------------------------------------------------------- @@ -94,11 +108,56 @@ public Action Command_Uptime(int client, int args) if (g_bRestart) ReplyToCommand(client, "[SM] Server is going to restart on next mapswitch"); else - ReplyToCommand(client, "[SM] Time until next restart: %.2fh", ((g_cvarMaxUptime.FloatValue * 3600.0) - GetEngineTime()) / 3600.0); + ReplyToCommand(client, "[SM] Time until next force-restart: %.2fh", ((g_cvarMaxUptime.FloatValue * 3600.0) - GetEngineTime()) / 3600.0); return Plugin_Handled; } +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OpenAdminPanel(int client) +{ + Menu menu = new Menu(MenuHandler_MainMenu); + + menu.SetTitle("Are you sure you want to force-restart the server?", client); + + char sBuffer[32]; + + Format(sBuffer, sizeof(sBuffer), "Yes."); + menu.AddItem("0", sBuffer); + + Format(sBuffer, sizeof(sBuffer), "No."); + menu.AddItem("1", sBuffer); + + menu.Display(client, MENU_TIME_FOREVER); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public int MenuHandler_MainMenu(Menu menu, MenuAction action, int client, int selection) +{ + char sMap[64]; + GetCurrentMap(sMap, sizeof(sMap)); + + switch(action) + { + case(MenuAction_Select): + { + switch(selection) + { + case(0): PrepareRestart(sMap, client, true); + case(1): PrintToChat(client, "[SM] You declined the force-restart."); + } + } + case(MenuAction_Cancel): + { + PrintToChat(client, "[SM] You declined the force-restart."); + } + } +} + //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- @@ -157,17 +216,14 @@ public bool IsItTimeToRestartForced() return false; g_bRestart = true; - NotifyAdmins(); return true; } //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- -public void PrepareRestart(char[] sMap) +public void PrepareRestart(char[] sMap, int client, bool bAdmin) { - NotifyAdmins(); - g_fCumulativeUptime += GetEngineTime(); char sUptime[64]; @@ -186,14 +242,52 @@ public void PrepareRestart(char[] sMap) delete NextmapFile; } - SimulateMapEnd(); - for(int i = 1; i <= MaxClients; i++) + if(bAdmin) { - if(IsClientConnected(i) && !IsFakeClient(i)) - ClientCommand(i, "retry"); + LogToFile("addons/sourcemod/logs/restarts.log", "%N successfully force-restarted the Server.", client); + PrintToChat(client, "[SM] You confirmed the force-restart."); + + CPrintToChatAll("{red}WARNING:{white} Restarting Server in 8 Seconds!"); + CPrintToChatAll("{red}WARNING:{white} Restarting Server in 8 Seconds"); + CPrintToChatAll("{red}WARNING:{white} You may disconnect, reconnect if necessary!"); + PrintCenterTextAll("WARNING: Restarting Server in 8 Seconds."); + + Panel hNotifyPanel = new Panel(GetMenuStyleHandle(MenuStyle_Radio)); + hNotifyPanel.DrawItem("WARNING: Restarting Server in 8 Seconds.", ITEMDRAW_RAWLINE); + hNotifyPanel.DrawItem("", ITEMDRAW_SPACER); + hNotifyPanel.DrawItem("IMPORTANT: You may disconnect, reconnect if necessary!", ITEMDRAW_RAWLINE); + for(int i = 1; i <= MaxClients; i++) + { + hNotifyPanel.Send(client, MenuHandler_NotifyPanel, 8); + } + delete hNotifyPanel; + + CreateTimer(8.0, AdminForceRestart); + } + else + { + SimulateMapEnd(); + for(int i = 1; i <= MaxClients; i++) + { + if(IsClientConnected(i) && !IsFakeClient(i)) + ClientCommand(i, "retry"); + } + + LogToFile("addons/sourcemod/logs/restarts.log", "Successfully force-restarted the Server."); + RequestFrame(Restart); + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +int MenuHandler_NotifyPanel(Menu hMenu, MenuAction iAction, int iParam1, int iParam2) +{ + switch (iAction) + { + case MenuAction_Select, MenuAction_Cancel: + delete hMenu; } - LogToFile("addons/sourcemod/logs/restarts.log", "Successfully force-restarted the Server."); - RequestFrame(Restart); } //---------------------------------------------------------------------------------------------------- @@ -201,8 +295,6 @@ public void PrepareRestart(char[] sMap) //---------------------------------------------------------------------------------------------------- public void PrepareRestartNight() { - NotifyAdmins(); - g_fCumulativeUptime += GetEngineTime(); char sUptime[64]; @@ -248,7 +340,7 @@ public Action BlockMapSwitch(int args) char sMap[64]; GetCmdArg(1, sMap, sizeof(sMap)); - PrepareRestart(sMap); + PrepareRestart(sMap, 0, false); return Plugin_Handled; } @@ -287,16 +379,33 @@ public void SetStartMap(char[] sMap) //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- -public void NotifyAdmins() +public Action ForceRestartMessage(Handle timer) { + if(!g_bRestart) + return Plugin_Continue; + + CPrintToChatAll("{red}WARNING:{white} Restarting Server when this Map ends!"); + CPrintToChatAll("{red}WARNING:{white} Restarting Server when this Map ends!"); + CPrintToChatAll("{red}WARNING:{white} You may disconnect, reconnect if necessary!"); + + return Plugin_Continue; +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public Action AdminForceRestart(Handle timer) +{ + SimulateMapEnd(); for(int i = 1; i <= MaxClients; i++) { - if(IsValidClient(i) && CheckCommandAccess(i, "", ADMFLAG_KICK)) - { - PrintToChat(i, "[SM] WARNING: Server will be restarted on mapswitch!"); - PrintToChat(i, "[SM] WARNING: Server will be restarted on mapswitch!"); - } + if(IsClientConnected(i) && !IsFakeClient(i)) + ClientCommand(i, "retry"); } + + RequestFrame(Restart); + + return Plugin_Handled; } //---------------------------------------------------------------------------------------------------- @@ -308,4 +417,4 @@ stock int IsValidClient(int client, bool nobots = true) return false; return IsClientInGame(client); -} +} \ No newline at end of file diff --git a/UptimeRestarts/scripting/UptimeRestarts.sp b/UptimeRestarts/scripting/UptimeRestarts.sp deleted file mode 100644 index 19d176d3..00000000 --- a/UptimeRestarts/scripting/UptimeRestarts.sp +++ /dev/null @@ -1,247 +0,0 @@ -#pragma semicolon 1 - -#include -#include -#include - -float g_fUptime; -int g_iRestartCountdown; -bool g_bForcedRestart; - -ConVar g_cvarMaxPlayersForControlledRestart; -ConVar g_cvarMinPlayersForForcedRestart; -ConVar g_cvarMinHoursUptimeForForcedRestart; -ConVar g_cvarDefaultMap; - -public Plugin myinfo = -{ - name = "UptimeRestart", - author = "Dogan", - description = "Display Server Uptime and do controlled Restarts", - version = "3.0.0", - url = "" -}; - -public void OnPluginStart() -{ - RegAdminCmd("uptime", Command_Uptime, ADMFLAG_GENERIC, "Displays server Uptime"); - RegAdminCmd("sm_uptime", Command_Uptime, ADMFLAG_GENERIC, "Displays server Uptime"); - - g_cvarMaxPlayersForControlledRestart = CreateConVar("sm_maxplayers_controlled_restart", "1", "Max Amount of Players Connected to Server to do a controlled restart in the night", FCVAR_NONE, true, 1.0, true, 64.0); - g_cvarMinPlayersForForcedRestart = CreateConVar("sm_minplayers_forced_restart", "55", "Min Amount of Players Connected to Server to do a forced restart in the afternoon", FCVAR_NONE, true, 1.0, true, 64.0); - g_cvarMinHoursUptimeForForcedRestart = CreateConVar("sm_minhours_forced_restart", "60", "Min Hours of Uptime to force a forced restart in the afternoon", FCVAR_NONE, true, 1.0, true, 96.0); - g_cvarDefaultMap = CreateConVar("sm_defaultmap", "ze_atix_panic_b3t", "default map of the server"); - - g_iRestartCountdown = 5; - g_bForcedRestart = false; - GetUptimeIfControlledRestart(); - SetCorrectDefaultMapAgain(); - - AutoExecConfig(true, "plugin.UptimeRestarts"); - - CreateTimer(60.0, CheckRestart, _, TIMER_REPEAT); - CreateTimer(20.0, ForceRestartMessage, _, TIMER_REPEAT); -} - -public void OnMapEnd() -{ - if(!g_bForcedRestart) - return; - - CPrintToChatAll("{red}WARNING:{white} Restarting Server now!"); - CPrintToChatAll("{red}WARNING:{white} Restarting Server now!"); - CPrintToChatAll("{red}WARNING:{white} Restarting Server now!"); - PrintCenterTextAll("WARNING: Restarting Server now!"); - - float fUptime = GetEngineTime(); - g_fUptime = g_fUptime + fUptime; - - char sUptime[64]; - FloatToString(g_fUptime, sUptime, sizeof(sUptime)); - File UptimeFile = OpenFile("uptime.txt", "w"); - UptimeFile.WriteLine(sUptime); - delete UptimeFile; - - char sNextmap[64]; - GetCurrentMap(sNextmap, sizeof(sNextmap)); - char sMapFile[64]; - Format(sMapFile, sizeof(sMapFile), "map %s", sNextmap); - - if(!StrEqual(sMapFile, "")) - { - DeleteFile("cfg/defaultmap.cfg"); - File NextmapFile = OpenFile("cfg/defaultmap.cfg", "w"); - NextmapFile.WriteLine(sMapFile); - delete NextmapFile; - } - - for(int i = 1; i <= MaxClients; i++) - { - if(IsClientConnected(i)) - ClientCommand(i, "retry"); - } - - LogToFile("addons/sourcemod/logs/restarts.txt", "Successfully force-restarted the Server."); - CreateTimer(0.01, ForceRestart); -} - -public Action ForceRestart(Handle timer) -{ - ServerCommand("_restart"); - return Plugin_Handled; -} - -public void SetCorrectDefaultMapAgain() -{ - DeleteFile("cfg/defaultmap.cfg"); - char sDefaultMap[64]; - GetConVarString(g_cvarDefaultMap, sDefaultMap, sizeof(sDefaultMap)); - char sMapFile[64]; - Format(sMapFile, sizeof(sMapFile), "map %s", sDefaultMap); - File NextmapFile = OpenFile("cfg/defaultmap.cfg", "w"); - NextmapFile.WriteLine(sMapFile); - delete NextmapFile; -} - -public void GetUptimeIfControlledRestart() -{ - File UptimeFile = OpenFile("uptime.txt", "r"); - - if(UptimeFile != null)//Server was restarted automatically by this plugin - { - char sUptime[64]; - UptimeFile.ReadLine(sUptime, sizeof(sUptime)); - g_fUptime = StringToFloat(sUptime); - delete UptimeFile; - DeleteFile("uptime.txt"); - } - else//Server crashed or restarted manually - LogToFile("addons/sourcemod/logs/restarts.txt", "Server crashed or was restarted manually."); -} - -public Action Command_Uptime(int client, int args) -{ - float fUptime = GetEngineTime(); - char sUptime[64]; - int iUptime = RoundFloat(fUptime); - - int iDays = (iUptime / 86400); - int iHours = (iUptime / 3600) % 24; - int iMinutes = (iUptime / 60) % 60; - int iSeconds = (iUptime % 60); - - if (iDays) - Format(sUptime, sizeof(sUptime), "%d Days %d Hours %d Minutes %d Seconds.", iDays, iHours, iMinutes, iSeconds); - else if (iHours) - Format(sUptime, sizeof(sUptime), "%d Hours %d Minutes %d Seconds.", iHours, iMinutes, iSeconds); - else if (iMinutes) - Format(sUptime, sizeof(sUptime), "%d Minutes %d Seconds.", iMinutes, iSeconds); - else - Format(sUptime, sizeof(sUptime), "%d Seconds.", iSeconds); - - ReplyToCommand(client, "[SM] Real Server Uptime: %s", sUptime); - - fUptime = GetEngineTime() + g_fUptime; - iUptime = RoundFloat(fUptime); - - iDays = (iUptime / 86400); - iHours = (iUptime / 3600) % 24; - iMinutes = (iUptime / 60) % 60; - iSeconds = (iUptime % 60); - - if (iDays) - Format(sUptime, sizeof(sUptime), "%d Days %d Hours %d Minutes %d Seconds.", iDays, iHours, iMinutes, iSeconds); - else if (iHours) - Format(sUptime, sizeof(sUptime), "%d Hours %d Minutes %d Seconds.", iHours, iMinutes, iSeconds); - else if (iMinutes) - Format(sUptime, sizeof(sUptime), "%d Minutes %d Seconds.", iMinutes, iSeconds); - else - Format(sUptime, sizeof(sUptime), "%d Seconds.", iSeconds); - - ReplyToCommand(client, "[SM] Cumulative Server Uptime: %s", sUptime); - return Plugin_Handled; -} - -public Action CheckRestart(Handle timer) -{ - if(g_bForcedRestart) - return Plugin_Continue; - - float fMinHoursUptime = g_cvarMinHoursUptimeForForcedRestart.FloatValue * 60.0 * 60.0; - - int iPlayers = GetClientCount(false); - for(int i = 1; i <= MaxClients; i++) - { - if(IsClientConnected(i) && IsFakeClient(i)) - iPlayers--; - } - - if((GetEngineTime() > fMinHoursUptime) && (iPlayers > g_cvarMinPlayersForForcedRestart.IntValue)) - { - g_bForcedRestart = true; - return Plugin_Continue; - } - - if(!IsItRestartTime() || GetEngineTime() < 57500) - return Plugin_Continue; - - if(iPlayers > g_cvarMaxPlayersForControlledRestart.IntValue) //jenz's autism bot - return Plugin_Continue; - - float fUptime = GetEngineTime(); - g_fUptime = g_fUptime + fUptime; - - char sUptime[64]; - FloatToString(g_fUptime, sUptime, sizeof(sUptime)); - File UptimeFile = OpenFile("uptime.txt", "w"); - UptimeFile.WriteLine(sUptime); - delete UptimeFile; - - CreateTimer(1.0, RestartCountdown, _, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE); - - return Plugin_Continue; -} - -stock bool IsItRestartTime() -{ - int iTime = GetTime(); - int iHour; - char sTime[32]; - - FormatTime(sTime, sizeof(sTime), "%H", iTime); - - iHour = StringToInt(sTime[0]); - - if (iHour >= 3 && iHour < 8) - return true; - - return false; -} - -public Action RestartCountdown(Handle timer) -{ - PrintCenterTextAll("WARNING: Restarting Server in %d.", g_iRestartCountdown); - CPrintToChatAll("{red}WARNING:{white} Restarting Server in %d.", g_iRestartCountdown); - g_iRestartCountdown--; - - if(g_iRestartCountdown < 0) - { - LogToFile("addons/sourcemod/logs/restarts.txt", "Successfully auto-restarted the Server."); - ServerCommand("_restart"); - return Plugin_Handled; - } - - return Plugin_Handled; //Server is dead by here anyway xD -} - -public Action ForceRestartMessage(Handle timer) -{ - if(!g_bForcedRestart) - return Plugin_Continue; - - CPrintToChatAll("{red}WARNING:{white} Restarting Server when this Map ends!"); - CPrintToChatAll("{red}WARNING:{white} Restarting Server when this Map ends!"); - CPrintToChatAll("{red}WARNING:{white} Remember to rejoin after couple seconds!"); - - return Plugin_Continue; -} \ No newline at end of file