Guest User

MafiaGuy's Registration System [SAMP]

a guest
Apr 10th, 2010
2,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.29 KB | None | 0 0
  1. //---------------------------------------------------------------------------------------------//
  2. // -----------------------------[ MafiaGuy's Simple Registration ]-----------------------------//
  3. // You are free to edit, copy/paste without credits but it would be nice to leave some //
  4. // The system saves: Password,Level,Money,Skin,Admin and IP //
  5. //---------------------------------------------------------------------------------------------//
  6.  
  7. #include <a_samp>
  8. #include <dini>
  9.  
  10. //#define DINI
  11.  
  12.  
  13. #define COLOR_GREEN 0x33AA33AA
  14. #define COLOR_RED 0xAA3333AA
  15. #define COLOR_YELLOW 0xFFFF00AA
  16. #define COLOR_WHITE 0xFFFFFFAA
  17.  
  18. #define SERVERNAME "SAMP RP"
  19.  
  20. #define DINI_REGISTER 1
  21. #define DINI_LOGIN 2
  22.  
  23. #define NORMAL_REGISTER 3
  24. #define NORMAL_LOGIN 4
  25.  
  26. forward Encrypt(string[]);
  27. forward SaveAccounts();
  28. forward ini_GetKey( line[] );
  29. forward ini_GetValue( line[] );
  30.  
  31. new pLogged[MAX_PLAYERS];
  32.  
  33. enum pInfo
  34. {
  35. pPassword[64],
  36. pIP[32],
  37. pRegistered,
  38. pAdmin,
  39. pLevel,
  40. pMoney,
  41. pSkin,
  42. };
  43. new PlayerInfo[MAX_PLAYERS][pInfo];
  44.  
  45. public OnFilterScriptInit()
  46. {
  47. print("\n--------------------------------------");
  48. print(" MafiaGuy's Simple Registration Loaded !!");
  49. print("--------------------------------------\n");
  50.  
  51. SetTimer("SaveAccounts",1,1800000); // 30 mins
  52. return 1;
  53. }
  54.  
  55. public OnFilterScriptExit()
  56. {
  57. return 1;
  58. }
  59.  
  60. stock PlayerName(playerid)
  61. {
  62. new name[24];
  63. GetPlayerName(playerid, name, 24);
  64. return name;
  65. }
  66. stock SetPlayerMoney(playerid, money)
  67. {
  68. ResetPlayerMoney(playerid);
  69. GivePlayerMoney(playerid, money);
  70. return 1;
  71. }
  72.  
  73. stock ShowRegistration(playerid)
  74. {
  75. new file[128];
  76. format(file, sizeof(file), "%s.ini", PlayerName(playerid));
  77. #if defined DINI
  78. if(dini_Exists(file))
  79. {
  80. ShowPlayerDialog(playerid, DINI_LOGIN, DIALOG_STYLE_INPUT, "Dini Login Procces","Please enter your password to procced","Login","Quit");
  81. return 1;
  82. }
  83. else if(!dini_Exists(file))
  84. {
  85. ShowPlayerDialog(playerid, DINI_REGISTER, DIALOG_STYLE_INPUT, "Dini Register Procces","Please enter your password to procced","Register","Quit");
  86. }
  87. #else
  88. if(fexist(file))
  89. {
  90. ShowPlayerDialog(playerid, NORMAL_LOGIN, DIALOG_STYLE_INPUT, "Normal Login Procces","Please enter your password to procced","Login","Quit");
  91. }
  92. else if(!fexist(file))
  93. {
  94. ShowPlayerDialog(playerid, NORMAL_REGISTER, DIALOG_STYLE_INPUT, "Normal Register Procces","Please enter your password to procced","Register","Quit");
  95. }
  96. #endif
  97. return 1;
  98. }
  99. stock udb_hash(buf[])
  100. {
  101. new length=strlen(buf);
  102. new s1 = 1;
  103. new s2 = 0;
  104. new n;
  105. for (n=0; n<length; n++)
  106. {
  107. s1 = (s1 + buf[n]) % 65521;
  108. s2 = (s2 + s1) % 65521;
  109. }
  110. return (s2 << 16) + s1;
  111. }
  112.  
  113. public OnPlayerRequestClass(playerid, classid)
  114. {
  115. if(pLogged[playerid] == 1)
  116. {
  117. SpawnPlayer(playerid);
  118. }
  119. return 1;
  120. }
  121.  
  122. public OnPlayerConnect(playerid)
  123. {
  124. new string[128];
  125. format(string,sizeof(string),"* Hello %s. Welcome on %s.",PlayerName(playerid),SERVERNAME);
  126. SendClientMessage(playerid, COLOR_YELLOW, string);
  127. ShowRegistration(playerid);
  128. return 1;
  129. }
  130.  
  131. public OnPlayerDisconnect(playerid, reason)
  132. {
  133. SendClientMessage(playerid, COLOR_WHITE, "See ya. Come again soon.");
  134. return 1;
  135. }
  136.  
  137. public OnPlayerSpawn(playerid)
  138. {
  139. SetPlayerSkin(playerid,PlayerInfo[playerid][pSkin]);
  140. SetPlayerScore(playerid,PlayerInfo[playerid][pLevel]);
  141. SetPlayerMoney(playerid,PlayerInfo[playerid][pMoney]);
  142. // Now if you want to make the player spawn on certain location use SetPlayerPos, or do it thought a public function //
  143. return 1;
  144. }
  145.  
  146. public OnPlayerDeath(playerid, killerid, reason)
  147. {
  148. return 1;
  149. }
  150.  
  151. public OnVehicleSpawn(vehicleid)
  152. {
  153. return 1;
  154. }
  155.  
  156. public OnVehicleDeath(vehicleid, killerid)
  157. {
  158. return 1;
  159. }
  160.  
  161. public OnPlayerText(playerid, text[])
  162. {
  163. return 1;
  164. }
  165.  
  166. public OnPlayerCommandText(playerid, cmdtext[])
  167. {
  168. new string[128],cmd[256],idx,tmp[128],player;
  169. cmd = strtok(cmd,idx);
  170.  
  171. if (strcmp(cmd, "/kick", true) == 0)
  172. {
  173. if(PlayerInfo[playerid][pAdmin] > 0 || IsPlayerAdmin(playerid))
  174. {
  175. tmp = strtok(cmdtext, idx);
  176. if(!strlen(tmp))
  177. {
  178. SendClientMessage(playerid, COLOR_WHITE, "/kick [ID]");
  179. }
  180. player = strval(tmp);
  181. format(string,sizeof(string),"Admin %s has kicked %s from the server.",PlayerName(playerid),PlayerName(player));
  182. SendClientMessageToAll(COLOR_RED,string);
  183. Kick(player);
  184. }
  185. else
  186. {
  187. SendClientMessage(playerid,COLOR_RED,"You are not allowed to use that command.");
  188. }
  189. return 1;
  190. }
  191. return 0;
  192. }
  193.  
  194. public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
  195. {
  196. return 1;
  197. }
  198.  
  199. public OnPlayerExitVehicle(playerid, vehicleid)
  200. {
  201. return 1;
  202. }
  203.  
  204. public OnPlayerStateChange(playerid, newstate, oldstate)
  205. {
  206. return 1;
  207. }
  208.  
  209. public OnPlayerEnterCheckpoint(playerid)
  210. {
  211. return 1;
  212. }
  213.  
  214. public OnPlayerLeaveCheckpoint(playerid)
  215. {
  216. return 1;
  217. }
  218.  
  219. public OnPlayerEnterRaceCheckpoint(playerid)
  220. {
  221. return 1;
  222. }
  223.  
  224. public OnPlayerLeaveRaceCheckpoint(playerid)
  225. {
  226. return 1;
  227. }
  228.  
  229. public OnRconCommand(cmd[])
  230. {
  231. return 1;
  232. }
  233.  
  234. public OnPlayerRequestSpawn(playerid)
  235. {
  236. return 1;
  237. }
  238.  
  239. public OnObjectMoved(objectid)
  240. {
  241. return 1;
  242. }
  243.  
  244. public OnPlayerObjectMoved(playerid, objectid)
  245. {
  246. return 1;
  247. }
  248.  
  249. public OnPlayerPickUpPickup(playerid, pickupid)
  250. {
  251. return 1;
  252. }
  253.  
  254. public OnVehicleMod(playerid, vehicleid, componentid)
  255. {
  256. return 1;
  257. }
  258.  
  259. public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
  260. {
  261. return 1;
  262. }
  263.  
  264. public OnVehicleRespray(playerid, vehicleid, color1, color2)
  265. {
  266. return 1;
  267. }
  268.  
  269. public OnPlayerSelectedMenuRow(playerid, row)
  270. {
  271. return 1;
  272. }
  273.  
  274. public OnPlayerExitedMenu(playerid)
  275. {
  276. return 1;
  277. }
  278.  
  279. public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
  280. {
  281. return 1;
  282. }
  283.  
  284. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  285. {
  286. return 1;
  287. }
  288.  
  289. public OnRconLoginAttempt(ip[], password[], success)
  290. {
  291. return 1;
  292. }
  293.  
  294. public OnPlayerUpdate(playerid)
  295. {
  296. new file[64];
  297. format(file, sizeof(file), "%s.ini", PlayerName(playerid)); // Player Folder
  298. #if defined DINI
  299. dini_IntSet(file,"Registered",PlayerInfo[playerid][pRegistered]);
  300. dini_IntSet(file,"Admin",PlayerInfo[playerid][pAdmin]);
  301. dini_IntSet(file,"Level",GetPlayerScore(playerid));
  302. dini_IntSet(file,"Skin",GetPlayerSkin(playerid));
  303. dini_IntSet(file,"Money", GetPlayerMoney(playerid));
  304. // dini_Set(file,"Name", PlayerName(playerid)); // This is a example of setting a string. But be sure to put pName[MAX_PLAYER_NAME], at the player enum at top.
  305. #else
  306. new File: hFile = fopen(file, io_write);
  307. if (hFile)
  308. {
  309. new key[32];
  310. format(key, 32, "Password=%s\n", PlayerInfo[playerid][pPassword]);fwrite(hFile, key);
  311. format(key, 32, "Registered=%d\n",PlayerInfo[playerid][pRegistered]);fwrite(hFile, key);
  312. format(key, 32, "Admin=%d\n",PlayerInfo[playerid][pAdmin]);fwrite(hFile, key);
  313. format(key, 32, "Level=%d\n",GetPlayerScore(playerid));fwrite(hFile, key);
  314. format(key, 32, "Skin=%d\n",GetPlayerSkin(playerid));fwrite(hFile, key);
  315. format(key, 32, "Money=%d\n",GetPlayerMoney(playerid));fwrite(hFile, key);
  316. fclose(hFile);
  317. }
  318. #endif
  319. return 1;
  320. }
  321. public SaveAccounts()
  322. {
  323. for(new i = 0; i < MAX_PLAYERS; i++)
  324. {
  325. new file[64];
  326. format(file, sizeof(file), "%s.ini", PlayerName(i));
  327. #if defined DINI
  328. dini_IntSet(file,"Admin",PlayerInfo[i][pAdmin]);
  329. dini_IntSet(file,"Level",GetPlayerScore(i));
  330. dini_IntSet(file,"Skin",GetPlayerSkin(i));
  331. dini_IntSet(file,"Money", GetPlayerMoney(i));
  332. #else
  333. new File: hFile = fopen(file, io_write);
  334. if (hFile)
  335. {
  336. new key[32];
  337. format(key, 32, "Password=%s\n", PlayerInfo[i][pPassword]);fwrite(hFile, key);
  338. format(key, 32, "Registered=%d\n",PlayerInfo[i][pRegistered]);fwrite(hFile, key);
  339. format(key, 32, "Admin=%d\n",PlayerInfo[i][pAdmin]);fwrite(hFile, key);
  340. format(key, 32, "Level=%d\n",GetPlayerScore(i));fwrite(hFile, key);
  341. format(key, 32, "Skin=%d\n",GetPlayerSkin(i));fwrite(hFile, key);
  342. format(key, 32, "Money=%d\n",GetPlayerMoney(i));fwrite(hFile, key);
  343. fclose(hFile);
  344. }
  345. #endif
  346. }
  347. }
  348. public OnPlayerStreamIn(playerid, forplayerid)
  349. {
  350. return 1;
  351. }
  352.  
  353. public OnPlayerStreamOut(playerid, forplayerid)
  354. {
  355. return 1;
  356. }
  357.  
  358. public OnVehicleStreamIn(vehicleid, forplayerid)
  359. {
  360. return 1;
  361. }
  362.  
  363. public OnVehicleStreamOut(vehicleid, forplayerid)
  364. {
  365. return 1;
  366. }
  367.  
  368. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  369. {
  370. new string[128];
  371. new file[64];
  372. new ip[15];
  373. GetPlayerIp(playerid,ip,sizeof(ip));
  374. format(file, sizeof(file), "%s.ini", PlayerName(playerid)); // Player Folder
  375. if(response)
  376. {
  377. switch(dialogid)
  378. {
  379. #if defined DINI
  380. case DINI_REGISTER:
  381. {
  382. dini_Create(file); // The password is hashed. If you dont want the hash just remove teh udb_hash() //
  383. dini_IntSet(file, "Password", udb_hash(inputtext));
  384. dini_Set(file,"IP",ip);
  385. dini_IntSet(file,"Registered",1);
  386. dini_IntSet(file,"Admin",0);
  387. dini_IntSet(file,"Level",1); // You can always changed these values
  388. dini_IntSet(file,"Skin",7); // Set the default skin.
  389. dini_IntSet(file,"Money",5000); // Money that the receives at start
  390. format(string,sizeof(string),"Your account has been successfully made with password: %s (Auto Logged-in)",inputtext);
  391. SendClientMessage(playerid,COLOR_YELLOW,string);
  392. PlayerPlaySound(playerid,1057,0.0,0.0,0.0);
  393. pLogged[playerid] = 1;
  394. SpawnPlayer(playerid);
  395. return 1;
  396. }
  397. case DINI_LOGIN:
  398. {
  399. new pw[64];
  400. format(pw, 24, dini_Get(file, "Password"));
  401. if(udb_hash(inputtext) == strval(pw))
  402. {
  403. PlayerInfo[playerid][pRegistered] = dini_Int(file,"Registered");
  404. PlayerInfo[playerid][pAdmin] = dini_Int(file,"Admin"); // dini_Int is used to load normal things with numbers.
  405. // format(PlayerInfo[playerid][pName],24,"%s",PlayerName(playerid)); This way you can set some string to variable. This wasnt the best example but it is good.
  406. SetPlayerScore(playerid, dini_Int(file, "Level"));
  407. SetPlayerSkin(playerid, dini_Int(file, "Skin"));
  408. GivePlayerMoney(playerid, dini_Int(file, "Money"));
  409. format(string,sizeof(string),"You have logged into your account successfully.",inputtext);
  410. SendClientMessage(playerid,COLOR_YELLOW,string);
  411. PlayerPlaySound(playerid,1057,0.0,0.0,0.0);
  412. pLogged[playerid] = 1;
  413. SpawnPlayer(playerid);
  414. }
  415. else
  416. {
  417. SendClientMessage(playerid, COLOR_RED, "Wrong password.");
  418. ShowPlayerDialog(playerid, DINI_LOGIN, DIALOG_STYLE_INPUT, "DINI Login Procces","Please re-enter your password to procced.","Login","Quit");
  419. }
  420. return 1;
  421. }
  422. #else
  423. case NORMAL_REGISTER:
  424. {
  425. format(string,sizeof(string),"Your account has been successfully made with password: %s",inputtext);
  426. SendClientMessage(playerid,COLOR_YELLOW,string);
  427. Encrypt(inputtext);
  428. new File: hFile = fopen(file, io_write);
  429. if (hFile)
  430. {
  431. new key[32];
  432. format(key, 32, "Password=%s\n", inputtext);fwrite(hFile, key);
  433. format(key, 32, "Registered=%d\n",PlayerInfo[playerid][pRegistered]);fwrite(hFile, key);
  434. format(key, 32, "Admin=%d\n",PlayerInfo[playerid][pAdmin]);fwrite(hFile, key);
  435. format(key, 32, "Level=%d\n",GetPlayerScore(playerid));fwrite(hFile, key);
  436. format(key, 32, "Skin=%d\n",GetPlayerSkin(playerid));fwrite(hFile, key);
  437. format(key, 32, "Money=%d\n",GetPlayerMoney(playerid));fwrite(hFile, key);
  438. fclose(hFile);
  439. }
  440. format(string,sizeof(string),"Your account has been successfully made with password: %s (Auto Logged-in)",inputtext);
  441. SendClientMessage(playerid,COLOR_YELLOW,string);
  442. pLogged[playerid] = 1;
  443. SpawnPlayer(playerid);
  444. }
  445. case NORMAL_LOGIN:
  446. {
  447. new File:hFile = fopen(file, io_read);
  448. if(hFile)
  449. {
  450. new PassData[128];
  451. new keytmp[256], valtmp[256];
  452. fread(hFile, PassData, sizeof(PassData));
  453. keytmp = ini_GetKey(PassData);
  454. if(strcmp(keytmp ,"Password", true) == 0)
  455. {
  456. valtmp = ini_GetValue( PassData );
  457. strmid(PlayerInfo[playerid][pPassword], valtmp, 0, strlen(valtmp)-1, 255);
  458. }
  459. if(strcmp(PlayerInfo[playerid][pPassword],inputtext, true ) == 0 )
  460. {
  461. new key[256], val[256];
  462. new Data[256];
  463. while(fread(hFile, Data, sizeof(Data)))
  464. {
  465. key = ini_GetKey( Data );
  466. if( strcmp( key , "Registered" , true ) == 0 ) { val = ini_GetValue( Data ); PlayerInfo[playerid][pRegistered] = strval( val ); }
  467. if( strcmp( key , "Admin" , true ) == 0 ) { val = ini_GetValue( Data ); PlayerInfo[playerid][pAdmin] = strval( val ); }
  468. if( strcmp( key , "Level" , true ) == 0 ) { val = ini_GetValue( Data ); PlayerInfo[playerid][pLevel] = strval( val ); }
  469. if( strcmp( key , "Skin" , true ) == 0 ) { val = ini_GetValue( Data ); PlayerInfo[playerid][pSkin] = strval( val ); }
  470. if( strcmp( key , "Money" , true ) == 0 ) { val = ini_GetValue( Data ); PlayerInfo[playerid][pMoney] = strval( val ); }
  471. // if( strcmp( key , "StringExample" , true ) == 0 ) { val = ini_GetValue( Data ); strmid(PlayerInfo[playerid][pStringExample], val, 0, strlen(val)-1, 255); }
  472. }
  473. fclose(hFile);
  474. pLogged[playerid] = 1;
  475. SendClientMessage(playerid,COLOR_YELLOW,"You have logged into your account successfully.");
  476. SpawnPlayer(playerid);
  477. }
  478. else
  479. {
  480. SendClientMessage(playerid, COLOR_WHITE, "SERVER: Password does not match your name.");
  481. fclose(hFile);
  482. return 1;
  483. }
  484. if(PlayerInfo[playerid][pRegistered] == 0)
  485. {
  486. GivePlayerMoney(playerid, 5000);
  487. SetPlayerScore(playerid, 1);
  488. PlayerInfo[playerid][pSkin] = 7;
  489. PlayerInfo[playerid][pRegistered] = 1;
  490. }
  491. }
  492. }
  493. #endif
  494. }
  495. }
  496. return 1;
  497. }
  498. public OnPlayerClickPlayer(playerid, clickedplayerid, source)
  499. {
  500. return 1;
  501. }
  502. public Encrypt(string[])
  503. {
  504. for(new x=0; x < strlen(string); x++)
  505. {
  506. string[x] += (3^x) * (x % 15);
  507. if(string[x] > (0xff))
  508. {
  509. string[x] -= 256;
  510. }
  511. }
  512. return 1;
  513. }
  514. stock ini_GetKey( line[] )
  515. {
  516. new keyRes[256];
  517. keyRes[0] = 0;
  518. if ( strfind( line , "=" , true ) == -1 ) return keyRes;
  519. strmid( keyRes , line , 0 , strfind( line , "=" , true ) , sizeof( keyRes) );
  520. return keyRes;
  521. }
  522.  
  523. stock ini_GetValue( line[] )
  524. {
  525. new valRes[256];
  526. valRes[0]=0;
  527. if ( strfind( line , "=" , true ) == -1 ) return valRes;
  528. strmid( valRes , line , strfind( line , "=" , true )+1 , strlen( line ) , sizeof( valRes ) );
  529. return valRes;
  530. }
  531. strtok(const string[], &index)
  532. {
  533. new length = strlen(string);
  534. while ((index < length) && (string[index] <= ' '))
  535. {
  536. index++;
  537. }
  538.  
  539. new offset = index;
  540. new result[20];
  541. while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
  542. {
  543. result[index - offset] = string[index];
  544. index++;
  545. }
  546. result[index - offset] = EOS;
  547. return result;
  548. }
Advertisement
Add Comment
Please, Sign In to add comment