18.04.2014, 23:33
Hey dude. See a example of a script that i did!
is a script made by me now test dude. (not is tested)
Код:
#include <a_samp> #include <a_mysql> #define MAX_SLOTS (25) #define DIALOG_LOGIN (1000) #define DIALOG_REGISTER (2000) // forwards forward MySQL_Start(); forward r@MySQL_Start(text[]); forward MySQL_CheckAccount(playerid); forward r@MySQL_CheckAccount(playerid); forward MySQL_CheckPassword(playerid,password[]); forward r@MySQL_CheckPassword(playerid); forward MySQL_CreateAccount(playerid,password[]); forward r@MySQL_CreateAccount(playerid); forward MySQL_LoadAccount(playerid); forward r@MySQL_LoadAccount(playerid); forward MySQL_SaveAccount(playerid); forward r@MySQL_SaveAccount(playerid); new const mysql_host[] = "localhost", mysql_user[] = "root", mysql_database[] = "server", mysql_password[] = "1234" ; enum pInfo { pIP[16], pLevel, pMoney, pBank, pSkin }; new Query[500], MysqlC, PlayerName[MAX_PLAYER_NAME], PlayerInfo[MAX_SLOTS][pInfo], playerIP[MAX_PLAYERS][16], pLogged[MAX_PLAYERS] ; public OnFilterScriptInit() // or OnGameModeInit() { print("--------------------------------------"); print(" MySQL Tutorial by HotStyle[]! "); print("--------------------------------------"); MySQL_Start(); return 1; } public OnPlayerConnect(playerid) { MySQL_CheckAccount(playerid); return 1; } public OnPlayerDisconnect(playerid, reason) { MySQL_SaveAccount(playerid); return 1; } public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) { switch(dialogid){ case DIALOG_LOGIN:{ if(!response) Kick(playerid); else { if(strlen(inputtext) < 4) return ShowPlayerDialog(playerid,DIALOG_LOGIN,3,"Login","Enter your password to login","Login","Cancel"); else MySQL_CheckPassword(playerid,inputtext); } } case DIALOG_REGISTER:{ if(!response) Kick(playerid); else { if( 5 < (strlen(inputtext)) > 25) return ShowPlayerDialog(playerid,DIALOG_REGISTER,3,"Register","Enter a password to register","Register","Cancel"); else MySQL_CreateAccount(playerid,inputtext); } } } return 1; } public MySQL_Start(){ MysqlC = mysql_connect(mysql_host,mysql_user,mysql_database,mysql_password); if(!MysqlC) return print("Unable to connect to database, check the settings again."); else { mysql_function_query(MysqlC, "CREATE TABLE IF NOT EXISTS `users` (\ `id` int(11) NOT NULL, AUTO_INCREMENT,\ `ip` varchar(25) NOT NULL, \ `name` varchar(25) NOT NULL, \ `password` varchar(25) NOT NULL, \ `level` int(11) NOT NULL, \ `bank` int(11) NOT NULL, \ `skin` int(11) NOT NULL, \ `money` int(11) NOT NULL, \ PRIMARY KEY(`id`)\ )",false,"r@MySQL_Start","s","users"); } return 1; } public r@MySQL_Start(text[]) return printf("database %s successfully verified.",text); public MySQL_CheckAccount(playerid){ GetPlayerName(playerid,PlayerName,MAX_PLAYER_NAME); format(Query,sizeof(Query),"SELECT * FROM `users` WHERE name='%s'",PlayerName); mysql_function_query(MysqlC,Query,true,"r@MySQL_CheckAccount","d",playerid); return 1; } public r@MySQL_CheckAccount(playerid){ new rows,fields,tmpIP[16]; cache_get_data(rows,fields,MysqlC); pLogged[playerid] = 0; GetPlayerIp(playerid,tmpIP,sizeof(tmpIP)); if(rows) { cache_get_field_content(0,"ip",Query,MysqlC); PlayerInfo[playerid][pIP] = strval(Query); if(strcmp(tmpIP,PlayerInfo[playerid][pIP],true) == 0) { SendClientMessage(playerid,-1,"You've been automatically logged."); MySQL_LoadAccount(playerid); pLogged[playerid] = 1; } ShowPlayerDialog(playerid,DIALOG_LOGIN,3,"Login","Enter your password to login","Login","Cancel"); } else { ShowPlayerDialog(playerid,DIALOG_REGISTER,3,"Register","Enter a password to register","Register","Cancel"); } return 1; } public MySQL_CheckPassword(playerid,password[]){ GetPlayerName(playerid,PlayerName,MAX_PLAYER_NAME); format(Query,sizeof(Query),"SELECT * FROM `users` WHERE name='%s' AND password='%s'",PlayerName,password); mysql_function_query(MysqlC,Query,true,"r@MySQL_CheckPassword","ds",playerid); return 1; } public r@MySQL_CheckPassword(playerid){ new rows,fields; cache_get_data(rows,fields,MysqlC); if(rows) MySQL_LoadAccount(playerid); else ShowPlayerDialog(playerid,DIALOG_LOGIN,3,"Login","Enter your password to login","Login","Cancel"); return 1; } public MySQL_CreateAccount(playerid,password[]){ GetPlayerName(playerid,PlayerName,MAX_PLAYER_NAME); GetPlayerIp(playerid,playerIP[playerid],16); format(Query,sizeof(Query),"INSERT INTO `users` (ip,name,password,level,money,bank,skin) VALUES ('%s','%s','%s','0','1000','0','240');",playerIP[playerid],PlayerName,password); mysql_function_query(MysqlC,Query,true,"r@MySQL_CreateAccount","s",playerid); return 1; } public r@MySQL_CreateAccount(playerid){ SendClientMessage(playerid,-1,"Successfully registered, wait a bit to load their account..."); MySQL_LoadAccount(playerid); return 1; } public MySQL_LoadAccount(playerid){ GetPlayerName(playerid,PlayerName,MAX_PLAYER_NAME); format(Query,sizeof(Query),"SELECT * FROM `users` WHERE name='%s'",PlayerName); mysql_function_query(MysqlC,Query,true,"r@MySQL_LoadAccount","d",playerid); return 1; } public r@MySQL_LoadAccount(playerid){ new rows,fields; cache_get_data(rows,fields,MysqlC); if(rows){ cache_get_field_content(0,"level",Query,MysqlC); PlayerInfo[playerid][pLevel] = strval(Query); cache_get_field_content(0,"money",Query,MysqlC); PlayerInfo[playerid][pMoney] = strval(Query); GivePlayerMoney(playerid,PlayerInfo[playerid][pMoney]); cache_get_field_content(0,"bank",Query,MysqlC); PlayerInfo[playerid][pBank] = strval(Query); cache_get_field_content(0,"skin",Query,MysqlC); PlayerInfo[playerid][pSkin] = strval(Query); SetPlayerSkin(playerid,PlayerInfo[playerid][pSkin]); GivePlayerMoney(playerid,PlayerInfo[playerid][pMoney]); } else SendClientMessage(playerid,-1,"There was an error with your account, please re-login!"),Kick(playerid); return 1; } public MySQL_SaveAccount(playerid){ GetPlayerName(playerid,PlayerName,MAX_PLAYER_NAME); GetPlayerIp(playerid,playerIP[playerid],16); format(Query,sizeof(Query),"UPDATE `users` SET level='%d',money='%d',bank='%d',skin='%d' WHERE name='%s', ip='%s'",PlayerInfo[playerid][pLevel],PlayerInfo[playerid][pMoney],PlayerInfo[playerid][pBank],PlayerInfo[playerid][pSkin],PlayerName, playerIP[playerid]); mysql_function_query(MysqlC,Query,false,"r@MySQL_SaveAccount","d",playerid); return 1; } public r@MySQL_SaveAccount(playerid) return printf("Account %d successfully saved.",playerid);