CaptainB

Dynamic MySQL GPS System 3.0

Nov 8th, 2018
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 8.79 KB | None | 0 0
  1. /**********************************
  2.  *                                *
  3.   *   Scripter:    CaptainBoi    *
  4.   *   Version:     3.0           *
  5.   *   Released:    07-11-2018    *
  6.  *                                *
  7.  **********************************/
  8.  
  9. /* Includes */
  10. #include <a_samp>
  11. #include <a_mysql>
  12. #include <streamer>
  13. #include <sscanf2>
  14. #include <zcmd>
  15.  
  16. /* MySQL Entities */
  17. #define     MYSQL_HOST      "127.0.0.1"
  18. #define     MYSQL_USER      "root"
  19. #define     MYSQL_PASS      ""
  20. #define     MYSQL_DB        "deathmatch"
  21.  
  22. /* Database */
  23. new MySQL: GPSDB;
  24.  
  25. /* Enumerator */
  26. enum
  27. {
  28.     DIALOG_CONTROL_GPS = 100,
  29.     DIALOG_ADD_GLOC,
  30.     DIALOG_DEL_GLOC,
  31.     DIALOG_TP_GLOC,
  32.     DIALOG_GPS,
  33.     DIALOG_GPS_LOC
  34. }
  35.  
  36. enum GPSData
  37. {
  38.     LocName[14],
  39.     Float: Pos[3],
  40.     Interior
  41. }
  42.  
  43. /* Variables */
  44. new gInfo[MAX_PLAYERS][GPSData];
  45. new GPSMarker[MAX_PLAYERS];
  46.  
  47. public OnFilterScriptInit()
  48. {
  49.     new MySQLOpt: option_id = mysql_init_options();
  50.     mysql_global_options(DUPLICATE_CONNECTIONS, true);
  51.     mysql_set_option(option_id, AUTO_RECONNECT, true);  
  52.     GPSDB = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB, option_id);
  53.    
  54.     if(GPSDB == MYSQL_INVALID_HANDLE || mysql_errno(GPSDB) != 0)
  55.     {
  56.         print("Cannot connect to mysql database");
  57.  
  58.         SendRconCommand("exit");
  59.         return 1;
  60.     }
  61.     else print("Connected to mysql database.");
  62.    
  63.     mysql_query(GPSDB, "CREATE TABLE IF NOT EXISTS `gpsdb` (\
  64.     `S.No` INTEGER PRIMARY KEY AUTO_INCREMENT,\
  65.     `LocationName` VARCHAR(100) NOT NULL,\
  66.     `PositionX` FLOAT DEFAULT 0,\
  67.     `PositionY` FLOAT DEFAULT 0,\
  68.     `PositionZ` FLOAT DEFAULT 0,\
  69.     `InteriorID` INT)", false);
  70.     return 1;
  71. }
  72.  
  73. public OnFilterScriptExit()
  74. {
  75.     mysql_close(GPSDB);
  76.     return 1;
  77. }
  78.  
  79. public OnPlayerConnect(playerid)
  80. {
  81.     GPSMarker[playerid] = 0;
  82.     return 1;
  83. }
  84.  
  85. public OnPlayerDisconnect(playerid, reason)
  86. {
  87.     GPSMarker[playerid] = 0;
  88.     return 1;
  89. }
  90.  
  91. /* Commands */
  92. CMD:agps(playerid, params[])
  93. {
  94.     ShowPlayerDialog(playerid, DIALOG_CONTROL_GPS, DIALOG_STYLE_TABLIST_HEADERS, "Please select an option.", "\
  95.     S.No\tOption\t-\tInformation\n\
  96.     1.\tAdd GPS Location\t-\tYou can add gps location in /gps command.\n\
  97.     2.\tDelete GPS Location\t-\tYou can remove/delete the gps locations.\n\
  98.     3.\tGoto GPS Location\t-\tYou can teleport to the gps location.\n\
  99.     4.\tShow GPS\t-\tYou can see all the locations created in gps.\n\
  100.     5.\tTurn GPS Off\t-\tYou can turn gps off if its on.", "Select", "Cancel");
  101.     return 1;
  102. }
  103.  
  104. CMD:gps(playerid,params[])
  105. {
  106.     ShowPlayerDialog(playerid, DIALOG_GPS, DIALOG_STYLE_TABLIST_HEADERS, "Please select an option.", "\
  107.     S.No\tOption\t-\tInformation\n\
  108.     1.\tShow GPS Locations\t-\tYou can see all the available GPS locations.\n\
  109.     2.\tTurn GPS Off\t-\tYou can turn gps off if its on.", "Select", "Cancel");
  110.     return 1;
  111. }
  112.  
  113. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  114. {
  115.     new Query[216], Query2[216], string[216], string2[216];
  116.     switch(dialogid)
  117.     {
  118.         case DIALOG_CONTROL_GPS:
  119.         {
  120.             if(response)
  121.             {
  122.                 switch(listitem)
  123.                 {
  124.                     case 0:
  125.                     {
  126.                         SendClientMessage(playerid, 0xFFFF00FF, "Please enter the location name to add it in /gps.");
  127.                         ShowPlayerDialog(playerid, DIALOG_ADD_GLOC, DIALOG_STYLE_INPUT, "Add GPS Location", "Please enter the location name to add it in /gps.", "Add", "Cancel");
  128.                     }
  129.                     case 1:
  130.                     {
  131.                         SendClientMessage(playerid, 0xFFFF00FF, "Please enter the location name to remove/delete the location in /gps.");
  132.                         ShowPlayerDialog(playerid, DIALOG_DEL_GLOC, DIALOG_STYLE_INPUT, "Delete GPS Location", "Please enter the location name to remove/delete the location in /gps.", "Delete", "Cancel");
  133.                     }
  134.                     case 2:
  135.                     {
  136.                         SendClientMessage(playerid, 0xFFFF00FF, "Please enter the location name to teleport through /gps location.");
  137.                         ShowPlayerDialog(playerid, DIALOG_TP_GLOC, DIALOG_STYLE_INPUT, "Teleport GPS Location", "Please enter the location name to teleport through /gps location.", "Teleport", "Cancel");
  138.                     }
  139.                     case 3:
  140.                     {
  141.                         return cmd_gps(playerid, "");
  142.                     }
  143.                     case 4:
  144.                     {
  145.                         if (GPSMarker[playerid] == 0) return SendClientMessage(playerid, 0xFF0000FF, "Error: Your GPS is already turned off!");
  146.                         DestroyDynamicMapIcon(GPSMarker[playerid]);
  147.                         GPSMarker[playerid] = 0;
  148.                         SendClientMessage(playerid, 0xFFFF00FF, "You have turned off your GPS.");
  149.                     }
  150.                 }
  151.             }
  152.             return 1;
  153.         }
  154.         case DIALOG_ADD_GLOC:
  155.         {
  156.             if(response)
  157.             {
  158.                 gInfo[playerid][Interior] = GetPlayerInterior(playerid);
  159.                 GetPlayerPos(playerid, gInfo[playerid][Pos][0], gInfo[playerid][Pos][1], gInfo[playerid][Pos][2]);
  160.                 mysql_format(GPSDB, Query, sizeof(Query), "SELECT * FROM `gpsdb` WHERE `LocationName` = '%e'", inputtext);
  161.                    
  162.                 new Cache:result = mysql_query(GPSDB, Query, true);
  163.                 mysql_escape_string(inputtext, gInfo[playerid][LocName]);
  164.                 if(cache_num_rows()) return SendClientMessage(playerid, 0xFF0000FF, "Error: That location name is already added.");
  165.                 mysql_format(GPSDB, Query2, sizeof(Query2), "INSERT INTO `gpsdb` (`LocationName`, `PositionX` , `PositionY` , `PositionZ`, `InteriorID`) VALUES ('%s', '%f', '%f', '%f', '%d')", inputtext, gInfo[playerid][Pos][0], gInfo[playerid][Pos][1], gInfo[playerid][Pos][2], gInfo[playerid][Interior]);
  166.                 mysql_query(GPSDB, Query2, false);
  167.                    
  168.                 format(string, sizeof(string), "You have added location: %s in /gps.", inputtext);
  169.                 SendClientMessage(playerid, 0xFFFF00FF, string);
  170.                 cache_delete(result);
  171.             }
  172.             return 1;
  173.         }
  174.         case DIALOG_DEL_GLOC:
  175.         {
  176.             if(response)
  177.             {
  178.                 mysql_format(GPSDB, Query, sizeof(Query),"DELETE FROM `gpsdb` WHERE `LocationName` = '%e'", inputtext);
  179.                 mysql_query(GPSDB, Query, false);
  180.                
  181.                 format(string, sizeof(string), "You have removed gps location: {F3FF02}%s", inputtext);
  182.                 SendClientMessage(playerid, 0xFFFF00FF, string);
  183.             }
  184.             return 1;
  185.         }
  186.         case DIALOG_TP_GLOC:
  187.         {
  188.             if(response)
  189.             {
  190.                 mysql_format(GPSDB, Query, sizeof(Query), "SELECT * FROM `gpsdb` WHERE `LocationName` = '%e'", inputtext);
  191.                 new Cache:result = mysql_query(GPSDB, Query, true);
  192.                 if(cache_num_rows())
  193.                 {
  194.                     cache_get_value_name(0, "LocationName", gInfo[playerid][LocName]);
  195.                     cache_get_value_name_float(0, "PositionX", gInfo[playerid][Pos][0]);
  196.                     cache_get_value_name_float(0, "PositionY", gInfo[playerid][Pos][1]);
  197.                     cache_get_value_name_float(0, "PositionZ", gInfo[playerid][Pos][2]);
  198.                     cache_get_value_name_int(0, "InteriorID", gInfo[playerid][Interior]);
  199.                     SetPlayerPos(playerid, gInfo[playerid][Pos][0], gInfo[playerid][Pos][1], gInfo[playerid][Pos][2]);
  200.                     format(string, sizeof(string), "You have been teleported to gps location: {F3FF02}'%s'", gInfo[playerid][LocName]);
  201.                     SendClientMessage(playerid, 0xFFFF00FF, string);
  202.                 }
  203.                 else SendClientMessage(playerid, 0xFF0000FF,"Error: The location you entered does not exist.");
  204.                 cache_delete(result);
  205.             }
  206.             return 1;
  207.         }
  208.         case DIALOG_GPS:
  209.         {
  210.             if(response)
  211.             {
  212.                 switch(listitem)
  213.                 {
  214.                     case 0:
  215.                     {
  216.                         new Cache:result = mysql_query(GPSDB,"SELECT LocationName, PositionX, PositionY, PositionZ FROM `gpsdb` WHERE `S.No` > -1");
  217.                         if(!cache_num_rows())
  218.                         {
  219.                             cache_delete(result);
  220.                             SendClientMessage(playerid, 0xFF0000FF,"Error: There is no gps locations added yet.");
  221.                             return 1;
  222.                         }
  223.                            
  224.                         for(new i,j = cache_num_rows(); i< j; i++)
  225.                         {
  226.                             cache_get_value_name(i,"LocationName", gInfo[playerid][LocName]);
  227.                             format(string, sizeof(string), "%s%d\t%s\n", string, i, gInfo[playerid][LocName]);
  228.                         }
  229.                         format(string2, sizeof(string2), "S.No\tLocation Name\n%s", string);
  230.                            
  231.                         SendClientMessage(playerid, 0xFFFF00FF, "Please select a location.");
  232.                         ShowPlayerDialog(playerid, DIALOG_GPS_LOC, DIALOG_STYLE_TABLIST_HEADERS, "Please select an option.", string2, "Select", "Cancel");
  233.                         cache_delete(result);
  234.                     }
  235.                     case 1:
  236.                     {
  237.                         if (GPSMarker[playerid] == 0) return SendClientMessage(playerid, 0xFF0000FF, "Error: Your GPS is already turned off!");
  238.                         DestroyDynamicMapIcon(GPSMarker[playerid]);
  239.                         GPSMarker[playerid] = 0;
  240.                         SendClientMessage(playerid, 0xFFFF00FF, "You have turned off your GPS.");
  241.                     }
  242.                 }
  243.             }
  244.             return 1;
  245.         }
  246.         case DIALOG_GPS_LOC:
  247.         {
  248.             if(response)
  249.             {
  250.                 if (GPSMarker[playerid] != 0)
  251.                 {
  252.                     DestroyDynamicMapIcon(GPSMarker[playerid]);
  253.                 }
  254.                 GPSMarker[playerid] = CreateDynamicMapIcon(gInfo[listitem][Pos][0], gInfo[listitem][Pos][1], gInfo[listitem][Pos][2], 41, 0, -1, -1, playerid, 100000.0);
  255.                 Streamer_SetIntData(STREAMER_TYPE_MAP_ICON, GPSMarker[playerid], E_STREAMER_STYLE, MAPICON_GLOBAL);
  256.                 Streamer_Update(playerid);
  257.                 format(string, sizeof(string), "The location: %s, has been marked on your mini-map.", inputtext);
  258.                 SendClientMessage(playerid, 0xFFFF00FF, string);
  259.             }
  260.             return 1;
  261.         }
  262.     }
  263.     return 1;
  264. }
Add Comment
Please, Sign In to add comment