#pragma semicolon 1
#include <amxmodx>
#include <amxmisc>
#include <reapi>
#include <sqlx>
new g_iStartTime, g_iMapTime, /*g_iNumPlayers,*/ g_szMapName[64], g_iMapGames, g_iTotalTime, g_szLastDate[32];
new Handle:sql;
new const db_name[] = "popular_maps";
new const table_maps[] = "maps_data";
#define MAX_QUERY_LENGTH 1024
enum _:TOTAL_SQL_TYPES {
SQL_TYPE_IGNORE,
SQL_TYPE_MAPDATA,
SQL_TYPE_MAPSTOP
};
public plugin_init() {
register_plugin("Popular Maps", "1.0", "MirDobro777");
RegisterHookChain(RG_CBasePlayer_Spawn, "on_player_spawn_post", .post = true);
RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", .post = false);
g_iStartTime = get_systime();
get_mapname(g_szMapName, charsmax(g_szMapName));
set_task(12.0, "CheckMap");
}
public on_restart_round_pre() {
if(get_member_game(m_bCompleteReset) && g_iMapTime < 1200) {
g_iStartTime = get_systime();
}
}
public on_player_spawn_post(id) {
g_iMapTime = get_systime() - g_iStartTime;
}
public OnConfigsExecuted() {
SQL_SetAffinity("sqlite");
sql = SQL_MakeDbTuple("", "", "", db_name); // Создание картежа данных для соединения с БД
SQL_SetCharset(sql, "utf8");
// запрос на создание таблицы
new szQuery[MAX_QUERY_LENGTH], szData[1] = SQL_TYPE_IGNORE;
formatex(szQuery, charsmax(szQuery), "CREATE TABLE IF NOT EXISTS `%s` (\
`mapname` TEXT NOT NULL,\
`mapgames` INTEGER NOT NULL DEFAULT 0,\
`maptime` INTEGER NOT NULL DEFAULT 0,\
`maplast` TEXT NOT NULL,\
PRIMARY KEY (mapname))", table_maps);
SQL_ThreadQuery(sql, "Sql_Handler", szQuery, szData, sizeof szData);
}
public CheckMap() {
new szQuery[MAX_QUERY_LENGTH], szData[1] = SQL_TYPE_MAPDATA;
formatex(szQuery, charsmax(szQuery), "SELECT * FROM `%s` WHERE `mapname` = '%s'", table_maps, g_szMapName);
SQL_ThreadQuery(sql, "Sql_Handler", szQuery, szData, sizeof szData);
}
public TopMapsGame() {
new szQuery[256], szData[1];
formatex(szQuery, charsmax(szQuery),"\
SELECT * FROM %s WHERE `mapgames` > 0 ORDER BY `mapgames` DESC LIMIT 10\
", table_maps);
szData[0] = SQL_TYPE_MAPSTOP;
// szData[1] = id;
SQL_ThreadQuery(sql, "Sql_Handler", szQuery, szData, sizeof szData);
return PLUGIN_HANDLED;
}
public UpdateMapGames() {
new szQuery[MAX_QUERY_LENGTH], szData[1] = SQL_TYPE_IGNORE;
formatex(szQuery, charsmax(szQuery),"\
UPDATE `%s` SET\
`mapgames` = '%d',\
`maptime` = '%d',\
`maplast` = '%s'\
WHERE `mapname` = '%s'\
", table_maps, g_iMapGames, g_iTotalTime, g_szLastDate, g_szMapName);
SQL_ThreadQuery(sql, "Sql_Handler", szQuery, szData, sizeof szData);
}
public Sql_Handler(iFailState, Handle:Query, szError[], iErrNum, szData[], size, Float:flQueryTime) {
switch(iFailState) {
case TQUERY_CONNECT_FAILED: {
log_to_file("fail_sql.txt" , "^nConnection failed^nError: #%d^n%s^n", iErrNum, szError);
return;
}
case TQUERY_QUERY_FAILED: {
log_to_file("fail_sql.txt" , "^nQuery failed^nError: #%d^n%s", iErrNum, szError);
new szLastQuery[96];
SQL_GetQueryString(Query, szLastQuery, charsmax(szLastQuery));
log_to_file("fail_sql.txt", "^nLast Query: %s^n", szLastQuery);
return;
}
}
new iDataType = szData[0];
switch(iDataType) {
case SQL_TYPE_MAPDATA: {
if(!SQL_NumResults(Query)) {
new szQuery[MAX_QUERY_LENGTH], szData[1] = SQL_TYPE_IGNORE;
formatex(szQuery, charsmax(szQuery), "INSERT INTO `%s` (`mapname`, `mapgames`, `maptime`, `maplast`)\
VALUES ('%s', '0', '0', '0')\
", table_maps, g_szMapName);
SQL_ThreadQuery(sql, "Sql_Handler", szQuery, szData, sizeof szData);
} else {
g_iMapGames = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "mapgames"));
g_iTotalTime = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "maptime"));
SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "maplast"), g_szLastDate, charsmax(g_szLastDate));
TopMapsGame();
}
}
case SQL_TYPE_MAPSTOP: {
new iNum, szMapName[64], iGames, iTime, szTime[32], szLast[32];
while(SQL_MoreResults(Query)) {
iNum++;
SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "mapname"), szMapName, charsmax(szMapName));
iGames = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "mapgames"));
iTime = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "maptime"));
SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "maplast"), szLast, charsmax(szLast));
total_time(szTime, charsmax(szTime), iTime);
log_to_file("top_maps.txt", "^n%d., %s^nИгр: %d^nВсего: %s^nДата: %s^n", iNum, szMapName, iGames, szTime, szLast);
SQL_NextRow(Query);
}
}
}
}
public plugin_end() {
if(g_iMapTime >= 1200) {
g_iMapGames++;
g_iTotalTime += g_iMapTime;
last_date(get_systime());
UpdateMapGames();
}
}
total_time(szTime[], iLen, iTime) {
new iD, iH, iM;
iD = iTime / 86400;
iH = (iTime - iD * 86400) / 3600;
iM = (iTime - iD * 86400 - iH * 3600) / 60;
formatex(szTime, iLen, "%d д. %d ч. %d м.", iD, iH, iM);
}
last_date(iTime) {
new szDate[32], szDay[3];
format_time(szDate, charsmax(szDate), "%d.%m.%Y - %H:%M:%S", iTime);
format_time(szDay, charsmax(szDay), "%w", iTime);
if(equal(szDay, "0")) formatex(g_szLastDate, charsmax(g_szLastDate), "%s, Вс", szDate);
else if(equal(szDay, "1")) formatex(g_szLastDate, charsmax(g_szLastDate), "%s, Пн", szDate);
else if(equal(szDay, "2")) formatex(g_szLastDate, charsmax(g_szLastDate), "%s, Вт", szDate);
else if(equal(szDay, "3")) formatex(g_szLastDate, charsmax(g_szLastDate), "%s, Ср", szDate);
else if(equal(szDay, "4")) formatex(g_szLastDate, charsmax(g_szLastDate), "%s, Чт", szDate);
else if(equal(szDay, "5")) formatex(g_szLastDate, charsmax(g_szLastDate), "%s, Пт", szDate);
else if(equal(szDay, "6")) formatex(g_szLastDate, charsmax(g_szLastDate), "%s, Сб", szDate);
}