Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <a_samp>
- #include <a_mysql>
- #define MAX_SOLTS (25)
- #define D_LOGIN 1
- #define D_REGISTER 2
- new const
- mysql_host[] = "localhost",
- mysql_user[] = "root",
- mysql_database[] = "contas",
- mysql_password[] = "";
- new Query[500];
- new MysqlC;
- new PlayerName[25];
- enum pInfo{
- pLevel,
- pMoney,
- pBank,
- pSkin,
- pWarn
- };
- new PlayerInfo[MAX_SOLTS][pInfo];
- 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);
- forward PlayerSpawn(playerid);
- public OnGameModeInit()
- {
- print("\n--------------------------------------");
- print(" MySQL Tutorial @BlueX");
- print("--------------------------------------\n");
- MySQL_Start();
- return 1;
- }
- public OnGameModeExit()
- {
- return 1;
- }
- public OnPlayerConnect(playerid)
- {
- MySQL_CheckAccount(playerid);
- return 1;
- }
- public OnPlayerDisconnect(playerid, reason)
- {
- MySQL_SaveAccount(playerid);
- return 1;
- }
- public OnPlayerSpawn(playerid)
- {
- return 1;
- }
- public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
- {
- switch(dialogid){
- case D_LOGIN:{
- if(!response)
- Kick(playerid);
- else {
- if(strlen(inputtext) < 4)
- return ShowPlayerDialog(playerid,D_LOGIN,3,"login","insira a sua password para logar","logar","sair");
- else
- MySQL_CheckPassword(playerid,inputtext);
- }
- }
- case D_REGISTER:{
- if(!response)
- Kick(playerid);
- else {
- if( 5 < (strlen(inputtext)) > 25)
- return ShowPlayerDialog(playerid,D_REGISTER,3,"registro","insira uma password para registrar","registrar","sair");
- 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("Não foi possivel conectar a database, verifique as definições novamente.");
- else {
- mysql_function_query(MysqlC,
- "CREATE TABLE IF NOT EXISTS `users` (\
- `id` int(11) NOT NULL, AUTO_INCREMENT,\
- `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("Tabela %s verificada com sucesso.",text);
- public MySQL_CheckAccount(playerid){
- GetPlayerName(playerid,PlayerName,25);
- 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;
- cache_get_data(rows,fields,MysqlC);
- if(rows){
- ShowPlayerDialog(playerid,D_LOGIN,3,"login","insira a sua password para logar","logar","sair");
- } else {
- ShowPlayerDialog(playerid,D_REGISTER,3,"registro","insira uma password para registrar","registrar","sair");
- }
- return 1;
- }
- public MySQL_CheckPassword(playerid,password[]){
- GetPlayerName(playerid,PlayerName,25);
- 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,D_LOGIN,3,"login","insira a sua password para logar","logar","sair");
- return 1;
- }
- public MySQL_CreateAccount(playerid,password[]){
- GetPlayerName(playerid,PlayerName,25);
- format(Query,sizeof(Query),"INSERT INTO `users` (name,password,level,money,bank,skin) VALUES ('%s','%s','2','4500','0','240');",PlayerName,password);
- mysql_function_query(MysqlC,Query,true,"r@MySQL_CreateAccount","s",playerid);
- return 1;
- }
- public r@MySQL_CreateAccount(playerid){
- SendClientMessage(playerid,-1,"Registrado com sucesso, aguarde um pouco...");
- MySQL_LoadAccount(playerid);
- return 1;
- }
- public MySQL_LoadAccount(playerid){
- GetPlayerName(playerid,PlayerName,25);
- 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);
- 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);
- PlayerSpawn(playerid);
- }
- else
- SendClientMessage(playerid,-1,"Houve um erro com a sua conta"),Kick(playerid);
- return 1;
- }
- public PlayerSpawn(playerid){
- SpawnPlayer(playerid);
- GivePlayerMoney(playerid,PlayerInfo[playerid][pMoney]);
- SetPlayerScore(playerid,PlayerInfo[playerid][pLevel]);
- SetPlayerSkin(playerid,PlayerInfo[playerid][pSkin]);
- SendClientMessage(playerid,-1,"Seja bem vindo!");
- return 1;
- }
- public MySQL_SaveAccount(playerid){
- GetPlayerName(playerid,PlayerName,25);
- format(Query,sizeof(Query),"UPDATE `users` SET level='%d',money='%d',bank='%d',skin='%d' WHERE name='%s'",PlayerInfo[playerid][pLevel],PlayerInfo[playerid][pMoney],PlayerInfo[playerid][pBank],PlayerInfo[playerid][pSkin],PlayerName);
- mysql_function_query(MysqlC,Query,false,"r@MySQL_SaveAccount","d",playerid);
- return 1;
- }
- public r@MySQL_SaveAccount(playerid)
- return printf("Conta %d salva com sucesso.",playerid);
Advertisement
Add Comment
Please, Sign In to add comment