Guest User

Untitled

a guest
Mar 30th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.96 KB | None | 0 0
  1. /*
  2.  
  3. ===================================
  4. Advanced MySQL registration system.
  5. By Carlton.
  6. ===================================
  7.  
  8. */
  9.  
  10. #include <a_samp>
  11. #include <a_mysql>
  12. #include <zcmd>
  13.  
  14. #define BLUE 0x0000FFFF
  15.  
  16. #define SQL_HOST "127.0.0.1"
  17. #define SQL_USER "root"
  18. #define SQL_PASSWORD ""
  19. #define SQL_DB "samp"
  20. #define SQL_TABLE "samptable" // The table to store the users in.
  21.  
  22. #define SQL_CHECK_ACCOUNT 1
  23. #define SQL_CHECK_IP 2
  24. #define SQL_CHECK_LOGIN 3
  25. #define SQL_REGISTER_PLAYER 4
  26. #define SQL_SAVE_ACCOUNT 5
  27.  
  28. #define ERROR_MESSAGE1 "The autologin failed, because your IP address didn't match the name."
  29. #define ERROR_MESSAGE2 "The password you have given does not match."
  30. #define REGMSSG "You have registered a account."
  31. #define LOGINMSSG1 "You have logged in."
  32. #define LOGINMSSG2 "You have autologged in"
  33.  
  34. #define function%0(%1) stock%0(%1)
  35. //#define AutoLogin // - Remove the // if you want a autologin when a player spawns.
  36. //#define Force_Login // - Remove the // if you want to force a player to login and register when that player joins.
  37. // Do not use AutoLogin and Force_Login at the same time!
  38.  
  39. new RegistrationSystemConnection, stringsize[256], pname[MAX_PLAYER_NAME];
  40.  
  41. enum Accinfo {
  42. bool:Account,
  43. bool:Logged,
  44. pip[16],
  45. Float: pHealth,
  46. Float: pArmour,
  47. }
  48. new AccountData[MAX_PLAYERS][Accinfo];
  49.  
  50. enum PA {
  51. IP[30],
  52. Money,
  53. AdminLevel,
  54. Float: Health,
  55. Float: Armour,
  56. Bank
  57. }
  58. new PlayerAccount[MAX_PLAYERS][PA];
  59.  
  60. enum Esc
  61. {
  62. Escape[128]
  63. }
  64. new SQL_Escape[Esc];
  65.  
  66. function SavePlayerAccount(playerid) {
  67. GetPlayerName(playerid, pname, sizeof(pname));
  68. GetPlayerHealth(playerid, AccountData[playerid][pHealth]);
  69. GetPlayerArmour(playerid, AccountData[playerid][pArmour]);
  70. format(stringsize, sizeof(stringsize), "UPDATE "SQL_TABLE" SET Money = %d, AdminLevel = %d, Health = %f, Armour = %f, Bank = %d WHERE Name = '%s'",
  71. GetPlayerMoney(playerid), PlayerAccount[playerid][AdminLevel], AccountData[playerid][pHealth], AccountData[playerid][pArmour], PlayerAccount[playerid][Bank], pname);
  72. mysql_query(stringsize, SQL_SAVE_ACCOUNT, playerid, RegistrationSystemConnection);
  73. }
  74.  
  75. function OnPlayerLogin(playerid) {
  76. // In this function you can make a user forcespawn. This is called after someone logged in..
  77.  
  78. GivePlayerMoney(playerid, PlayerAccount[playerid][Money]);
  79. SetPlayerHealth(playerid, PlayerAccount[playerid][Health]);
  80. SetPlayerHealth(playerid, PlayerAccount[playerid][Armour]);
  81. AccountData[playerid][Logged] = true;
  82. }
  83.  
  84. function OnPlayerRegister(playerid) {
  85. // In this function you can make a user forcespawn. This is called after someone registered.
  86.  
  87. AccountData[playerid][Account] = true;
  88. #if defined Force_Login
  89. {
  90. ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Login","Enter your password below:","Login","Cancel"); }
  91. #endif
  92. }
  93.  
  94. function ConnectToDB() {
  95. mysql_debug(1);
  96. RegistrationSystemConnection = mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASSWORD);
  97. if(mysql_ping(RegistrationSystemConnection) == -1) {
  98. mysql_reconnect(RegistrationSystemConnection);
  99. }
  100. }
  101.  
  102. function LoginPlayer(playerid, reason) {
  103. GetPlayerName(playerid, pname, sizeof(pname));
  104. format(stringsize, sizeof(stringsize), "SELECT * FROM "SQL_TABLE"", pname);
  105. mysql_query(stringsize, -1, -1, RegistrationSystemConnection);
  106. mysql_store_result();
  107. new playerfilesplit[8][128], playerloadingarray[128];
  108. mysql_fetch_row_format(playerloadingarray,"|", RegistrationSystemConnection);
  109. split(playerloadingarray, playerfilesplit, '|');
  110. PlayerAccount[playerid][Money] = strval(playerfilesplit[3]);
  111. PlayerAccount[playerid][AdminLevel] = strval(playerfilesplit[4]);
  112. PlayerAccount[playerid][Health] = floatstr(playerfilesplit[5]);
  113. PlayerAccount[playerid][Armour] = floatstr(playerfilesplit[6]);
  114. PlayerAccount[playerid][Bank] = strval(playerfilesplit[7]);
  115. mysql_free_result(RegistrationSystemConnection);
  116. switch(reason) {
  117. case 1: {
  118. SendClientMessage(playerid, BLUE, LOGINMSSG1);
  119. }
  120. case 2: {
  121. SendClientMessage(playerid, BLUE, LOGINMSSG2);
  122. }
  123. }
  124. OnPlayerLogin(playerid);
  125. }
  126.  
  127. function RegisterPlayer(playerid, inputtedpassword[]) {
  128. GetPlayerName(playerid, pname, sizeof(pname));
  129. //GetPlayerIp(playerid, AccountData[playerid][pip], sizeof(AccountData[playerid][pip]) );
  130. new plrIP[20];
  131. GetPlayerIp(playerid, plrIP, sizeof(plrIP));
  132. GetPlayerHealth(playerid, AccountData[playerid][pHealth]);
  133. GetPlayerArmour(playerid, AccountData[playerid][pArmour]);
  134. format(stringsize, sizeof(stringsize), "INSERT INTO "SQL_TABLE" (Name, Password, IP, Money, AdminLevel, Health, Armour, Bank) VALUES('%s', md5('%s'), '%s', %d, 0, '%f', '%f', 0)", pname, inputtedpassword, plrIP,
  135. GetPlayerMoney(playerid), AccountData[playerid][pHealth], AccountData[playerid][pArmour]);
  136. mysql_query(stringsize, SQL_REGISTER_PLAYER, playerid, RegistrationSystemConnection);
  137. OnPlayerRegister(playerid);
  138. }
  139.  
  140. function split(const strsrc[], strdest[][], delimiter)
  141. {
  142. new i, li;
  143. new aNum;
  144. new len;
  145. while(i <= strlen(strsrc)){
  146. if(strsrc[i]==delimiter || i==strlen(strsrc)){
  147. len = strmid(strdest[aNum], strsrc, li, i, 128);
  148. strdest[aNum][len] = 0;
  149. li = i+1;
  150. aNum++;
  151. }
  152. i++;
  153. }
  154. return 1;
  155. }
  156.  
  157.  
  158. public OnFilterScriptInit() {
  159. ConnectToDB();
  160. }
  161.  
  162. public OnFilterScriptExit() {
  163. mysql_close(RegistrationSystemConnection);
  164. }
  165.  
  166. public OnPlayerConnect(playerid) {
  167. GetPlayerName(playerid, pname, sizeof(pname));
  168. format(stringsize, sizeof(stringsize), "SELECT * FROM "SQL_TABLE" WHERE Name = '%s'", pname);
  169. // mysql_query(stringsize, SQL_CHECK_ACCOUNT, playerid, RegistrationSystemConnection);
  170. mysql_query(stringsize, -1,-1, RegistrationSystemConnection);
  171. mysql_store_result(RegistrationSystemConnection);
  172. if(mysql_num_rows(RegistrationSystemConnection) > 0) {
  173. AccountData[playerid][Account] = true;
  174. }
  175. else AccountData[playerid][Account] = false;
  176. mysql_free_result(RegistrationSystemConnection);
  177.  
  178. #if defined AutoLogin
  179. {
  180. if(AccountData[playerid][Account] == true) {
  181. new plrIP[20];
  182. GetPlayerIp(playerid, plrIP, sizeof(plrIP));
  183. format(stringsize, sizeof(stringsize), "SELECT * FROM "SQL_TABLE" WHERE Name = '%s' AND IP = '%s'", pname, plrIP);
  184. mysql_query(stringsize, -1, -1, RegistrationSystemConnection);
  185. mysql_store_result(RegistrationSystemConnection);
  186. if(mysql_num_rows(RegistrationSystemConnection) > 0) {
  187. LoginPlayer(playerid, 2);
  188. }
  189. else return SendClientMessage(playerid, BLUE, ERROR_MESSAGE1);
  190. mysql_free_result(RegistrationSystemConnection);
  191. }
  192. }
  193. #endif
  194. #if defined Force_Login
  195. {
  196. if(AccountData[playerid][Account] == true) {
  197. ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Login","Enter your password below:","Login","Cancel");
  198. }
  199. else ShowPlayerDialog(playerid,2,DIALOG_STYLE_INPUT,"Register","Enter your password below:","Register","Cancel");
  200. }
  201. #endif
  202. return 1;
  203. }
  204.  
  205. public OnPlayerDisconnect(playerid) {
  206. SavePlayerAccount(playerid);
  207. }
  208.  
  209. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  210. {
  211. switch(dialogid) {
  212. case 1: {
  213. if(!strlen(inputtext)) return ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Login","Enter your password below:","Login","Cancel");
  214. if(!response) return ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Login","Enter your password below:","Login","Cancel");
  215. mysql_real_escape_string(inputtext, SQL_Escape[Escape], RegistrationSystemConnection);
  216. format(stringsize, sizeof(stringsize), "SELECT * FROM "SQL_TABLE" WHERE Name = '%s' AND Password = md5('%s') LIMIT 1", pname, SQL_Escape[Escape]);
  217. mysql_query(stringsize, -1, -1, RegistrationSystemConnection);
  218. mysql_store_result(RegistrationSystemConnection);
  219. if(mysql_num_rows(RegistrationSystemConnection) > 0) {
  220. LoginPlayer(playerid, 1);
  221. }
  222. else return SendClientMessage(playerid, BLUE, ERROR_MESSAGE2);
  223. }
  224. case 2: {
  225. if(!strlen(inputtext)) return ShowPlayerDialog(playerid,2,DIALOG_STYLE_INPUT,"Register","Enter your password below:","Register","Cancel");
  226. if(!response) return ShowPlayerDialog(playerid,2,DIALOG_STYLE_INPUT,"Register","Enter your password below:","Register","Cancel");
  227. mysql_real_escape_string(inputtext, SQL_Escape[Escape], RegistrationSystemConnection);
  228. RegisterPlayer(playerid, SQL_Escape[Escape]);
  229. }
  230. }
  231. return 0;
  232. }
  233.  
  234. public OnQueryFinish( query[], resultid, extraid, connectionHandle )
  235. {
  236. switch(resultid) {
  237. /* case SQL_CHECK_ACCOUNT: {
  238. mysql_store_result(RegistrationSystemConnection);
  239. if(mysql_num_rows(RegistrationSystemConnection) > 0) {
  240. AccountData[extraid][Account] = true;
  241. }
  242. else AccountData[extraid][Account] = false;
  243. mysql_free_result(RegistrationSystemConnection);
  244. }
  245. case SQL_CHECK_LOGIN: {
  246. mysql_store_result(RegistrationSystemConnection);
  247. if(mysql_num_rows(RegistrationSystemConnection) > 1) {
  248. LoginPlayer(extraid, 1);
  249. }
  250. else return SendClientMessage(extraid, BLUE, ERROR_MESSAGE2);
  251. mysql_free_result(RegistrationSystemConnection);
  252. }
  253. case SQL_CHECK_IP: {
  254. mysql_store_result(RegistrationSystemConnection);
  255. if(mysql_num_rows(RegistrationSystemConnection) > 0) {
  256. LoginPlayer(extraid, 2);
  257. }
  258. else return SendClientMessage(extraid, BLUE, ERROR_MESSAGE1);
  259. mysql_free_result(RegistrationSystemConnection);
  260. }*/
  261. case SQL_REGISTER_PLAYER: {
  262. SendClientMessage(extraid, BLUE, REGMSSG);
  263. }
  264. case SQL_SAVE_ACCOUNT: {
  265.  
  266. }
  267. }
  268. return 1;
  269. }
  270.  
  271. command(register, playerid, params[]) {
  272. if(AccountData[playerid][Account] == false && AccountData[playerid][Logged] == false) {
  273. ShowPlayerDialog(playerid,2,DIALOG_STYLE_INPUT,"Register","Enter your password below:","Register","Cancel");
  274. }
  275. return 1;
  276. }
  277.  
  278. command(login, playerid, params[]) {
  279. if(AccountData[playerid][Account] == true && AccountData[playerid][Logged] == false) {
  280. ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Login","Enter your password below:","Login","Cancel");
  281. }
  282. return 1;
  283. }
Advertisement
Add Comment
Please, Sign In to add comment