- #include < amxmodx > 
- #include < sqlx > 
-   
- #define FLAGS "t" 
-   
- #pragma semicolon 1 
-   
- new const g_sPrefix[ ] = "hlmod.hu"; 
-   
- new const g_sSQLInfos[ ][ ] = 
- { 
-     "Host",     // HOST 
-     "Username", // USERNAME 
-     "Password", // PASSWORD 
-     "Database"  // DATABASE 
- }; 
-   
- new Handle: g_hSqlTuple; 
- new g_iPoints[ MAX_PLAYERS ], g_sAuthId[ MAX_PLAYERS ][ 32 ], g_iSavedMonth; 
-   
- public plugin_init( ) 
- { 
-     register_plugin( "Top 3 VIP (Sql)", "1.0", "Demon" ); 
-     register_cvar( "Top 3 VIP (Sql)", "By Demon", FCVAR_SERVER ); 
-      
-     if ( InitSQL( ) ) 
-     { 
-         Load( ); 
-          
-         register_concmd( "say /ptop15", "cmdPTop15" ); 
-         register_concmd( "say /prank", "cmdPRank" ); 
-         register_concmd( "say /pontok", "cmdMyPoints" ); 
-         register_concmd( "say_team /ptop15", "cmdPTop15" ); 
-         register_concmd( "say_team /prank", "cmdPRank" ); 
-         register_concmd( "say_team /pontok", "cmdMyPoints" ); 
-          
-         register_event( "DeathMsg", "onDeath", "a" ); 
-          
-         set_task( 60.0, "Check", 56789, _, _, "b" ); 
-         set_task( 180.0, "Advertising", 98765, _, _, "b" ); 
-     } 
- } 
-   
- public cmdPTop15( iPlayerId ) { LoadDatas( iPlayerId, 3 ); } 
- public cmdPRank( iPlayerId ) { LoadDatas( iPlayerId, 2 ); } 
- public cmdMyPoints( iPlayerId ) { client_print_color( iPlayerId, iPlayerId, "^4[%s] ^1Pontjaid száma: ^3%d^1.", g_sPrefix, g_iPoints[ iPlayerId ] ); } 
-   
- InitSQL( ) 
- { 
-     g_hSqlTuple = SQL_MakeDbTuple( g_sSQLInfos[ 0 ], g_sSQLInfos[ 1 ], g_sSQLInfos[ 2 ], g_sSQLInfos[ 3 ] ); 
-      
-     if( g_hSqlTuple == Empty_Handle ) return -1; 
-      
-     new iError, sError[ 128 ]; 
-     new Handle: hDb = SQL_Connect( g_hSqlTuple, iError, sError, charsmax( sError ) ); 
-      
-     if( hDb == Empty_Handle ) 
-     { 
-         log_amx( "Nem lehet csatlakozni az adatbazishoz: (%d) %s", iError, sError ); 
-         return -1; 
-     } 
-      
-     new Handle: hQuery = SQL_PrepareQuery( hDb, "CREATE TABLE IF NOT EXISTS `point_system` (`authid` varchar(32) NOT NULL PRIMARY KEY, `username` varchar(32) NOT NULL, `points` INT(11) NOT NULL);" ); 
-      
-     if( !SQL_Execute( hQuery ) ) 
-     { 
-         SQL_QueryError( hQuery, sError, charsmax( sError ) ); 
-         log_amx( "Tabla letrehozas sikertelen: %s", sError ); 
-         return -1; 
-     } 
-      
-     SQL_FreeHandle( hQuery ); 
-     SQL_FreeHandle( hDb ); 
-     return 1; 
- } 
-   
- public SaveDatas( iPlayerId, iMode ) 
- { 
-     static sText[ 512 ], sName[ 32 ]; 
-     get_user_name( iPlayerId, sName, charsmax( sName ) ); 
-     MakeStringSQLSafe( sName, charsmax( sName ) ); 
-      
-     if ( iMode == 1 ) formatex( sText, charsmax( sText ), "REPLACE INTO `point_system` (`authid`,`username`,`points`) VALUES (^"%s^", ^"%s^", %d);", g_sAuthId[ iPlayerId ], sName, g_iPoints[ iPlayerId ] ); 
-     else 
-     { 
-         arrayset( g_iPoints, 0, sizeof( g_iPoints ) ); 
-         formatex( sText, charsmax( sText ), "TRUNCATE `point_system`;" ); 
-     } 
-      
-     SQL_ThreadQuery( g_hSqlTuple, "ThreadHandler", sText, { 0 }, 1 ); 
- } 
-   
- public LoadDatas( iPlayerId, iMode ) 
- { 
-     static sText[ 128 ], iArray[ 1 ]; 
-     iArray[ 0 ] = iPlayerId; 
-      
-     switch( iMode ) 
-     { 
-         case 1: formatex( sText, charsmax( sText ), "SELECT `points` FROM `point_system` WHERE `authid` = ^"%s^";", g_sAuthId[ iPlayerId ] ); 
-         case 2:  
-         {  
-             iArray[ 0 ] += 100; 
-             formatex( sText, charsmax( sText ), "SELECT COUNT(*) AS players, rank.num AS prank FROM `point_system`, (SELECT COUNT(*) AS num FROM `point_system` WHERE `points` > %d ) AS rank;", g_iPoints[ iPlayerId ] ); 
-         } 
-         case 3: 
-         { 
-             iArray[ 0 ] += 200; 
-             formatex( sText, charsmax( sText ), "SELECT `points`, `username` FROM `point_system` ORDER BY `points` DESC LIMIT 15;" ); 
-         } 
-         case 4: 
-         { 
-             iArray[ 0 ] = 300; 
-             formatex( sText, charsmax( sText ), "SELECT `authid`, `username` FROM `point_system` ORDER BY `points` DESC LIMIT 3;" ); 
-         } 
-     } 
-     SQL_ThreadQuery( g_hSqlTuple, "ThreadHandler", sText, iArray, sizeof ( iArray ) ); 
- } 
-   
- public ThreadHandler( iFailState, Handle: hQuery, const sError[ ], iError, const iARRAY[ ], iArraySize, Float: fQueueTime ) 
- { 
-     switch( iFailState ) 
-     { 
-         case TQUERY_CONNECT_FAILED:  
-         {  
-             log_amx( "Nem lehet csatlakozni az adatbazishoz: (%d) %s", iError, sError ); 
-             set_fail_state( "Nem lehet csatlakozni az adatbazishoz: (%d) %s", iError, sError ); 
-         } 
-         case TQUERY_QUERY_FAILED:  
-         {  
-             log_amx( "Hibas lekerdezes: (%d) %s", iError, sError ); 
-             return; 
-         } 
-     } 
-      
-     new iPlayerId = iARRAY[ 0 ]; 
-     if ( iPlayerId ) 
-     { 
-         if ( iPlayerId < 100 )  
-         { 
-             if ( SQL_NumResults( hQuery ) > 0 )  
-                 g_iPoints[ iPlayerId ] = SQL_ReadResult( hQuery, SQL_FieldNameToNum( hQuery, "points" ) ); 
-         } 
-         else if ( iPlayerId < 200 ) 
-         { 
-             iPlayerId -= 100; 
-             new iRank = SQL_ReadResult( hQuery, SQL_FieldNameToNum( hQuery, "prank" ) ); 
-             new iPlayers = SQL_ReadResult( hQuery, SQL_FieldNameToNum( hQuery, "players" ) ); 
-             client_print_color( iPlayerId, iPlayerId, "^4[%s] ^1Te pont rankod ^3%d ^1a ^3%d^1-ból/bõl ^3%d ^1ponttal.", g_sPrefix, iRank == 0 ? 1 : iRank, iPlayers == 0 ? 1 : iPlayers, g_iPoints[ iPlayerId ] ); 
-         } 
-         else if ( iPlayerId < 300 ) 
-         { 
-             static iLen, sMotd[ 2048 ], sName[ 32 ], i, iPoints, iMLen = charsmax( sMotd ); 
-             iLen = 0; 
-              
-             iPlayerId -= 200; 
-          
-             iLen = formatex( sMotd, iMLen, "<meta charset=utf-8><body bgcolor=#000000><font color=#FFB000><pre>" ); 
-             iLen += formatex( sMotd[ iLen ], iMLen - iLen, "%2s %-22.22s %6s^n", "#", "Név", "Pont" ); 
-              
-             i = 1; 
-             while ( SQL_MoreResults( hQuery ) ) 
-             { 
-                 SQL_ReadResult( hQuery, SQL_FieldNameToNum( hQuery, "username" ), sName, charsmax( sName ) ); 
-                 iPoints = SQL_ReadResult( hQuery, SQL_FieldNameToNum( hQuery, "points" ) ); 
-                 switch ( i ) 
-                 { 
-                     case 1: iLen += formatex( sMotd[iLen], iMLen - iLen, "<font color=ff0bb7>%2d %-22.22s %d</font>^n", i, sName, iPoints ); 
-                     case 2: iLen += formatex( sMotd[iLen], iMLen - iLen, "<font color=#07fcff>%2d %-22.22s %d</font>^n", i, sName, iPoints ); 
-                     case 3: iLen += formatex( sMotd[iLen], iMLen - iLen, "<font color=#0BF402>%2d %-22.22s %d</font>^n", i, sName, iPoints ); 
-                     default: iLen += formatex( sMotd[iLen], iMLen - iLen, "%2d %-22.22s %d^n", i, sName, iPoints ); 
-                 } 
-                  
-                 ++i;     
-                 SQL_NextRow( hQuery ); 
-             } 
-              
-             show_motd( iPlayerId, sMotd, "Pont TOP15" ); 
-         } 
-         else 
-         { 
-             new sFile[ 64 ] = "addons/amxmodx/configs/users.ini"; 
-             new pFile = fopen( sFile, "rt" ); 
-              
-             if ( pFile ) 
-             {    
-                 new sNewLine[ 128 ], i = 1, sName[ 32 ], sAuthId[ 32 ]; 
-                 write_file( sFile, "; VIP rendszer altal sorsolt VIP tagok: ", 0 ); 
-                 if ( SQL_NumResults( hQuery ) > 0 ) 
-                 { 
-                     while ( SQL_MoreResults( hQuery ) ) 
-                     { 
-                         SQL_ReadResult( hQuery, SQL_FieldNameToNum( hQuery, "username" ), sName, charsmax( sName ) ); 
-                         SQL_ReadResult( hQuery, SQL_FieldNameToNum( hQuery, "authid" ), sAuthId, charsmax( sAuthId ) ); 
-                          
-                         formatex( sNewLine, charsmax( sNewLine ), "^"%s^" ^"^" ^"%s^" ^"ce^" //%s", sAuthId, FLAGS, sName ); 
-                         write_file( sFile, sNewLine, i ); 
-                         client_print_color( 0, print_team_default, "^4[%s] ^3%s VIP tagságban részesültélt mivel benne van a PTOP3-ban (rankja: ^4PTOP%d^3).", g_sPrefix, sName, i ); 
-                          
-                         ++i; 
-                          
-                         SQL_NextRow( hQuery ); 
-                     } 
-                 } 
-                 else 
-                 { 
-                     write_file( sFile, "", 1 ); 
-                     write_file( sFile, "", 2 ); 
-                     write_file( sFile, "", 3 ); 
-                 } 
-                 Save( ); 
-                 server_cmd( "amx_reloadadmins" ); 
-                 SaveDatas( 0, 2 ); 
-                 if ( task_exists( 56789 ) ) remove_task( 56789 ); 
-                  
-                 fclose( pFile ); 
-             } 
-         } 
-     } 
-      
-     return; 
- } 
-   
- public client_putinserver( iPlayerId ) 
- { 
-     g_iPoints[ iPlayerId ] = 0; 
-     g_sAuthId[ iPlayerId ][ 0 ] = EOS; 
-      
-     if ( is_user_bot( iPlayerId ) || is_user_hltv( iPlayerId ) ) return; 
-      
-     get_user_authid( iPlayerId, g_sAuthId[ iPlayerId ], charsmax( g_sAuthId[ ] ) ); 
-      
-     LoadDatas( iPlayerId, 1 ); 
- } 
-   
- public Advertising( ) 
- { 
-     static iAdv = -1; 
-     ++iAdv; 
-     switch( iAdv ) 
-     { 
-         case 0: client_print_color( 0, print_team_default, "^4[%s] ^3Minden hónap 1. napján a PTOP3 játékos VIP tagságban részesül.", g_sPrefix ); 
-         case 1: 
-         { 
-             static sMonth[ 5 ], sDay[ 5 ], sYear[ 7 ], iMonth, iDay, iYear; 
-             get_time( "%d", sDay, charsmax( sDay )  ); 
-             get_time( "%m", sMonth, charsmax( sMonth ) ); 
-             get_time( "%Y", sYear, charsmax( sYear ) ); 
-              
-             iMonth = str_to_num( sMonth ); 
-             iDay = str_to_num( sDay ); 
-             iYear = str_to_num( sYear ); 
-              
-             client_print_color( 0, print_team_default, "^4[%s] ^1Még ^3%d nap ^1van a következõ VIP sorsolásig.", g_sPrefix, ( GetDaysInMonth( iMonth, iYear ) - iDay ) ); 
-         } 
-         case 2: client_print_color( 0, print_team_default, "^4[%s] ^1A pont rankod megtekintéséhez Ãrd be, hogy ^3/prank^1.", g_sPrefix ); 
-         case 3: client_print_color( 0, print_team_default, "^4[%s] ^1A Pont TOP15 megtekintéséhez Ãrd be, hogy ^3/ptop15^1.", g_sPrefix ); 
-         default: iAdv = -1; 
-     } 
- } 
-   
- public onDeath( ) 
- { 
-     new iKiller = read_data( 1 ); 
-      
-     if ( is_user_bot( iKiller ) ) return; 
-      
-     new iVictim = read_data( 2 ); 
-     new iHs = read_data( 3 ); 
-     new bTK = get_user_team( iKiller ) == get_user_team( iVictim ); 
-     new sWpn[ 3 ]; 
-     read_data( 4, sWpn, charsmax( sWpn ) ); 
-      
-     if ( iKiller != iVictim ) 
-     { 
-         if ( !bTK && !iHs && sWpn[ 1 ] != 'r' && sWpn[ 0 ] != 'k' ) g_iPoints[ iKiller ] += 1; 
-         else if ( !bTK && ( iHs || sWpn[ 1 ] == 'r' || sWpn[ 0 ] == 'k' ) ) g_iPoints[ iKiller ] += 2; 
-          
-         SaveDatas( iKiller, 1 ); 
-     } 
- } 
-   
- public Check( ) 
- {    
-     static sDay[ 5 ], sMonth[ 5 ], iDay, iMonth; 
-     get_time( "%d", sDay, charsmax( sDay ) ); 
-     get_time( "%m", sMonth, charsmax( sMonth ) ); 
-      
-     iDay = str_to_num( sDay ); 
-     iMonth = str_to_num( sMonth ); 
-      
-     if ( iDay > 1 )  
-         if ( task_exists( 56789 ) ) remove_task( 56789 ); 
-      
-     if ( iMonth == g_iSavedMonth || iDay != 1 ) return; 
-     LoadDatas( 0, 4 ); 
- } 
-   
- public Save( ) 
- {  
-     new sMonth[ 5 ], iMonth, sFile[ 200 ], sNewLine[ 16 ]; 
-     get_time( "%m", sMonth, charsmax( sMonth ) ); 
-              
-     iMonth = g_iSavedMonth = str_to_num( sMonth ); 
-      
-     format( sFile, charsmax( sFile ), "addons/amxmodx/configs/pontrendszer_by_demon", sFile); 
-   
-     if ( !dir_exists( sFile ) ) mkdir( sFile ); 
-      
-     format( sFile, 199, "%s/honap.txt", sFile ); 
-      
-     formatex( sNewLine, charsmax( sNewLine ), "^"%d^"", iMonth ); 
-     write_file( sFile, sNewLine, 0 ); 
- } 
-   
- public Load( ) 
- {  
-     new pFile  = fopen( "addons/amxmodx/configs/pontrendszer_by_demon/honap.txt", "rt" ); 
-      
-     if ( pFile ) 
-     { 
-         new sLine[ 8 ]; 
-         fgets( pFile, sLine, charsmax( sLine ) ); 
-          
-         trim( sLine ); 
-         remove_quotes( sLine ); 
-         g_iSavedMonth = str_to_num( sLine ); 
-          
-         fclose( pFile ); 
-     } 
-     else g_iSavedMonth = 0; 
- } 
-   
- public plugin_end( ) { SQL_FreeHandle( g_hSqlTuple ); } 
-   
- stock GetDaysInMonth( iMonth, iYear ) 
- { 
-     switch( iMonth ) 
-     { 
-         case 1, 3, 5, 7, 8, 10, 12: return 31; 
-         case 2: return ( ( iYear % 4 ) == 0 ) ? 29 : 28; 
-     } 
-     return 30; 
- } 
-   
- stock MakeStringSQLSafe( sName[ ], iLen ) 
- { 
-     replace_all( sName, iLen, "'", "*" ); 
-     replace_all( sName, iLen, "^"", "*" ); 
-     replace_all( sName, iLen, "`", "*" ); 
- }