Guest User

Gerenciador de Contas

a guest
Sep 11th, 2015
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3.  
  4. #define DIALOG_LOGIN 1
  5. #define DIALOG_REGISTRO 2
  6.  
  7. #define MYSQL_USER "root"
  8. #define MYSQL_HOST "127.0.0.1"
  9. #define MYSQL_DB ""
  10. #define MYSQL_SENHA ""
  11.  
  12. new mysql,
  13. ErrouASenha[MAX_PLAYERS]
  14. ;
  15.  
  16. enum Player {
  17. Senha,
  18. Admin,
  19. bool:Logado
  20. }
  21. new PlayerInfo[MAX_PLAYERS][Player];
  22.  
  23. // Tabela a ser criada: contas
  24. // Valores: Nome,Senha,Admin
  25.  
  26. //------------------------------------------------------------------------------
  27.  
  28. public OnGameModeInit() {
  29. mysql = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_DB,MYSQL_SENHA);
  30. return 1;
  31. }
  32.  
  33. //------------------------------------------------------------------------------
  34.  
  35. public OnPlayerConnect(playerid) {
  36. if(PlayerInfo[playerid][Logado] == false) {
  37. new lol[69];
  38. format(lol, sizeof(lol),"SELECT * FROM contas WHERE Nome = '%s'",nome(playerid));
  39.  
  40. mysql_query(mysql, lol);
  41. mysql_store_result();
  42.  
  43. if(mysql_num_rows() != 0) {
  44. ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_PASSWORD,"Conectando","Essa conta já é registrada.\n\nDigite a senha para entrar","Entrar","Sair");
  45. }
  46. else {
  47. ShowPlayerDialog(playerid,DIALOG_REGISTRO,DIALOG_STYLE_PASSWORD,"Registrando","Essa conta ainda não é registrada.\n\nDigite uma senha para se registrar","Registrar","Sair");
  48. }
  49. mysql_free_result();
  50. }
  51. else {
  52. PlayerInfo[playerid][Logado] = true;
  53. }
  54. return 1;
  55. }
  56.  
  57. public OnPlayerDisconnect(playerid, reason) {
  58. if(PlayerInfo[playerid][Logado] == true) {
  59. SalvarConta(playerid);
  60. }
  61. return 1;
  62. }
  63.  
  64. //------------------------------------------------------------------------------
  65.  
  66. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
  67. if(dialogid == DIALOG_REGISTRO) {
  68. if(!response) {
  69. SendClientMessage(playerid,-1,"Você foi kickado por cancelar o registro.");
  70. Kick(playerid);
  71. }
  72. else {
  73. new str[256];
  74. format(str, sizeof(str),"INSERT INTO contas (Nome,Senha,Admin) VALUES ('%s',sha1('%s'),'0')",nome(playerid),inputtext,PlayerInfo[playerid][Admin]);
  75.  
  76. mysql_query(mysql, str);
  77. mysql_store_result();
  78.  
  79. PlayerInfo[playerid][Logado] = true;
  80. }
  81. }
  82. if(dialogid == DIALOG_LOGIN) {
  83. if(response) {
  84. new str[256];
  85. format(str, sizeof(str),"SELECT * FROM contas WHERE Nome = '%s' AND Senha = sha1('%s')",nome(playerid),inputtext);
  86.  
  87. mysql_query(mysql, str);
  88. mysql_store_result();
  89.  
  90. if(mysql_num_rows() == 0) {
  91. ErrouASenha[playerid] ++;
  92.  
  93. if(ErrouASenha[playerid] == 5) {
  94. SendClientMessage(playerid,-1,"você foi kickado por errar a senha 5 vezes.");
  95. Kick(playerid);
  96. return 0;
  97. }
  98. return ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_PASSWORD,"Conectando","Ops, Senha Incorreta!\n\nDigite a senha certa para entrar:\n\n","Entrar","Sair");
  99. }
  100. else if(mysql_num_rows() > 0) {
  101. CarregarConta(playerid);
  102. PlayerInfo[playerid][Logado] = true;
  103. }
  104. mysql_free_result();
  105. }
  106. }
  107. return 1;
  108. }
  109.  
  110. //------------------------------------------------------------------------------
  111.  
  112. SalvarConta(playerid) {
  113. new arquivo[256];
  114. format(arquivo,sizeof(arquivo),"UPDATE contas SET Admin = %d HERE Nome = '%s'",
  115.  
  116. PlayerInfo[playerid][Admin],
  117. nome(playerid));
  118.  
  119. mysql_query(mysql, arquivo);
  120. return 1;
  121. }
  122.  
  123. CarregarConta(playerid) {
  124. new arquivo[256],savingstring[256];
  125. format(arquivo,sizeof(arquivo),"SELECT * FROM contas WHERE Nome = '%s'",nome(playerid));
  126.  
  127. mysql_query(mysql, arquivo);
  128. mysql_store_result();
  129.  
  130. while(mysql_fetch_row_format(arquivo,"|")) {
  131. mysql_fetch_field_row(savingstring, "Nome"); nome(playerid);
  132. mysql_fetch_field_row(savingstring, "Admin"); PlayerInfo[playerid][Admin] = strval(savingstring);
  133. }
  134. mysql_free_result();
  135. return 1;
  136. }
  137.  
  138. nome(playerid) {
  139. new Name[MAX_PLAYERS];
  140. GetPlayerName(playerid,Name,sizeof(Name));
  141. return Name;
  142. }
  143. //------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment