13.11.2015, 19:30
I have problem while saving database as string or '%s'. in my database is empty. I want save username after load username at "mysql_login" but if i "disconnect", database saving empty username.
please help ...
This is my GM, sorry i cant choose as needed, so i "copy paste" all of my GM.
please help ...
Код:
#include <a_samp> #include <a_mysql> #include <md5> #include <zcmd> #include <sscanf2> #define SQL_HOST "127.0.0.1" #define SQL_USER "root" #define SQL_PASS "xxxxxxxxxxx" #define SQL_DB "xxxxxxxxxxxxxx" enum pInfo { pMoney, pLogins, pGender, pSkin, pAdmin, pUname, Float:pPosX, Float:pPosY, Float:pPosZ, Float:pPosA } new PlayerInfo[MAX_PLAYERS][pInfo]; new MoneyGiven[MAX_PLAYERS]; new IsRegistered[MAX_PLAYERS]; new Logged[MAX_PLAYERS]; new JustLogged[MAX_PLAYERS]; new pskins[] = {1,2,3,4,5,6,7,8}; new wskins[] = {11,12,13}; //////////////////////////////////////////////////////////////////////////////// stock GetName(playerid) { new pName[24]; GetPlayerName(playerid, pName, 24); return pName; } stock GetIP(playerid) { new ip[16]; GetPlayerIp(playerid, ip, sizeof(ip)); return ip; } stock MySQL_Login(playerid) { new query[300], pName[24], savingstring[20]; GetPlayerName(playerid, pName, 24); format(query, sizeof(query), "SELECT * FROM playerchar WHERE charname = '%s'", pName); mysql_query(query); mysql_store_result(); while(mysql_fetch_row_format(query,"|")) { mysql_fetch_field_row(savingstring, "level"); SetPlayerScore(playerid, strval(savingstring)); mysql_fetch_field_row(savingstring, "skin"); PlayerInfo[playerid][pSkin] = strval(savingstring); mysql_fetch_field_row(savingstring, "money"); MoneyGiven[playerid] = strval(savingstring); mysql_fetch_field_row(savingstring, "posx"); PlayerInfo[playerid][pPosX] = strval(savingstring); mysql_fetch_field_row(savingstring, "posy"); PlayerInfo[playerid][pPosY] = strval(savingstring); mysql_fetch_field_row(savingstring, "posz"); PlayerInfo[playerid][pPosZ] = strval(savingstring); mysql_fetch_field_row(savingstring, "posa"); PlayerInfo[playerid][pPosA] = strval(savingstring); mysql_fetch_field_row(savingstring, "logins"); PlayerInfo[playerid][pLogins] = strval(savingstring); mysql_fetch_field_row(savingstring, "gender"); PlayerInfo[playerid][pGender] = strval(savingstring); mysql_fetch_field_row(savingstring, "admin"); PlayerInfo[playerid][pAdmin] = strval(savingstring); mysql_fetch_field_row(savingstring, "username"); PlayerInfo[playerid][pUname] = strval(savingstring); } JustLogged[playerid] = 1; Logged[playerid] = 1; PlayerInfo[playerid][pLogins]++; if(MoneyGiven[playerid] != -1) { GivePlayerMoney(playerid, MoneyGiven[playerid]); MoneyGiven[playerid] = -1; } format(query, sizeof(query), "SELECT logins FROM playerchar WHERE charname = '%s' AND logins='%i'", pName, 0); mysql_query(query); mysql_store_result(); new rows = mysql_num_rows(); if(!rows) { TogglePlayerSpectating(playerid, false); SetSpawnInfo(playerid, 0, PlayerInfo[playerid][pSkin], PlayerInfo[playerid][pPosX], PlayerInfo[playerid][pPosY], PlayerInfo[playerid][pPosZ], PlayerInfo[playerid][pPosA], 0, 0, 0, 0, 0, 0 ); SpawnPlayer(playerid); new st[128]; format(st,128,"%s telah terkoneksi ke server IDRP!",GetName(playerid),GetName(playerid)); SendClientMessageToAll(0x33CCFFAA,st); new s[64]; format(s,64,"Selamat datang kembali! Semoga sehat selalu.",GetName(playerid)); SendClientMessage(playerid,0x33AA33AA,s); } else { ShowPlayerDialog(playerid, 15550, DIALOG_STYLE_LIST, "Sialhkan pilih mau dimana kamu mendarat!", "Stasiun Unity\nStasiun Market\nBandara Los Santos", "Oke", "Cancel"); } mysql_free_result(); return 1; } stock SavePlayerAccount(playerid) { if(Logged[playerid] == 1) { new Float:x, Float:y, Float:z, Float:a; GetPlayerPos(playerid, x, y, z); GetPlayerFacingAngle(playerid, a); PlayerInfo[playerid][pPosX] = x; PlayerInfo[playerid][pPosY] = y; PlayerInfo[playerid][pPosZ] = z; PlayerInfo[playerid][pPosA] = a; PlayerInfo[playerid][pSkin] = GetPlayerSkin(playerid); new level = GetPlayerScore(playerid); new money = GetPlayerMoney(playerid); new query[300], pName[24], nquery[300]; GetPlayerName(playerid, pName, 24); format(query, sizeof(query), "UPDATE playerchar SET level='%d', money='%d' WHERE charname='%s'", level, money, pName); format(nquery,300,"UPDATE playerchar SET admin='%d', username = '%s', skin='%d', logins='%d', posx='%f', posy='%f', posz='%f', posa='%f' WHERE charname='%s'", PlayerInfo[playerid][pAdmin], PlayerInfo[playerid][pUname], PlayerInfo[playerid][pSkin], PlayerInfo[playerid][pLogins], PlayerInfo[playerid][pPosX], PlayerInfo[playerid][pPosY], PlayerInfo[playerid][pPosZ], PlayerInfo[playerid][pPosA], pName); mysql_query(query); mysql_query(nquery); } return 1; } //////////////////////////////////////////////////////////////////////////////// main() {} //////////////////////////////////////////////////////////////////////////////// public OnGameModeInit() { SetGameModeText("IDRP Beta Version"); mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS); if(!mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS)) printf("MySQL connection attempt %d failed!"); else printf("MySQL connection sucessed!"); mysql_debug(1); return 1; } //////////////////////////////////////////////////////////////////////////////// public OnGameModeExit() { return 1; } //////////////////////////////////////////////////////////////////////////////// public OnPlayerRequestClass(playerid) { TogglePlayerSpectating(playerid, true); return 1; } //////////////////////////////////////////////////////////////////////////////// public OnPlayerConnect(playerid) { TogglePlayerSpectating(playerid, true); MoneyGiven[playerid] = -1; JustLogged[playerid] = 0; new query[300], pName[24]; GetPlayerName(playerid, pName, 24); format(query, sizeof(query), "SELECT * FROM playerchar WHERE charname = '%s' LIMIT 1", pName); mysql_query(query); mysql_store_result(); new rows = mysql_num_rows(); if(!rows) { ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_MSGBOX, "OPS...!","Kamu {FF0000}BELUM TERDAFTAR{FFFFFF}, silahkan daftar di {0000FF}idrp.co.id {FFFFFF}atau {0000FF}http://88.202.230.9!","Quit",""); } if(rows == 1) { ShowPlayerDialog(playerid, 15500, DIALOG_STYLE_PASSWORD, "Login","Your user is {FF0000}registered{FFFFFF}! Please {0000FF}login{FFFFFF} with your password below!","Login","Cancel"); IsRegistered[playerid] = 1; } new nquery[300]; format(nquery, sizeof(nquery), "UPDATE playerchar SET ip='%s' WHERE charname='%s'",GetIP(playerid),pName); mysql_query(nquery); mysql_free_result(); return 1; } //////////////////////////////////////////////////////////////////////////////// public OnPlayerDisconnect(playerid, reason) { SavePlayerAccount(playerid); return 1; } forward SaveStats(playerid); public SaveStats(playerid) { if(Logged[playerid] == 1) { new Float:x, Float:y, Float:z, Float:a; GetPlayerPos(playerid, x, y, z); GetPlayerFacingAngle(playerid, a); PlayerInfo[playerid][pPosX] = x; PlayerInfo[playerid][pPosY] = y; PlayerInfo[playerid][pPosZ] = z; PlayerInfo[playerid][pPosA] = a; PlayerInfo[playerid][pSkin] = GetPlayerSkin(playerid); new level = GetPlayerScore(playerid); new money = GetPlayerMoney(playerid); new query[300], pName[24], nquery[300]; GetPlayerName(playerid, pName, 24); format(query, sizeof(query), "UPDATE playerchar SET level='%d', money='%d' WHERE charname='%s'", level, money, pName); format(nquery,300,"UPDATE playerchar SET admin='%s', skin='%d', logins='%d', posx='%f', posy='%f', posz='%f', posa='%f', ip='%s' WHERE charname='%s'", PlayerInfo[playerid][pAdmin], PlayerInfo[playerid][pSkin], PlayerInfo[playerid][pLogins], PlayerInfo[playerid][pPosX], PlayerInfo[playerid][pPosY], PlayerInfo[playerid][pPosZ], PlayerInfo[playerid][pPosA], GetIP(playerid), pName); mysql_query(query); mysql_query(nquery); } return 1; } //////////////////////////////////////////////////////////////////////////////// public OnPlayerSpawn(playerid) { return 1; } //////////////////////////////////////////////////////////////////////////////// public OnPlayerDeath(playerid, killerid, reason) { return 1; } public OnVehicleSpawn(vehicleid) { return 1; } public OnVehicleDeath(vehicleid, killerid) { return 1; } public OnPlayerText(playerid, text[]) { return 1; } //////////////////////////////////////////////////////////////////////////////// public OnPlayerCommandText(playerid, cmdtext[]) { return SendClientMessage(playerid, -1, "IDRP - Command tidak ada, type {FF0000}/help {FFFFFF}untuk melihat command yang ada"); } //////////////////////////////////////////////////////////////////////////////// public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger) { return 1; } public OnPlayerExitVehicle(playerid, vehicleid) { return 1; } public OnPlayerStateChange(playerid, newstate, oldstate) { return 1; } public OnPlayerEnterCheckpoint(playerid) { return 1; } public OnPlayerLeaveCheckpoint(playerid) { return 1; } public OnPlayerEnterRaceCheckpoint(playerid) { return 1; } public OnPlayerLeaveRaceCheckpoint(playerid) { return 1; } public OnRconCommand(cmd[]) { return 1; } //////////////////////////////////////////////////////////////////////////////// public OnPlayerRequestSpawn(playerid) { TogglePlayerSpectating(playerid, true); return 1; } //////////////////////////////////////////////////////////////////////////////// public OnObjectMoved(objectid) { return 1; } public OnPlayerObjectMoved(playerid, objectid) { return 1; } public OnPlayerPickUpPickup(playerid, pickupid) { return 1; } public OnVehicleMod(playerid, vehicleid, componentid) { return 1; } public OnVehiclePaintjob(playerid, vehicleid, paintjobid) { return 1; } public OnVehicleRespray(playerid, vehicleid, color1, color2) { return 1; } public OnPlayerSelectedMenuRow(playerid, row) { return 1; } public OnPlayerExitedMenu(playerid) { return 1; } public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid) { return 1; } public OnPlayerKeyStateChange(playerid, newkeys, oldkeys) { return 1; } public OnRconLoginAttempt(ip[], password[], success) { return 1; } //////////////////////////////////////////////////////////////////////////////// public OnPlayerUpdate(playerid) { if(GetPlayerPing(playerid) > 500) { new st[128]; format(st,128,"%s telah dikick dari server karena ping terlalu besar (%d)", GetName(playerid), GetPlayerPing(playerid)); SendClientMessageToAll(0xAA3333AA,st); SetTimer("KickPlayer", 25, false); } return 1; } forward KickPlayer(playerid); public KickPlayer(playerid) { Kick(playerid); } //////////////////////////////////////////////////////////////////////////////// public OnPlayerStreamIn(playerid, forplayerid) { return 1; } public OnPlayerStreamOut(playerid, forplayerid) { return 1; } public OnVehicleStreamIn(vehicleid, forplayerid) { return 1; } public OnVehicleStreamOut(vehicleid, forplayerid) { return 1; } //////////////////////////////////////////////////////////////////////////////// public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) { TogglePlayerSpectating(playerid, true); if(dialogid == 15000) { ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_MSGBOX, "OPS...!","Kamu {FF0000}BELUM TERDAFTAR{FFFFFF}, silahkan daftar di {0000FF}idrp.co.id {FFFFFF}atau {0000FF}http://88.202.230.9!","Quit",""); Kick(playerid); } if(dialogid == 15500) { if(!response) { Kick(playerid); } if(response) { new query[200], pName[24], pPass[100]; // GetPlayerName(playerid, pName, 24); mysql_real_escape_string(inputtext, pPass); format(query, sizeof(query), "SELECT charname FROM playerchar WHERE charname = '%s' AND password = md5('%s')", pName, pPass); mysql_query(query); mysql_store_result(); new numrows = mysql_num_rows(); if(numrows == 1) MySQL_Login(playerid); if(!numrows) { ShowPlayerDialog(playerid, 15500, DIALOG_STYLE_PASSWORD, "Login","Your user is {FF0000}registered{FFFFFF}! Please {0000FF}login{FFFFFF} with your password below!\n{FF0000} The password you typed was incorrect!","Login","Cancel"); } mysql_free_result(); } } if(dialogid == 15550) { if(!response) { ShowPlayerDialog(playerid, 15550, DIALOG_STYLE_LIST, "Sialhkan pilih mau dimana kamu mendarat!", "Stasiun Unity\nStasiun Market\nBandara Los Santos", "Oke", "Cancel"); } if(response) { switch(listitem) { case 0: { TogglePlayerSpectating(playerid, false); SetSpawnInfo( playerid, 0, 5, 1743.0961,-1861.7201,13.5772,359.9062, 0, 0, 0, 0, 0, 0 ); // spawn unity SpawnPlayer(playerid); SetPlayerSkin(playerid, pskins[random(3)]); new st[128]; format(st,128,"%s telah terkoneksi ke server IDRP!",GetName(playerid)); SendClientMessageToAll(0x33CCFFAA,st); GameTextForPlayer(playerid,"~w~Selamat Datang ~n~di IDRP",5000,1); } case 1: { TogglePlayerSpectating(playerid, false); SetSpawnInfo( playerid, 0, 5, 822.3559,-1362.3359,-0.5078,310.9041, 0, 0, 0, 0, 0, 0 ); // spawn market SpawnPlayer(playerid); SetPlayerSkin(playerid, pskins[random(3)]); new st[128]; format(st,128,"%s telah terkoneksi ke server IDRP!",GetName(playerid)); SendClientMessageToAll(0x33CCFFAA,st); GameTextForPlayer(playerid,"~w~Selamat Datang ~n~di IDRP",5000,1); } case 2: { TogglePlayerSpectating(playerid, false); SetSpawnInfo( playerid, 0, 5, 1686.1090,-2241.6809,13.5469,180.3645, 0, 0, 0, 0, 0, 0 ); // spawn bandara SpawnPlayer(playerid); if(PlayerInfo[playerid][pGender] == 1)SetPlayerSkin(playerid, pskins[random(8)]); if(PlayerInfo[playerid][pGender] == 0)SetPlayerSkin(playerid, wskins[random(3)]); new st[128]; format(st,128,"%s telah terkoneksi ke server IDRP!",GetName(playerid)); SendClientMessageToAll(0x33CCFFAA,st); GameTextForPlayer(playerid,"~w~Selamat Datang ~n~di IDRP",5000,1); } } } } return 1; } public OnPlayerClickPlayer(playerid, clickedplayerid, source) { return 1; }