Guest User

Car Licensing System

a guest
Oct 29th, 2010
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 8.14 KB | None | 0 0
  1. /*
  2.     * License System: By TheHoodRat *
  3.     * Based on the East Coast Roleplay licensing system. *
  4.     * This filterscript is automatically built in with Dini, to save the licenses. *
  5.     * Credits to DracoBlue for Dini *
  6.    
  7.     * WARNING: You must create a folder called Licenses in your scriptfiles folder! *
  8.     * It is required to create the folder or else the server will crash! *
  9. */
  10.  
  11. #include <a_samp>
  12.  
  13. #define DINI_MAX_STRING 255
  14.  
  15. new Menu:licensemenu;
  16. new playerfile[256];
  17. new pname[MAX_PLAYER_NAME];
  18.  
  19. enum cInfo{Car}; new LicenseInfo[MAX_PLAYERS][cInfo];
  20.  
  21. public OnFilterScriptInit()
  22. {
  23.     licensemenu = CreateMenu("Select an option", 1, 50.0, 180.0, 200.0, 200.0);
  24.    
  25.     AddMenuItem(licensemenu, 0, "Car License - $50");
  26.    
  27.     AddStaticPickup(1239, 23, 361.8299,173.6111,1008.3828, 0);
  28.     Create3DTextLabel("Licenses\nType /buylicense to buy a license.",0x33CCFFAA,361.8299,173.6111,1008.3828,40.0,0);
  29.     return 1;
  30. }
  31.  
  32. public OnFilterScriptExit()
  33. {
  34.     return 1;
  35. }
  36.  
  37. public OnPlayerCommandText(playerid, cmdtext[])
  38. {
  39.     if (strcmp("/buylicense", cmdtext, true, 10) == 0) {
  40.     if(!IsPlayerInRangeOfPoint(playerid, 3.0, 361.8299,173.6111,1008.3828)){SendClientMessage(playerid, 0xAA3333AA, "ERROR: You must be at City Hall!"); return 1;}
  41.     ShowMenuForPlayer(licensemenu, playerid);
  42.     return 1;} return 0;
  43. }
  44.  
  45. public OnPlayerDisconnect(playerid, reason)
  46. {
  47.     GetPlayerName(playerid, pname, sizeof(pname));
  48.     format(playerfile, sizeof(playerfile), "Licenses/%s.ini",pname);
  49.     dini_IntSet(playerfile, "Car License", LicenseInfo[playerid][Car]);
  50.     return 1;
  51. }
  52.  
  53. public OnPlayerConnect(playerid)
  54. {
  55.     GetPlayerName(playerid, pname, sizeof(pname));
  56.     format(playerfile, sizeof(playerfile), "Licenses/%s.ini",pname);
  57.     return 1;
  58. }
  59.  
  60. public OnPlayerRequestSpawn(playerid)
  61. {
  62.     GetPlayerName(playerid, pname, sizeof(pname));
  63.     format(playerfile, sizeof(playerfile), "Licenses/%s.ini",pname);
  64.     LicenseInfo[playerid][Car] = dini_Int(playerfile, "Car License");
  65.     return 1;
  66. }
  67.  
  68. public OnPlayerStateChange(playerid, newstate, oldstate)
  69. {
  70.     if(newstate == PLAYER_STATE_DRIVER)
  71.     {
  72.      if(LicenseInfo[playerid][Car] < 1)
  73.      {
  74.      SendClientMessage(playerid, 0xFF6347AA, "You can't drive this car, you don't have a license!");
  75.      RemovePlayerFromVehicle(playerid);
  76.      return 1; }
  77.  }
  78.     return 1;
  79. }
  80.  
  81. public OnPlayerSelectedMenuRow(playerid, row)
  82. {
  83.     HideMenuForPlayer(licensemenu, playerid);
  84.     new Menu:current;
  85.     current = GetPlayerMenu(playerid);
  86.     if(current == licensemenu) {
  87.         switch(row) {
  88.             case 0:
  89.             {
  90.             if(GetPlayerMoney(playerid) < 50)
  91.             {
  92.             SendClientMessage(playerid, 0xFF6347AA, "You can't afford the license, sorry!");
  93.             return 1;
  94.             }
  95.             LicenseInfo[playerid][Car] = 1;
  96.             GivePlayerMoney(playerid, - 50);
  97.             SendClientMessage(playerid, 0x33CCFFAA, "You have bought a Car License for $50!");
  98.             LicenseInfo[playerid][Car] = 1;
  99.             GetPlayerName(playerid, pname, sizeof(pname));
  100.             format(playerfile, sizeof(playerfile), "Licenses/%s.ini",pname);
  101.             dini_Set(playerfile, "Car License", LicenseInfo[playerid][Car]);
  102.             }
  103.             //case 1:
  104.             //{
  105.             //}
  106.         }
  107.         return 1;
  108.     }
  109.     return 1;
  110. }
  111.  
  112. public OnPlayerExitedMenu(playerid)
  113. {
  114.     TogglePlayerControllable(playerid, 1);
  115.     return 1;
  116. }
  117.  
  118. /*
  119.  
  120. * DINI BY DRACOBLUE *
  121.  
  122. */
  123.  
  124. stock dini_Exists(filename[]) {
  125.     return fexist(filename);
  126. }
  127.  
  128. stock dini_Remove(filename[]) {
  129.     return fremove(filename);
  130. }
  131.  
  132. stock dini_Create(filename[]) {
  133.     if (fexist(filename)) return false;
  134.     new File:fhnd;
  135.     fhnd=fopen(filename,io_write);
  136.     if (fhnd) {
  137.         fclose(fhnd);
  138.         return true;
  139.     }
  140.     return false;
  141. }
  142.  
  143. stock dini_Set(filename[],key[],value[]) {
  144.     new key_length = strlen(key);
  145.     new value_length = strlen(value);
  146.     if (key_length==0 || key_length+value_length+2>DINI_MAX_STRING) return false;
  147.  
  148.     new File:fohnd, File:fwhnd;
  149.     new tmpres[DINI_MAX_STRING];
  150.     new bool:wasset=false;
  151.  
  152.     format(tmpres,sizeof(tmpres),"%s.part",filename);
  153.     fremove(tmpres);
  154.     fohnd=fopen(filename,io_read);
  155.     if (!fohnd) return false;
  156.  
  157.     fwhnd=fopen(tmpres,io_write);
  158.     if (!fwhnd) {
  159.         fclose(fohnd);
  160.         return false;
  161.     }
  162.  
  163.     while (fread(fohnd,tmpres)) {
  164.         if (
  165.             !wasset
  166.             && tmpres[key_length]=='='
  167.             && !strcmp(tmpres, key, true, key_length)
  168.         ) {
  169.                 format(tmpres,sizeof(tmpres),"%s=%s",key,value);
  170.                 wasset=true;
  171.         } else {
  172.             DINI_StripNewLine(tmpres);
  173.         }
  174.         fwrite(fwhnd,tmpres);
  175.         fwrite(fwhnd,"\r\n");
  176.     }
  177.  
  178.     if (!wasset) {
  179.         format(tmpres,sizeof(tmpres),"%s=%s",key,value);
  180.         fwrite(fwhnd,tmpres);
  181.         fwrite(fwhnd,"\r\n");
  182.     }
  183.  
  184.     fclose(fohnd);
  185.     fclose(fwhnd);
  186.  
  187.     format(tmpres,sizeof(tmpres),"%s.part",filename);
  188.     if (DINI_fcopytextfile(tmpres,filename)) {
  189.         return fremove(tmpres);
  190.     }
  191.     return false;
  192. }
  193.  
  194.  
  195. stock dini_IntSet(filename[],key[],value) {
  196.    new valuestring[DINI_MAX_STRING];
  197.    format(valuestring,DINI_MAX_STRING,"%d",value);
  198.    return dini_Set(filename,key,valuestring);
  199. }
  200.  
  201. stock dini_Int(filename[],key[]) {
  202.    return strval(dini_Get(filename,key));
  203. }
  204.  
  205.  
  206. stock dini_FloatSet(filename[],key[],Float:value) {
  207.    new valuestring[DINI_MAX_STRING];
  208.    format(valuestring,DINI_MAX_STRING,"%f",value);
  209.    return dini_Set(filename,key,valuestring);
  210. }
  211.  
  212. stock Float:dini_Float(filename[],key[]) {
  213.    return floatstr(dini_Get(filename,key));
  214. }
  215.  
  216. stock dini_Bool(filename[],key[]) {
  217.    return strval(dini_Get(filename,key));
  218. }
  219.  
  220. stock dini_BoolSet(filename[],key[],value) {
  221.     if (value) {
  222.         return dini_Set(filename,key,"1");
  223.     }
  224.     return dini_Set(filename,key,"0");
  225. }
  226.  
  227. stock dini_Unset(filename[],key[]) {
  228.     new key_length = strlen(key);
  229.     if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;
  230.  
  231.     new File:fohnd, File:fwhnd;
  232.     new tmpres[DINI_MAX_STRING];
  233.  
  234.     format(tmpres,DINI_MAX_STRING,"%s.part",filename);
  235.     fremove(tmpres);
  236.  
  237.     fohnd=fopen(filename,io_read);
  238.     if (!fohnd) return false;
  239.  
  240.     fwhnd=fopen(tmpres,io_write);
  241.     if (!fwhnd) {
  242.         fclose(fohnd);
  243.         return false;
  244.     }
  245.  
  246.     while (fread(fohnd,tmpres)) {
  247.         if (
  248.             tmpres[key_length]=='='
  249.             && !strcmp(tmpres, key, true, key_length)
  250.         ) {
  251.         } else {
  252.             DINI_StripNewLine(tmpres);
  253.             fwrite(fwhnd,tmpres);
  254.             fwrite(fwhnd,"\r\n");
  255.         }
  256.     }
  257.  
  258.     fclose(fohnd);
  259.     fclose(fwhnd);
  260.  
  261.     format(tmpres,DINI_MAX_STRING,"%s.part",filename);
  262.     if (DINI_fcopytextfile(tmpres,filename)) {
  263.         return fremove(tmpres);
  264.     }
  265.     return false;
  266. }
  267.  
  268. stock dini_Get(filename[],key[]) {
  269.     new tmpres[DINI_MAX_STRING];
  270.  
  271.     new key_length = strlen(key);
  272.     if (key_length==0 || key_length+2>DINI_MAX_STRING) return tmpres;
  273.  
  274.     new File:fohnd;
  275.     fohnd=fopen(filename,io_read);
  276.     if (!fohnd) return tmpres;
  277.  
  278.     while (fread(fohnd,tmpres)) {
  279.         if (
  280.             tmpres[key_length]=='='
  281.             && !strcmp(tmpres, key, true, key_length)
  282.         ) {
  283.             DINI_StripNewLine(tmpres);
  284.             strmid(tmpres, tmpres, key_length + 1, strlen(tmpres), DINI_MAX_STRING);
  285.             fclose(fohnd);
  286.             return tmpres;
  287.         }
  288.     }
  289.     fclose(fohnd);
  290.     return tmpres;
  291. }
  292.  
  293.  
  294. stock dini_Isset(filename[],key[]) {
  295.     new key_length = strlen(key);
  296.     if (key_length==0 || key_length+2>DINI_MAX_STRING) return false;
  297.  
  298.     new File:fohnd;
  299.     fohnd=fopen(filename,io_read);
  300.     if (!fohnd) return false;
  301.  
  302.     new tmpres[DINI_MAX_STRING];
  303.     while (fread(fohnd,tmpres)) {
  304.         if (
  305.                 tmpres[key_length]=='='
  306.             &&  !strcmp(tmpres, key, true, key_length)
  307.         ) {
  308.             fclose(fohnd);
  309.             return true;
  310.         }
  311.     }
  312.     fclose(fohnd);
  313.     return false;
  314. }
  315.  
  316.  
  317.  
  318. stock DINI_StripNewLine(string[]) {
  319.     new len = strlen(string);
  320.     if (string[0]==0) return ;
  321.     if ((string[len - 1] == '\n') || (string[len - 1] == '\r')) {
  322.         string[len - 1] = 0;
  323.         if (string[0]==0) return ;
  324.         if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
  325.     }
  326. }
  327.  
  328. stock DINI_fcopytextfile(oldname[],newname[]) {
  329.     new File:ohnd,File:nhnd;
  330.     if (!fexist(oldname)) return false;
  331.     ohnd=fopen(oldname,io_read);
  332.     if (!ohnd) return false;
  333.     nhnd=fopen(newname,io_write);
  334.     if (!nhnd) {
  335.         fclose(ohnd);
  336.         return false;
  337.     }
  338.     new tmpres[DINI_MAX_STRING];
  339.     while (fread(ohnd,tmpres)) {
  340.         DINI_StripNewLine(tmpres);
  341.         format(tmpres,sizeof(tmpres),"%s\r\n",tmpres);
  342.         fwrite(nhnd,tmpres);
  343.     }
  344.     fclose(ohnd);
  345.     fclose(nhnd);
  346.     return true;
  347. }
Advertisement
Add Comment
Please, Sign In to add comment