Advertisement
Guest User

Login system with dialogs (y_ini)

a guest
Jul 16th, 2011
1,521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.70 KB | None | 0 0
  1. #define FILTERSCRIPT
  2.  
  3. #include <a_samp>
  4. #include <YSI\y_ini>
  5. #include <dudb>
  6.  
  7. #define PATH "/Users/%s.ini"
  8.  
  9. #define PLAYER_MUST_REGISTER
  10. #define REGISTERED_MONEY 5000
  11. #define AUTOLOGIN
  12.  
  13. #define white 0xFFFFFFA
  14. #define red 0xFF0000AA
  15. #define lime 0x00FF00FF
  16. #define yellow 0xFFFF00FF
  17.  
  18. #define DIALOG_REGISTER 999
  19. #define DIALOG_LOGIN 998
  20.  
  21. #pragma unused ret_memcpy
  22.  
  23. enum iDetails {
  24.     Pass,
  25.     Cash,
  26.     Score
  27. };
  28. new pInfo[MAX_PLAYERS][iDetails];
  29.  
  30. #if defined AUTOLOGIN
  31.     new pIP[MAX_PLAYERS][16];
  32. #endif
  33.  
  34. new pLogged[MAX_PLAYERS];
  35.  
  36. stock PlayerPath(playerid) {
  37.     new iStr[256],name[MAX_PLAYER_NAME];
  38.     GetPlayerName(playerid,name,sizeof(name));
  39.     format(iStr,sizeof(iStr),PATH,name);
  40.     return iStr;
  41. }
  42. public OnFilterScriptInit()
  43. {
  44.     print("\n--------------------------------------");
  45.     print("Login & Register system by Seven_of_Nine");
  46.     print("--------------------------------------\n");
  47.     return 1;
  48. }
  49.  
  50. public OnFilterScriptExit()
  51. {
  52.     return 1;
  53. }
  54.  
  55. public OnGameModeExit()
  56. {
  57.     return 1;
  58. }
  59.  
  60. forward UserDataLoad_data(playerid,name[],value[]);
  61.  
  62. public UserDataLoad_data(playerid,name[],value[]) {
  63.     INI_Int("Pass",pInfo[playerid][Pass]);
  64.     #if defined AUTOLOGIN
  65.         INI_String("IP",pIP[playerid],16);
  66.     #endif
  67.     INI_Int("Cash",pInfo[playerid][Cash]);
  68.     INI_Int("Score",pInfo[playerid][Score]);
  69.     return 1;
  70. }
  71.  
  72. public OnPlayerConnect(playerid)
  73. {
  74.     pLogged[playerid] = 0;
  75.     #if defined AUTOLOGIN
  76.         new tmpIP[16];
  77.         GetPlayerIp(playerid,tmpIP,sizeof(tmpIP));
  78.     #endif
  79.     if(fexist(PlayerPath(playerid))) {
  80.         INI_ParseFile(PlayerPath(playerid), "UserDataLoad_%s", .bExtra = true, .extra = playerid);
  81.         #if defined AUTOLOGIN
  82.             if(strcmp(tmpIP,pIP[playerid],true) == 0) {
  83.                 pLogged[playerid] = 1;
  84.                 SetPlayerScore(playerid,pInfo[playerid][Score]);
  85.                 GivePlayerMoney(playerid,pInfo[playerid][Cash]);
  86.                 SendClientMessage(playerid,lime,"You've been auto-logged in. [IP match]");
  87.                 return 1;
  88.             }
  89.         #endif
  90.         ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_INPUT,"Login","Please enter your password below.","Login","Leave");
  91.     } else {
  92.         ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_INPUT,"Register","Please register by entering a password below.","Register","Leave");
  93.     }
  94.     return 1;
  95. }
  96.  
  97. public OnPlayerDisconnect(playerid, reason)
  98. {
  99.     if(pLogged[playerid] == 1) {
  100.         new INI:iFile = INI_Open(PlayerPath(playerid));
  101.         INI_SetTag(iFile,"data");
  102.         INI_WriteInt(iFile,"Cash",GetPlayerMoney(playerid));
  103.         INI_WriteInt(iFile,"Score",GetPlayerScore(playerid));
  104.         INI_Close(iFile);
  105.     }
  106.     pLogged[playerid] = 0;
  107.     return 1;
  108. }
  109.  
  110. public OnPlayerRequestSpawn(playerid)
  111. {
  112.     if(pLogged[playerid] == 0) return SendClientMessage(playerid,yellow,"You must register or login before spawning!");
  113.     return 1;
  114. }
  115.  
  116. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  117. {
  118.     if(dialogid == DIALOG_REGISTER) {
  119.         GetPlayerIp(playerid,pIP[playerid],16);
  120.         if(!response) Kick(playerid);
  121.         if(!strlen(inputtext)) return ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_INPUT,"Register","Please enter a password.","Register","Leave");
  122.         new INI:iFile = INI_Open(PlayerPath(playerid));
  123.         INI_SetTag(iFile,"data");
  124.         INI_WriteInt(iFile,"Pass",udb_hash(inputtext));
  125.         #if defined AUTOLOGIN
  126.             INI_WriteString(iFile,"IP",pIP[playerid]);
  127.         #endif
  128.         INI_WriteInt(iFile,"Cash",REGISTERED_MONEY);
  129.         INI_WriteInt(iFile,"Score",0);
  130.         INI_Close(iFile);
  131.         pLogged[playerid] = 1;
  132.         new iStr[128];
  133.         format(iStr,sizeof(iStr),"You've successfully registered with the password \"%s\".",inputtext);
  134.         SendClientMessage(playerid,yellow,iStr);
  135.         return 1;
  136.     }
  137.     if(dialogid == DIALOG_LOGIN) {
  138.         if(!response) Kick(playerid);
  139.         new iStr[128],gTries;
  140.         if(gTries == 0) gTries = 1;
  141.         if(gTries == 3) {
  142.             new pName[30];
  143.             GetPlayerName(playerid,pName,sizeof(pName));
  144.             format(iStr,sizeof(iStr),"%s has been kicked for exceeding login tries.",pName);
  145.             SendClientMessageToAll(red,iStr);
  146.             return Kick(playerid);
  147.         }
  148.         if(!strlen(inputtext)) {
  149.             format(iStr,sizeof(iStr),"Please enter your password. Tries: %i/3",gTries);
  150.             return ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_INPUT,"Login",iStr,"Login","Leave");
  151.         }
  152.         if(udb_hash(inputtext) == pInfo[playerid][Pass]) {
  153.             pLogged[playerid] = 1;
  154.             SendClientMessage(playerid,lime,"You've successfully logged in.");
  155.             SetPlayerScore(playerid,pInfo[playerid][Score]);
  156.             GivePlayerMoney(playerid,pInfo[playerid][Cash]);
  157.         } else {
  158.             format(iStr,sizeof(iStr),"Incorrect password. Tries: %i/3",gTries);
  159.             ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_INPUT,"Register",iStr,"Login","Leave");
  160.             gTries++;
  161.             return 1;
  162.         }
  163.         return 1;
  164.     }
  165.     return 1;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement