forked from UNLOZE/sm-plugins
added new cooldown system that tracks minutes played into database rows and sets maps on cooldown until they fallen down enough of the listing
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
#pragma semicolon 1
|
||||
#define PLUGIN_AUTHOR "jenz"
|
||||
#define PLUGIN_VERSION "1.0"
|
||||
#include <sourcemod>
|
||||
#include <cstrike>
|
||||
#include <mapchooser_extended>
|
||||
|
||||
int g_iPlayerCount_time_track;
|
||||
int g_iTopMapsToMarkWithCooldown;
|
||||
int g_iUnmarkCooldowns;
|
||||
int g_iCurrentPlayerCount;
|
||||
int g_iMapStartTime;
|
||||
Database g_hDatabase;
|
||||
|
||||
public Plugin myinfo =
|
||||
{
|
||||
name = "map_tracker_cooldown",
|
||||
author = PLUGIN_AUTHOR,
|
||||
description = "assigns cooldowns on maps and tracks their played time on the server",
|
||||
version = PLUGIN_VERSION,
|
||||
url = "www.unloze.com"
|
||||
};
|
||||
|
||||
public void OnPluginStart()
|
||||
{
|
||||
if (!g_hDatabase)
|
||||
{
|
||||
Database.Connect(SQL_OnDatabaseConnect, "unloze_playtimestats");
|
||||
}
|
||||
|
||||
ConVar cvar;
|
||||
HookConVarChange((cvar = CreateConVar("mce_min_players_track_time", "20", "Playercount in full number required for tracking minutes spend on a map.")), Cvar_playersCount);
|
||||
g_iPlayerCount_time_track = cvar.IntValue;
|
||||
delete cvar;
|
||||
|
||||
ConVar cvar1;
|
||||
HookConVarChange((cvar1 = CreateConVar("mce_top_ordered_maps_on_cooldown", "30", "These highest listed maps will receive the cooldown boolean")), Cvar_TopMarkedWithCooldown);
|
||||
g_iTopMapsToMarkWithCooldown = cvar1.IntValue;
|
||||
delete cvar1;
|
||||
|
||||
ConVar cvar2;
|
||||
HookConVarChange((cvar2 = CreateConVar("mce_UnmarkCooldowns", "120", "How far down the list does a map need to be for getting unmarked again")), Cvar_UnmarkedCooldown);
|
||||
g_iUnmarkCooldowns = cvar2.IntValue;
|
||||
delete cvar2;
|
||||
|
||||
g_iCurrentPlayerCount = GetClientCount(false);
|
||||
CreateTimer(30.0, CheckPlayerCount, _, TIMER_REPEAT);
|
||||
}
|
||||
|
||||
public Action CheckPlayerCount(Handle timer)
|
||||
{
|
||||
g_iCurrentPlayerCount = GetClientCount(false);
|
||||
return Plugin_Continue;
|
||||
}
|
||||
|
||||
public void Cvar_playersCount(ConVar convar, const char[] oldValue, const char[] newValue)
|
||||
{
|
||||
g_iPlayerCount_time_track = convar.IntValue;
|
||||
}
|
||||
|
||||
public void Cvar_TopMarkedWithCooldown(ConVar convar, const char[] oldValue, const char[] newValue)
|
||||
{
|
||||
g_iTopMapsToMarkWithCooldown = convar.IntValue;
|
||||
}
|
||||
|
||||
public void Cvar_UnmarkedCooldown(ConVar convar, const char[] oldValue, const char[] newValue)
|
||||
{
|
||||
g_iUnmarkCooldowns = convar.IntValue;
|
||||
}
|
||||
|
||||
public void SQL_OnDatabaseConnect(Database db, const char[] error, any data)
|
||||
{
|
||||
if(!db || strlen(error))
|
||||
{
|
||||
LogError("Database error: %s", error);
|
||||
return;
|
||||
}
|
||||
g_hDatabase = db;
|
||||
UpdateTopListedMapsWithCooldown();
|
||||
}
|
||||
|
||||
public void OnMapStart()
|
||||
{
|
||||
int i_port = GetConVarInt(FindConVar("hostport"));
|
||||
if (i_port != 27019)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
g_iMapStartTime = GetTime();
|
||||
if (!g_hDatabase)
|
||||
{
|
||||
Database.Connect(SQL_OnDatabaseConnect, "unloze_playtimestats");
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateTopListedMapsWithCooldown();
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateTopListedMapsWithCooldown()
|
||||
{
|
||||
//just set every map in top g_iTopMapsToMarkWithCooldown on cooldown
|
||||
char sQuery[512];
|
||||
Format(sQuery, sizeof(sQuery), "update unloze_playtimestats.map_minutes_played set on_cooldown = true where mapname in ( SELECT mapname FROM (select mmp.mapname from unloze_playtimestats.map_minutes_played mmp order by mmp.minutes desc limit %i) as top) and on_cooldown is false", g_iTopMapsToMarkWithCooldown);
|
||||
g_hDatabase.Query(SQL_FinishedQuery1, sQuery, _, DBPrio_Low);
|
||||
}
|
||||
|
||||
public void SQL_FinishedQuery1(Database db, DBResultSet results, const char[] error, DataPack data)
|
||||
{
|
||||
delete results;
|
||||
if (!db || strlen(error))
|
||||
{
|
||||
LogError("Query error: %s", error);
|
||||
}
|
||||
delete data;
|
||||
UpdateUnmarkCooldown();
|
||||
}
|
||||
|
||||
public void UpdateUnmarkCooldown()
|
||||
{
|
||||
//if map in listing fell below g_iUnmarkCooldowns then set the bool to false again.
|
||||
char sQuery[512];
|
||||
Format(sQuery, sizeof(sQuery), "update unloze_playtimestats.map_minutes_played set on_cooldown = false where mapname not in ( SELECT mapname FROM (select mmp.mapname from unloze_playtimestats.map_minutes_played mmp order by mmp.minutes desc limit %i) as top) and on_cooldown is true", g_iUnmarkCooldowns);
|
||||
g_hDatabase.Query(SQL_FinishedQuery2, sQuery, _, DBPrio_Low);
|
||||
}
|
||||
|
||||
public void SQL_FinishedQuery2(Database db, DBResultSet results, const char[] error, DataPack data)
|
||||
{
|
||||
delete results;
|
||||
if (!db || strlen(error))
|
||||
{
|
||||
LogError("Query error: %s", error);
|
||||
}
|
||||
delete data;
|
||||
SelectMapsToCooldownForMapChooser();
|
||||
}
|
||||
|
||||
public void SelectMapsToCooldownForMapChooser()
|
||||
{
|
||||
char sQuery[512];
|
||||
Format(sQuery, sizeof(sQuery), "select mapname, map_list_index from (select mapname, ROW_NUMBER() OVER(ORDER BY minutes DESC) as map_list_index, mmp.on_cooldown from unloze_playtimestats.map_minutes_played mmp) as targets where on_cooldown");
|
||||
g_hDatabase.Query(SQL_FinishedQuerySelectMaps, sQuery, _, DBPrio_Low);
|
||||
}
|
||||
|
||||
public void SQL_FinishedQuerySelectMaps(Database db, DBResultSet results, const char[] error, DataPack data)
|
||||
{
|
||||
if (!db || strlen(error))
|
||||
{
|
||||
LogError("Query error in SQL_FinishedQuerySelectMaps: %s", error);
|
||||
delete results;
|
||||
return;
|
||||
}
|
||||
|
||||
while (results.RowCount && results.FetchRow())
|
||||
{
|
||||
char mapname[256];
|
||||
results.FetchString(0, mapname, sizeof(mapname));
|
||||
int map_list_index = results.FetchInt(1);
|
||||
ExcludeMapListingPriority(mapname, g_iUnmarkCooldowns - map_list_index);
|
||||
}
|
||||
delete results;
|
||||
}
|
||||
|
||||
public void OnMapEnd()
|
||||
{
|
||||
int i_port = GetConVarInt(FindConVar("hostport"));
|
||||
if (i_port != 27019)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (g_iCurrentPlayerCount > g_iPlayerCount_time_track)
|
||||
{
|
||||
int elapsed = GetTime() - g_iMapStartTime;
|
||||
int minutes = RoundToFloor(elapsed / 60.0);
|
||||
LogMessage("minutes: %i", minutes);
|
||||
|
||||
char Mapname[128];
|
||||
GetCurrentMap(Mapname, sizeof(Mapname));
|
||||
|
||||
char sQuery[512];
|
||||
Format(sQuery, sizeof(sQuery), "INSERT INTO `map_minutes_played` (`mapname`, `minutes`, `updated_on`) VALUES ('%s', '%i', NOW()) ON DUPLICATE KEY UPDATE `minutes` = `minutes` + '%i', `updated_on` = NOW()", Mapname, minutes, minutes);
|
||||
g_hDatabase.Query(SQL_FinishedQuery, sQuery, _, DBPrio_Low);
|
||||
}
|
||||
}
|
||||
|
||||
public void SQL_FinishedQuery(Database db, DBResultSet results, const char[] error, DataPack data)
|
||||
{
|
||||
delete results;
|
||||
if (!db || strlen(error))
|
||||
{
|
||||
LogError("Query error: %s", error);
|
||||
}
|
||||
delete data;
|
||||
}
|
||||
Reference in New Issue
Block a user