Guest User

Untitled

a guest
Sep 20th, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3.  
  4. #define MAX_SOLTS (25)
  5. #define D_LOGIN 1
  6. #define D_REGISTER 2
  7.  
  8. new const
  9. mysql_host[] = "localhost",
  10. mysql_user[] = "root",
  11. mysql_database[] = "contas",
  12. mysql_password[] = "";
  13.  
  14. new Query[500];
  15. new MysqlC;
  16.  
  17. new PlayerName[25];
  18.  
  19. enum pInfo{
  20. pLevel,
  21. pMoney,
  22. pBank,
  23. pSkin,
  24. pWarn
  25. };
  26.  
  27. new PlayerInfo[MAX_SOLTS][pInfo];
  28.  
  29. forward MySQL_Start();
  30. forward r@MySQL_Start(text[]);
  31.  
  32. forward MySQL_CheckAccount(playerid);
  33. forward r@MySQL_CheckAccount(playerid);
  34.  
  35. forward MySQL_CheckPassword(playerid,password[]);
  36. forward r@MySQL_CheckPassword(playerid);
  37.  
  38. forward MySQL_CreateAccount(playerid,password[]);
  39. forward r@MySQL_CreateAccount(playerid);
  40.  
  41. forward MySQL_LoadAccount(playerid);
  42. forward r@MySQL_LoadAccount(playerid);
  43.  
  44. forward MySQL_SaveAccount(playerid);
  45. forward r@MySQL_SaveAccount(playerid);
  46.  
  47. forward PlayerSpawn(playerid);
  48.  
  49. public OnGameModeInit()
  50. {
  51. print("\n--------------------------------------");
  52. print(" MySQL Tutorial @BlueX");
  53. print("--------------------------------------\n");
  54. MySQL_Start();
  55. return 1;
  56. }
  57.  
  58. public OnGameModeExit()
  59. {
  60. return 1;
  61. }
  62.  
  63. public OnPlayerConnect(playerid)
  64. {
  65. MySQL_CheckAccount(playerid);
  66. return 1;
  67. }
  68.  
  69. public OnPlayerDisconnect(playerid, reason)
  70. {
  71. MySQL_SaveAccount(playerid);
  72. return 1;
  73. }
  74.  
  75. public OnPlayerSpawn(playerid)
  76. {
  77. return 1;
  78. }
  79.  
  80. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  81. {
  82. switch(dialogid){
  83. case D_LOGIN:{
  84. if(!response)
  85. Kick(playerid);
  86. else {
  87. if(strlen(inputtext) < 4)
  88. return ShowPlayerDialog(playerid,D_LOGIN,3,"login","insira a sua password para logar","logar","sair");
  89. else
  90. MySQL_CheckPassword(playerid,inputtext);
  91. }
  92. }
  93. case D_REGISTER:{
  94. if(!response)
  95. Kick(playerid);
  96. else {
  97. if( 5 < (strlen(inputtext)) > 25)
  98. return ShowPlayerDialog(playerid,D_REGISTER,3,"registro","insira uma password para registrar","registrar","sair");
  99. else
  100. MySQL_CreateAccount(playerid,inputtext);
  101. }
  102. }
  103. }
  104. return 1;
  105. }
  106.  
  107. public MySQL_Start(){
  108. MysqlC = mysql_connect(mysql_host,mysql_user,mysql_database,mysql_password);
  109. if(!MysqlC)
  110. return print("Não foi possivel conectar a database, verifique as definições novamente.");
  111. else {
  112. mysql_function_query(MysqlC,
  113. "CREATE TABLE IF NOT EXISTS `users` (\
  114. `id` int(11) NOT NULL, AUTO_INCREMENT,\
  115. `name` varchar(25) NOT NULL, \
  116. `password` varchar(25) NOT NULL, \
  117. `level` int(11) NOT NULL, \
  118. `bank` int(11) NOT NULL, \
  119. `skin` int(11) NOT NULL, \
  120. `money` int(11) NOT NULL, \
  121. PRIMARY KEY(`id`)\
  122. )",false,"r@MySQL_Start","s","users");
  123. }
  124. return 1;
  125. }
  126.  
  127. public r@MySQL_Start(text[])
  128. return printf("Tabela %s verificada com sucesso.",text);
  129.  
  130. public MySQL_CheckAccount(playerid){
  131. GetPlayerName(playerid,PlayerName,25);
  132. format(Query,sizeof(Query),"SELECT * FROM `users` WHERE name='%s'",PlayerName);
  133. mysql_function_query(MysqlC,Query,true,"r@MySQL_CheckAccount","d",playerid);
  134. return 1;
  135. }
  136.  
  137. public r@MySQL_CheckAccount(playerid){
  138. new rows,fields;
  139. cache_get_data(rows,fields,MysqlC);
  140. if(rows){
  141. ShowPlayerDialog(playerid,D_LOGIN,3,"login","insira a sua password para logar","logar","sair");
  142. } else {
  143. ShowPlayerDialog(playerid,D_REGISTER,3,"registro","insira uma password para registrar","registrar","sair");
  144. }
  145. return 1;
  146. }
  147.  
  148. public MySQL_CheckPassword(playerid,password[]){
  149. GetPlayerName(playerid,PlayerName,25);
  150. format(Query,sizeof(Query),"SELECT * FROM `users` WHERE name='%s' AND password='%s'",PlayerName,password);
  151. mysql_function_query(MysqlC,Query,true,"r@MySQL_CheckPassword","ds",playerid);
  152. return 1;
  153. }
  154.  
  155. public r@MySQL_CheckPassword(playerid){
  156. new rows,fields;
  157. cache_get_data(rows,fields,MysqlC);
  158. if(rows)
  159. MySQL_LoadAccount(playerid);
  160. else
  161. ShowPlayerDialog(playerid,D_LOGIN,3,"login","insira a sua password para logar","logar","sair");
  162. return 1;
  163. }
  164.  
  165. public MySQL_CreateAccount(playerid,password[]){
  166. GetPlayerName(playerid,PlayerName,25);
  167. format(Query,sizeof(Query),"INSERT INTO `users` (name,password,level,money,bank,skin) VALUES ('%s','%s','2','4500','0','240');",PlayerName,password);
  168. mysql_function_query(MysqlC,Query,true,"r@MySQL_CreateAccount","s",playerid);
  169. return 1;
  170. }
  171.  
  172. public r@MySQL_CreateAccount(playerid){
  173. SendClientMessage(playerid,-1,"Registrado com sucesso, aguarde um pouco...");
  174. MySQL_LoadAccount(playerid);
  175. return 1;
  176. }
  177.  
  178. public MySQL_LoadAccount(playerid){
  179. GetPlayerName(playerid,PlayerName,25);
  180. format(Query,sizeof(Query),"SELECT * FROM `users` WHERE name='%s'",PlayerName);
  181. mysql_function_query(MysqlC,Query,true,"r@MySQL_LoadAccount","d",playerid);
  182. return 1;
  183. }
  184.  
  185. public r@MySQL_LoadAccount(playerid){
  186. new rows,fields;
  187. cache_get_data(rows,fields,MysqlC);
  188. if(rows){
  189. cache_get_field_content(0,"level",Query,MysqlC);
  190. PlayerInfo[playerid][pLevel] = strval(Query);
  191.  
  192. cache_get_field_content(0,"money",Query,MysqlC);
  193. PlayerInfo[playerid][pMoney] = strval(Query);
  194.  
  195. cache_get_field_content(0,"bank",Query,MysqlC);
  196. PlayerInfo[playerid][pBank] = strval(Query);
  197.  
  198. cache_get_field_content(0,"skin",Query,MysqlC);
  199. PlayerInfo[playerid][pSkin] = strval(Query);
  200.  
  201. PlayerSpawn(playerid);
  202. }
  203. else
  204. SendClientMessage(playerid,-1,"Houve um erro com a sua conta"),Kick(playerid);
  205. return 1;
  206. }
  207.  
  208. public PlayerSpawn(playerid){
  209. SpawnPlayer(playerid);
  210.  
  211. GivePlayerMoney(playerid,PlayerInfo[playerid][pMoney]);
  212. SetPlayerScore(playerid,PlayerInfo[playerid][pLevel]);
  213. SetPlayerSkin(playerid,PlayerInfo[playerid][pSkin]);
  214.  
  215. SendClientMessage(playerid,-1,"Seja bem vindo!");
  216. return 1;
  217. }
  218.  
  219. public MySQL_SaveAccount(playerid){
  220. GetPlayerName(playerid,PlayerName,25);
  221. 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);
  222. mysql_function_query(MysqlC,Query,false,"r@MySQL_SaveAccount","d",playerid);
  223. return 1;
  224. }
  225.  
  226. public r@MySQL_SaveAccount(playerid)
  227. return printf("Conta %d salva com sucesso.",playerid);
Advertisement
Add Comment
Please, Sign In to add comment