Guest User

SAMP REGISTER/LOGIN

a guest
Mar 13th, 2016
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.39 KB | None | 0 0
  1. //By UltraScripter////////////////////////////////////////////////////////
  2. //By UltraScripter////////////////////////////////////////////////////////
  3.  
  4. #include <a_samp>
  5. #include <YSI\y_ini>
  6.  
  7. #define REGISTER 1
  8. #define LOGIN 2
  9.  
  10. new bool:Active;
  11.  
  12. new Attempts[MAX_PLAYERS];
  13. new PlayerText:LOADSC;
  14. new PlayerText:CKTD[2];
  15.  
  16. enum PlayerInfo
  17. {
  18. Pass,
  19. Float:x,
  20. Float:y,
  21. Float:z,
  22. Float:a,
  23. Cash,
  24. Score,
  25. Kills,
  26. Deaths
  27. }
  28. new pInfo[MAX_PLAYERS][PlayerInfo];
  29.  
  30. forward PlayerData_Data(playerid, name[], value[]);
  31.  
  32. public PlayerData_Data(playerid, name[], value[])
  33. {
  34. INI_Int("Password", pInfo[playerid][Pass]);
  35. INI_Float("X", pInfo[playerid][x]);
  36. INI_Float("Y", pInfo[playerid][y]);
  37. INI_Float("Z", pInfo[playerid][z]);
  38. INI_Float("A", pInfo[playerid][a]);
  39. INI_Int("Cash", pInfo[playerid][Cash]);
  40. INI_Int("Score", pInfo[playerid][Score]);
  41. INI_Int("Kills", pInfo[playerid][Kills]);
  42. INI_Int("Deaths", pInfo[playerid][Deaths]);
  43. return 1;
  44. }
  45.  
  46. stock User(playerid)
  47. {
  48. new Str[128], pName[MAX_PLAYER_NAME];
  49. GetPlayerName(playerid, pName, sizeof(pName));
  50. format(Str, sizeof(Str), "Users/%s.ini", pName);
  51. return Str;
  52. }
  53.  
  54. public OnGameModeInit()
  55. {
  56. // Don't use these lines if it's a filterscript
  57. SetGameModeText("Blank Script");
  58. AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
  59. return 1;
  60. }
  61.  
  62. public OnGameModeExit()
  63. {
  64. return 1;
  65. }
  66.  
  67. public OnPlayerRequestClass(playerid, classid)
  68. {
  69. SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
  70. SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
  71. SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
  72. return 1;
  73. }
  74.  
  75. ShowRegistrationMenu(playerid, id)
  76. {
  77. if(id == 1)
  78. {
  79. LOADSC = CreatePlayerTextDraw(playerid, 0.0, 0.0, "loadsc8:loadsc8");
  80. PlayerTextDrawFont(playerid, LOADSC, 4);
  81. PlayerTextDrawAlignment(playerid, LOADSC, 2);
  82. PlayerTextDrawUseBox(playerid, LOADSC, 1);
  83. PlayerTextDrawTextSize(playerid, LOADSC, 650.0, 470.0);
  84. PlayerTextDrawShow(playerid, LOADSC);
  85. CKTD[0] = CreatePlayerTextDraw(playerid, 150.0, 240.0, "REGISTER");
  86. PlayerTextDrawFont(playerid, CKTD[0], 3);
  87. PlayerTextDrawSetOutline(playerid, CKTD[0], 1);
  88. PlayerTextDrawAlignment(playerid, CKTD[0], 2);
  89. PlayerTextDrawLetterSize(playerid, CKTD[0], 1.2, 1.2);
  90. PlayerTextDrawTextSize(playerid, CKTD[0], 17.0, 17.0);
  91. PlayerTextDrawColor(playerid, CKTD[0], 0x00C600FF);
  92. PlayerTextDrawSetSelectable(playerid, CKTD[0], 1);
  93. PlayerTextDrawShow(playerid, CKTD[0]);
  94. CKTD[1] = CreatePlayerTextDraw(playerid, 500.0, 240.0, "LOGIN");
  95. PlayerTextDrawFont(playerid, CKTD[1], 3);
  96. PlayerTextDrawSetOutline(playerid, CKTD[1], 1);
  97. PlayerTextDrawAlignment(playerid, CKTD[1], 2);
  98. PlayerTextDrawLetterSize(playerid, CKTD[1], 1.2, 1.2);
  99. PlayerTextDrawTextSize(playerid, CKTD[1], 17.0, 17.0);
  100. PlayerTextDrawColor(playerid, CKTD[1], 0xF20000FF);
  101. PlayerTextDrawSetSelectable(playerid, CKTD[1], 1);
  102. PlayerTextDrawShow(playerid, CKTD[1]);
  103. }
  104. if(id == 2)
  105. {
  106. LOADSC = CreatePlayerTextDraw(playerid, 0.0, 0.0, "loadsc8:loadsc8");
  107. PlayerTextDrawFont(playerid, LOADSC, 4);
  108. PlayerTextDrawAlignment(playerid, LOADSC, 2);
  109. PlayerTextDrawUseBox(playerid, LOADSC, 1);
  110. PlayerTextDrawTextSize(playerid, LOADSC, 650.0, 470.0);
  111. PlayerTextDrawShow(playerid, LOADSC);
  112. CKTD[0] = CreatePlayerTextDraw(playerid, 150.0, 240.0, "REGISTER");
  113. PlayerTextDrawFont(playerid, CKTD[0], 3);
  114. PlayerTextDrawSetOutline(playerid, CKTD[0], 1);
  115. PlayerTextDrawAlignment(playerid, CKTD[0], 2);
  116. PlayerTextDrawLetterSize(playerid, CKTD[0], 1.2, 1.2);
  117. PlayerTextDrawTextSize(playerid, CKTD[0], 17.0, 17.0);
  118. PlayerTextDrawColor(playerid, CKTD[0], 0xF20000FF);
  119. PlayerTextDrawSetSelectable(playerid, CKTD[0], 1);
  120. PlayerTextDrawShow(playerid, CKTD[0]);
  121. CKTD[1] = CreatePlayerTextDraw(playerid, 500.0, 240.0, "LOGIN");
  122. PlayerTextDrawFont(playerid, CKTD[1], 3);
  123. PlayerTextDrawSetOutline(playerid, CKTD[1], 1);
  124. PlayerTextDrawAlignment(playerid, CKTD[1], 2);
  125. PlayerTextDrawLetterSize(playerid, CKTD[1], 1.2, 1.2);
  126. PlayerTextDrawTextSize(playerid, CKTD[1], 17.0, 17.0);
  127. PlayerTextDrawColor(playerid, CKTD[1], 0x00C600FF);
  128. PlayerTextDrawSetSelectable(playerid, CKTD[1], 1);
  129. PlayerTextDrawShow(playerid, CKTD[1]);
  130. }
  131. return 1;
  132. }
  133.  
  134. DestroyRegistrationMenu(playerid)
  135. {
  136. PlayerTextDrawDestroy(playerid, LOADSC);
  137. PlayerTextDrawDestroy(playerid, CKTD[0]);
  138. PlayerTextDrawDestroy(playerid, CKTD[1]);
  139. return 1;
  140. }
  141.  
  142. public OnPlayerConnect(playerid)
  143. {
  144. Attempts[playerid] = 0;
  145. if(!fexist(User(playerid)))
  146. {
  147. ShowRegistrationMenu(playerid, 1);
  148. SelectTextDraw(playerid, 0x9DEF89FF);
  149. Active = true;
  150. }
  151. else
  152. {
  153. ShowRegistrationMenu(playerid, 2);
  154. SelectTextDraw(playerid, 0x9DEF89FF);
  155. Active = true;
  156. }
  157. return 1;
  158. }
  159.  
  160. public OnPlayerDisconnect(playerid, reason)
  161. {
  162. if(fexist(User(playerid)))
  163. {
  164. new INI:File = INI_Open(User(playerid));
  165. new Float:px, Float:py, Float:pz, Float:pa;
  166. GetPlayerPos(playerid, px, py, pz);
  167. GetPlayerFacingAngle(playerid, pa);
  168. INI_WriteFloat(File, "X", px);
  169. INI_WriteFloat(File, "Y", py);
  170. INI_WriteFloat(File, "Z", pz);
  171. INI_WriteFloat(File, "A", pa);
  172. INI_WriteInt(File, "Cash", GetPlayerMoney(playerid));
  173. INI_WriteInt(File, "Score", GetPlayerScore(playerid));
  174. INI_WriteInt(File, "Kills", pInfo[playerid][Kills]);
  175. INI_WriteInt(File, "Deaths", pInfo[playerid][Deaths]);
  176. INI_Close(File);
  177. }
  178. return 1;
  179. }
  180.  
  181. public OnPlayerSpawn(playerid)
  182. {
  183. return 1;
  184. }
  185.  
  186. public OnPlayerDeath(playerid, killerid, reason)
  187. {
  188. pInfo[killerid][Kills] ++;
  189. pInfo[playerid][Deaths] ++;
  190. return 1;
  191. }
  192.  
  193. public OnVehicleSpawn(vehicleid)
  194. {
  195. return 1;
  196. }
  197.  
  198. public OnVehicleDeath(vehicleid, killerid)
  199. {
  200. return 1;
  201. }
  202.  
  203. public OnPlayerText(playerid, text[])
  204. {
  205. return 1;
  206. }
  207.  
  208. public OnPlayerCommandText(playerid, cmdtext[])
  209. {
  210. if (strcmp("/changepassword", cmdtext, true, 10) == 0)
  211. {
  212. if(fexist(User(playerid)))
  213. {
  214. ShowPlayerDialog(playerid, 7, DIALOG_STYLE_PASSWORD, "{00FF00}Change password", "{FFFFFF}First login to change password.", "Login", "Cancel");
  215. }
  216. return 1;
  217. }
  218. return 0;
  219. }
  220.  
  221. public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
  222. {
  223. return 1;
  224. }
  225.  
  226. public OnPlayerExitVehicle(playerid, vehicleid)
  227. {
  228. return 1;
  229. }
  230.  
  231. public OnPlayerStateChange(playerid, newstate, oldstate)
  232. {
  233. return 1;
  234. }
  235.  
  236. public OnPlayerEnterCheckpoint(playerid)
  237. {
  238. return 1;
  239. }
  240.  
  241. public OnPlayerLeaveCheckpoint(playerid)
  242. {
  243. return 1;
  244. }
  245.  
  246. public OnPlayerEnterRaceCheckpoint(playerid)
  247. {
  248. return 1;
  249. }
  250.  
  251. public OnPlayerLeaveRaceCheckpoint(playerid)
  252. {
  253. return 1;
  254. }
  255.  
  256. public OnRconCommand(cmd[])
  257. {
  258. return 1;
  259. }
  260.  
  261. public OnPlayerRequestSpawn(playerid)
  262. {
  263. return 1;
  264. }
  265.  
  266. public OnObjectMoved(objectid)
  267. {
  268. return 1;
  269. }
  270.  
  271. public OnPlayerObjectMoved(playerid, objectid)
  272. {
  273. return 1;
  274. }
  275.  
  276. public OnPlayerPickUpPickup(playerid, pickupid)
  277. {
  278. return 1;
  279. }
  280.  
  281. public OnVehicleMod(playerid, vehicleid, componentid)
  282. {
  283. return 1;
  284. }
  285.  
  286. public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
  287. {
  288. return 1;
  289. }
  290.  
  291. public OnVehicleRespray(playerid, vehicleid, color1, color2)
  292. {
  293. return 1;
  294. }
  295.  
  296. public OnPlayerSelectedMenuRow(playerid, row)
  297. {
  298. return 1;
  299. }
  300.  
  301. public OnPlayerExitedMenu(playerid)
  302. {
  303. return 1;
  304. }
  305.  
  306. public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
  307. {
  308. return 1;
  309. }
  310.  
  311. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  312. {
  313. return 1;
  314. }
  315.  
  316. public OnRconLoginAttempt(ip[], password[], success)
  317. {
  318. return 1;
  319. }
  320.  
  321. public OnPlayerUpdate(playerid)
  322. {
  323. return 1;
  324. }
  325.  
  326. public OnPlayerStreamIn(playerid, forplayerid)
  327. {
  328. return 1;
  329. }
  330.  
  331. public OnPlayerStreamOut(playerid, forplayerid)
  332. {
  333. return 1;
  334. }
  335.  
  336. public OnVehicleStreamIn(vehicleid, forplayerid)
  337. {
  338. return 1;
  339. }
  340.  
  341. public OnVehicleStreamOut(vehicleid, forplayerid)
  342. {
  343. return 1;
  344. }
  345.  
  346. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  347. {
  348. if(dialogid == REGISTER)
  349. {
  350. if(!strlen(inputtext)) return ShowPlayerDialog(playerid, REGISTER, DIALOG_STYLE_PASSWORD, "{FFFF00}Register", "{FF0000}You have to register.", "Register", "Quit");
  351. if(!response) return Kick(playerid);
  352. if(response)
  353. {
  354. new INI:File = INI_Open(User(playerid));
  355. INI_WriteInt(File, "Password", udb_hash(inputtext));
  356. INI_Close(File);
  357. PlayerPlaySound(playerid, 1057, 0.0, 0.0, 0.0);
  358. SendClientMessage(playerid, 0x00FF00FF, "You registered succesfully.");
  359. new String[256], Name[MAX_PLAYER_NAME], dd, mm, yy, h, m;
  360. GetPlayerName(playerid, Name, sizeof(Name));
  361. getdate(yy, mm, dd);
  362. gettime(h, m);
  363. format(String, sizeof(String), "{FFFFFF}Name: {FFFF00}%s\n{FFFFFF}Date: {FFFF00}%02d/%02d/%02d\n{FFFFFF}Time: {00FF00}%02d:%02d\n{FFFFFF}Password: {FFFF00}%s\n{00773C}Press f8 to screenshot your password.", Name, dd, mm, yy, h, m, inputtext);
  364. ShowPlayerDialog(playerid, 11, DIALOG_STYLE_MSGBOX, "{00FF00}Registration info", String, "Ok", "");
  365. DestroyRegistrationMenu(playerid);
  366. }
  367. }
  368. if(dialogid == LOGIN)
  369. {
  370. if(!response) return Kick(playerid);
  371. if(!strlen(inputtext)) return ShowPlayerDialog(playerid, LOGIN, DIALOG_STYLE_PASSWORD, "{FF0000}Login", "{FF0000}You have to login.", "Login", "Quit");
  372. if(response)
  373. {
  374. INI_ParseFile(User(playerid), "PlayerData_%s", .bExtra = true, .extra = playerid);
  375. if(udb_hash(inputtext) == pInfo[playerid][Pass])
  376. {
  377. SpawnPlayer(playerid);
  378. SetPlayerPos(playerid, pInfo[playerid][x], pInfo[playerid][y], pInfo[playerid][z]);
  379. SetPlayerFacingAngle(playerid, pInfo[playerid][a]);
  380. GivePlayerMoney(playerid, pInfo[playerid][Cash]);
  381. SetPlayerScore(playerid, pInfo[playerid][Score]);
  382. pInfo[playerid][Kills] = pInfo[playerid][Kills];
  383. pInfo[playerid][Deaths] = pInfo[playerid][Deaths];
  384. PlayerPlaySound(playerid, 1057, 0.0, 0.0, 0.0);
  385. SendClientMessage(playerid, 0x00FF00FF, "Logged in succesfully.");
  386. DestroyRegistrationMenu(playerid);
  387. }
  388. else
  389. {
  390. if(Attempts[playerid] <= 2)
  391. {
  392. PlayerPlaySound(playerid, 1058, 0.0, 0.0, 0.0);
  393. Attempts[playerid] ++;
  394. new String[128];
  395. format(String, sizeof(String), "{FF0000}Wrong password {FFFF00}%d/3 {FFFFFF}attempts, left try again.", Attempts[playerid]);
  396. ShowPlayerDialog(playerid, LOGIN, DIALOG_STYLE_PASSWORD, "{FF0000}Login", String, "Login", "Quit");
  397. }
  398. if(Attempts[playerid] == 3)
  399. {
  400. PlayerPlaySound(playerid, 1058, 0.0, 0.0, 0.0);
  401. ShowPlayerDialog(playerid, 12, DIALOG_STYLE_MSGBOX, "{FF0000}Login fail", "{FF0000}You have been kicked because you failed to login 3/3 attempts.", "Ok", "");
  402. SetTimerEx("KickForBadLogin", 500, false, "i", playerid);
  403. DestroyRegistrationMenu(playerid);
  404. }
  405. }
  406. }
  407. }
  408. if(dialogid == 7)
  409. {
  410. if(!response) return SendClientMessage(playerid, 0xFF0000FF, "You canceled login before password change.");
  411. if(!strlen(inputtext)) return ShowPlayerDialog(playerid, 7, DIALOG_STYLE_PASSWORD, "{00FF00}Change password", "{FFFFFF}First login to change password.", "Login", "Cancel");
  412. if(response)
  413. {
  414. INI_ParseFile(User(playerid), "PlayerData_%s", .bExtra = true, .extra = playerid);
  415. if(udb_hash(inputtext) == pInfo[playerid][Pass])
  416. {
  417. PlayerPlaySound(playerid, 1057, 0.0, 0.0, 0.0);
  418. SendClientMessage(playerid, 0x00FF00FF, "Password correct you can change your password now.");
  419. ShowPlayerDialog(playerid, 8, DIALOG_STYLE_PASSWORD, "{00FF00}Change password", "{00FF00}Change your password to whatever you want.", "Done", "Cancel");
  420. }
  421. else
  422. {
  423. PlayerPlaySound(playerid, 1057, 0.0, 0.0, 0.0);
  424. SendClientMessage(playerid, 0xFF0000FF, "Wrong password.");
  425. }
  426. }
  427. }
  428. if(dialogid == 8)
  429. {
  430. if(!response) return SendClientMessage(playerid, 0xFF0000FF, "You canceled password change.");
  431. if(!strlen(inputtext)) return ShowPlayerDialog(playerid, 8, DIALOG_STYLE_PASSWORD, "{00FF00}Change password", "{00FF00}Change your password to whatever you want.", "Done", "Cancel");
  432. if(response)
  433. {
  434. new INI:File = INI_Open(User(playerid));
  435. INI_WriteInt(File, "Password", udb_hash(inputtext));
  436. INI_Close(File);
  437. PlayerPlaySound(playerid, 1057, 0.0, 0.0, 0.0);
  438. SendClientMessage(playerid, 0x00FF00FF, "Password changed succesfully.");
  439. new String[128];
  440. format(String, sizeof(String), "{FFFFFF}Password: {00FF00}%s\n{00773C}Press f8 to screenshot your new password.", inputtext);
  441. ShowPlayerDialog(playerid, 17, DIALOG_STYLE_MSGBOX, "{00FF00}New password info", String, "Ok", "");
  442. }
  443. }
  444. return 1;
  445. }
  446.  
  447. forward KickForBadLogin(playerid);
  448.  
  449. public KickForBadLogin(playerid)
  450. {
  451. Kick(playerid);
  452. return 1;
  453. }
  454.  
  455. public OnPlayerClickPlayer(playerid, clickedplayerid, source)
  456. {
  457. return 1;
  458. }
  459.  
  460. public OnPlayerClickTextDraw(playerid, Text:clickedid)
  461. {
  462. if(clickedid == Text:INVALID_TEXT_DRAW)
  463. {
  464. if(Active == false) return 0;
  465. if(Active == true)
  466. {
  467. DestroyRegistrationMenu(playerid);
  468. CancelSelectTextDraw(playerid);
  469. ShowPlayerDialog(playerid, 10, DIALOG_STYLE_MSGBOX, "{FF0000}Kicked", "{FFFFFF}You have been {FF0000}kicked {FFFFFF}for not logging or registering you can join later to register or login.", "Ok", "");
  470. SetTimerEx("KickForEsc", 500, false, "i", playerid);
  471. }
  472. }
  473. return 1;
  474. }
  475.  
  476. public OnPlayerClickPlayerTextDraw(playerid, PlayerText:playertextid)
  477. {
  478. if(playertextid != PlayerText:INVALID_TEXT_DRAW)
  479. {
  480. if(playertextid == CKTD[0])
  481. {
  482. if(fexist(User(playerid)))
  483. {
  484. PlayerPlaySound(playerid, 1058, 0.0, 0.0, 0.0);
  485. SendClientMessage(playerid, 0xFF0000FF, "You allready registered.");
  486. }
  487. else
  488. {
  489. PlayerPlaySound(playerid, 1057, 0.0, 0.0, 0.0);
  490. new String[256], Name[MAX_PLAYER_NAME], dd, mm, yy, h, m;
  491. GetPlayerName(playerid, Name, sizeof(Name));
  492. getdate(yy, mm, dd);
  493. gettime(h, m);
  494. format(String, sizeof(String), "{FFFFFF}Name: {FFFF00}%s\n{FFFFFF}Date: {FFFF00}%02d/%02d/%02d\n{FFFFFF}Time: {00FF00}%02d:%02d", Name, dd, mm, yy, h, m);
  495. CancelSelectTextDraw(playerid);
  496. Active = false;
  497. ShowPlayerDialog(playerid, REGISTER, DIALOG_STYLE_PASSWORD, "{FFFF00}Register", String, "Register", "Quit");
  498. }
  499. }
  500. if(playertextid == CKTD[1])
  501. {
  502. if(!fexist(User(playerid)))
  503. {
  504. PlayerPlaySound(playerid, 1058, 0.0, 0.0, 0.0);
  505. SendClientMessage(playerid, 0xFF0000FF, "You are not registered to this server.");
  506. }
  507. else
  508. {
  509. PlayerPlaySound(playerid, 1057, 0.0, 0.0, 0.0);
  510. CancelSelectTextDraw(playerid);
  511. Active = false;
  512. new Name[MAX_PLAYER_NAME], String[256];
  513. GetPlayerName(playerid, Name, sizeof(Name));
  514. format(String, sizeof(String), "{FFFFFF}Welcome Back {00FF00}%s {FFFFFF}please login.", Name);
  515. ShowPlayerDialog(playerid, LOGIN, DIALOG_STYLE_PASSWORD, "{FFFF00}Login", String, "Login", "Quit");
  516. }
  517. }
  518. }
  519. return 1;
  520. }
  521.  
  522. forward KickForEsc(playerid);
  523.  
  524. public KickForEsc(playerid)
  525. {
  526. Kick(playerid);
  527. return 1;
  528. }
  529.  
  530.  
  531. //credits to DracoBlue for his hasher
  532. stock udb_hash(buf[]) {
  533. new length=strlen(buf);
  534. new s1 = 1;
  535. new s2 = 0;
  536. new n;
  537. for (n=0; n<length; n++)
  538. {
  539. s1 = (s1 + buf[n]) % 65521;
  540. s2 = (s2 + s1) % 65521;
  541. }
  542. return (s2 << 16) + s1;
  543. }
  544.  
  545. //By UltraScripter////////////////////////////////////////////////////////
  546. //By UltraScripter////////////////////////////////////////////////////////
Add Comment
Please, Sign In to add comment