sm-plugins/UptimeRestarts/scripting/UptimeRestarts.sp

236 lines
6.7 KiB
SourcePawn
Raw Normal View History

2019-11-06 18:57:55 +01:00
#pragma semicolon 1
#include <sourcemod>
#include <files>
#include <multicolors>
2019-11-06 18:57:55 +01:00
float g_fUptime;
int g_iRestartCountdown;
bool g_bForcedRestart;
ConVar g_cvarMaxPlayersForControlledRestart;
ConVar g_cvarMinPlayersForForcedRestart;
ConVar g_cvarMinHoursUptimeForForcedRestart;
2019-11-06 18:57:55 +01:00
public Plugin myinfo =
{
name = "UptimeRestart",
author = "Dogan",
description = "Display Server Uptime and do controlled Restarts",
version = "2.0.0",
2019-11-06 18:57:55 +01:00
url = ""
};
public void OnPluginStart()
{
2019-11-07 15:52:16 +01:00
RegAdminCmd("uptime", Command_Uptime, ADMFLAG_GENERIC, "Displays server Uptime");
RegAdminCmd("sm_uptime", Command_Uptime, ADMFLAG_GENERIC, "Displays server Uptime");
2019-11-06 18:57:55 +01:00
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);
2019-11-06 18:57:55 +01:00
g_iRestartCountdown = 5;
g_bForcedRestart = false;
2019-11-06 18:57:55 +01:00
GetUptimeIfControlledRestart();
GetNextmapIfForcedRestart();
AutoExecConfig(true, "plugin.UptimeRestarts");
2019-11-06 18:57:55 +01:00
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!");
char sNextmap[64];
GetNextMap(sNextmap, sizeof(sNextmap));
if(!StrEqual(sNextmap, ""))
{
File NextmapFile = OpenFile("nextmap.txt", "w");
NextmapFile.WriteLine(sNextmap);
delete NextmapFile;
}
RequestFrame(ForceRestart);
}
public void GetNextmapIfForcedRestart()
{
File NextmapFile = OpenFile("nextmap.txt", "r");
if(NextmapFile != null)//There was a nextmap set
{
char sNextmap[64];
NextmapFile.ReadLine(sNextmap, sizeof(sNextmap));
delete NextmapFile;
DeleteFile("nextmap.txt");
ForceChangeLevel(sNextmap, "Set to Map that was next before Force Restart happened");
}
2019-11-06 18:57:55 +01:00
}
public void GetUptimeIfControlledRestart()
{
File UptimeFile = OpenFile("uptime.txt", "r");
2019-11-07 14:59:46 +01:00
if(UptimeFile != null)//Server was restarted automatically by this plugin
2019-11-06 18:57:55 +01:00
{
char sUptime[64];
UptimeFile.ReadLine(sUptime, sizeof(sUptime));
g_fUptime = StringToFloat(sUptime);
delete UptimeFile;
DeleteFile("uptime.txt");
}
2019-11-07 14:59:46 +01:00
else//Server crashed or restarted manually
LogToFile("addons/sourcemod/logs/restarts.txt", "Server crashed or was restarted manually.");
2019-11-06 18:57:55 +01:00
}
public Action Command_Uptime(int client, int args)
{
2019-11-07 15:52:16 +01:00
float fUptime = GetEngineTime();
2019-11-06 18:57:55 +01:00
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);
2019-11-07 15:52:16 +01:00
ReplyToCommand(client, "[SM] Real Server Uptime: %s", sUptime);
2019-11-06 18:57:55 +01:00
2019-11-07 15:52:16 +01:00
fUptime = GetEngineTime() + g_fUptime;
iUptime = RoundFloat(fUptime);
2019-11-06 18:57:55 +01:00
2019-11-07 15:52:16 +01:00
iDays = (iUptime / 86400);
iHours = (iUptime / 3600) % 24;
iMinutes = (iUptime / 60) % 60;
iSeconds = (iUptime % 60);
2019-11-06 18:57:55 +01:00
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);
2019-11-07 15:52:16 +01:00
ReplyToCommand(client, "[SM] Cumulative Server Uptime: %s", sUptime);
2019-11-06 18:57:55 +01:00
return Plugin_Handled;
}
public Action CheckRestart(Handle timer)
{
if(g_bForcedRestart)
2019-11-06 18:57:55 +01:00
return Plugin_Continue;
float fMinHoursUptime = g_cvarMinHoursUptimeForForcedRestart.FloatValue * 60.0 * 60.0;
2019-11-06 18:57:55 +01:00
int iPlayers = GetClientCount(false);
for(int i = 1; i <= MaxClients; i++)
{
2020-07-07 13:06:04 +02:00
if(IsClientConnected(i) && IsFakeClient(i))
2019-11-06 18:57:55 +01:00
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
2019-11-06 18:57:55 +01:00
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);
2019-11-13 14:44:40 +01:00
iHour = StringToInt(sTime[0]);
2019-11-06 18:57:55 +01:00
2019-11-17 13:17:01 +01:00
if (iHour >= 3 && iHour < 8)
2019-11-06 18:57:55 +01:00
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);
2019-11-06 18:57:55 +01:00
g_iRestartCountdown--;
if(g_iRestartCountdown < 0)
{
2019-11-06 19:02:47 +01:00
LogToFile("addons/sourcemod/logs/restarts.txt", "Successfully auto-restarted the Server.");
2019-11-06 18:57:55 +01:00
ServerCommand("_restart");
return Plugin_Handled;
}
return Plugin_Handled; //Server is dead by here anyway xD
}
public void ForceRestart()
{
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;
LogToFile("addons/sourcemod/logs/restarts.txt", "Successfully force-restarted the Server.");
ServerCommand("_restart");
}
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} Remember to join after couple seconds!");
return Plugin_Continue;
2019-11-06 18:57:55 +01:00
}