Piszę sobie rank pod zombi, ale mam problem z połączeniem się z bazą danych. Tak przynajmniej mi się wydaje, bo po wejści na serwer powinna wykonać się funkcja dodająca gracza do bazy danych, ale się tak nie dzieje. Plugin pisany na podstawie tutka z waszego forum.
Kod:
#include <amxmodx>
#include <sqlx>
#include <zombieplague>
#include <zp50_core>
#include <zp50_gamemodes>
#define LIBRARY_NEMESIS "zp50_class_nemesis"
#include <zp50_class_nemesis>
#define LIBRARY_SURVIVOR "zp50_class_survivor"
#include <zp50_class_survivor>
#define LIBRARY_SNIPER "zp50_class_sniper"
#include <zp50_class_sniper>
#define LIBRARY_ASSASSIN "zp50_class_assassin"
#include <zp50_class_assassin>
new Handle:Connect;
new iKillZombie[33], iKillHuman[33];
new iInfections[33], iInfected[33];
new iAntidoted[33];
new iDeath[33];
new iLastZombie[33], iLastHuman[33];
new iNemesis[33], iAssassin[33];
new iSurvivor[33], iSniper[33];
new iFirstZombie[33];
public plugin_init() {
register_plugin("[ZP] Rank", "1.0", "Szapko");
register_event("DeathMsg", "DeathEvent", "a");
Connect = SQL_MakeDbTuple("IP", " ", "PASS", " ");
}
public plugin_natives() {
set_module_filter("module_filter");
set_native_filter("native_filter");
}
public module_filter(const module[]) {
if (equal(module, LIBRARY_NEMESIS) || equal(module, LIBRARY_SURVIVOR) || equal(module, LIBRARY_SNIPER) || equal(module, LIBRARY_ASSASSIN))
return PLUGIN_HANDLED;
return PLUGIN_CONTINUE;
}
public native_filter(const name[], index, trap) {
if (!trap)
return PLUGIN_HANDLED;
return PLUGIN_CONTINUE;
}
check_data(id) {
new DataID[1];
new qCommand[512], PlayerID[32];
DataID[0] = id;
get_user_name(id, PlayerID, charsmax(PlayerID));
format(qCommand, sizeof qCommand-1, "SELECT * FROM zp_rank WHERE 'zp_nick'=%s", PlayerID);
SQL_ThreadQuery(Connect, "handler_check", qCommand, DataID, 1);
}
public save_rank(id) {
new DataID[1];
new qCommand[512], PlayerID[32];
DataID[0] = id;
get_user_name(id, PlayerID, charsmax(PlayerID));
format(qCommand, sizeof qCommand-1, "INSERT INTO zp_rank VALUES ('', %s, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i);", PlayerID, iKillZombie[id], iKillHuman[id], iDeath[id], iInfections[id], iInfected[id], iAntidoted[id], iLastZombie[id], iLastHuman[id], iFirstZombie[id], iNemesis[id], iAssassin[id] ,iSurvivor[id] ,iSniper[id]);
SQL_ThreadQuery(Connect, "handler_save", qCommand, DataID, 1);
return PLUGIN_CONTINUE;
}
remove_all(id) {
iKillZombie[id] = 0;
iKillHuman[id] = 0;
iDeath[id] = 0;
iInfections[id] = 0;
iInfected[id] = 0;
iAntidoted[id] = 0;
iLastZombie[id] = 0;
iLastHuman[id] = 0;
iFirstZombie[id] = 0;
iNemesis[id] = 0;
iAssassin[id] = 0;
iSurvivor[id] = 0;
iSniper[id] = 0;
}
public client_disconnect(id) {
check_data(id);
remove_all(id);
}
public client_putinserver(id) {
remove_all(id);
check_data(id);
}
public plugin_end() {
//SQL_FreeHandle(Connect);
}
public handler_save(FailState, Handle:Query, Errorcode, Error[], Data[], DataSize) {
//new id = Data[0];
if (Errorcode) {
log_amx("[RANK] Blad w zapytaniu: %s [save_rank]", Error);
}
if (FailState == TQUERY_CONNECT_FAILED) {
log_amx("[RANK] Nie mozna podlaczyc sie do bazy danych");
return PLUGIN_CONTINUE;
} else if(FailState == TQUERY_QUERY_FAILED) {
log_amx("[RANK] Zapytanie anulowane [save_rank]");
return PLUGIN_CONTINUE;
}
return PLUGIN_CONTINUE;
}
public handler_check(FailState, Handle:Query, Errorcode, Error[], Data[], DataSize) {
new id = Data[0];
if (Errorcode) {
log_amx("[RANK] Blad w zapytaniu: %s [check_data]", Error);
}
if (FailState == TQUERY_CONNECT_FAILED) {
log_amx("[RANK] Nie mozna podlaczyc sie do bazy danych");
return PLUGIN_CONTINUE;
} else if(FailState == TQUERY_QUERY_FAILED) {
log_amx("[RANK] Zapytanie anulowane [check_data]");
return PLUGIN_CONTINUE;
}
if (!SQL_MoreResults(Query)) {
save_rank(id);
} else {
iKillZombie[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "zp_kill_zombie"));
iKillHuman[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "zp_kill_human"));
iDeath[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "zp_death"));
iInfections[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "zp_infections"));
iInfected[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "zp_infected"));
iAntidoted[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "zp_antidoted"));
iLastZombie[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "zp_last_zombie"));
iLastHuman[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "zp_last_human"));
iFirstZombie[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "zp_first_zombie"));
iNemesis[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "zp_nemesis"));
iAssassin[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "zp_assassin"));
iSurvivor[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "zp_survivor"));
iSniper[id] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "zp_sniper"));
}
return PLUGIN_CONTINUE;
}
Jest to tylko część kodu odpowiadająca za zapisywanie i wczytywanie. Oczywiście dane do serwera wypełniam tak jak trzeba, bo mam na serwerze już amxbans, który działa i korzystam z tej samej bazy danych.