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