Advertisement
Guest User

Untitled

a guest
Oct 18th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3.  
  4. forward OnUserRegister(query[], index, extraid, connectionHandle);
  5. forward OnConnectResponse(query[], index, extraid, connectionHandle);
  6. forward OnUserLogin(query[], index, extraid, connectionHandle);
  7. forward OnUserUpdate(query[], index, extraid, connectionHandle);
  8.  
  9. //MySQL Configuration
  10. #define SQL_HOST "host"
  11. #define SQL_DB "database"
  12. #define SQL_USER "username"
  13. #define SQL_PASS "password"
  14.  
  15. #define TABLENAME "users"
  16.  
  17. #define GREY 0xAFAFAFAA
  18. #define RED 0xFF0000AA
  19. #define YELLOW 0xFFFF00AA
  20. #define LIGHTBLUE 0x33CCFFAA
  21.  
  22. #define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
  23.  
  24. //global variables
  25. new
  26. bool:LoggedIn[MAX_PLAYERS] = { false, ... },
  27. bool:AccRegistered[MAX_PLAYERS] = { false, ... },
  28. Wrongattempt[MAX_PLAYERS],
  29. TimerSet[MAX_PLAYERS],
  30. pLogtimer[MAX_PLAYERS],
  31. PlayerMoney[MAX_PLAYERS];
  32.  
  33. public OnFilterScriptInit()
  34. {
  35. mysql_debug(1);
  36. mysql_connect(SQL_HOST, SQL_USER,SQL_DB, SQL_PASS);
  37. SetupTable(); //run this only once
  38. }
  39.  
  40. SetupTable()
  41. {
  42. return mysql_query("CREATE TABLE IF NOT EXISTS `"TABLENAME"`(`id` int(11) NOT NULL auto_increment PRIMARY KEY,`Username` varchar(30) NOT NULL,`Password` varchar(50) NOT NULL,`Money` int(10) NOT NULL default '0')");
  43. }
  44.  
  45. public OnFilterScriptExit()
  46. {
  47. printf("OnFilterScriptExit()");
  48. mysql_close();
  49. }
  50.  
  51. RegisterAccount(playerid,pass[])
  52. {
  53. new
  54. pName[MAX_PLAYER_NAME],
  55. query[256];
  56.  
  57. GetPlayerName(playerid,pName,sizeof(pName));
  58. mysql_real_escape_string(pName,pName);
  59. mysql_real_escape_string(pass,pass);
  60. format(query,sizeof(query),"INSERT INTO `"TABLENAME"` (Username,Password) VALUES ('%s',md5('%s'))",pName,pass);
  61. mysql_query_callback(playerid,query,"OnUserRegister");
  62. return 1;
  63. }
  64.  
  65. public OnUserRegister(query[], index, extraid, connectionHandle)
  66. {
  67. new string[128],pName[MAX_PLAYER_NAME];
  68. if(IsPlayerConnected(index)) {
  69. GetPlayerName(index,pName,sizeof pName);
  70. format(string,sizeof(string),">> Account %s successfully registered - Remember your password for later use.",pName);
  71. SendClientMessage(index,GREY,string);
  72. SendClientMessage(index,GREY,"You have been automatically logged in");
  73. LoggedIn[index] = true;
  74. AccRegistered[index] = true;
  75. }
  76. return 1;
  77. }
  78.  
  79. LoginPlayer(playerid,pass[])
  80. {
  81. new
  82. pName[MAX_PLAYER_NAME],
  83. query[256];
  84.  
  85. GetPlayerName(playerid,pName,sizeof(pName));
  86.  
  87. mysql_real_escape_string(pName,pName);
  88. mysql_real_escape_string(pass,pass);
  89. format(query,sizeof(query),"SELECT Money FROM `"TABLENAME"` WHERE Username = '%s' AND Password = md5('%s') LIMIT 1",pName,pass);
  90. mysql_query_callback(playerid,query,"OnUserLogin");
  91. return 1;
  92. }
  93.  
  94. public OnUserLogin(query[], index, extraid, connectionHandle)
  95. {
  96. new string[128],pName[MAX_PLAYER_NAME];
  97. if(IsPlayerConnected(index)) {
  98. mysql_store_result();
  99. if(mysql_num_rows() == 1) {
  100. PlayerMoney[index] = mysql_fetch_int();
  101. GivePlayerMoney(index,PlayerMoney[index]);
  102. LoggedIn[index] = true;
  103. format(string,sizeof(string),">> You have been successfully logged in. (Money: %d)",PlayerMoney[index]);
  104. SendClientMessage(index,GREY,string);
  105. mysql_free_result();
  106. } else {
  107. Wrongattempt[index] += 1;
  108. printf("Bad log in attempt by %s (Total attempts: %d)",pName,Wrongattempt[index]);
  109. if(Wrongattempt[index] >= 3) {
  110. SendClientMessage(index,RED,">> You have been kicked.( 3 times wrong pass )");
  111. mysql_free_result();
  112. return Kick(index);
  113. }
  114. mysql_free_result();
  115. SendClientMessage(index,RED,">> Wrong Password");
  116. }
  117. } else {
  118. //to avoid "commands out of sync" errors
  119. mysql_store_result();
  120. mysql_free_result();
  121. }
  122. return 1;
  123. }
  124.  
  125.  
  126. public OnPlayerDisconnect(playerid,reason)
  127. {
  128. if(pLogtimer[playerid] != 0) KillTimer(pLogtimer[playerid]);
  129. new
  130. query[300],
  131. pName[MAX_PLAYER_NAME];
  132.  
  133. GetPlayerName(playerid,pName,sizeof(pName));
  134.  
  135. if(LoggedIn[playerid]) {
  136. new Float:arm;
  137. GetPlayerArmour(playerid,arm);
  138. format(query,sizeof(query),"UPDATE `"TABLENAME"` SET `Money`='%d' WHERE (`Username` = '%s')",GetPlayerMoney(playerid),pName);
  139. mysql_query_callback(playerid,query,"OnUserUpdate");
  140. }
  141. return 1;
  142. }
  143.  
  144. public OnUserUpdate(query[], index, extraid, connectionHandle)
  145. {
  146. printf("Data of playerid %d has been updated",index);
  147. return 1;
  148. }
  149.  
  150. public OnPlayerConnect(playerid)
  151. {
  152. new
  153. query[256],
  154. pname[MAX_PLAYER_NAME];
  155. Wrongattempt[playerid] = 0;
  156. LoggedIn[playerid] = false;
  157. TimerSet[playerid] = 0;
  158.  
  159. GetPlayerName(playerid,pname,sizeof(pname));
  160. format(query,sizeof(query),"SELECT * FROM `"TABLENAME"` WHERE Username = '%s'",pname);
  161. mysql_query_callback(playerid,query,"OnConnectResponse");
  162. return 1;
  163. }
  164.  
  165. public OnConnectResponse(query[], index, extraid, connectionHandle)
  166. {
  167. new string[128],pName[MAX_PLAYER_NAME];
  168. if(IsPlayerConnected(index)) {
  169. GetPlayerName(index,pName,sizeof pName);
  170. mysql_store_result();
  171. if(mysql_num_rows() > 0) {
  172. format(string,sizeof(string),">> This account (%s) is registered.Please login by using /login [pass]",pName);
  173. SendClientMessage(index,GREY,string);
  174. AccRegistered[index] = true;
  175. pLogtimer[index] = SetTimerEx("LoginKick",30000,0,"d",index);
  176. } else {
  177. format(string,sizeof(string),">> Welcome %s, you can register by using /register [pass]",pName);
  178. SendClientMessage(index,GREY,string);
  179. AccRegistered[index] = false;
  180. }
  181. mysql_free_result();
  182. } else {
  183. //to avoid "commands out of sync" errors
  184. mysql_store_result();
  185. mysql_free_result();
  186. }
  187. return 1;
  188. }
  189.  
  190. forward LoginKick(playerid);
  191. public LoginKick(playerid)
  192. {
  193. if(!LoggedIn[playerid]) KickEx(playerid,"Not logged in");
  194. else
  195. {
  196. KillTimer(pLogtimer[playerid]);
  197. pLogtimer[playerid] = 0;
  198. }
  199. return 1;
  200. }
  201.  
  202. public OnPlayerCommandText(playerid, cmdtext[])
  203. {
  204. dcmd(register,8,cmdtext);
  205. dcmd(login,5,cmdtext);
  206. return 0;
  207. }
  208.  
  209. dcmd_login(playerid, params[])
  210. {
  211. if(LoggedIn[playerid])
  212. {
  213. return SendClientMessage(playerid,RED,">> You are already logged in");
  214. }
  215. if(!AccRegistered[playerid])
  216. {
  217. return SendClientMessage(playerid,RED,">> This Account is not registered. ( Use /register [pass] )");
  218. }
  219. if(!strlen(params))
  220. {
  221. return SendClientMessage(playerid,RED,"SYNTAX: /login [password]");
  222. }
  223. LoginPlayer(playerid,params);
  224. return true;
  225. }
  226.  
  227. dcmd_register(playerid, params[])
  228. {
  229. new pName[MAX_PLAYER_NAME];
  230. GetPlayerName(playerid,pName,sizeof(pName));
  231. if(AccRegistered[playerid])
  232. {
  233. return SendClientMessage(playerid,RED,">> This account is already registered. ( /login [pass] )");
  234. }
  235. if(LoggedIn[playerid])
  236. {
  237. return SendClientMessage(playerid,RED,">> You are already logged in");
  238. }
  239. if(!strlen(params))
  240. {
  241. return SendClientMessage(playerid,RED,"SYNTAX: /register [password]");
  242. }
  243. if(strlen(params) < 6)
  244. {
  245. return SendClientMessage(playerid,RED,">> The password should contain 6 characters at least.");
  246. }
  247. RegisterAccount(playerid,params);
  248. return 1;
  249. }
  250.  
  251. stock KickEx(playerid,reason[])
  252. {
  253. new
  254. string[128],
  255. MsgAll[128],
  256. pName[MAX_PLAYER_NAME];
  257. GetPlayerName(playerid,pName,sizeof(pName));
  258. format(string,sizeof(string),"You have been kicked: ");
  259. strcat(string,reason,sizeof(string));
  260. SendClientMessage(playerid,RED,string);
  261. Kick(playerid);
  262. format(MsgAll,sizeof(MsgAll),">> %s has been kicked.(Reason: %s)",pName,reason);
  263. SendClientMessageToAll(GREY,MsgAll);
  264. return 1;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement