#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#pragma tabsize 0
new Handle:g_hConVar_iSelectCommands;
new g_iSelectCommands;
new Handle:g_hConVar_iMin;
new g_iMin;
new Handle:g_hConVar_iMax;
new g_iMax;
new Handle:g_hConVar_iDelayTime;
new g_iDelay;
new Handle:g_hSQLdb;
new String:g_sAuth[66][32];
new g_iLastTime[66];
public OnPluginStart()
{
g_hConVar_iSelectCommands = CreateConVar("thc_rpg_bonus_choicebonus", "1", "Получаемый бонус\n1 Уровень\n2 Опыт\n3 Кредиты", 0, false, 0.0, false, 0.0);
g_hConVar_iMin = CreateConVar("thc_rpg_bonus_minvalue", "1", "Минимальное значение получаемого бонуса.", 0, false, 0.0, false, 0.0);
g_hConVar_iMax = CreateConVar("thc_rpg_bonus_maxvalue", "3", "Максимальное значение получаемого бонуса.", 0, false, 0.0, false, 0.0);
g_hConVar_iDelayTime = CreateConVar("thc_rpg_bonus_delaytime", "86400", "Через сколько секунд игрок может получить следующий бонус.\n86400 = 1 день.", 0, false, 0.0, false, 0.0);
RegServerCmd("thc_rpg_bonus_trygivebonus", Command_TryGiveBonus, "<userid>", 0);
AutoExecConfig(true, "thc_rpg_bonus", "sourcemod");
}
public OnConfigsExecuted()
{
decl String:sBuffer[256];
g_hSQLdb = SQLite_UseDatabase("thc_rpg_bonus", sBuffer, 255);
if(g_hSQLdb == INVALID_HANDLE)
{
LogError("SQLite connection failed: %s", sBuffer);
SetFailState("SQLite connection failed: %s", sBuffer);
}
SQL_LockDatabase(g_hSQLdb);
SQL_FastQuery(g_hSQLdb, "CREATE TABLE IF NOT EXISTS `bonuses` (`auth` TEXT(32), `time` INTEGER);");
SQL_UnlockDatabase(g_hSQLdb);
g_iSelectCommands = GetConVarInt(g_hConVar_iSelectCommands);
g_iMin = GetConVarInt(g_hConVar_iMin);
g_iMax = GetConVarInt(g_hConVar_iMax);
g_iDelay = GetConVarInt(g_hConVar_iDelayTime);
return 0;
}
public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
{
CreateNative("TryGiveBonus", Native_TryGiveBonus);
return APLRes:0;
}
public OnClientPutInServer(iClient)
{
if (!IsFakeClient(iClient) && !IsClientSourceTV(iClient))
{
GetClientAuthString(iClient, g_sAuth[iClient], 31, true);
g_iLastTime[iClient] = 0;
}
return 0;
}
public Action:Command_TryGiveBonus(iArgs)
{
decl String:sUserId[8];
GetCmdArg(1, sUserId, 7);
new iClient = GetClientOfUserId(StringToInt(sUserId, 10));
if (0 < iClient)
{
TryGiveBonus(iClient);
}
return Action:0;
}
TryGiveBonus(iClient)
{
new iCurrentTime = GetTime({0,0});
if (g_iLastTime[iClient] > iCurrentTime)
{
PrintToConsole(iClient, "пожалуйста, не спамьте!");
PrintToChat(iClient, "пожалуйста, не спамьте!");
return 0;
}
g_iLastTime[iClient] = iCurrentTime + 2;
decl String:sQuery[256];
Format(sQuery, 255, "SELECT `time` FROM `bonuses` WHERE `auth` = '%s'", g_sAuth[iClient]);
SQL_TQuery(g_hSQLdb, SQL_OnTryGiveBonus, sQuery, iClient, DBPriority:1);
return 0;
}
public Native_TryGiveBonus(Handle:hPlugins, iNumParams)
{
new iClient = GetNativeCell(1);
if (iClient < 1 || iClient > MaxClients)
{
return ThrowNativeError(23, "Invalid client index (%d)", iClient);
}
if (!IsClientInGame(iClient))
{
return ThrowNativeError(23, "Client %d is not in game", iClient);
}
TryGiveBonus(iClient);
return 1;
}
public SQL_OnTryGiveBonus(Handle:hOwner, Handle:hQuery, String:sError[], any:iClient)
{
if (hQuery)
{
decl String:sQuery[256];
if (SQL_FetchRow(hQuery))
{
new zero1 = 0;
new zero2 = 0;
new iCurrentTime = GetTime({0,0});
new iExpiriedTime = SQL_FetchInt(hQuery, zero1, zero2);
if (iExpiriedTime < iCurrentTime)
{
iCurrentTime = g_iDelay + iCurrentTime;
Format(sQuery, 255, "UPDATE `bonuses` SET `time` = %d WHERE `auth` = '%s'", iCurrentTime, g_sAuth[iClient]);
SQL_TQuery(g_hSQLdb, SQL_OnEmptyCallback, sQuery, any:0, DBPriority:1);
GiveBonus(iClient);
}
else
{
decl String:sExpiriedTime[64];
decl String:sCurrentTime[64];
FormatTime(sExpiriedTime, 63, "%d/%m/%Y - %H:%M", iExpiriedTime);
FormatTime(sCurrentTime, 63, "%d/%m/%Y - %H:%M", iCurrentTime);
PrintToChat(iClient, "Вы сможете получить бонус не раньше %s, сейчас %s", sExpiriedTime, sCurrentTime);
ClientCommand(iClient, "play buttons/button11.wav");
}
}
else
{
Format(sQuery, 255, "INSERT INTO `bonuses` (`auth`, `time`) VALUES ('%s', %d)", g_sAuth[iClient], GetTime({0,0}) + g_iDelay);
SQL_TQuery(g_hSQLdb, SQL_OnEmptyCallback, sQuery, any:0, DBPriority:1);
GiveBonus(iClient);
}
}
return 0;
}
public SQL_OnEmptyCallback(Handle:hOwner, Handle:hQuery, String:sError[], any:data)
{
return 0;
}
GiveBonus(iClient)
{
new iBonus = GetRandomInt(g_iMin, g_iMax);
g_iSelectCommands = GetConVarInt(g_hConVar_iSelectCommands);
switch(g_iSelectCommands)
{
case 1:
{
ClientCommand(iClient, "play ambient/weather/rain_drip5.wav");
ServerCommand("thc_rpg_level add @%d %d", GetClientUserId(iClient), iBonus);
PrintToChat(iClient, "Ваш бонус\x01: \x03%d уровней", iBonus);
}
case 2:
{
ClientCommand(iClient, "play ambient/weather/rain_drip5.wav");
ServerCommand("thc_rpg_xp add @%d %d", GetClientUserId(iClient), iBonus);
PrintToChat(iClient, "Ваш бонус\x01: \x03%d опыта", iBonus);
}
case 3:
{
ClientCommand(iClient, "play ambient/weather/rain_drip5.wav");
ServerCommand("thc_rpg_credits add @%d %d", GetClientUserId(iClient), iBonus);
PrintToChat(iClient, "Ваш бонус\x01: \x03%d кредитов", iBonus);
}
default:
{
}
}
}