From d8bc96701f275385ec1dbc46151a28cd3dc83647 Mon Sep 17 00:00:00 2001 From: Dogan Date: Wed, 6 Nov 2019 18:57:55 +0100 Subject: [PATCH] New Plugin: UptimeRestarts --- UptimeRestarts/scripting/UptimeRestarts.sp | 157 +++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 UptimeRestarts/scripting/UptimeRestarts.sp diff --git a/UptimeRestarts/scripting/UptimeRestarts.sp b/UptimeRestarts/scripting/UptimeRestarts.sp new file mode 100644 index 00000000..d3f93658 --- /dev/null +++ b/UptimeRestarts/scripting/UptimeRestarts.sp @@ -0,0 +1,157 @@ +#pragma semicolon 1 + +#include +#include + +float g_fUptime; +int g_iRestartCountdown; + +public Plugin myinfo = +{ + name = "UptimeRestart", + author = "Dogan", + description = "Display Server Uptime and do controlled Restarts", + version = "1.0.0", + url = "" +}; + +public void OnPluginStart() +{ + RegAdminCmd("uptime", Command_Uptime, ADMFLAG_GENERIC, "Displays server Uptime (controlled Restarts not included)"); + RegAdminCmd("sm_uptime", Command_Uptime, ADMFLAG_GENERIC, "Displays server Uptime (controlled Restarts not included)"); + + RegAdminCmd("c_uptime", Command_C_Uptime, ADMFLAG_RCON, "Displays server Uptime since last controlled Restart"); + RegAdminCmd("sm_c_uptime", Command_C_Uptime, ADMFLAG_RCON, "Displays server Uptime since last controlled Restart"); + + g_iRestartCountdown = 5; + GetUptimeIfControlledRestart(); + + CreateTimer(60.0, CheckRestart, _, TIMER_REPEAT); +} + +public void GetUptimeIfControlledRestart() +{ + File UptimeFile = OpenFile("uptime.txt", "r"); + + if(UptimeFile != null) + { + char sUptime[64]; + UptimeFile.ReadLine(sUptime, sizeof(sUptime)); + g_fUptime = StringToFloat(sUptime); + delete UptimeFile; + DeleteFile("uptime.txt"); + } +} + +public Action Command_Uptime(int client, int args) +{ + float fUptime = GetEngineTime() + g_fUptime; + + 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] Server Uptime: %s", sUptime); + return Plugin_Handled; +} + +public Action Command_C_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] Server Uptime since last controlled restart: %s", sUptime); + return Plugin_Handled; +} + +public Action CheckRestart(Handle timer) +{ + if(!IsItRestartTime) + return Plugin_Continue; + + if(GetEngineTime() < 57500) //16 hours + return Plugin_Continue; + + int iPlayers = GetClientCount(false); + for(int i = 1; i <= MaxClients; i++) + { + if(IsClientConnected(i) && IsFakeClient(i)) + iPlayers--; + } + if(iPlayers > 1) //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[2]); + + if (iHour >= 3 || iHour < 6) + return true; + + return false; +} + +public Action RestartCountdown(Handle timer) +{ + PrintCenterTextAll("WARNING: Restarting Server in %d.", g_iRestartCountdown); + PrintToChatAll("WARNING: Restarting Server in %d.", g_iRestartCountdown); + g_iRestartCountdown--; + + if(g_iRestartCountdown < 0) + { + LogToFile("addons/sourcemod/configs/restarts/restarts.txt", "Successfully auto-restarted the Server."); + ServerCommand("_restart"); + return Plugin_Handled; + } + + return Plugin_Handled; //Server is dead by here anyway xD +} \ No newline at end of file