SHARE
TWEET

[MySQL: Un-Threaded] jGarage [V1.0b][strcmp version]

Jstylezzz Mar 24th, 2014 229 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. *                                                                                                                       jGarage V1.0b - strcmp version
  3. *                                                                                                       by Jstylezzz
  4. *
  5. *
  6. *
  7. *   To create garages, log in as Rcon and use the command '/creategarage'. To remove a garage use '/removegarage' when standing on it's pickup.
  8. *       Use '/garagehelp' to view the available commands. If the server crashes when creating a garage, you probably forgot the 'garages' folder
  9. *   in the scriptfiles folder. Create it.
  10. *   If you have any questions suggestions or if you find bugs, please post in the release thread. Also, keep the credits, that's all I ask. Thanks!
  11. *
  12. *
  13. */
  14.  
  15. //=== INCLUDES ===//
  16. #include <a_samp> //Credits to the SA-MP Team
  17. #include <a_mysql> //Credits to BlueG
  18. #include <streamer> //Credits to Incognito
  19. #include <sscanf2> //Credits to Y_Less
  20.  
  21. //=== DEFINES ===//
  22.  
  23. //Config defines, change to your likings
  24. #define MAX_GARAGES 100 //Max garages to be created in the server
  25. #define GARAGE_OWNED_PICKUP 1559 //Change this to the pickup model you prefer. Default: White arrow (diamond)
  26. #define GARAGE_FREE_PICKUP 1273 //Change this to the pickup model you prefer. Default: Green house
  27. #define GARAGE_OWNED_TEXT "Owner: %s\nLocked: %s" //This text will appear at all owned garages
  28. #define GARAGE_FREE_TEXT "FOR SALE!\n Price: %d\n\nUse /buygarage to buy this garage." //This text will appear at all garages that are for sale
  29. #define DD 200.0 //The streamdistance for the textlabels
  30. #define TXTCOLOR 0xF9C50FFF //The textcolor for the textlabels
  31. #define COLOR_USAGE 0xBB4D4DFF //The textcolor for the 'command usage' message
  32. #define COLOR_SUCCESS 0x00AE00FF //The textcolor for the 'command sucessfull' message
  33. #define COLOR_ERROR 0xFF0000FF //The textcolor for the 'error' message
  34. #define COLOR_ORANGE 0xFFA500FF //The color orange
  35. #define COLOR_LIGHTBLUE 0xADD8E6FF //The color light blue
  36.  
  37. #define HOST "localhost"
  38. #define USER "root"
  39. #define PASS ""
  40. #define DB "SAMP"
  41.  
  42. //System defines, no need to change stuff here
  43. #define SCRIPT_VERSION "V1.0b"
  44.  
  45. //=== ENUMS ===//
  46. enum garageInfo{
  47.  
  48.         Owner[24], //Holds the name of the owner
  49.         Owned, //Holds the owned value (1 if owned, 0 if for sale)
  50.         Locked, //The locked status of the garage (0 unlocked, 1 locked)
  51.         Price, //The price of the garage
  52.         Float:PosX, //The outside X position of the garage
  53.         Float:PosY, //The outside Y position of the garage
  54.         Float:PosZ, //The outside Z position of the garage
  55.         Interior, //The internal interior number of the garage
  56.         UID //Unique ID, keeps a unique ID of the garages so the virtualworld doesn't mix up when deleting and reloading garages
  57. }
  58.  
  59.  
  60. //=== NEWS ===//
  61. new gInfo[MAX_GARAGES][garageInfo]; //This is used to access variable from our enumerator
  62. new garageCount; //This will hold the total of loaded garages
  63. new Float:GarageInteriors[][] = //This array holds the coordinates, facing angle and interior ID of the garages.
  64. {
  65.         {616.4642, -124.4003, 997.5993, 90.0, 3.0}, // Small garage
  66.     {617.0011, -74.6962, 997.8426, 90.0, 2.0}, // Medium garage
  67.     {606.4268, -9.9375, 1000.7485, 270.0, 1.0} //Big garage
  68.  
  69. };
  70. new Text3D:garageLabel[MAX_GARAGES]; //Will hold the garage label
  71. new garagePickup[MAX_GARAGES]; //Will hold the garage pickup
  72. new lastGarage[MAX_PLAYERS]; //Will hold the last garage ID the player went in to
  73.  
  74. //=== NATIVE PUBLICS ===//
  75.  
  76. public OnFilterScriptInit()
  77. {
  78.         print("\n--------------------------------------");
  79.         printf(" jGarage %s by Jstylezzz loading..",SCRIPT_VERSION);
  80.         print("--------------------------------------\n");
  81.         mysql_debug(1);
  82.         mysql_connect(HOST,USER,DB,PASS);
  83.         Load_Garages();
  84.  
  85.         return 1;
  86. }
  87.  
  88. public OnFilterScriptExit()
  89. {
  90.         Save_Garages();
  91.         Remove_PickupsAndLabels();
  92.         return 1;
  93. }
  94.  
  95. //=== STOCKS ===//
  96. stock Load_Garages() //Loads all garages
  97. {
  98.         garageCount = 1; //Make a debug garage
  99.         new sql[128] = "SELECT * FROM `garages`";
  100.         new i;
  101.         mysql_query(sql);
  102.         mysql_store_result();
  103.         while(mysql_fetch_row(sql))
  104.         {
  105.  
  106.                 sscanf(sql, "e<p<|>s[24]dddfffdd>", gInfo[i]);
  107.                 printf("DEBUG: %s %d %d %d %f %f %f %d %d",gInfo[i][Owner],gInfo[i][Owned],gInfo[i][Locked],gInfo[i][Price],gInfo[i][PosX],gInfo[i][PosY],gInfo[i][PosZ],gInfo[i][Interior],gInfo[i][UID]);
  108.                 UpdateGarageInfo(i);
  109.                 garageCount++;
  110.                 i++;
  111.  
  112.         }
  113.         mysql_free_result();
  114.         printf("[jGarage]: Loaded %d garages.",garageCount-1); //The debug garage isn't a real garage, so we take 1 away
  115.         garageCount++; //To prevent overwriting/not detecting of garages
  116. }
  117. stock Save_Garages() //Saves all the garages
  118. {
  119.         for(new i=0; i < garageCount+1; i++)
  120.         {
  121.                 new sql[256];
  122.                 format(sql,sizeof(sql),"UPDATE `garages` SET \
  123.                 `Owner`='%s',\
  124.                 `Owned`='%d',\
  125.                 `Locked`='%d',\
  126.                 `Price`='%d',\
  127.                 `PosX`='%f',\
  128.                 `PosY`='%f',\
  129.                 `PosZ`='%f',\
  130.                 `Interior`='%d',\
  131.                 `UID`='%d'\
  132.                 WHERE `UID`='%d'",
  133.                 gInfo[i][Owner],
  134.                 gInfo[i][Owned],
  135.                 gInfo[i][Locked],
  136.                 gInfo[i][Price],
  137.                 gInfo[i][PosX],
  138.                 gInfo[i][PosY],
  139.                 gInfo[i][PosZ],
  140.         gInfo[i][Interior],
  141.                 gInfo[i][UID],
  142.                 gInfo[i][UID]);
  143.                 mysql_query(sql);
  144.         }
  145. }
  146. stock Save_Garage(gid) //Saves a specific garage
  147. {
  148.         new sql[256];
  149.         format(sql,sizeof(sql),"UPDATE `garages` SET \
  150.         `Owner`='%s',\
  151.         `Owned`='%d',\
  152.         `Locked`='%d',\
  153.         `Price`='%d',\
  154.         `PosX`='%f',\
  155.         `PosY`='%f',\
  156.         `PosZ`='%f',\
  157.         `Interior`='%d',\
  158.         `UID`='%d'\
  159.         WHERE `UID`='%d'",
  160.         gInfo[gid][Owner],
  161.         gInfo[gid][Owned],
  162.         gInfo[gid][Locked],
  163.         gInfo[gid][Price],
  164.         gInfo[gid][PosX],
  165.         gInfo[gid][PosY],
  166.         gInfo[gid][PosZ],
  167.         gInfo[gid][Interior],
  168.         gInfo[gid][UID],
  169.         gInfo[gid][UID]);
  170.         mysql_query(sql);
  171.  
  172. }
  173. stock UpdateGarageInfo(gid) //Updates/creates the garage text and label
  174. {
  175.         //Get rid of the old label and pickup (if existing)
  176.         DestroyDynamic3DTextLabel(garageLabel[gid]);
  177.         DestroyDynamicPickup(garagePickup[gid]);
  178.  
  179.         //Re-create them with the correct data
  180.         new ltext[128];
  181.         if(gInfo[gid][Owned] == 1) //If the garage is owned
  182.         {
  183.             format(ltext,128,GARAGE_OWNED_TEXT,gInfo[gid][Owner],GetLockedStatus(gInfo[gid][Locked]));
  184.             garageLabel[gid] = CreateDynamic3DTextLabel(ltext, TXTCOLOR, gInfo[gid][PosX],gInfo[gid][PosY],gInfo[gid][PosZ]+0.1,DD);
  185.             garagePickup[gid] = CreateDynamicPickup(GARAGE_OWNED_PICKUP,1,gInfo[gid][PosX],gInfo[gid][PosY],gInfo[gid][PosZ]+0.2);
  186.         }
  187.         if(gInfo[gid][Owned] == 0)
  188.         {
  189.             format(ltext,128,GARAGE_FREE_TEXT,gInfo[gid][Price]);
  190.             garageLabel[gid] = CreateDynamic3DTextLabel(ltext, TXTCOLOR, gInfo[gid][PosX],gInfo[gid][PosY],gInfo[gid][PosZ]+0.1,DD);
  191.             garagePickup[gid] = CreateDynamicPickup(GARAGE_FREE_PICKUP,1,gInfo[gid][PosX],gInfo[gid][PosY],gInfo[gid][PosZ]);
  192.         }
  193. }
  194. stock GetLockedStatus(value) //Returns 'Locked' or 'Unlocked' according to the value given
  195. {
  196.         new out[64];
  197.         if(value == 1)
  198.         {
  199.             out = "Yes";
  200.         }
  201.         else
  202.         {
  203.             out = "No";
  204.         }
  205.         return out;
  206. }
  207. stock GetPlayerNameEx(playerid)
  208. {
  209.         new pName[24];
  210.         GetPlayerName(playerid,pName,24);
  211.         return pName;
  212. }
  213. stock Remove_PickupsAndLabels()
  214. {
  215.         for(new i=0; i < garageCount+1; i++)
  216.         {
  217.             DestroyDynamic3DTextLabel(garageLabel[i]);
  218.             DestroyDynamicPickup(garagePickup[i]);
  219.         }
  220. }
  221. //=== COMMANDS ===//
  222. public OnPlayerCommandText(playerid, cmdtext[])
  223. {
  224.         if(strcmp("/garagehelp", cmdtext, true, 11) == 0)
  225.         {
  226.                 SendClientMessage(playerid, COLOR_ORANGE, "jGarage commands:");
  227.                 if(!IsPlayerAdmin(playerid))
  228.                 {
  229.                         SendClientMessage(playerid, COLOR_LIGHTBLUE, "/genter | /gexit | /lockgarage | /buygarage | /sellgarage");
  230.                 }
  231.                 else
  232.                 {
  233.                         SendClientMessage(playerid, COLOR_LIGHTBLUE, "/creategarage | /removegarage | /garagetypes | /genter | /gexit | /lockgarage | /buygarage | /sellgarage");
  234.                 }
  235.                 return 1;
  236.         }
  237.         if(strcmp("/garagetypes", cmdtext, true, 12) == 0)
  238.         {
  239.             if(!IsPlayerAdmin(playerid)) return 0;
  240.             SendClientMessage(playerid, COLOR_ORANGE, "jGarage info - Garage types");
  241.                 SendClientMessage(playerid, COLOR_LIGHTBLUE, "Type 0: Small garage");
  242.                 SendClientMessage(playerid, COLOR_LIGHTBLUE, "Type 1: Medium garage");
  243.                 SendClientMessage(playerid, COLOR_LIGHTBLUE, "Type 2: Big garage");
  244.                 return 1;
  245.         }
  246.         if(strcmp("/creategarage", cmdtext, true, 13) == 0)
  247.         {
  248.                 if(!IsPlayerAdmin(playerid)) return 0;
  249.                 if(garageCount == MAX_GARAGES) return SendClientMessage(playerid, COLOR_USAGE, "The max. amount of garages is reached. Increase the limit in the jGarage filterscript.");
  250.                 new price, type;
  251.                 if(sscanf(cmdtext[14],"dd",price, type)) return SendClientMessage(playerid, COLOR_USAGE, "Usage: /creategarage <price> <type>  || Use /garagetypes for a list of garage types.");
  252.                 new Float:X, Float:Y, Float:Z;
  253.                 GetPlayerPos(playerid, X,Y,Z);
  254.                 format(gInfo[garageCount][Owner],24,"the State");
  255.                 gInfo[garageCount][Owned] = 0;
  256.                 gInfo[garageCount][Price] = price;
  257.                 gInfo[garageCount][Interior] = type;
  258.                 gInfo[garageCount][UID] = garageCount;
  259.                 gInfo[garageCount][PosX] = X;
  260.                 gInfo[garageCount][PosY] = Y;
  261.                 gInfo[garageCount][PosZ] = Z;
  262.                 gInfo[garageCount][Locked] = 1;
  263.                 new sql[128];
  264.                 format(sql,sizeof(sql),"INSERT INTO `garages` (UID) VALUES ('%d')",garageCount); //Insert the UID into the SQL database, and save the data based on the UID
  265.                 mysql_query(sql);
  266.                 Save_Garage(garageCount);
  267.                 UpdateGarageInfo(garageCount);
  268.                 garageCount++;
  269.                 SendClientMessage(playerid,COLOR_SUCCESS,"Garage created!");
  270.                 return 1;
  271.         }
  272.         if(strcmp("/removegarage", cmdtext, true, 13) == 0)
  273.         {
  274.             if(!IsPlayerAdmin(playerid)) return 0;
  275.             for(new i=0; i < garageCount+1; i++)
  276.                 {
  277.                     if(IsPlayerInRangeOfPoint(playerid, 3.0, gInfo[i][PosX], gInfo[i][PosY], gInfo[i][PosZ]))
  278.                     {
  279.                         new sql[128];
  280.                                 format(sql,sizeof(sql),"DELETE FROM `garages` WHERE `UID`='%d'",gInfo[i][UID]);
  281.                                 mysql_query(sql);
  282.                         format(gInfo[i][Owner],24,"REMOVED");
  283.                                 gInfo[i][Owned] = -999;
  284.                                 gInfo[i][Price] = -999;
  285.                                 gInfo[i][Interior] = -999;
  286.                                 gInfo[i][UID] = -999;
  287.                                 gInfo[i][PosX] = -999;
  288.                                 gInfo[i][PosY] = -999;
  289.                                 gInfo[i][PosZ] = -999;
  290.                                 gInfo[i][Locked] = -999;
  291.                                 DestroyDynamic3DTextLabel(garageLabel[i]);
  292.                         DestroyDynamicPickup(garagePickup[i]);
  293.                                 SendClientMessage(playerid, COLOR_SUCCESS, "You have removed this garage.");
  294.                                 return 1;
  295.                         }
  296.                 }
  297.                 SendClientMessage(playerid, COLOR_ERROR,"Error: You're not near any garage.");
  298.                 return 1;
  299.         }
  300.         if(strcmp("/genter", cmdtext, true, 7) == 0)
  301.         {
  302.                 for(new i=0; i < garageCount+1; i++)
  303.                 {
  304.                     if(IsPlayerInRangeOfPoint(playerid, 3.0, gInfo[i][PosX], gInfo[i][PosY], gInfo[i][PosZ]))
  305.                     {
  306.  
  307.                         if(gInfo[i][Locked] == 1 && strcmp(GetPlayerNameEx(playerid),gInfo[i][Owner])) return SendClientMessage(playerid,COLOR_ERROR,"Error: You're not the owner of this garage. It's locked, you can't enter.");
  308.                         new gtype = gInfo[i][Interior];
  309.                         if(!IsPlayerInAnyVehicle(playerid))
  310.                                 {
  311.                                         SetPlayerVirtualWorld(playerid,gInfo[i][UID]);
  312.                                         SetPlayerInterior(playerid,floatround(GarageInteriors[gtype][4]));
  313.                                         SetPlayerPos(playerid,GarageInteriors[gtype][0],GarageInteriors[gtype][1],GarageInteriors[gtype][2]);
  314.                                         lastGarage[playerid] = i;
  315.                                 }
  316.                                 else
  317.                                 {
  318.                                         new vid = GetPlayerVehicleID(playerid);
  319.                                     LinkVehicleToInterior(vid,floatround(GarageInteriors[gtype][4]));
  320.                                     SetVehicleVirtualWorld(vid,gInfo[i][UID]);
  321.                         SetPlayerVirtualWorld(playerid,gInfo[i][UID]);
  322.                                         SetPlayerInterior(playerid,floatround(GarageInteriors[gtype][4]));
  323.                                         SetVehiclePos(vid,GarageInteriors[gtype][0],GarageInteriors[gtype][1],GarageInteriors[gtype][2]);
  324.                                         lastGarage[playerid] = i;
  325.                                 }
  326.                                 return 1;
  327.  
  328.                         }
  329.                 }
  330.                 SendClientMessage(playerid,COLOR_ERROR,"Error: You're not near any garage. ");
  331.                 return 1;
  332.         }
  333.         if(strcmp("/gexit", cmdtext, true, 6) == 0)
  334.         {
  335.                 if(lastGarage[playerid] >= 0)
  336.                 {
  337.                     new lg = lastGarage[playerid];
  338.                     if(!IsPlayerInAnyVehicle(playerid))
  339.                     {
  340.                         SetPlayerPos(playerid,gInfo[lg][PosX],gInfo[lg][PosY],gInfo[lg][PosZ]);
  341.                         SetPlayerInterior(playerid,0);
  342.                         SetPlayerVirtualWorld(playerid,0);
  343.                         }
  344.                         else
  345.                         {
  346.                             new vid = GetPlayerVehicleID(playerid);
  347.                                 LinkVehicleToInterior(vid,0);
  348.                                 SetVehicleVirtualWorld(vid,0);
  349.                                 SetVehiclePos(vid,gInfo[lg][PosX],gInfo[lg][PosY],gInfo[lg][PosZ]);
  350.                             SetPlayerVirtualWorld(playerid,0);
  351.                             SetPlayerInterior(playerid,0);
  352.                         }
  353.                         lastGarage[playerid] = -999;
  354.                 }
  355.                 else return SendClientMessage(playerid,COLOR_ERROR,"Error: You're not in any garage.");
  356.                 return 1;
  357.         }
  358.  
  359.         if(strcmp("/buygarage", cmdtext, true, 10) == 0)
  360.         {
  361.             for(new i=0; i < garageCount+1; i++)
  362.                 {
  363.                         if(IsPlayerInRangeOfPoint(playerid, 3.0, gInfo[i][PosX], gInfo[i][PosY], gInfo[i][PosZ]))
  364.                         {
  365.                                 if(gInfo[i][Owned] == 1) return SendClientMessage(playerid, COLOR_ERROR,"Error: This garage is already owned.");
  366.                                 if(GetPlayerMoney(playerid) < gInfo[i][Price]) return SendClientMessage(playerid,COLOR_ERROR,"Error: You don't have enough money to buy this garage.");
  367.                                 GivePlayerMoney(playerid,-gInfo[i][Price]);
  368.                                 gInfo[i][Price]-= random(5000); //Take some money off of the original price
  369.                                 format(gInfo[i][Owner],24,"%s",GetPlayerNameEx(playerid));
  370.                                 gInfo[i][Owned] = 1;
  371.                                 Save_Garage(i);
  372.                                 UpdateGarageInfo(i);
  373.                                 SendClientMessage(playerid,COLOR_SUCCESS,"You have successfully bought this garage.");
  374.                                 return 1;
  375.                         }
  376.                 }
  377.                 SendClientMessage(playerid,COLOR_ERROR,"Error: You're not near any garage.");
  378.                 return 1;
  379.         }
  380.         if(strcmp("/lockgarage", cmdtext, true, 10) == 0)
  381.         {
  382.                 for(new i=0; i < garageCount+1; i++)
  383.                 {
  384.                         if(IsPlayerInRangeOfPoint(playerid, 3.0, gInfo[i][PosX], gInfo[i][PosY], gInfo[i][PosZ]))
  385.                         {
  386.                                 if(strcmp(gInfo[i][Owner],GetPlayerNameEx(playerid))) return SendClientMessage(playerid,COLOR_ERROR,"Error: You're not the owner of this garage.");
  387.                                 if(gInfo[i][Locked] == 1)
  388.                                 {
  389.                                     gInfo[i][Locked] = 0;
  390.                                     UpdateGarageInfo(i);
  391.                                     Save_Garage(i);
  392.                         SendClientMessage(playerid,COLOR_SUCCESS,"You have unlocked your garage.");
  393.                                     return 1;
  394.                                 }
  395.                                 else
  396.                                 {
  397.                                     gInfo[i][Locked] = 1;
  398.                                     UpdateGarageInfo(i);
  399.                         Save_Garage(i);
  400.                         SendClientMessage(playerid,COLOR_SUCCESS,"You have locked your garage.");
  401.                                     return 1;
  402.                                 }
  403.                         }
  404.                 }
  405.                 SendClientMessage(playerid,COLOR_ERROR,"Error: You're not near any garage.");
  406.                 return 1;
  407.         }
  408.         if(strcmp("/sellgarage", cmdtext, true, 11) == 0)
  409.         {
  410.                 for(new i=0; i < garageCount+1; i++)
  411.                 {
  412.                         if(IsPlayerInRangeOfPoint(playerid, 3.0, gInfo[i][PosX], gInfo[i][PosY], gInfo[i][PosZ]))
  413.                 {
  414.                                 if(strcmp(gInfo[i][Owner],GetPlayerNameEx(playerid))) return SendClientMessage(playerid,COLOR_ERROR,"Error: You're not the owner of this garage.");
  415.                                 GivePlayerMoney(playerid,gInfo[i][Price]-random(500));
  416.                                 gInfo[i][Owned] = 0;
  417.                                 format(gInfo[i][Owner],24,"the State");
  418.                                 gInfo[i][Locked] = 1;
  419.                                 UpdateGarageInfo(i);
  420.                                 Save_Garage(i);
  421.                                 SendClientMessage(playerid, COLOR_SUCCESS,"You have successfully sold your garage.");
  422.                                 return 1;
  423.                           }
  424.                 }
  425.                 SendClientMessage(playerid, COLOR_ERROR,"You're not near any garage.");
  426.                 return 1;
  427.         }
  428.         return 0;
  429. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top