#include <amxmodx>
#include <sqlx>
#include <csstats>
#pragma tabsize 0 /* Впадлу.... */
/* константы */
#define VIP_TEST_HOUR 3 ////на сколько времени выдать права(в часах)
#define S_MINUTE 3600
#define S_HOUR 3600
#define DB_SAVEBY_STEAMID 0
#define DB_SAVEBY_IP 1
#define DB_SAVEBY_NAME 2
#define SAVE_TYPE 2 //0 - по стимид 1 -по айпи 2 - по нику
#define DB_SAVE 2 //1-sqlite 2- mysql
#define MAX_PLAYER 32
#define MIN_RANK 1500 /* Чтобы новые аккаунты не брали */
new const szTableName[] = "vip_test" //Название таблицы
new const szDBHost[] = "94.198.51.162" // db1.myarena.ru
new const szDBUser[] = "bd_4877" //Пользователь
new const szDBPass[] = "1413696" //Пароль от бд
new const szBDName[] = "bd_4877" //название бд
#define VIP_FLAG "tm" //Какие флаги выдавать?
/* Переменные */
new szKeyName[16];
new bool:PlayerAlreadyUseVip[MAX_PLAYER+1];
new EndTime[MAX_PLAYER+1];
new Handle:g_SqlTuple;
new g_Error[512];
public plugin_init()
{
register_plugin("Vip Test", "1.0", "daywer")
/* команда вызова */
register_clcmd("say /viptest","CmdVipTest")
register_clcmd("say viptest","CmdVipTest")
/* Меню */
register_menucmd(register_menuid("\yV.I.P тест"), 1023, "hnd_viptest_menu")
/* Для сохранения*/
DB_GetKeyName( szKeyName, 15 );
switch(DB_SAVE)
{
case 1:SQL_SetAffinity( "sqlite" );
case 2:SQL_SetAffinity( "mysql" );
}
}
/* Вызов главного меню*/
public CmdVipTest(idPlayer)
{
if(!is_user_connected(idPlayer))
return;
static pos, cache[512]
pos = 0
new keys;
if(PlayerAlreadyUseVip[idPlayer])
{
keys = (1<<1)|(1<<9)
new szEndTime[44],EndPlayerTime;
EndPlayerTime = EndTime[idPlayer] - get_systime();
get_human_time(EndPlayerTime,szEndTime,sizeof(szEndTime)-1)
if(EndTime[idPlayer] > get_systime())
pos += formatex(cache[pos], 511-pos, "\yV.I.P тест^nДо конца теста %s",szEndTime)
else
pos += formatex(cache[pos], 511-pos, "\yV.I.P тест^n\rТест окончен!")
pos += formatex(cache[pos], 511-pos, "^n\y|\r1\y|.\dПолучить V.I.P на %d часов",VIP_TEST_HOUR)
pos += formatex(cache[pos], 511-pos, "^n\y|\r2\y|.\yИнформация")
}
else
{
keys = (1<<0)|(1<<1)|(1<<9)
pos += formatex(cache[pos], 511-pos, "\yV.I.P тест^n")
pos += formatex(cache[pos], 511-pos, "^n\y|\r1\y|.\yПолучить V.I.P на \r%d \y часов",VIP_TEST_HOUR)
pos += formatex(cache[pos], 511-pos, "^n\y|\r2\y|.\yИнформация")
}
pos += formatex(cache[pos], 511-pos, "^n^n^n0. \wВыход^n")
show_menu( idPlayer, keys, cache, -1)
return;
}
/* обработчик меню*/
public hnd_viptest_menu(idPlayer,key)
{
if(!is_user_connected(idPlayer))
return;
switch (key)
{
case 0:
{
new stats[8],bodyhits[8], rank;
rank = get_user_stats(idPlayer, stats,bodyhits)
if(rank > MIN_RANK)
{
client_print(idPlayer,print_chat,"Прости,но твой rank должен быть ниже %d!",MIN_RANK)
CmdVipTest(idPlayer)
return;
}
client_print(idPlayer,print_chat,"Бесплатный период активирован! В бой!!!")
new szTemp[512],ENDTime;
ENDTime = (get_systime() +(VIP_TEST_HOUR*S_HOUR))
new szKey[66];
DB_GetKey( idPlayer, szKey, 65 );
format(szTemp,charsmax(szTemp),"INSERT INTO `%s` ( `%s` , `timeend`)VALUES ('%s','%i');",szTableName,szKeyName,szKey,ENDTime)
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
EndTime[idPlayer] = ENDTime;
PlayerAlreadyUseVip[idPlayer] = true;
set_user_flags(idPlayer, read_flags( VIP_FLAG))
}
case 1:
{
show_motd(idPlayer,"addons/amxmodx/configs/vip_test.txt", "VIP Тест")
}
default:return;
}
return;
}
/* Загрузка бд*/
public plugin_cfg()
Sql_Init()
/* Коннект к базе*/
public Sql_Init()
{
g_SqlTuple = SQL_MakeDbTuple( szDBHost, szDBUser, szDBPass, szBDName );
new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
if(SqlConnection == Empty_Handle)
set_fail_state(g_Error)
new Handle:Queries
new szSendQuery[100];
format(szSendQuery,99,"CREATE TABLE IF NOT EXISTS %s (%s varchar(64),timeend INT(16))",szTableName,szKeyName)
Queries = SQL_PrepareQuery(SqlConnection,szSendQuery)
if(!SQL_Execute(Queries))
{
SQL_QueryError(Queries,g_Error,charsmax(g_Error))
set_fail_state(g_Error)
}
SQL_FreeHandle(Queries)
SQL_FreeHandle(SqlConnection)
}
/* Игрок подкл к серверу*/
public client_putinserver(idPlayer)
{
PlayerAlreadyUseVip[idPlayer] = false;
EndTime[idPlayer] = 0;
Load_Sql(idPlayer)
}
/* загрузка данных о игроке*/
public Load_Sql(idPlayer)
{
new szTemp[512]
new Data[1]
new szKey[66];
DB_GetKey( idPlayer, szKey, 65 );
Data[0] = idPlayer
format(szTemp,charsmax(szTemp),"SELECT * FROM `%s` WHERE (`%s`.`%s` = '%s')",szTableName,szTableName,szKeyName,szKey)
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)
}
/* загрузка 2 данных о игроке*/
public register_client(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
if(FailState == TQUERY_CONNECT_FAILED)
{
log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error)
}
else if(FailState == TQUERY_QUERY_FAILED)
{
log_amx("Load Query failed. [%d] %s", Errcode, Error)
}
new idPlayer
idPlayer = Data[0]
if(SQL_NumResults(Query) < 1)
{
//Игрока нет в базе его ждет халявная випка
}
else
{
PlayerAlreadyUseVip[idPlayer] = true;
EndTime[idPlayer] = SQL_ReadResult(Query, 1);
if(EndTime[idPlayer] > get_systime())
set_user_flags(idPlayer, read_flags( VIP_FLAG))
}
return PLUGIN_HANDLED
}
public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
SQL_FreeHandle(Query)
return PLUGIN_HANDLED
}
/* Секунды в минуты,часы*/
get_human_time(time,output[],len)
{
new m,h
if(time)formatex(output,len,"%d сек",time)
if(time/S_MINUTE)
{
m=time/S_MINUTE
formatex(output,len,"%d мин.",m)
}
if(time/S_HOUR)
{
h=time/S_HOUR
m=(time-(h*S_HOUR))/S_MINUTE
new szHour[16];
switch(h)
{
case 1,21:format(szHour,charsmax(szHour),"час")
case 2..4,22..24:format(szHour,charsmax(szHour),"часа")
case 5..20:format(szHour,charsmax(szHour),"часов")
}
formatex(output,len,"%d %s и %d мин",h,szHour,m)
}
}
/* Закрывает сессию,но не твою*/
public plugin_end()
SQLITE_Close()
SQLITE_Close()
{
if ( g_SqlTuple )
{
SQL_FreeHandle(g_SqlTuple);
}
}
/* Получает данные о игроке(зависит от настройки)*/
DB_GetKey( id, szKey[], len )
{
switch(SAVE_TYPE)
{
case DB_SAVEBY_NAME:
{
get_user_name( id, szKey, len );
replace_all( szKey, len, "'", "''" );
}
case DB_SAVEBY_IP: get_user_ip( id, szKey, len, 1 );
case DB_SAVEBY_STEAMID: get_user_authid( id, szKey, len );
}
}
/* Главный ключ сохранения игрока*/
DB_GetKeyName( szKeyName[], len )
{
switch( SAVE_TYPE )
{
case DB_SAVEBY_NAME: copy( szKeyName, len, "player_name" );
case DB_SAVEBY_IP: copy( szKeyName, len, "player_ip" );
case DB_SAVEBY_STEAMID: copy( szKeyName, len, "player_steamid" );
}
}