· 10 years ago · Sep 23, 2016, 07:24 PM
1#include <sourcemod>
2#include <basecomm>
3#include <colorvariables>
4#include <sdktools_functions>
5
6#define PLUGIN_VERSION "2.1"
7#define MAX_COLOURS 16
8
9//Handles
10Handle g_hDatabase = null;
11
12//Strings
13char g_sError[120];
14char g_sVipTagString[MAXPLAYERS + 1][256];
15char g_sVipNameColourString[MAXPLAYERS + 1][16];
16char g_sVipTextColourString[MAXPLAYERS + 1][16];
17
18//Booleans
19bool g_bDatabaseReady;
20bool g_bTagLoaded[MAXPLAYERS + 1];
21
22//Integars
23int g_iSelectedElement[MAXPLAYERS + 1];
24
25//Strings
26char sColors[MAX_COLOURS][] =
27{
28 "{default}", "{red}", "{lightred}", "{darkred}", "{bluegrey}", "{blue}",
29 "{darkblue}", "{purple}", "{orchid}", "{yellow}", "{gold}", "{lightgreen}",
30 "{green}", "{lime}", "{grey}", "{grey2}"
31};
32
33public Plugin myinfo =
34{
35 name = "Custom Vip Tags",
36 author = "kriss",
37 description = "Set custom tag prefix'",
38 version = PLUGIN_VERSION,
39 url = "www.extreme-network.net"
40};
41
42public void OnPluginStart()
43{
44 DatabaseConnect();
45
46 LoadTranslations("common.phrases");
47
48 RegAdminCmd("viptag", Command_VipTag, ADMFLAG_CUSTOM6, "Sets a custom tag prefix in the chat.");
49 RegAdminCmd("vipcolours", Command_VipColours, ADMFLAG_CUSTOM6, "Displays a menu to pick your colours.");
50
51 if(g_bDatabaseReady)
52 {
53 LoadAllClientTags();
54
55 PrintToChatAll("[Vip Tags] All custom tags have been loaded.");
56 }
57 else
58 {
59 PrintToChatAll("[Vip Tags] Failed loading tags, database handle is invalid.");
60 }
61}
62
63public void OnClientDisconnect(int client)
64{
65 g_bTagLoaded[client] = false;
66
67 strcopy(g_sVipNameColourString[client], 16, "{default}");
68 strcopy(g_sVipTextColourString[client], 16, "{default}");
69}
70
71public Action OnClientSayCommand(int client, const char[] command, const char[] args)
72{
73 if(g_bTagLoaded[client])
74 {
75 if(args[0] == '@' || args[0] == '/' || BaseComm_IsClientGagged(client))
76 {
77 return Plugin_Continue;
78 }
79
80 if(StrEqual(command, "say"))
81 {
82 switch(GetClientTeam(client))
83 {
84 case 2:
85 {
86 CPrintToChatAll("%s{default}[%s{default}] %s%N{default}: %s%s", !IsPlayerAlive(client) ? "{yellow}*DEAD* " : "", g_sVipTagString[client], g_sVipNameColourString[client], client, g_sVipTextColourString[client], args);
87 }
88 case 3:
89 {
90 CPrintToChatAll("%s{default}[%s{default}] %s%N{default}: %s%s", !IsPlayerAlive(client) ? "{bluegrey}*DEAD* " : "", g_sVipTagString[client], g_sVipNameColourString[client], client, g_sVipTextColourString[client], args);
91 }
92 }
93 }
94 if(StrEqual(command, "say_team"))
95 {
96 for(int i = 1; i <= MaxClients; i++)
97 {
98 if(IsValidClient(i))
99 {
100 if(GetClientTeam(client) == GetClientTeam(i))
101 {
102 switch(GetClientTeam(client))
103 {
104 case 1:
105 {
106 CPrintToChat(i, "{defualt}(Spectators) {default}[%s{default}] %s%N{default}: %s%s", g_sVipTagString[client], g_sVipNameColourString[client], client, g_sVipTextColourString[client], args);
107 }
108 case 2:
109 {
110 CPrintToChat(i, "{yellow}%s(Terrorists) {default}[%s{default}] %s%N{default}: %s%s", !IsPlayerAlive(client) ? "*DEAD*" : "", g_sVipTagString[client], g_sVipNameColourString[client], client,g_sVipTextColourString[client], args);
111 }
112 case 3:
113 {
114 CPrintToChat(i, "{bluegrey}%s(Counter-Terrorists) {default}[%s{default}] %s%N{default}: %s%s", !IsPlayerAlive(client) ? "*DEAD*" : "", g_sVipTagString[client], g_sVipNameColourString[client], client, g_sVipTextColourString[client], args);
115 }
116 }
117 }
118 }
119 }
120 }
121
122 PrintToServer("%L: %s", client, args);
123
124 return Plugin_Handled;
125 }
126
127 return Plugin_Continue;
128}
129
130public Action Timer_LoadTag(Handle timer, int client)
131{
132 if(g_bDatabaseReady)
133 {
134 if(LoadClientTag(client))
135 {
136 g_bTagLoaded[client] = true;
137 }
138 }
139}
140
141public Action Command_VipTag(int client, int args)
142{
143 if(IsValidClient(client))
144 {
145 if(g_bDatabaseReady)
146 {
147 if(args < 1)
148 {
149 if(g_bTagLoaded[client])
150 {
151 char sQuery[256], sAuthId[32];
152 GetClientAuthId(client, AuthId_SteamID64, sAuthId, sizeof(sAuthId), true);
153 Format(sQuery, sizeof(sQuery), "DELETE FROM VipTags WHERE steamid = '%s'", sAuthId);
154
155 if(!SQL_FastQuery(g_hDatabase, sQuery))
156 {
157 SQL_GetError(g_hDatabase, g_sError, sizeof(g_sError));
158 ReplyToCommand(client, "[Vip Tags] Failed removing tag from database. (Error: %s)", g_sError);
159 }
160 else
161 {
162 g_bTagLoaded[client] = false;
163
164 strcopy(g_sVipNameColourString[client], 16, "{default}");
165 strcopy(g_sVipTextColourString[client], 16, "{default}");
166
167 ReplyToCommand(client, "[Vip Tags] Your current tag has been removed.");
168 }
169 }
170 else
171 {
172 ReplyToCommand(client, "[Vip Tags] Usage: viptag <tag format>");
173 }
174
175 return Plugin_Handled;
176 }
177
178 char sArg[256];
179 GetCmdArgString(sArg, sizeof(sArg));
180
181 if(GetTrueTagLength(sArg) > 24)
182 {
183 ReplyToCommand(client, "[Vip Tags] The entered tag exceeds the size limit");
184 return Plugin_Handled;
185 }
186
187 if(!g_bTagLoaded[client])
188 {
189 char sQuery[256], sAuthId[32];
190 GetClientAuthId(client, AuthId_SteamID64, sAuthId, sizeof(sAuthId), true);
191 Format(sQuery, sizeof(sQuery), "INSERT INTO VipTags (steamid, tagformat) VALUES ('%s', \"%s\")", sAuthId, sArg);
192
193 if(!SQL_FastQuery(g_hDatabase, sQuery))
194 {
195 SQL_GetError(g_hDatabase, g_sError, sizeof(g_sError));
196 ReplyToCommand(client, "[Vip Tags] Failed saving tag to database. (Error: %s)", g_sError);
197 }
198 else
199 {
200 strcopy(g_sVipTagString[client], 256, sArg);
201
202 g_bTagLoaded[client] = true;
203
204 ReplyToCommand(client, "[Vip Tags] Your vip tag has been saved and set.");
205 }
206 }
207 else
208 {
209 char sQuery[256], sAuthId[32];
210 GetClientAuthId(client, AuthId_SteamID64, sAuthId, sizeof(sAuthId), true);
211 Format(sQuery, sizeof(sQuery), "UPDATE VipTags SET tagformat = \"%s\" WHERE steamid = '%s'", sArg, sAuthId);
212
213 if(!SQL_FastQuery(g_hDatabase, sQuery))
214 {
215 SQL_GetError(g_hDatabase, g_sError, sizeof(g_sError));
216 ReplyToCommand(client, "[Vip Tags] Failed updating tag to database. (Error: %s)", g_sError);
217 }
218 else
219 {
220 strcopy(g_sVipTagString[client], 256, sArg);
221
222 ReplyToCommand(client, "[Vip Tags] Your vip tag has been saved and updated.");
223 }
224 }
225 }
226 else
227 {
228 ReplyToCommand(client, "[Vip Tags] This command cannot be used since there is no database connection.");
229 }
230 }
231
232 return Plugin_Handled;
233}
234
235public Action Command_VipColours(int client, int args)
236{
237 if(IsValidClient(client))
238 {
239 if(g_bTagLoaded[client])
240 {
241 Menu menu = new Menu(MenuHandler_VipColours);
242
243 menu.SetTitle("Vip Colours Menu");
244
245 menu.AddItem("1", "Set Name Colour");
246 menu.AddItem("2", "Set Chat Colour");
247
248 menu.Display(client, MENU_TIME_FOREVER);
249 }
250 else
251 {
252 ReplyToCommand(client, "[SM] You must have a custom tag active to use this command.");
253 }
254 }
255
256 return Plugin_Handled;
257}
258
259public int MenuHandler_VipColours(Menu menu, MenuAction action, int param1, int param2)
260{
261 switch(action)
262 {
263 case MenuAction_Select:
264 {
265 char sInfo[32];
266 menu.GetItem(param2, sInfo, sizeof(sInfo));
267
268 g_iSelectedElement[param1] = StringToInt(sInfo);
269
270 SendColoursMenu(param1);
271 }
272
273 case MenuAction_End: delete menu;
274 }
275}
276
277public void SendColoursMenu(int client)
278{
279 if(IsValidClient(client))
280 {
281 Menu menu = new Menu(MenuHandler_Colours);
282
283 menu.SetTitle("Set %s Colour", g_iSelectedElement[client] == 1 ? "Name" : "Chat");
284
285 menu.AddItem("{default}", "Default");
286 menu.AddItem("{red}", "Red");
287 menu.AddItem("{lightred}", "Lightred");
288 menu.AddItem("{darkred}", "Darkred");
289 menu.AddItem("{bluegrey}", "Bluegrey");
290 menu.AddItem("{blue}", "Blue");
291 menu.AddItem("{darkblue}", "Darkblue");
292 menu.AddItem("{purple}", "Purple");
293 menu.AddItem("{orchid}", "Orchid");
294 menu.AddItem("{yellow}", "Yellow");
295 menu.AddItem("{gold}", "Gold");
296 menu.AddItem("{lightgreen}", "Lightgreen");
297 menu.AddItem("{green}", "Green");
298 menu.AddItem("{lime}", "Lime");
299 menu.AddItem("{grey}", "Grey");
300 menu.AddItem("{grey2}", "Grey2");
301
302 menu.Display(client, MENU_TIME_FOREVER);
303 }
304}
305
306public int MenuHandler_Colours(Menu menu, MenuAction action, int param1, int param2)
307{
308 switch(action)
309 {
310 case MenuAction_Select:
311 {
312 char sInfo[32];
313 menu.GetItem(param2, sInfo, sizeof(sInfo));
314
315 if(g_iSelectedElement[param1] == 1)
316 {
317 char sQuery[256], sAuthId[32];
318 GetClientAuthId(param1, AuthId_SteamID64, sAuthId, sizeof(sAuthId), true);
319 Format(sQuery, sizeof(sQuery), "UPDATE VipTags SET namecolour = '%s' WHERE steamid = '%s'", sInfo, sAuthId);
320
321 if(!SQL_FastQuery(g_hDatabase, sQuery))
322 {
323 SQL_GetError(g_hDatabase, g_sError, sizeof(g_sError));
324 PrintToChat(param1, "[Vip Tags] Failed saving name colour to database. (Error: %s)", g_sError);
325 }
326 else
327 {
328 strcopy(g_sVipNameColourString[param1], 16, sInfo);
329
330 PrintToChat(param1, "[Vip Tags] Your name colour has been saved.");
331 }
332 }
333 else if(g_iSelectedElement[param1] == 2)
334 {
335 char sQuery[256], sAuthId[32];
336 GetClientAuthId(param1, AuthId_SteamID64, sAuthId, sizeof(sAuthId), true);
337 Format(sQuery, sizeof(sQuery), "UPDATE VipTags SET textcolour = '%s' WHERE steamid = '%s'", sInfo, sAuthId);
338
339 if(!SQL_FastQuery(g_hDatabase, sQuery))
340 {
341 SQL_GetError(g_hDatabase, g_sError, sizeof(g_sError));
342 PrintToChat(param1, "[Vip Tags] Failed saving chat colour to database. (Error: %s)", g_sError);
343 }
344 else
345 {
346 strcopy(g_sVipTextColourString[param1], 16, sInfo);
347
348 PrintToChat(param1, "[Vip Tags] Your chat colour has been saved.");
349 }
350 }
351 }
352
353 case MenuAction_End: delete menu;
354 }
355}
356
357static void DatabaseConnect()
358{
359 g_hDatabase = SQL_Connect("vip-tags", true, g_sError, sizeof(g_sError));
360
361 if(g_hDatabase == null)
362 {
363 delete g_hDatabase;
364
365 g_bDatabaseReady = false;
366
367 SetFailState("[Vip Tags] Failed on connection to database: %s", g_sError);
368 }
369 else
370 {
371 char query[512];
372 Format(query, sizeof(query), "CREATE TABLE IF NOT EXISTS `VipTags` (`steamid` varchar(32) NOT NULL, `tagformat` varchar(256) NOT NULL, `namecolour` varchar(16) NOT NULL DEFAULT '{default}', `textcolour` varchar(16) NOT NULL DEFAULT '{default}', UNIQUE KEY `steamid` (`steamid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
373
374 if (!SQL_FastQuery(g_hDatabase, query))
375 {
376 SQL_GetError(g_hDatabase, g_sError, sizeof(g_sError));
377 PrintToServer("[Vip Tags] Failed creating database tables (error: %s)", g_sError);
378 }
379 else
380 {
381 g_bDatabaseReady = true;
382 }
383 }
384}
385
386static bool LoadClientTag(int client)
387{
388 if(g_bDatabaseReady && IsValidClient(client))
389 {
390 Handle hResult;
391 char sQuery[256], sAuthId[32];
392 GetClientAuthId(client, AuthId_SteamID64, sAuthId, sizeof(sAuthId), true);
393 Format(sQuery, sizeof(sQuery), "SELECT tagformat, namecolour, textcolour FROM VipTags WHERE steamid = '%s'", sAuthId);
394
395 hResult = SQL_Query(g_hDatabase, sQuery);
396 if(hResult == null)
397 {
398 SQL_GetError(g_hDatabase, g_sError, sizeof(g_sError));
399 PrintToServer("[Vip Tags] Failed to query (error: %s)", g_sError);
400 }
401 else
402 {
403 if(SQL_FetchRow(hResult))
404 {
405 if(SQL_FetchString(hResult, 0, g_sVipTagString[client], 256) > 0)
406 {
407 SQL_FetchString(hResult, 1, g_sVipNameColourString[client], 16);
408 SQL_FetchString(hResult, 2, g_sVipTextColourString[client], 16);
409
410 return true;
411 }
412 }
413 }
414
415 delete hResult;
416 }
417
418 return false;
419}
420
421static void LoadAllClientTags()
422{
423 for(int i = 1; i <= MaxClients; i++)
424 {
425 if(LoadClientTag(i))
426 {
427 g_bTagLoaded[i] = true;
428 }
429 }
430}
431
432stock int GetTrueTagLength(const char[] tag)
433{
434 char sArgFiltered[256];
435 strcopy(sArgFiltered, sizeof(sArgFiltered), tag);
436
437 for(int i; i < MAX_COLOURS; i++)
438 {
439 if(StrContains(sArgFiltered, sColors[i], false) != -1)
440 {
441 ReplaceString(sArgFiltered, sizeof(sArgFiltered), sColors[i], "", false);
442 }
443 }
444
445 return strlen(sArgFiltered);
446}
447
448stock bool IsValidClient(int client)
449{
450 return (client >= 1 && client <= MaxClients && IsClientConnected(client) && IsClientInGame(client) && !IsClientSourceTV(client));
451}