Spoiler
/* Plugin generated by AMXX-Studio */
#include <amxmodx>
#include <nvault>
#include <sqlx>
#include <ColorChat>
#define VERSION "0.2"
new pCvar_Kill
new pCvar_Survive;
new pCvar_MinPlayers;
new pCvar_SaveType;
new g_iMaxClients;
new bool:g_bEnoughPlayers = false;
new g_sPrefix[] = "ExpMod";
new g_PlayerExp[33];
new bool:g_bWasLoad[33] = false;
#if defined WORKING_ON_IP
new const g_sServerAdress[] = "Write your server IP";
#endif
new g_NvaultFile;
new const g_sNvaultName[] = "ExpMod";
enum _:SQLData
{
SQLD_Host,
SQLD_User,
SQLD_Password,
SQLD_Database
};
new g_sSQLData[SQLData][32];
new g_pSQLD_Cvars[SQLData];
new g_sTable[32];
new Handle:g_hTuple;
new bool:g_bRemote;
new g_sCreateTableSql[] = "\
CREATE TABLE IF NOT EXISTS `%s`\
(\
`sid` VARCHAR(32),\
`exp` INT(10)\
)";
public plugin_init(){
register_plugin("Silnik ExpMod", VERSION, "");
#if defined WORKING_ON_IP
new sServerAdress[24];
get_user_ip(0, sServerAdress, sizeof sServerAdress - 1);
if(!equali(g_sServerAdress, sServerAdress))
set_fail_state("Dont try copy this plugin <img src='https://amxx.pl/public/style_emoticons/<#EMO_DIR#>/smile.png' class='bbc_emoticon' alt=':)' />");
#endif
register_event("DeathMsg", "eventDeathMsg", "a");
register_logevent("eventRoundStart", 2, "1=Round_Start");
register_logevent("eventRoundEnd", 2 , "1=Round_End");
register_cvar("exp_kill", "10");
register_cvar("exp_survive", "15");
register_cvar("exp_minplayers", "0");
register_cvar("exp_savetype", "1");
/* savetype: 0 - none, 1 - SQL, 2 - nvault */
pCvar_Kill = get_cvar_num("exp_kill");
pCvar_Survive = get_cvar_num("exp_survive");
pCvar_MinPlayers = get_cvar_num("exp_minplayers");
pCvar_SaveType = get_cvar_num("exp_savetype");
register_clcmd("say asd", "asd");
g_NvaultFile = nvault_open(g_sNvaultName);
if(g_NvaultFile == INVALID_HANDLE)
log_amx("Couldn't open nvault file (%s).", g_sNvaultName);
g_pSQLD_Cvars[SQLD_Host] = register_cvar("exp_sql_host", "127.0.0.1");
g_pSQLD_Cvars[SQLD_User] = register_cvar("exp_sql_user", "root");
g_pSQLD_Cvars[SQLD_Password] = register_cvar("exp_sql_password", "");
g_pSQLD_Cvars[SQLD_Database] = register_cvar("exp_sql_database", "tescik");
register_cvar("exp_sql_table", "tabela");
register_cvar("exp_sql_remote_access", "0"); /* if database with server are on this same server, you would use 1, if aren't, use 0 */
SqlConnect();
g_iMaxClients = get_maxplayers();
}
public asd(id){
client_print(id, print_chat, "%d", g_PlayerExp[id]);
}
public SqlConnect(){
get_pcvar_string(g_pSQLD_Cvars[SQLD_Host], g_sSQLData[SQLD_Host], 31);
get_pcvar_string(g_pSQLD_Cvars[SQLD_User], g_sSQLData[SQLD_User], 31);
get_pcvar_string(g_pSQLD_Cvars[SQLD_Password], g_sSQLData[SQLD_Password], 31);
get_pcvar_string(g_pSQLD_Cvars[SQLD_Database], g_sSQLData[SQLD_Database], 31);
get_cvar_string("exp_sql_table", g_sTable, 31);
g_bRemote = bool:get_cvar_num("exp_sql_remote_access");
g_hTuple = SQL_MakeDbTuple(g_sSQLData[SQLD_Host], g_sSQLData[SQLD_User], g_sSQLData[SQLD_Password], g_sSQLData[SQLD_Database]);
new Handle:hConnection, error, sError[256];
if((hConnection = SQL_Connect(g_hTuple, error, sError, 255))){
if(g_bRemote){
new Handle:hQuery;
hQuery = SQL_PrepareQuery(hConnection, g_sCreateTableSql, g_sTable);
if(!SQL_Execute(hQuery)){
SQL_QueryError(hQuery, sError, 255);
log_amx("%s", sError);
}
}
else{
new sCommand[512];
formatex(sCommand, 511, g_sCreateTableSql, g_sTable);
SQL_ThreadQuery(g_hTuple, "handleSqlCreateTable", sCommand);
}
}
else{
log_amx("%s", sError);
}
}
public handleSqlCreateTable(FailState, Handle:Query, Error[], Errcode, Data[], DataSize){
if(Errcode)
log_amx("Error on Table query: %s",Error)
if(FailState == TQUERY_CONNECT_FAILED){
log_amx("Could not connect to SQL database.")
return 0;
}
else if(FailState == TQUERY_QUERY_FAILED){
log_amx("Table Query failed.")
return 0;
}
return 0;
}
public handleSqlSaveData(FailState, Handle:Query, Error[], Errcode, Data[], DataSize){
if(Errcode)
log_amx("Error on Table query: %s", Error);
if(FailState == TQUERY_CONNECT_FAILED){
log_amx("Could not connect to SQL database.");
return 0;
}
else if(FailState == TQUERY_QUERY_FAILED){
log_amx("Table Query failed");
return 0;
}
new id = Data[0];
if(SQL_MoreResults(Query) > 0){
new sCommand[512], sAUTHID[32];
get_user_authid(id, sAUTHID, sizeof sAUTHID - 1);
formatex(sCommand, 511, "UPDATE %s SET `exp` = %i WHERE `sid` = '%s'", g_sTable, g_PlayerExp[id], sAUTHID);
SQL_ThreadQuery(g_hTuple, "handleSqlSaveDataOther", sCommand);
}
else{
new sCommand[512], sAUTHID[32];
get_user_authid(id, sAUTHID, sizeof sAUTHID - 1);
formatex(sCommand, 511, "INSERT INTO %s (`sid`, `exp`) VALUES('%s', %i)", g_sTable, sAUTHID, g_PlayerExp[id]);
SQL_ThreadQuery(g_hTuple, "handleSqlSaveDataOther", sCommand);
}
return 0;
}
public handleSqlSaveDataOther(FailState, Handle:Query, Error[], Errcode, Data[], DataSize){
if(Errcode){
log_amx("Error on Table query: %s", Error);
}
if(FailState == TQUERY_CONNECT_FAILED){
log_amx("Could not connect to SQL database");
return 0;
}
else if(FailState == TQUERY_QUERY_FAILED){
log_amx("Table Query failed");
return 0;
}
return 0;
}
public handleSqlLoadData(FailState, Handle:Query, Error[], Errcode, Data[], DataSize){
if(Errcode)
log_amx("Error on Table query: %s",Error);
if(FailState == TQUERY_CONNECT_FAILED){
log_amx("Could not connect to SQL database");
return 0;
}
else if(FailState == TQUERY_QUERY_FAILED){
log_amx("Table Query failed");
return 0;
}
new id = Data[0];
if(SQL_MoreResults(Query)){
g_PlayerExp[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "exp"));
}
else {
g_bWasLoad[id] = true; /* change to true for don't double save to SQL */
SaveData(id);
}
return 0;
}
SaveData(id){
if(!is_user_connected(id))
return PLUGIN_CONTINUE;
else if(!g_bWasLoad[id])
LoadData(id);
if(pCvar_SaveType == 1){
new Handle:hConnection, error, sError[256];
if((hConnection = SQL_Connect(g_hTuple, error, sError, 255))){
new sAUTHID[32];
get_user_authid(id, sAUTHID, sizeof sAUTHID - 1);
if(g_bRemote){
new Handle:hQuery;
hQuery = SQL_PrepareQuery(hConnection, "SELECT * FROM %s WHERE `sid` = '%s'", g_sTable, sAUTHID);
if(!SQL_Execute(hQuery)){
SQL_QueryError(hQuery, sError, 255);
log_amx("%s", sError);
}
else{
if(SQL_MoreResults(hQuery) > 0){
hQuery = SQL_PrepareQuery(hConnection, "UPDATE %s SET `exp` = %i WHERE `sid` = '%s'", g_sTable, g_PlayerExp[id], sAUTHID);
if(!SQL_Execute(hQuery)){
SQL_QueryError(hQuery, sError, 255);
log_amx("%s", sError);
}
}
else{
hQuery = SQL_PrepareQuery(hConnection, "INSERT INTO %s (`sid`, `exp`) VALUES('%s', %i)", g_sTable, sAUTHID, g_PlayerExp[id]);
if(!SQL_Execute(hQuery)){
SQL_QueryError(hQuery, sError, 255);
log_amx("%s", sError);
}
}
}
}
else{
new sCommand[512], iData[1];
formatex(sCommand, 511, "SELECT * FROM %s WHERE `sid` = '%s'", g_sTable, sAUTHID);
iData[0] = id;
SQL_ThreadQuery(g_hTuple, "handleSqlSaveData", sCommand, iData, 1);
}
}
else log_amx("%s", sError);
}
else if(pCvar_SaveType == 2){
new sAUTHID[32], sCommand[512];
get_user_authid(id, sAUTHID, sizeof sAUTHID - 1);
formatex(sCommand, sizeof sCommand - 1, "#%d", g_PlayerExp[id]);
nvault_set(g_NvaultFile, sAUTHID, sCommand);
}
return PLUGIN_CONTINUE;
}
LoadData(id){
if(is_user_connected(id) && !g_bWasLoad[id])
g_bWasLoad[id] = true;
if(pCvar_SaveType == 1){
new Handle:hConnection, error, sError[256];
if((hConnection = SQL_Connect(g_hTuple, error, sError, 255))){
new sAUTHID[32];
get_user_authid(id, sAUTHID, sizeof sAUTHID - 1);
if(g_bRemote){
new Handle:hQuery;
hQuery = SQL_PrepareQuery(hConnection, "SELECT * FROM %s WHERE `sid` = '%s'", g_sTable, sAUTHID);
if(!SQL_Execute(hQuery)){
SQL_QueryError(hQuery, sError, 255);
log_amx("%s", sError);
}
else{
if(SQL_MoreResults(hQuery)){
g_PlayerExp[id] = SQL_ReadResult(hQuery, SQL_FieldNameToNum(hQuery, "exp"));
}
else SaveData(id);
}
}
else{
new sCommand[512], iData[1];
formatex(sCommand, 511, "SELECT * FROM %s WHERE `sid` = '%s'", g_sTable, sAUTHID);
iData[0] = id;
SQL_ThreadQuery(g_hTuple, "handleSqlLoadData", sCommand, iData, 1);
}
}
else{
log_amx("%s", sError);
}
}
else if(pCvar_SaveType == 2){
new sAUTHID[32], sCommand[512], sPlayerExp[16];
get_user_authid(id, sAUTHID, sizeof sAUTHID - 1);
nvault_get(g_NvaultFile, sAUTHID, sCommand, sizeof sCommand - 1);
replace_all(sCommand, charsmax(sCommand), " ", "_");
replace_all(sCommand, charsmax(sCommand), "#", " ");
parse(sCommand, sPlayerExp, sizeof sPlayerExp - 1);
g_PlayerExp[id] = str_to_num(sPlayerExp);
}
}
public client_authorized(id){
g_bWasLoad[id] = false;
g_PlayerExp[id] = 0;
LoadData(id);
}
public client_disconnect(id){
SaveData(id);
g_bWasLoad[id] = false;
}
public eventRoundStart(){
if(get_playersnum() >= pCvar_MinPlayers)
g_bEnoughPlayers = true;
else
g_bEnoughPlayers = false;
if(pCvar_SaveType == 2){
nvault_close(g_NvaultFile);
g_NvaultFile = nvault_open(g_sNvaultName);
if(g_NvaultFile == INVALID_HANDLE)
log_amx("Couldn't open nvault file (%s).", g_sNvaultName);
}
return PLUGIN_CONTINUE;
}
public eventRoundEnd(){
if(!g_bEnoughPlayers)
return PLUGIN_CONTINUE;
new iPlayers[32], iNum;
get_players(iPlayers, iNum);
for(new g = 0; g < iNum; g++){
new id = iPlayers[g];
if(!is_user_connected(id) || !is_user_alive(id))
continue;
if(get_user_team(id) == 1){
g_PlayerExp[id] += pCvar_Survive;
ColorChat(id, GREY, "^x04[%s]^x03 Zdobyles^x01 %d^x03 doswiadczenia za przezycie rundy^x01!", g_sPrefix, pCvar_Survive);
SaveData(id);
}
}
}
public eventDeathMsg(){
new KillerID = read_data(1);
new VictimID = read_data(2);
if(IsPlayer(KillerID) && NotSameTeam(KillerID, VictimID) && KillerID != VictimID && g_bEnoughPlayers){
g_PlayerExp[KillerID] += pCvar_Kill;
ColorChat(KillerID, GREY, "^x04[%s]^x03 Zdobyles^x01 %d^x03 doswiadczenia za zabicie przeciwnika^x01!", g_sPrefix, pCvar_Kill);
}
SaveData(KillerID);
return PLUGIN_CONTINUE;
}
bool:IsPlayer(index)
return ((1 <= index <= g_iMaxClients) && is_user_connected(index));
bool:NotSameTeam(index1, index2)
return (get_user_team(index1) != get_user_team(index2));
Użytkownik funfel edytował ten post 02.10.2011 18:08


Dodatki SourceMod
















