CaptainB

Dynamic MySQL GPS System

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