/*
* Daily Config Loader (olds.ro workshop card LIB-1534)
*
* Executes a different config file for each day of the week, so weekends
* can have other rules or settings than weekdays:
* addons/amxmodx/configs/daily/mon.cfg, tue.cfg, wed.cfg, thu.cfg,
* fri.cfg, sat.cfg, sun.cfg
* The day is read from the server clock.
*
* When the file runs:
* - on the first map after the server starts, always;
* - with olds_daily_recheck 1, also on every map change (server.cfg runs
* again then and would undo the day's settings) and at midnight, when
* the next day's file runs (checked once a minute).
* The file is executed at plugin_cfg, after server.cfg and amxx.cfg, so its
* settings win over theirs; per-map configs (configs/maps/<map>.cfg) still
* run after it. A missing file is skipped: one line in the AMXX log says so,
* and the same missing file is not logged again on later maps.
*
* Cvar (saved with its description in configs/plugins/olds_dailycfg.cfg the
* first time the plugin starts):
* olds_daily_recheck 1 1 = run the day's file again on every map change
* and switch files at midnight; 0 = only on the
* first map after the server starts.
*
* Admin command (ADMIN_CFG, flag "h"; also from the server console):
* amx_dailycfg today's day and file, what ran last
* amx_dailycfg apply execute today's file now
*
* Requires AMX Mod X 1.10 or newer. No extra modules; ReAPI is not used.
*
* Copyright (C) 2026 alexit (olds.ro workshop)
* SPDX-License-Identifier: GPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details: <https://www.gnu.org/licenses/>.
*/
#include <amxmodx>
#include <amxmisc>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Daily Config Loader";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
new const DICTIONARY[] = "olds_dailycfg.txt";
// File names by get_time("%w"): 0 = Sunday ... 6 = Saturday.
new const DAY_FILES[7][] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
// Day names for the AMXX log (the log is not translated).
new const DAY_LOG_NAMES[7][] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
// Dictionary keys of the day names shown to admins.
new const DAY_KEYS[7][] =
{
"OLDS_DAILY_SUN", "OLDS_DAILY_MON", "OLDS_DAILY_TUE", "OLDS_DAILY_WED",
"OLDS_DAILY_THU", "OLDS_DAILY_FRI", "OLDS_DAILY_SAT"
};
// Engine localinfo keys. Unlike plugin variables they keep their value over
// map changes, until the server stops.
new const INFO_LAST_DAY[] = "olds_daily_day"; // day whose file was last handled
new const INFO_LOGGED_MISSING[] = "olds_daily_miss"; // day whose missing file was logged
// Seconds between two checks for a new day.
const Float:CHECK_INTERVAL = 60.0;
const TASK_CHECK = 1534;
const NO_DAY = -1;
new g_recheck; // olds_daily_recheck
// Day whose file this map handled; NO_DAY until then.
new g_currentDay = NO_DAY;
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
register_dictionary(DICTIONARY);
bind_pcvar_num(create_cvar("olds_daily_recheck", "1", FCVAR_NONE,
"1 = run the day's config again on every map change and at midnight, 0 = only on the first map",
true, 0.0, true, 1.0), g_recheck);
AutoExecConfig(true, "olds_dailycfg");
register_concmd("amx_dailycfg", "cmd_dailycfg", ADMIN_CFG, "OLDS_DAILY_HELP", -1, true);
}
public plugin_cfg()
{
daily_map_start(current_weekday());
set_task(CHECK_INTERVAL, "task_check", TASK_CHECK, .flags = "b");
}
/**
* Map start. Public so that a test can call it with any weekday.
*/
public daily_map_start(weekday)
{
if (!is_weekday(weekday))
return;
new lastDay = read_day_info(INFO_LAST_DAY);
// lastDay is NO_DAY only on the first map after the server started.
if (lastDay != NO_DAY && !g_recheck)
{
g_currentDay = lastDay;
server_print("[olds_dailycfg] olds_daily_recheck is 0: no daily config is executed on this map.");
return;
}
execute_day(weekday, lastDay != weekday);
}
/**
* Runs once a minute: after midnight, executes the new day's file.
*/
public task_check()
{
daily_recheck(current_weekday());
}
/**
* Re-check. Public so that a test can call it with any weekday.
*/
public daily_recheck(weekday)
{
if (!g_recheck || !is_weekday(weekday) || weekday == g_currentDay)
return;
execute_day(weekday, true);
}
public cmd_dailycfg(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED;
new today = current_weekday();
new path[PLATFORM_MAX_PATH];
day_path(today, path, charsmax(path));
new argument[16];
read_argv(1, argument, charsmax(argument));
if (equali(argument, "apply"))
{
if (execute_day(today, today != g_currentDay))
console_print(id, "%L", id, "OLDS_DAILY_EXECUTED", path);
else
console_print(id, "%L", id, "OLDS_DAILY_NOT_FOUND", path);
return PLUGIN_HANDLED;
}
if (argument[0])
{
console_print(id, "%L", id, "OLDS_DAILY_USAGE");
return PLUGIN_HANDLED;
}
console_print(id, "%L", id, "OLDS_DAILY_STATUS", id, DAY_KEYS[today], path,
id, file_exists(path) ? "OLDS_DAILY_FOUND" : "OLDS_DAILY_MISSING");
if (g_currentDay == NO_DAY)
console_print(id, "%L", id, "OLDS_DAILY_LAST_NONE");
else
console_print(id, "%L", id, "OLDS_DAILY_LAST", id, DAY_KEYS[g_currentDay], DAY_FILES[g_currentDay]);
console_print(id, "%L", id, g_recheck ? "OLDS_DAILY_RECHECK_ON" : "OLDS_DAILY_RECHECK_OFF");
return PLUGIN_HANDLED;
}
/**
* Executes the day's file if it exists. newDay = the first time on this day,
* which is written to the AMXX log; later runs only go to the console.
* Returns true if the file was executed.
*/
bool:execute_day(weekday, bool:newDay)
{
new path[PLATFORM_MAX_PATH];
day_path(weekday, path, charsmax(path));
g_currentDay = weekday;
write_day_info(INFO_LAST_DAY, weekday);
if (!file_exists(path))
{
if (read_day_info(INFO_LOGGED_MISSING) != weekday)
{
log_amx("No daily config for %s: %s does not exist, nothing was executed.",
DAY_LOG_NAMES[weekday], path);
write_day_info(INFO_LOGGED_MISSING, weekday);
}
return false;
}
// Found again: should it go missing later, that is logged once more.
write_day_info(INFO_LOGGED_MISSING, NO_DAY);
server_cmd("exec %s", path);
if (newDay)
log_amx("Executed the daily config for %s: %s", DAY_LOG_NAMES[weekday], path);
else
server_print("[olds_dailycfg] Executed the daily config for %s again: %s", DAY_LOG_NAMES[weekday], path);
return true;
}
day_path(weekday, path[], maxLength)
{
get_configsdir(path, maxLength);
format(path, maxLength, "%s/daily/%s.cfg", path, DAY_FILES[weekday]);
}
current_weekday()
{
new text[4];
get_time("%w", text, charsmax(text));
return str_to_num(text);
}
bool:is_weekday(weekday)
{
return 0 <= weekday <= 6;
}
read_day_info(const key[])
{
new text[4];
get_localinfo(key, text, charsmax(text));
if (!text[0])
return NO_DAY;
new weekday = str_to_num(text);
return is_weekday(weekday) ? weekday : NO_DAY;
}
write_day_info(const key[], weekday)
{
new text[4];
if (is_weekday(weekday))
num_to_str(weekday, text, charsmax(text));
set_localinfo(key, text);
}