CaptainB

Dynamic MySQL GPS System 1.2

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