· 9 years ago · Jan 05, 2017, 11:38 AM
1#define FILTERSCRIPT
2
3
4#include <a_samp>
5#include <a_mysql>
6#include <foreach>
7#include <streamer>
8#include <sscanf2>
9#include <CMD>
10
11
12#define MYSQL_HOST "localhost"
13#define MYSQL_USER "root"
14#define MYSQL_DATABASE "labelsys"
15#define MYSQL_PASSWORD ""
16
17
18#define red 0xFF0000FF
19#define green 0x00FF00FF
20#define SCM SendClientMessage
21
22#define MAX_LABELS 1000
23#define LABEL_DISTANCE 20.00
24
25#define DIALOG_CREATE_LABEL 30
26#define DIALOG_EDIT_LABEL 40
27
28enum LabelData
29{
30 ID,
31 Text3D:Label,
32 Text[74],
33 Color,
34 Interior,
35 VirtualWorld,
36 Float:PosX,
37 Float:PosY,
38 Float:PosZ
39};
40new xInfo[MAX_LABELS][LabelData],
41 Iterator:Labels<MAX_LABELS>,
42 MySQL:mysql;
43
44new SelectedLabel[MAX_PLAYERS];
45
46public OnFilterScriptInit() {
47
48 mysql_log(ALL);
49 mysql = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
50 if(mysql_errno() != 0)
51 print("[MySQL] Failed Connection");
52 else
53 print("[MySQL] Successfully Connected");
54
55 mysql_tquery(mysql, "CREATE TABLE IF NOT EXISTS `DynamicLabels` (\
56 `ID` int(5) NOT NULL AUTO_INCREMENT UNIQUE KEY,\
57 `Text` varchar(74) NOT NULL,\
58 `Color` int(8) NOT NULL,\
59 `Interior` int(5) NOT NULL,\
60 `VirtualWorld` int(5) NOT NULL,\
61 `PosX` float NOT NULL,\
62 `PosY` float NOT NULL,\
63 `PosZ` float NOT NULL)");
64
65 mysql_tquery(mysql, "SELECT * FROM `DynamicLabels`", "LoadDynamicLabels" "");
66 return 1;
67}
68
69public OnFilterScriptExit() {
70
71 DestroyAllDynamic3DTextLabels();
72 return 1;
73}
74
75public OnPlayerConnect(playerid) {
76
77 SelectedLabel[playerid] = -1;
78 return 1;
79}
80
81public OnPlayerDisconnect(playerid, reason) {
82
83 SelectedLabel[playerid] = -1;
84 return 1;
85}
86
87public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
88
89 switch(dialogid) {
90
91 case DIALOG_CREATE_LABEL: {
92
93 if(response) {
94
95 switch(listitem) {
96
97 case 0: {
98
99 ShowPlayerDialog(playerid, DIALOG_CREATE_LABEL+1, DIALOG_STYLE_INPUT, "Label Text", "Enter the label text below", "Next", "Close");
100 }
101 case 1: {
102
103 ShowPlayerDialog(playerid, DIALOG_CREATE_LABEL+2, DIALOG_STYLE_INPUT, "Label Color", "Enter the label color below", "Next", "Close");
104 }
105 case 2: {
106
107 ShowPlayerDialog(playerid, DIALOG_CREATE_LABEL+3, DIALOG_STYLE_INPUT, "Label Interior", "Enter the label interior below", "Next", "Close");
108 }
109 case 3: {
110
111 ShowPlayerDialog(playerid, DIALOG_CREATE_LABEL+4, DIALOG_STYLE_INPUT, "Label Virtual World", "Enter the label virtual world below", "Next", "Close");
112 }
113 case 4: {
114
115 new id = Iter_Free(Labels);
116 if(id == -1) return SCM(playerid, red, "You can't create more labels");
117 if(strlen(xInfo[id][Text]) < 1) return SCM(playerid, red, "Please enter a valid text");
118 if(IsPlayerInAnyVehicle(playerid)) return SCM(playerid, red, "You must be on foot to create the label");
119
120 GetPlayerPos(playerid, xInfo[id][PosX], xInfo[id][PosY], xInfo[id][PosZ]);
121
122 xInfo[id][Label] = CreateDynamic3DTextLabel(xInfo[id][Text], xInfo[id][Color], xInfo[id][PosX], xInfo[id][PosY],
123 xInfo[id][PosZ], LABEL_DISTANCE, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, xInfo[id][VirtualWorld], xInfo[id][Interior]);
124
125 new query[200];
126 format(query, sizeof(query), "You have created a new label with ID: %i", id);
127 SCM(playerid, green, query);
128
129 mysql_format(mysql, query, sizeof(query), "INSERT INTO `DynamicLabels` (Text, Color, VirtualWorld, Interior, PosX, PosY, PosZ) \
130 VALUES ('%e', %i, %i, %i, %f, %f, %f)", xInfo[id][Text], xInfo[id][Color], xInfo[id][VirtualWorld], xInfo[id][Interior], xInfo[id][PosX],
131 xInfo[id][PosY], xInfo[id][PosZ]);
132 mysql_tquery(mysql, query, "OnLabelCreated", "i", id);
133 }
134 }
135 }
136 }
137 case DIALOG_CREATE_LABEL+1: {
138
139 if(response) {
140
141 new id = Iter_Free(Labels);
142 if(isnull(inputtext)) return SCM(playerid, red, "Please enter a valid text");
143
144 format(xInfo[id][Text], 74, inputtext);
145 FilterNewLines(inputtext, strlen(inputtext));
146 UpdateDynamic3DTextLabelText(xInfo[id][Label], xInfo[id][Color], inputtext);
147
148 cmd_createlabel(playerid, "");
149 }
150 }
151 case DIALOG_CREATE_LABEL+2: {
152
153 if(response) {
154
155 new id = Iter_Free(Labels);
156 if(strlen(inputtext) != 6) return SCM(playerid, red, "Invalid hex color");
157
158 new color;
159 if (sscanf(inputtext, "x", color)) return SCM(playerid, red, "Please enter a valid color (RRGGBB)");
160
161 color = (color << 8) | 0xFF;
162
163 xInfo[id][Color] = color;
164 cmd_createlabel(playerid, "");
165 }
166 }
167 case DIALOG_CREATE_LABEL+3: {
168
169 if(response) {
170
171 new id = Iter_Free(Labels);
172 if(!IsNumeric(inputtext)) return SCM(playerid, red, "You have entered an invalid interior ID");
173
174 xInfo[id][Interior] = strval(inputtext);
175 cmd_createlabel(playerid, "");
176 }
177 }
178 case DIALOG_CREATE_LABEL+4: {
179
180 if(response) {
181
182 new id = Iter_Free(Labels);
183 if(!IsNumeric(inputtext)) return SCM(playerid, red, "You have entered an invalid virtual world ID");
184 if(strval(inputtext) == -1) return SCM(playerid, red, "You have entered an invalid virtual world ID");
185
186 xInfo[id][VirtualWorld] = strval(inputtext);
187 cmd_createlabel(playerid, "");
188 }
189 }
190 case DIALOG_EDIT_LABEL: {
191
192 if(response) {
193
194 switch(listitem) {
195
196 case 0: {
197
198 ShowPlayerDialog(playerid, DIALOG_EDIT_LABEL+1, DIALOG_STYLE_INPUT, "Label Text", "Enter the label text below", "Next", "Close");
199 }
200 case 1: {
201
202 ShowPlayerDialog(playerid, DIALOG_EDIT_LABEL+2, DIALOG_STYLE_INPUT, "Label Color", "Enter the label color below", "Next", "Close");
203 }
204 case 2: {
205
206 ShowPlayerDialog(playerid, DIALOG_EDIT_LABEL+3, DIALOG_STYLE_INPUT, "Label Interior", "Enter the label interior below", "Next", "Close");
207 }
208 case 3: {
209
210 ShowPlayerDialog(playerid, DIALOG_EDIT_LABEL+4, DIALOG_STYLE_INPUT, "Label Virtual World", "Enter the label virtual world below", "Next", "Close");
211 }
212 case 4: {
213
214 new id = SelectedLabel[playerid];
215 SetPlayerPos(playerid, xInfo[id][PosX], xInfo[id][PosY], xInfo[id][PosZ]);
216 SetPlayerInterior(playerid, xInfo[id][Interior]);
217 SetPlayerVirtualWorld(playerid, xInfo[id][VirtualWorld]);
218 }
219 case 5: {
220
221 new id = SelectedLabel[playerid];
222 DestroyDynamic3DTextLabel(xInfo[id][Label]);
223
224 GetPlayerPos(playerid, xInfo[id][PosX], xInfo[id][PosY], xInfo[id][PosZ]);
225
226 xInfo[id][Label] = CreateDynamic3DTextLabel(xInfo[id][Text], xInfo[id][Color], xInfo[id][PosX], xInfo[id][PosY],
227 xInfo[id][PosZ], LABEL_DISTANCE, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, xInfo[id][VirtualWorld], xInfo[id][Interior]);
228 }
229 case 6: {
230
231 new id = SelectedLabel[playerid];
232 DestroyDynamic3DTextLabel(xInfo[id][Label]);
233 Iter_Remove(Labels, id);
234
235 new query[60];
236 mysql_format(mysql, query, sizeof(query), "DELETE FROM `DynamicLabels` WHERE `ID` = %i", xInfo[id][ID]);
237 mysql_tquery(mysql, query);
238 }
239 }
240 }
241 }
242 case DIALOG_EDIT_LABEL+1: {
243
244 if(response) {
245
246 new id = SelectedLabel[playerid];
247 if(isnull(inputtext)) return SCM(playerid, red, "Please enter a valid text");
248
249 format(xInfo[id][Text], 74, inputtext);
250 FilterNewLines(inputtext, strlen(inputtext));
251 UpdateDynamic3DTextLabelText(xInfo[id][Label], xInfo[id][Color], inputtext);
252
253 new strcmd[5];
254 format(strcmd, sizeof(strcmd), "%i", id);
255 cmd_editlabel(playerid, strcmd);
256 }
257 }
258 case DIALOG_EDIT_LABEL+2: {
259
260 if(response) {
261
262 new id = SelectedLabel[playerid];
263
264 if(strlen(inputtext) != 6) return SCM(playerid, red, "Invalid hex color");
265
266 new color;
267 if (sscanf(inputtext, "x", color)) return SCM(playerid, red, "Please enter a valid color (RRGGBB)");
268
269 color = (color << 8) | 0xFF;
270
271 xInfo[id][Color] = color;
272 UpdateDynamic3DTextLabelText(xInfo[id][Label], color, xInfo[id][Text]);
273
274 new strcmd[5];
275 format(strcmd, sizeof(strcmd), "%i", id);
276 cmd_editlabel(playerid, strcmd);
277 }
278 }
279 case DIALOG_EDIT_LABEL+3: {
280
281 if(response) {
282
283 if(!IsNumeric(inputtext)) return SCM(playerid, red, "You have entered an invalid interior ID");
284
285 new id = SelectedLabel[playerid];
286 DestroyDynamic3DTextLabel(xInfo[id][Label]);
287
288 GetPlayerPos(playerid, xInfo[id][PosX], xInfo[id][PosY], xInfo[id][PosZ]);
289
290 xInfo[id][Label] = CreateDynamic3DTextLabel(xInfo[id][Text], xInfo[id][Color], xInfo[id][PosX], xInfo[id][PosY],
291 xInfo[id][PosZ], LABEL_DISTANCE, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, xInfo[id][VirtualWorld], strval(inputtext));
292
293 new strcmd[5];
294 format(strcmd, sizeof(strcmd), "%i", id);
295 cmd_editlabel(playerid, strcmd);
296 }
297 }
298 case DIALOG_EDIT_LABEL+4: {
299
300 if(response) {
301
302 if(!IsNumeric(inputtext)) return SCM(playerid, red, "You have entered an invalid virtual world ID");
303 if(strval(inputtext) == -1) return SCM(playerid, red, "You have entered an invalid virtual world ID");
304
305 new id = SelectedLabel[playerid];
306 DestroyDynamic3DTextLabel(xInfo[id][Label]);
307
308 GetPlayerPos(playerid, xInfo[id][PosX], xInfo[id][PosY], xInfo[id][PosZ]);
309
310 xInfo[id][Label] = CreateDynamic3DTextLabel(xInfo[id][Text], xInfo[id][Color], xInfo[id][PosX], xInfo[id][PosY],
311 xInfo[id][PosZ], LABEL_DISTANCE, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, strval(inputtext), xInfo[id][Interior]);
312
313 new strcmd[5];
314 format(strcmd, sizeof(strcmd), "%i", id);
315 cmd_editlabel(playerid, strcmd);
316 }
317 }
318 }
319 return 0;
320}
321
322forward OnLabelCreated(labelid);
323public OnLabelCreated(labelid) {
324
325 xInfo[labelid][ID] = cache_insert_id();
326 Iter_Add(Labels, labelid);
327 return 1;
328}
329
330forward LoadDynamicLabels();
331public LoadDynamicLabels() {
332
333 new rows = cache_num_rows();
334
335 if(rows) {
336
337 for(new i; i < rows; i++) {
338
339 new id = Iter_Free(Labels);
340
341 cache_get_value_name_int(i, "ID", xInfo[id][ID]);
342 cache_get_value_name(i, "Text", xInfo[id][Text]);
343 cache_get_value_name_int(i, "Color", xInfo[id][Color]);
344 cache_get_value_name_int(i, "Interior", xInfo[id][Interior]);
345 cache_get_value_name_int(i, "VirtualWorld", xInfo[id][VirtualWorld]);
346 cache_get_value_name_float(i, "PosX", xInfo[id][PosX]);
347 cache_get_value_name_float(i, "PosY", xInfo[id][PosY]);
348 cache_get_value_name_float(i, "PosZ", xInfo[id][PosZ]);
349
350 FilterNewLines(xInfo[id][Text], strlen(xInfo[id][Text]));
351
352 xInfo[id][Label] = CreateDynamic3DTextLabel(xInfo[id][Text], xInfo[id][Color], xInfo[id][PosX], xInfo[id][PosY],
353 xInfo[id][PosZ], LABEL_DISTANCE, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, xInfo[id][VirtualWorld], xInfo[id][Interior]);
354
355 Iter_Add(Labels, id);
356 }
357 printf("Loaded %i dynamic labels", rows);
358 }
359}
360
361CMD:createlabel(playerid, params[]) {
362
363 if(!IsPlayerAdmin(playerid)) return SCM(playerid, red, "You must be an administrator to use this command");
364
365 new id = Iter_Free(Labels);
366
367 xInfo[id][Interior] = 0;
368 xInfo[id][VirtualWorld] = 0;
369
370 new string[300];
371 format(string, sizeof(string), "{FFFFFF}Text: {00FF00}%s\n{FFFFFF}Color: {00FF00}%x\n{FFFFFF}Interior: {00FF00}%i\n{FFFFFF}Virtual World: {00FF00}%i\n{00FF00}Create Label",
372 xInfo[id][Text], xInfo[id][Color], xInfo[id][Interior], xInfo[id][VirtualWorld]);
373
374 ShowPlayerDialog(playerid, DIALOG_CREATE_LABEL, DIALOG_STYLE_LIST, "Create 3D Label", string, "Select", "Cancel");
375 return 1;
376}
377
378CMD:editlabel(playerid, params[]) {
379
380 if(!IsPlayerAdmin(playerid)) return SCM(playerid, red, "You must be an administrator to use this command");
381
382 new id;
383 if(sscanf(params, "i", id)) return SCM(playerid, red, "Edit label: /editlabel <ID>");
384 if(!Iter_Contains(Labels, id)) return SCM(playerid, red, "Invalid label ID");
385
386 SelectedLabel[playerid] = id;
387
388 new string[300];
389 format(string, sizeof(string), "{FFFFFF}Text: {00FF00}%s\n{FFFFFF}Color: {00FF00}%x\n{FFFFFF}Interior: {00FF00}%i\n{FFFFFF}Virtual World: {00FF00}%i\n\
390 {00FF00}Teleport to Label\nMove Label\n{FF0000}Destroy Label", xInfo[id][Text], xInfo[id][Color], xInfo[id][Interior], xInfo[id][VirtualWorld]);
391
392 ShowPlayerDialog(playerid, DIALOG_EDIT_LABEL, DIALOG_STYLE_LIST, "Edit 3D Label", string, "Select", "Cancel");
393 return 1;
394}
395
396CMD:labels(playerid, params[]) {
397
398 if(!IsPlayerAdmin(playerid)) return SCM(playerid, red, "You must be an administrator to use this command");
399 if(Iter_Count(Labels) == 0) return SCM(playerid, red, "There are no labels currently");
400
401 new string[128], s[300];
402 foreach(new i : Labels) {
403
404 format(string, sizeof(string), "{FFFFFF}ID: {00FF00}%i {FFFFFF}Text: {00FF00}%s\n", i, xInfo[i][Text]);
405 strcat(s, string);
406 }
407
408 ShowPlayerDialog(playerid, 1, DIALOG_STYLE_LIST, "3D Labels", s, "Close", "");
409 return 1;
410}
411
412IsNumeric(string[])
413{
414 for (new i = 0, j = strlen(string); i < j; i++)
415 {
416 if (string[i] > '9' || string[i] < '0') return 0;
417 }
418 return 1;
419}
420
421FilterNewLines(text[], length) // Credits to Konstantinos
422{
423 for (new i; i != length; i++)
424 {
425 if (text[i] == '\\' && i != length - 1 && text[i + 1] == 'n')
426 {
427 text[i] = ' ';
428 text[i + 1] = '\n';
429 i++;
430 }
431 }
432}