#include < amxmodx >
#include < fakemeta >
#include < sqlx >
#define PLUGIN_NAME "Level__System"
#define PLUGIN_VERSION "0.1"
#define PLUGIN_AUTHOR "FirsT / Bos93"
#define MAX_CLIENTS 32
new Host[] = "..."
//new Host[] = "..."
new User[] = "..."
new Pass[] = "..."
new Db[] = "..."
new Handle:g_SqlTuple
new g_Error[512]
#define NEW_LEVEL "Lvl_system/promoted.wav"
#define ID_SHOWHUD (taskid - TASK_SHOWHUD)
enum (+= 100)
{
TASK_SHOWHUD = 10
}
new const MAX_LEVELS[16] =
{
50, //1
100, //2
200, //3
290, //4
360, //5
480, //6
580, //7
700, //8
850, //9
1000, //10
1150, //11
1300, //12
1500, //13
1800, //14
2000, //15
2500 //16
}
const Float:HUD_STATS_X = 0.01;
const Float:HUD_STATS_Y = 0.9025;
const Float:HUD_SPECT_X = 0.70;
const Float:HUD_SPECT_Y = 0.925;
const PEV_SPEC_TARGET = pev_iuser2
new g_iLevel[ MAX_CLIENTS + 1 ],
g_iExp[ MAX_CLIENTS + 1 ],
g_playername[ MAX_CLIENTS + 1 ][ MAX_CLIENTS ];
public plugin_init( )
{
register_plugin ( PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR );
register_event( "DeathMsg", "EV_DeathMsg", "a" );
set_task(1.0, "MySql_Init")
}
public plugin_precache()
{
precache_sound( NEW_LEVEL );
}
public plugin_natives()
{
register_native("zp_get_user_level", "native_get_user_level", 1)
register_native("zp_set_user_level", "native_set_user_level", 1)
register_native("zp_get_user_exp", "native_get_user_exp", 1)
register_native("zp_set_user_exp", "native_set_user_exp", 1)
}
public client_putinserver( iPlayer )
{
get_user_name(iPlayer, g_playername[iPlayer], charsmax(g_playername[]))
set_task(1.0, "ShowHUD", iPlayer+TASK_SHOWHUD, _, _, "b");
Load_MySql( iPlayer )
}
public client_disconnect( iPlayer )
{
Save_MySql( iPlayer )
}
public MySql_Init()
{
// we tell the API that this is the information we want to connect to,
// just not yet. basically it's like storing it in global variables
g_SqlTuple = SQL_MakeDbTuple(Host,User,Pass,Db)
// ok, we're ready to connect
new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
if(SqlConnection == Empty_Handle)
// stop the plugin with an error message
set_fail_state(g_Error)
new Handle:Queries
// we must now prepare some random queries
Queries = SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS tutorial (steamid varchar(32),exp INT(11),level INT(11))")
if(!SQL_Execute(Queries))
{
// if there were any problems
SQL_QueryError(Queries,g_Error,charsmax(g_Error))
set_fail_state(g_Error)
}
// close the handle
SQL_FreeHandle(Queries)
// you free everything with SQL_FreeHandle
SQL_FreeHandle(SqlConnection)
}
public plugin_end()
{
// free the tuple - note that this does not close the connection,
// since it wasn't connected in the first place
SQL_FreeHandle(g_SqlTuple)
}
public Load_MySql(id)
{
new szSteamId[32], szTemp[512]
get_user_authid(id, szSteamId, charsmax(szSteamId))
new Data[1]
Data[0] = id
//we will now select from the table `tutorial` where the steamid match
format(szTemp,charsmax(szTemp),"SELECT * FROM `tutorial` WHERE (`tutorial`.`steamid` = '%s')", szSteamId)
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)
}
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 id
id = Data[0]
if(SQL_NumResults(Query) < 1)
{
//.if there are no results found
new szSteamId[32]
get_user_authid(id, szSteamId, charsmax(szSteamId)) // get user's steamid
// if its still pending we can't do anything with it
if (equal(szSteamId,"ID_PENDING"))
return PLUGIN_HANDLED
new szTemp[512]
// now we will insturt the values into our table.
format(szTemp,charsmax(szTemp),"INSERT INTO `tutorial` ( `steamid` , `exp`, `level`) VALUES ('%s',%i,%i);",szSteamId,g_iExp[id], g_iLevel[id])
//DEBUG
server_print("%s", szTemp)
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
}
else
{
new iExp, sExp[11],
iLevel, sLevel[11]
iExp = SQL_FieldNameToNum(Query, "exp")
iLevel = SQL_FieldNameToNum(Query, "level")
SQL_ReadResult(Query, iExp, sExp, charsmax(sExp))
SQL_ReadResult(Query, iLevel, sLevel, charsmax(sLevel))
g_iExp[id] = str_to_num(sExp)
g_iLevel[id] = str_to_num(sLevel)
}
return PLUGIN_HANDLED
}
public Save_MySql(id)
{
new szSteamId[32], szTemp[512]
get_user_authid(id, szSteamId, charsmax(szSteamId))
// Here we will update the user hes information in the database where the steamid matches.
format(szTemp,charsmax(szTemp),"UPDATE `tutorial` SET `Level` = '%i' , `exp` = '%i' WHERE `tutorial`.`steamid` = '%s';",g_iLevel[id],g_iExp[id], szSteamId)
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
}
public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
SQL_FreeHandle(Query)
return PLUGIN_HANDLED
}
public EV_DeathMsg( )
{
static iVictim, iKiller;
iVictim = read_data( 2 );
iKiller = read_data( 1 );
if( !is_user_connected( iVictim ) )
return;
if( iKiller == iVictim || !iKiller )
return;
g_iExp[ iKiller ] += 1;
while( g_iExp[ iKiller ] >= MAX_LEVELS[ g_iLevel[ iKiller ] ] )
{
g_iLevel[ iKiller ] += 1;
color_print( iKiller , "!y[!gСервер!y] Поздравляем! Вы повысили свой уровень!");
emit_sound(iKiller, CHAN_AUTO, "Lvl_system/promoted.wav", 0.5, 0.80, 0, 100)
}
}
public ShowHUD(taskid)
{
static iPlayer
iPlayer = ID_SHOWHUD;
if (!is_user_alive( iPlayer ) )
{
iPlayer = pev(iPlayer, PEV_SPEC_TARGET)
// Target not alive
if (!is_user_alive(iPlayer) ) return;
}
if (iPlayer != ID_SHOWHUD)
{
set_hudmessage( 255, 255, 255, HUD_SPECT_X, HUD_SPECT_Y, 0, 5.0, 3.0, 1.0, 1.0, -1 );
show_hudmessage( ID_SHOWHUD , "Уровень: %d | Опыт: %d из %d", g_iLevel[ ID_SHOWHUD ] , g_iExp[ ID_SHOWHUD ] , (MAX_LEVELS[g_iLevel[ ID_SHOWHUD ]]) );
}
else
{
set_hudmessage( 255, 102, 021, HUD_STATS_X, HUD_STATS_Y, 0, 5.0, 3.0, 1.0, 1.0, -1 );
show_hudmessage( ID_SHOWHUD , "Уровень: %d | Опыт: %d из %d", g_iLevel[ ID_SHOWHUD ] , g_iExp[ ID_SHOWHUD ] , (MAX_LEVELS[g_iLevel[ ID_SHOWHUD ]]));
}
}
public native_get_user_exp(id)
{
return g_iExp[id];
}
public native_set_user_exp(id, amount)
{
g_iExp[id] = amount;
}
public native_get_user_level(id)
{
return g_iLevel[id];
}
public native_set_user_level(id, amount)
{
g_iLevel[id] = amount;
}
stock color_print( const id , const input[], any:...)
{
new count = 1, players[32]
static msg[191]
vformat(msg, 190, input, 3)
replace_all(msg, 190, "!g", "^4" ) // Green Color
replace_all(msg, 190, "!y", "^1" ) // Default Color
replace_all(msg, 190, "!team", "^3" ) // Team Color
if (id) players[0] = id; else get_players(players, count, "ch")
{
for (new i = 0; i < count; i++)
{
if (is_user_connected(players[i]))
{
message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("SayText" ), _, players[i])
write_byte(players[i]);
write_string(msg);
message_end();
}
}
}
}