Проверенный
Пользователь
- Сообщения
- 1,032
- Реакции
- 828
- Помог
- 10 раз(а)
Задача плагина независимо хранить различный тип онлайна игроков
Далее по коду, как уже выяснилось, могут быть много нюансов, один из таких описан в соседней мною темой, когда вызывался spawn игрока при ClientPutInServer
Поэтому на всякий случай, спрошу еще об одном нюансе.
Можно ли как-то перейти в зрители, или, сменив команду минуя хук
- Общий онлайн
- Общий онлайн за исключением учета зрителей
- Общий онлайн самой игры, то есть когда только живой
Далее по коду, как уже выяснилось, могут быть много нюансов, один из таких описан в соседней мною темой, когда вызывался spawn игрока при ClientPutInServer
Поэтому на всякий случай, спрошу еще об одном нюансе.
Можно ли как-то перейти в зрители, или, сменив команду минуя хук
RG_CBasePlayer_Killed
?
C++:
#include < amxmodx >
#include < amxmisc >
#include < reapi >
#include < sqlx >
#pragma loadlib sqlite
#pragma semicolon 1
new const LOGS_FOLDER_NAME[ ] = "online_players"; // Наименование каталога ведение журналов
new const LOGS_FILE_NAME[ ] = "online_players_error"; // Наименование журналов для ведение ошибочной информации
new const SQL_DATABASE[ ] = "online_players"; // Наименование базы данных
new const SQL_TABLE[ ] = "online_players"; // Наименование таблицы базы данных
const ELLIPSES_ARG = 4;
const RESET_VALUE = 0;
const MAX_LENGTH__PATH = 64;
const MAX_LENGTH__AUTHID = 64;
const MAX_LENGTH__SQL_QUERY = 1024;
const MAX_LENGTH__TIME = 32;
const MAX_LENGTH__MESSAGE = 512;
enum _: SQL_DATA_STRUCT
{
SQL_DATA__QUERY_TYPE,
SQL_DATA__PLAYER_USERID,
SQL_DATA__PLAYER_AUTHID[ MAX_LENGTH__AUTHID ]
}
enum
{
QUERY_TYPE__TABLE_CREATE,
QUERY_TYPE__INSERT_ONLINE_PLAYER,
QUERY_TYPE__GET_ONLINE_PLAYER,
QUERY_TYPE__UPDATE_ONLINE_PLAYER
}
enum _: SQL_TABLE_COLUMNS_ENUM
{
SQL_TABLE_COLUMN__PLAYER_AUTHID,
SQL_TABLE_COLUMN__TOTAL_ONLINE_PLAYER,
SQL_TABLE_COLUMN__GAMING_ONLINE_PLAYER,
SQL_TABLE_COLUMN__LIVE_ONLINE_PLAYER
}
new const SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMNS_ENUM ][ ] =
{
"player_authid",
"total_online_player",
"gaming_online_player",
"live_online_player"
};
new g_iPlGameTime;
new Handle: g_hSqlTuple;
new g_sLogsDir[ MAX_LENGTH__PATH ];
new g_sSqlQuery[ MAX_LENGTH__SQL_QUERY ];
new g_iPlTotalOnline[ MAX_PLAYERS + 1 ];
new g_iPlGamingOnline[ MAX_PLAYERS + 1 ];
new g_iPlLiveOnline[ MAX_PLAYERS + 1 ];
new g_iCachePlTotalOnline[ MAX_PLAYERS + 1 ];
new g_iCachePlGamingOnline[ MAX_PLAYERS + 1 ];
new g_iCachePlLiveOnline[ MAX_PLAYERS + 1 ];
new g_bAuthPlayer[ MAX_PLAYERS + 1 ];
new g_eSqlData[ SQL_DATA_STRUCT ];
new bool: g_bSpectator[ MAX_PLAYERS + 1 ];
new bool: g_bAlive[ MAX_PLAYERS + 1 ];
public plugin_init( )
{
register_plugin( "Online Players", "1.0", "Javekson" );
RegisterHookChain( RG_CBasePlayer_Spawn, "CBasePlayer_Spawn", .post = true );
RegisterHookChain( RG_CBasePlayer_Killed, "CBasePlayer_Killed", .post = true );
RegisterHookChain( RG_HandleMenu_ChooseTeam, "HandleMenu_ChooseTeam", .post = true );
RegisterHookChain( RG_CBasePlayer_RoundRespawn, "CBasePlayer_RoundRespawn", .post = true );
}
public plugin_cfg( )
{
get_localinfo( "amxx_logs", g_sLogsDir, charsmax( g_sLogsDir ) );
formatex( g_sLogsDir, charsmax( g_sLogsDir ), "%s/%s", g_sLogsDir, LOGS_FOLDER_NAME );
if( !dir_exists( g_sLogsDir ) )
{
mkdir( g_sLogsDir );
}
SQL_SetAffinity( "sqlite" );
g_hSqlTuple = SQL_MakeDbTuple( "", "", "", SQL_DATABASE );
func_SqlQueryTableCreate( );
}
public client_connect( id )
{
g_bAuthPlayer[ id ] = false;
g_bSpectator[ id ] = true;
g_iPlTotalOnline[ id ] = RESET_VALUE;
g_iPlGamingOnline[ id ] = RESET_VALUE;
g_iPlLiveOnline[ id ] = RESET_VALUE;
g_iCachePlTotalOnline[ id ] = RESET_VALUE;
g_iCachePlGamingOnline[ id ] = RESET_VALUE;
g_iCachePlLiveOnline[ id ] = RESET_VALUE;
}
public client_authorized( id, const sAuthID[ ] )
{
if( is_user_bot( id ) || is_user_hltv( id ) )
{
return PLUGIN_CONTINUE;
}
g_bAuthPlayer[ id ] = true;
func_SqlQueryGetOnlinePlayer( id, sAuthID );
return PLUGIN_CONTINUE;
}
public client_disconnected( id )
{
if( g_bAuthPlayer[ id ] )
{
g_iPlGameTime = get_user_time( id );
/* ------------------------------ */
g_iPlTotalOnline[ id ] += g_iPlGameTime - g_iCachePlTotalOnline[ id ];
/* ------------------------------ */
if( !g_bSpectator[ id ] )
{
g_iPlGamingOnline[ id ] += g_iPlGameTime - g_iCachePlGamingOnline[ id ];
}
/* ------------------------------ */
if( g_bAlive[ id ] )
{
g_iPlLiveOnline[ id ] += get_user_time( id ) - g_iCachePlLiveOnline[ id ];
}
/* ------------------------------ */
new sAuthID[ MAX_LENGTH__AUTHID ];
get_user_authid( id, sAuthID, charsmax( sAuthID ) );
func_SqlQueryUpdateOnlinePlayer( id, sAuthID );
}
}
public CBasePlayer_Spawn( const id )
{
if( is_user_alive( id ) )
{
g_iPlGameTime = get_user_time( id );
/* ------------------------------ */
if( g_bSpectator[ id ] )
{
g_bSpectator[ id ] = false;
g_iCachePlGamingOnline[ id ] = g_iPlGameTime;
}
/* ------------------------------ */
if( g_bAlive[ id ] )
{
g_iPlLiveOnline[ id ] += g_iPlGameTime - g_iCachePlLiveOnline[ id ];
}
g_iCachePlLiveOnline[ id ] = g_iPlGameTime;
g_bAlive[ id ] = true;
}
}
public CBasePlayer_Killed( id )
{
g_bAlive[ id ] = false;
g_iPlGameTime = get_user_time( id );
g_iPlLiveOnline[ id ] += g_iPlGameTime - g_iCachePlLiveOnline[ id ];
g_iCachePlLiveOnline[ id ] = g_iPlGameTime;
}
public CBasePlayer_RoundRespawn( const id )
{
if( g_bAuthPlayer[ id ] )
{
g_iPlGameTime = get_user_time( id );
/* ------------------------------ */
g_iPlTotalOnline[ id ] += g_iPlGameTime - g_iCachePlTotalOnline[ id ];
g_iCachePlTotalOnline[ id ] = g_iPlGameTime;
/* ------------------------------ */
g_iPlGamingOnline[ id ] += g_iPlGameTime - g_iCachePlGamingOnline[ id ];
g_iCachePlGamingOnline[ id ] = g_iPlGameTime;
/* ------------------------------ */
new sAuthID[ MAX_LENGTH__AUTHID ];
get_user_authid( id, sAuthID, charsmax( sAuthID ) );
func_SqlQueryUpdateOnlinePlayer( id, sAuthID );
}
}
public HandleMenu_ChooseTeam( const id, const MenuChooseTeam: iTeam )
{
switch( iTeam )
{
case MenuChoose_Spec:
{
if( !g_bSpectator[ id ] )
{
g_iPlGamingOnline[ id ] += get_user_time( id ) - g_iCachePlGamingOnline[ id ];
g_bSpectator[ id ] = true;
}
}
}
}
func_SqlQueryTableCreate( )
{
formatex( g_sSqlQuery, charsmax( g_sSqlQuery ),
"CREATE TABLE IF NOT EXISTS `%s` ( \
`%s` TEXT NOT NULL PRIMARY KEY ASC, \
`%s` INTEGER NOT NULL, \
`%s` INTEGER NOT NULL, \
`%s` INTEGER NOT NULL )",
SQL_TABLE,
SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMN__PLAYER_AUTHID ],
SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMN__TOTAL_ONLINE_PLAYER ],
SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMN__GAMING_ONLINE_PLAYER ],
SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMN__LIVE_ONLINE_PLAYER ]
);
func_MakeSqlQuery( QUERY_TYPE__TABLE_CREATE );
}
func_SqlQueryGetOnlinePlayer( const id, const sAuthID[ ] )
{
formatex( g_sSqlQuery, charsmax( g_sSqlQuery ),
"SELECT * FROM `%s` WHERE `%s` = '%s'",
SQL_TABLE, SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMN__PLAYER_AUTHID ], sAuthID
);
g_eSqlData[ SQL_DATA__PLAYER_USERID ] = get_user_userid( id );
copy( g_eSqlData[ SQL_DATA__PLAYER_AUTHID ], charsmax( g_eSqlData[ SQL_DATA__PLAYER_AUTHID ] ), sAuthID );
func_MakeSqlQuery( QUERY_TYPE__GET_ONLINE_PLAYER );
}
func_SqlQueryInsertOnlinePlayer( const id, const sAuthID[ ] )
{
formatex( g_sSqlQuery, charsmax( g_sSqlQuery ),
"INSERT INTO `%s` ( `%s`, `%s`, `%s`, `%s` ) \
VALUES ( '%s', %d, %d, %d )",
SQL_TABLE,
SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMN__PLAYER_AUTHID ],
SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMN__TOTAL_ONLINE_PLAYER ],
SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMN__GAMING_ONLINE_PLAYER ],
SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMN__LIVE_ONLINE_PLAYER ],
sAuthID,
g_iPlTotalOnline[ id ],
g_iPlGamingOnline[ id ],
g_iPlLiveOnline[ id ]
);
func_MakeSqlQuery( QUERY_TYPE__INSERT_ONLINE_PLAYER );
}
func_SqlQueryUpdateOnlinePlayer( const id, const sAuthID[ ] )
{
formatex( g_sSqlQuery, charsmax( g_sSqlQuery ),
"UPDATE `%s` SET `%s` = %d, `%s` = %d, `%s` = %d WHERE `%s` = '%s'",
SQL_TABLE,
SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMN__TOTAL_ONLINE_PLAYER ],
g_iPlTotalOnline[ id ],
SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMN__GAMING_ONLINE_PLAYER ],
g_iPlGamingOnline[ id ],
SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMN__LIVE_ONLINE_PLAYER ],
g_iPlLiveOnline[ id ],
SQL_TABLE_COLUMNS[ SQL_TABLE_COLUMN__PLAYER_AUTHID ],
sAuthID
);
func_MakeSqlQuery( QUERY_TYPE__UPDATE_ONLINE_PLAYER );
}
public handler_SqlQuery( iFailState, Handle: hQueryHandle, sError[ ], iErrorCode, eSqlData[ ], iDataSize, Float: fQueryTime )
{
if( iFailState != TQUERY_SUCCESS )
{
SQL_GetQueryString( hQueryHandle, g_sSqlQuery, charsmax( g_sSqlQuery ) );
func_Logging( g_sLogsDir, LOGS_FILE_NAME, "^"[SQL] Query error^"" );
func_Logging( g_sLogsDir, LOGS_FILE_NAME, "^"[SQL] Error^" ^"#%d^", ^"%s^"", iErrorCode, sError );
func_Logging( g_sLogsDir, LOGS_FILE_NAME, "^"[SQL] Query^" ^"%s^"", g_sSqlQuery );
return PLUGIN_CONTINUE;
}
switch( eSqlData[ SQL_DATA__QUERY_TYPE ] )
{
case QUERY_TYPE__TABLE_CREATE:
{
return PLUGIN_CONTINUE;
}
case QUERY_TYPE__GET_ONLINE_PLAYER:
{
new iPlUserID = eSqlData[ SQL_DATA__PLAYER_USERID ];
new iPlayerID = find_player_ex( FindPlayer_MatchUserId | FindPlayer_IncludeConnecting, iPlUserID );
if( iPlayerID )
{
if( SQL_MoreResults( hQueryHandle ) )
{
g_iPlTotalOnline[ iPlayerID ] = SQL_ReadResult( hQueryHandle, 1 );
g_iPlGamingOnline[ iPlayerID ] = SQL_ReadResult( hQueryHandle, 2 );
g_iPlLiveOnline[ iPlayerID ] = SQL_ReadResult( hQueryHandle, 3 );
}
else
{
new sAuthID[ MAX_LENGTH__AUTHID ];
copy( sAuthID, charsmax( sAuthID ), eSqlData[ SQL_DATA__PLAYER_AUTHID ] );
func_SqlQueryInsertOnlinePlayer( iPlayerID, sAuthID );
}
}
}
case QUERY_TYPE__INSERT_ONLINE_PLAYER:
{
return PLUGIN_CONTINUE;
}
case QUERY_TYPE__UPDATE_ONLINE_PLAYER:
{
return PLUGIN_CONTINUE;
}
}
return PLUGIN_CONTINUE;
}
stock func_MakeSqlQuery( iQueryType )
{
g_eSqlData[ SQL_DATA__QUERY_TYPE ] = iQueryType;
SQL_ThreadQuery( g_hSqlTuple, "handler_SqlQuery", g_sSqlQuery, g_eSqlData, sizeof( g_eSqlData ) );
}
stock func_Logging( const sLogsDir[ ], const sFileName[ ], const sMessage[ ], any:... )
{
new sFmtMsg[ MAX_LENGTH__MESSAGE ];
vformat( sFmtMsg, charsmax( sFmtMsg ), sMessage, ELLIPSES_ARG );
new sFileTime[ MAX_LENGTH__TIME ];
get_time( "%m.%Y", sFileTime, charsmax( sFileTime ) );
new sLogFile[ MAX_LENGTH__PATH + 32 ];
formatex( sLogFile, charsmax( sLogFile ), "%s/%s_%s.log", sLogsDir, sFileName, sFileTime );
new iFileID = fopen( sLogFile, "at" );
new sRecordTime[ MAX_LENGTH__TIME ];
get_time( "%d.%m.%Y - %H:%M:%S", sRecordTime, charsmax( sRecordTime ) );
fprintf( iFileID, "^"%s^" %s^n", sRecordTime, sFmtMsg );
fclose( iFileID );
}