PHP Code:
#include < amxmodx >
#include < sqlx >
#define COLORED_CHAT //comment this to remove the colored chat
//#define UPDATE_NICK //uncomment this to update nick in the db whenever player has different nick
#if defined COLORED_CHAT
#if AMXX_VERSION_NUM < 183
#include < cromchat >
#endif
#endif
// sql stuff
new const host[] = "";
new const user[] = "";
new const pass[] = "";
new const db[] = "";
new const table[] = "db_bets";
new const Float:TIME = 20.0; // when to disable bets
new const CHAT_PREFIX[] = "[BETS]";
new Handle:tuple;
new Array:hSaveTeamBet;
new Trie:hSaveSteamID;
enum _:BetUtils
{
PlayerSteamID[ 30 ],
PlayerName[ 32 ],
Vote
}
enum _:Teams
{
TEAM_UNASSIGNED = 0,
TEAM_T = 1,
TEAM_CT = 2,
TEAM_SPECTATOR = 3,
}
new bool:canBet;
public plugin_init()
{
register_plugin( "Bets DB", "1.0", "DusT" );
register_clcmd( "say", "CmdSay" );
// events taken from instinctpt1's plugin
register_event("TextMsg", "RoundEnd", "a", "2&#Game_C", "2&#Game_w", "2&#Game_will_restart_in");
register_event( "SendAudio", "T_Wins", "a", "2&%!MRAD_terwin" );
register_event( "SendAudio", "CT_Wins", "a", "2&%!MRAD_ctwin" );
register_event( "HLTV", "RoundStart", "a", "1=0", "2=0" );
tuple = SQL_MakeDbTuple( host, user, pass, db );
}
public plugin_cfg()
{
hSaveSteamID = TrieCreate();
hSaveTeamBet = ArrayCreate( 64, 1 );
set_task( 0.1, "SQL_Init" );
}
public plugin_end()
{
SQL_FreeHandle( tuple );
ArrayDestroy( hSaveTeamBet );
TrieDestroy( hSaveSteamID );
}
public RoundStart()
{
canBet = true;
set_task( TIME, "DisableBet" );
new players[ 32 ], num;
get_players( players, num, "e", "SPECTATOR" );
for( new i; i < num; i++ )
#if defined COLORED_CHAT
client_print_color( players[ i ], print_team_red, "^3%s ^4Bet on the TEAM Winner!! say ^3^"bet CT^"^4 or ^3^"bet T^".", CHAT_PREFIX );
#else
client_print( players[ i ], print_chat, "%s Bet on the TEAM Winner!! say ^"bet CT^" or ^"bet T^".", CHAT_PREFIX );
#endif
}
public DisableBet()
canBet = false;
public RoundEnd()
TeamWins( TEAM_UNASSIGNED );
public CT_Wins()
TeamWins( TEAM_CT );
public T_Wins()
TeamWins( TEAM_T );
public CmdSay( id )
{
if( TEAM_T <= get_user_team( id ) <= TEAM_CT )
return PLUGIN_CONTINUE;
new argv[ 64 ];
read_argv( 1, argv, charsmax( argv ) );
new cmd[ 5 ], team[ 4 ];
parse( argv, cmd, charsmax( cmd ), team, charsmax( team ) );
if( equali( cmd, "bet" ) )
{
if( !canBet )
{
#if defined COLORED_CHAT
client_print_color( id, print_team_red, "^3%s ^4You cannot vote right now.", CHAT_PREFIX );
#else
client_print( id, print_chat, "%s You cannot vote right now.", CHAT_PREFIX );
#endif
return PLUGIN_CONTINUE;
}
new bet[ BetUtils ];
get_user_authid( id, bet[ PlayerSteamID ], charsmax( bet[ PlayerSteamID ] ) );
get_user_name( id, bet[ PlayerName ], charsmax( bet[ PlayerName ] ) );
if( TrieKeyExists( hSaveSteamID, bet[ PlayerSteamID ] ) || !team[ 0 ] )
{
#if defined COLORED_CHAT
client_print_color( id, print_team_red, "^3%s ^4You have already voted for this round.", CHAT_PREFIX );
#else
client_print( id, print_chat, "%s You have already voted for this round.", CHAT_PREFIX );
#endif
return PLUGIN_CONTINUE;
}
new iTeam;
switch( team[ 0 ] )
{
case 'C','c':
{
if( team[ 1 ] == 'T' || team[ 1 ] == 't' )
{
iTeam = TEAM_CT;
}
}
case 'T','t':
{
iTeam = TEAM_T;
}
}
if( iTeam )
{
#if defined COLORED_CHAT
client_print_color( id, print_team_red, "^3%s ^4You voted for ^3%s.", CHAT_PREFIX, iTeam == TEAM_CT? "CT":"T" );
#else
client_print( id, print_chat, "%s You voted for %s.", CHAT_PREFIX, iTeam == TEAM_CT? "CT":"T" );
#endif
bet[ Vote ] = iTeam;
TrieSetCell( hSaveSteamID, bet[ PlayerSteamID ], ArrayPushArray( hSaveTeamBet, bet, sizeof bet ) );
}
}
return PLUGIN_CONTINUE;
}
public TeamWins( team )
{
if( team == TEAM_CT || team == TEAM_T )
{
new max = ArraySize( hSaveTeamBet );
new bet[ BetUtils ];
new bool:isWinner;
new query[ 384 ];
new escName[ 64 ];
for( new i; i < max; i++ )
{
ArrayGetArray( hSaveTeamBet, i, bet, sizeof bet );
new id = find_player( "c", bet[ PlayerSteamID ] );
if( id )
{
#if defined COLORED_CHAT
client_print_color( id, print_team_red, "^3%s ^4You ^3%s^4 the bet!", CHAT_PREFIX, team==bet[ Vote ]? "WON":"LOST" );
#else
client_print( id, print_chat, "%s You %s the bet!", CHAT_PREFIX, team==bet[ Vote ]? "WON":"LOST" );
#endif
}
isWinner = team==bet[ Vote ]? true:false;
SQL_QuoteString( Empty_Handle, escName, charsmax( escName ), bet[ PlayerName ] );
#if defined UPDATE_NICK
formatex( query, charsmax( query ), "INSERT INTO `%s` VALUES( NULL, '%s', '%s', %d, %d ) ON DUPLICATE KEY UPDATE `player_nick`= '%s', `bets_won` = `bets_won` + %d, `bets_lost` = `bets_lost` + %d;", table, escName, bet[ PlayerSteamID ], isWinner? 1:0, isWinner? 0:1, escName, isWinner? 1:0, isWinner? 0:1 );
#else
formatex( query, charsmax( query ), "INSERT INTO `%s` VALUES( NULL, '%s', '%s', %d, %d ) ON DUPLICATE KEY UPDATE `bets_won` = `bets_won` + %d, `bets_lost` = `bets_lost` + %d;", table, escName, bet[ PlayerSteamID ], isWinner? 1:0, isWinner? 0:1, isWinner? 1:0, isWinner? 0:1 );
#endif
SQL_ThreadQuery( tuple, "IgnoreHandle", query );
}
}
ArrayClear( hSaveTeamBet );
TrieClear( hSaveSteamID );
}
public SQL_Init()
{
new query[ 256 ];
formatex( query, charsmax( query ), "CREATE TABLE IF NOT EXISTS `%s` ( id INT NOT NULL AUTO_INCREMENT, player_nick VARCHAR(32) NOT NULL, player_id VARCHAR(30) NOT NULL UNIQUE, bets_won INT NOT NULL, bets_lost INT NOT NULL, PRIMARY KEY(id) );", table );
SQL_ThreadQuery( tuple, "IgnoreHandle", query );
}
public IgnoreHandle( failState, Handle:query, error[], errNum )
{
if( errNum )
{
set_fail_state( error );
}
SQL_FreeHandle( query );
}