Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified plugins/server-refresh.smx
Binary file not shown.
208 changes: 184 additions & 24 deletions scripting/server-refresh.sp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ int
g_iWeeklyRestart,
g_iWeeklyRestartWait,
g_iWeeklyRestartType,
g_iWeeklyRestartIgnorePlayers;
g_iWeeklyRestartIgnorePlayers,
g_iLastHourlyRestart,
g_iLastDailyRestart,
g_iLastWeeklyRestart;

char
g_sHourlyRestartTime[32],
Expand Down Expand Up @@ -104,7 +107,7 @@ public void OnPluginStart()

cvWeeklyRestartDay = CreateConVar("sm_restart_weekly_day", "sun", "On which day should the restart happen in 3 letters (Default = sun)");
cvWeeklyRestartDay.GetString(g_sWeeklyRestartDay, sizeof(g_sWeeklyRestartDay));
strcopy(g_sWeeklyRestartDay, sizeof(g_sWeeklyRestartDay), StringToLower(g_sWeeklyRestartDay));
StringToLower(g_sWeeklyRestartDay);

cvWeeklyRestartTime = CreateConVar("sm_restart_weekly_time", "0500", "At what hour and minute should the restart happen in 4 digits (Minimum = 0000, Maximum = 2359, Default = 0500)");
cvWeeklyRestartTime.GetString(g_sWeeklyRestartTime, sizeof(g_sWeeklyRestartTime));
Expand All @@ -115,6 +118,9 @@ public void OnPluginStart()
cvWeeklyRestartIgnorePlayers = CreateConVar("sm_restart_weekly_ignoreplayers", "0", "Ignore players on weekly restart (0 = No, 1 = Yes, Default = 0)", _, true, 0.0, true, 1.0);
g_iWeeklyRestartIgnorePlayers = cvWeeklyRestartIgnorePlayers.IntValue;

// register admin commands
RegAdminCmd("sm_restart_schedule", Command_ShowSchedule, ADMFLAG_RCON, "Display all configured restart schedules");

// hooking cvar changes
cvRestartMessage.AddChangeHook(OnCvarChanged);
cvRestartMapMessage.AddChangeHook(OnCvarChanged);
Expand All @@ -140,7 +146,7 @@ public void OnPluginStart()
}

// called when a cvar is changed
public int OnCvarChanged(ConVar cvar, const char[] oldValue, const char[] newValue)
public void OnCvarChanged(ConVar cvar, const char[] oldValue, const char[] newValue)
{
// updating cvar values when they change
if (cvar == cvRestartMessage)
Expand Down Expand Up @@ -195,12 +201,15 @@ public int OnCvarChanged(ConVar cvar, const char[] oldValue, const char[] newVal
g_iWeeklyRestartWait = cvWeeklyRestartWait.IntValue;

if (cvar == cvWeeklyRestartDay)
cvWeeklyRestartDay.GetString(g_sDailyRestartTime, sizeof(g_sDailyRestartTime));
cvWeeklyRestartDay.GetString(g_sWeeklyRestartDay, sizeof(g_sWeeklyRestartDay));

if (cvar == cvWeeklyRestartTime)
{
cvWeeklyRestartTime.GetString(g_sWeeklyRestartTime, sizeof(g_sWeeklyRestartTime));
strcopy(g_sWeeklyRestartTime, sizeof(g_sWeeklyRestartTime), StringToLower(g_sWeeklyRestartTime));

if (cvar == cvWeeklyRestartDay)
{
cvWeeklyRestartDay.GetString(g_sWeeklyRestartDay, sizeof(g_sWeeklyRestartDay));
StringToLower(g_sWeeklyRestartDay);
}

if (cvar == cvWeeklyRestartType)
Expand Down Expand Up @@ -247,14 +256,24 @@ public Action Timer_HourlyRestart(Handle hTimer)
// check the hourly restart time
if (StrEqual(sTime, g_sHourlyRestartTime))
{
// if there is nobody in the server or it should be ignored
if (GetConnectedPlayerCount() == 0 || g_iHourlyRestartIgnorePlayers == 1)
// get current timestamp for cooldown check
int iCurrentTime = GetTime();

// check cooldown (prevent multiple restarts in same minute)
if (iCurrentTime - g_iLastHourlyRestart >= 61)
{
// restart
ExecuteRestart(g_iHourlyRestartType, g_iHourlyRestartWait);
// if there is nobody in the server or it should be ignored
if (GetConnectedPlayerCount() == 0 || g_iHourlyRestartIgnorePlayers == 1)
{
// update last restart time
g_iLastHourlyRestart = iCurrentTime;
// restart
ExecuteRestart(g_iHourlyRestartType, g_iHourlyRestartWait);
}
}
}
}
return Plugin_Continue;
}

// timer function for restarting daily
Expand All @@ -270,13 +289,23 @@ public Action Timer_DailyRestart(Handle hTimer)
// check the daily restart time
if (StrEqual(sTime, g_sDailyRestartTime))
{
// if there is nobody in the server or it should be ignored
if (GetConnectedPlayerCount() == 0 || g_iDailyRestartIgnorePlayers == 1)
// get current timestamp for cooldown check
int iCurrentTime = GetTime();

// check cooldown (prevent multiple restarts in same minute)
if (iCurrentTime - g_iLastDailyRestart >= 61)
{
ExecuteRestart(g_iDailyRestartType, g_iDailyRestartWait);
// if there is nobody in the server or it should be ignored
if (GetConnectedPlayerCount() == 0 || g_iDailyRestartIgnorePlayers == 1)
{
// update last restart time
g_iLastDailyRestart = iCurrentTime;
ExecuteRestart(g_iDailyRestartType, g_iDailyRestartWait);
}
}
}
}
return Plugin_Continue;
}

// timer function for restarting weekly
Expand Down Expand Up @@ -315,21 +344,31 @@ public Action Timer_WeeklyRestart(Handle hTimer)
// check the weekly restart day
if (StringToInt(sTime) == iTime)
{
// get the current minute
FormatTime(sTime, sizeof(sTime), "%M");
// get the current time for hour and minute check
FormatTime(sTime, sizeof(sTime), "%H%M");

// check the weekly restart hour time
// check the weekly restart time
if (StrEqual(sTime, g_sWeeklyRestartTime))
{
// if there is nobody in the server or it should be ignored
if (GetConnectedPlayerCount() == 0 || g_iWeeklyRestartIgnorePlayers == 1)
// get current timestamp for cooldown check
int iCurrentTime = GetTime();

// check cooldown (prevent multiple restarts in same minute)
if (iCurrentTime - g_iLastWeeklyRestart >= 61)
{
// restart
ExecuteRestart(g_iWeeklyRestartType, g_iWeeklyRestartWait);
// if there is nobody in the server or it should be ignored
if (GetConnectedPlayerCount() == 0 || g_iWeeklyRestartIgnorePlayers == 1)
{
// update last restart time
g_iLastWeeklyRestart = iCurrentTime;
// restart
ExecuteRestart(g_iWeeklyRestartType, g_iWeeklyRestartWait);
}
}
}
}
}
return Plugin_Continue;
}

// function for restarting
Expand Down Expand Up @@ -411,17 +450,138 @@ public Action Timer_Restart(Handle hTimer, int iType)
return Plugin_Handled;
}

// command handler for displaying restart schedules
public Action Command_ShowSchedule(int client, int args)
{
char sBuffer[1024];
char sTemp[256];

// header
strcopy(sBuffer, sizeof(sBuffer), "\n===== SERVER RESTART SCHEDULE CONFIGURATION =====\n");

// empty server restart
if (g_iEmptyRestart == 1)
{
Format(sTemp, sizeof(sTemp), "\n[EMPTY] Server restart when empty: ENABLED");
StrCat(sBuffer, sizeof(sBuffer), sTemp);
Format(sTemp, sizeof(sTemp), "\n └─ Wait time: %d seconds", g_iEmptyRestartWait);
StrCat(sBuffer, sizeof(sBuffer), sTemp);
Format(sTemp, sizeof(sTemp), "\n └─ Restart type: %s", (g_iEmptyRestartType == 0) ? "Map change" : "Server restart");
StrCat(sBuffer, sizeof(sBuffer), sTemp);
}
else
{
StrCat(sBuffer, sizeof(sBuffer), "\n[EMPTY] Server restart when empty: DISABLED");
}

// hourly restart
if (g_iHourlyRestart == 1)
{
Format(sTemp, sizeof(sTemp), "\n\n[HOURLY] Hourly restart: ENABLED");
StrCat(sBuffer, sizeof(sBuffer), sTemp);
Format(sTemp, sizeof(sTemp), "\n └─ Time: Every hour at minute %s", g_sHourlyRestartTime);
StrCat(sBuffer, sizeof(sBuffer), sTemp);
Format(sTemp, sizeof(sTemp), "\n └─ Wait time: %d seconds", g_iHourlyRestartWait);
StrCat(sBuffer, sizeof(sBuffer), sTemp);
Format(sTemp, sizeof(sTemp), "\n └─ Restart type: %s", (g_iHourlyRestartType == 0) ? "Map change" : "Server restart");
StrCat(sBuffer, sizeof(sBuffer), sTemp);
Format(sTemp, sizeof(sTemp), "\n └─ Ignore players: %s", (g_iHourlyRestartIgnorePlayers == 1) ? "YES (restart even with players)" : "NO (only when empty)");
StrCat(sBuffer, sizeof(sBuffer), sTemp);
}
else
{
StrCat(sBuffer, sizeof(sBuffer), "\n\n[HOURLY] Hourly restart: DISABLED");
}

// daily restart
if (g_iDailyRestart == 1)
{
Format(sTemp, sizeof(sTemp), "\n\n[DAILY] Daily restart: ENABLED");
StrCat(sBuffer, sizeof(sBuffer), sTemp);
char sHour[3], sMinute[3];
sHour[0] = g_sDailyRestartTime[0];
sHour[1] = g_sDailyRestartTime[1];
sHour[2] = '\0';
sMinute[0] = g_sDailyRestartTime[2];
sMinute[1] = g_sDailyRestartTime[3];
sMinute[2] = '\0';
Format(sTemp, sizeof(sTemp), "\n └─ Time: Every day at %s:%s", sHour, sMinute);
StrCat(sBuffer, sizeof(sBuffer), sTemp);
Format(sTemp, sizeof(sTemp), "\n └─ Wait time: %d seconds", g_iDailyRestartWait);
StrCat(sBuffer, sizeof(sBuffer), sTemp);
Format(sTemp, sizeof(sTemp), "\n └─ Restart type: %s", (g_iDailyRestartType == 0) ? "Map change" : "Server restart");
StrCat(sBuffer, sizeof(sBuffer), sTemp);
Format(sTemp, sizeof(sTemp), "\n └─ Ignore players: %s", (g_iDailyRestartIgnorePlayers == 1) ? "YES (restart even with players)" : "NO (only when empty)");
StrCat(sBuffer, sizeof(sBuffer), sTemp);
}
else
{
StrCat(sBuffer, sizeof(sBuffer), "\n\n[DAILY] Daily restart: DISABLED");
}

// weekly restart
if (g_iWeeklyRestart == 1)
{
char sDayName[32];
if (StrEqual(g_sWeeklyRestartDay, "sun")) strcopy(sDayName, sizeof(sDayName), "Sunday");
else if (StrEqual(g_sWeeklyRestartDay, "mon")) strcopy(sDayName, sizeof(sDayName), "Monday");
else if (StrEqual(g_sWeeklyRestartDay, "tue")) strcopy(sDayName, sizeof(sDayName), "Tuesday");
else if (StrEqual(g_sWeeklyRestartDay, "wed")) strcopy(sDayName, sizeof(sDayName), "Wednesday");
else if (StrEqual(g_sWeeklyRestartDay, "thu")) strcopy(sDayName, sizeof(sDayName), "Thursday");
else if (StrEqual(g_sWeeklyRestartDay, "fri")) strcopy(sDayName, sizeof(sDayName), "Friday");
else if (StrEqual(g_sWeeklyRestartDay, "sat")) strcopy(sDayName, sizeof(sDayName), "Saturday");
else strcopy(sDayName, sizeof(sDayName), "Unknown");

Format(sTemp, sizeof(sTemp), "\n\n[WEEKLY] Weekly restart: ENABLED");
StrCat(sBuffer, sizeof(sBuffer), sTemp);
Format(sTemp, sizeof(sTemp), "\n └─ Day: %s", sDayName);
StrCat(sBuffer, sizeof(sBuffer), sTemp);
char sWeeklyHour[3], sWeeklyMinute[3];
sWeeklyHour[0] = g_sWeeklyRestartTime[0];
sWeeklyHour[1] = g_sWeeklyRestartTime[1];
sWeeklyHour[2] = '\0';
sWeeklyMinute[0] = g_sWeeklyRestartTime[2];
sWeeklyMinute[1] = g_sWeeklyRestartTime[3];
sWeeklyMinute[2] = '\0';
Format(sTemp, sizeof(sTemp), "\n └─ Time: %s:%s", sWeeklyHour, sWeeklyMinute);
StrCat(sBuffer, sizeof(sBuffer), sTemp);
Format(sTemp, sizeof(sTemp), "\n └─ Wait time: %d seconds", g_iWeeklyRestartWait);
StrCat(sBuffer, sizeof(sBuffer), sTemp);
Format(sTemp, sizeof(sTemp), "\n └─ Restart type: %s", (g_iWeeklyRestartType == 0) ? "Map change" : "Server restart");
StrCat(sBuffer, sizeof(sBuffer), sTemp);
Format(sTemp, sizeof(sTemp), "\n └─ Ignore players: %s", (g_iWeeklyRestartIgnorePlayers == 1) ? "YES (restart even with players)" : "NO (only when empty)");
StrCat(sBuffer, sizeof(sBuffer), sTemp);
}
else
{
StrCat(sBuffer, sizeof(sBuffer), "\n\n[WEEKLY] Weekly restart: DISABLED");
}

StrCat(sBuffer, sizeof(sBuffer), "\n\n===================================================");

// display to client
if (client == 0)
{
PrintToServer(sBuffer);
}
else
{
PrintToConsole(client, sBuffer);
PrintToChat(client, "[Server Refresh] Schedule information printed to console. Check your console!");
}

return Plugin_Handled;
}

// stock for converting a string to lower case
stock char StringToLower(char[] sFormat)
stock void StringToLower(char[] sFormat)
{
for (int i = 0; i < strlen(sFormat); i++)
{
if (sFormat[i] != '\0') {
sFormat[i] = CharToLower(sFormat[i]);
}
}

return sFormat;
}

// stock for getting the number of players connected in the server
Expand Down Expand Up @@ -453,5 +613,5 @@ public Plugin myinfo =
name = "Titan 2 - Server Refresh",
description = "All inclusive server restart features in one plugin, ensures your server is always refreshed and prepared for the next load.",
author = "myst",
version = "2.0",
version = "2.1",
}