· 9 years ago · Jan 01, 2017, 11:54 PM
1#include <sdkhooks>
2#include <sdktools>
3#include <csgocolors>
4#include <emitsoundany>
5#define MAX_HITSOUNDS 65
6
7//
8#define SOUND_AMBIENT_CHANNEL 8
9
10new Handle:DB;
11new Handle:g_ChatCommands;
12new Handle:g_AccessFlag;
13//
14enum Hitsound_Info
15{
16 String:Name[256],
17 String:Path[256]
18}
19
20enum USER_SETTINGS
21{
22 Index,
23 Volume
24}
25
26new Hitsounds[MAX_HITSOUNDS][Hitsound_Info];
27new UserSettings[MAXPLAYERS + 1][USER_SETTINGS];
28new String:g_menuCommands[32][32];
29new bool:User_Bool[MAXPLAYERS + 1];
30
31public Plugin:myinfo =
32{
33 name = "PT'Fun Hitsounds",
34 author = "Erroler",
35 description = "",
36 version = "13.37",
37 url = "ptfun.net"
38}
39
40public OnPluginStart()
41{
42 SQL_TConnect(OnDatabaseConnect, "sm_hitsounds");
43 g_ChatCommands = CreateConVar("sm_hs_chatcommands", "!hitsound /hitsound !hitsounds /hitsounds !sonsdano /sonsdano" , "String of text players must type to open the hitsound menu");
44 g_AccessFlag = CreateConVar("sm_hs_accessflag", "o" , "Flag required to open hitsounds menu")
45 AddCommandListener(Command_Say, "say");
46 AddCommandListener(Command_Say, "say_team");
47 LoadTranslations("hitsound.phrases");
48 for (new i = 1 ; i <= MaxClients; i++)
49 {
50 if(IsClientConnected(i) && IsClientInGame(i)) OnClientAuthorized(i, "");
51 }
52}
53
54public OnDatabaseConnect(Handle:owner, Handle:hndl, const String:error[], any:data)
55{
56 if(hndl == INVALID_HANDLE || strlen(error) > 0)
57 {
58 PrintToServer("Error connecting to database: %s", error);
59 SetFailState("Lost connection to database. Reconnecting on map change.");
60 }
61 DB = INVALID_HANDLE;
62 DB = hndl;
63 new String:qweryy[] = "CREATE TABLE IF NOT EXISTS `user_settings` ( `SteamID` varchar(40) NOT NULL, `pindex` int(4) NOT NULL DEFAULT '64', `volume` int(4) NOT NULL DEFAULT '75', `prop` int(4) NOT NULL DEFAULT '0', PRIMARY KEY (`SteamID`), UNIQUE KEY `SteamID` (`SteamID`))";
64 SQL_TQuery(DB, SQL_DoNothing, qweryy);
65}
66
67public OnConfigsExecuted()
68{
69 new String:saychat[128];
70 GetConVarString(g_ChatCommands, saychat , sizeof(saychat));
71 ExplodeString(saychat, " ", g_menuCommands, sizeof(g_menuCommands), sizeof(g_menuCommands[]));
72}
73
74public Action:Command_Say(client, const String:command[], args)
75{
76 if (client <= 0 || client > MaxClients || !IsClientInGame(client))
77 return Plugin_Continue;
78
79 decl String:text[256];
80 GetCmdArgString(text, sizeof(text));
81 StripQuotes(text);
82
83 for (new index = 0; index < sizeof(g_menuCommands); index++)
84 {
85 if(StrEqual(" ", text) || StrEqual("", text))
86 {
87 return Plugin_Handled;
88 }
89 if (StrEqual(g_menuCommands[index], text))
90 {
91 new String:flag[2];
92 GetConVarString(g_AccessFlag, flag, sizeof(flag))
93 if(flag[0] == '\0') Open_Interface(client);
94 new chat_flag;
95 chat_flag = ReadFlagString(flag);
96 if(GetUserFlagBits(client) & chat_flag) Open_Interface(client);
97 else (CPrintToChatEx(client,client, "%t", "NotAllowedToAcessMainMenu"));
98 return Plugin_Handled;
99 }
100 }
101
102 return Plugin_Continue;
103}
104
105public OnMapStart()
106{
107 new x = 0;
108 decl String:path_cfg[512];
109 BuildPath(Path_SM, path_cfg, 512, "configs/hitsounds/hitsounds.ini");
110 if (FileExists(path_cfg))
111 {
112 new Handle:file = OpenFile(path_cfg, "r");
113 decl String:line[128];
114 while(!IsEndOfFile(file) && ReadFileLine(file, line, sizeof(line)))
115 {
116 if(line[0] != '\n' && line[0] != '\0')
117 {
118 TrimString(line);
119 new len = BreakString(line, Hitsounds[x][Name], 256);
120 BreakString(line[len], Hitsounds[x][Path], 256);
121 decl String:DL[150];
122 Format(DL, sizeof(DL), "sound/%s", Hitsounds[x][Path]);
123 if (FileExists(DL))
124 {
125 PrecacheSoundAny(Hitsounds[x][Path]);
126 AddFileToDownloadsTable(DL);
127 }
128 else { LogError("Couldn't find the hitsound: %s", Hitsounds[x][Path]); x--; }
129 x++;
130 }
131 }
132 CloseHandle(file);
133 }
134 else
135 {
136 SetFailState("Config file doesn't exist, aborting.");
137 }
138}
139
140// Sdkhooks thingy
141public OnClientPutInServer(client)
142{
143 SDKHook(client, SDKHook_OnTakeDamagePost, OnTakeDamagePostCallback);
144}
145
146public OnClientDisconnect(client)
147{
148 if(IsClientInGame(client)) SDKUnhook(client, SDKHook_OnTakeDamagePost, OnTakeDamagePostCallback);
149}
150
151// Load Client Settings
152
153public OnClientAuthorized(client, const String:auth[])
154{
155 if(!IsFakeClient(client)) LoadSettings(client);
156}
157
158LoadSettings(client)
159{
160 decl String:query[256], String:steamid[40];
161 GetClientAuthString(client, steamid, sizeof(steamid));
162 FormatEx(query, sizeof(query), "SELECT * FROM user_settings WHERE SteamID = '%s'", steamid);
163 SQL_TQuery(DB, SQL_ThreadedCallback, query, client);
164}
165
166public SQL_ThreadedCallback(Handle:owner, Handle:hndl, const String:error[], any:client)
167{
168 if (hndl == INVALID_HANDLE)
169 {
170 LogError("SQL Query Error: %s", error);
171 SetFailState("Lost connection to database. Reconnecting on map change.");
172 }
173
174 if(SQL_HasResultSet(hndl) && SQL_FetchRow(hndl))
175 {
176 UserSettings[client][Index] = SQL_FetchInt(hndl, 1);
177 UserSettings[client][Volume] = SQL_FetchInt(hndl, 2);
178 User_Bool[client] = bool:SQL_FetchInt(hndl, 3);
179 }
180 else
181 {
182 decl String:Query[180], String:Steamid[40];
183 GetClientAuthString(client, Steamid, sizeof(Steamid));
184 FormatEx(Query, sizeof(Query), "INSERT INTO `user_settings` (`SteamID`) VALUES ('%s');", Steamid);
185 SQL_TQuery(DB, SQL_DoNothing, Query);
186 UserSettings[client][Index] = 64;
187 UserSettings[client][Volume] = 75;
188 User_Bool[client] = false;
189 }
190}
191
192public SQL_DoNothing(Handle:owner, Handle:hndl, const String:error[], any:data)
193{
194 if(hndl == INVALID_HANDLE || strlen(error) > 0)
195 {
196 LogError("SQL query errors: %s", error);
197 SetFailState("Lost connection to database. Reconnecting on map change.");
198 }
199}
200
201// Finish Loading Client Settings
202//Menus
203
204Open_Interface(client)
205{
206 // create menu
207 new Handle:interface_menus = CreateMenu(Interface_wepmenu);
208 new String:entry[128];
209 new inte = UserSettings[client][Index];
210 Format(entry, sizeof(entry), "%t", "Change Hitsounds", Hitsounds[inte][Name]);
211 AddMenuItem(interface_menus, "changehit", entry);
212
213 if(User_Bool[client])
214 {
215 Format(entry, sizeof(entry), "%t", "Change Volume PROP");
216 AddMenuItem(interface_menus, "volume", entry, ITEMDRAW_DISABLED);
217 }
218 else
219 {
220 if(UserSettings[client][Volume] <= 100)
221 {
222 Format(entry, sizeof(entry), "%t", "Change Volume", UserSettings[client][Volume]);
223 AddMenuItem(interface_menus, "volume", entry);
224 }
225 else
226 {
227 Format(entry, sizeof(entry), "%t", "Change Volume equal to game volume");
228 AddMenuItem(interface_menus, "volume", entry);
229 }
230 }
231 decl String:PROP[100];
232 if (User_Bool[client]) FormatEx(PROP, sizeof(PROP), "[✓] %t", "Volume Proportional to damage done");
233 else FormatEx(PROP, sizeof(PROP), "[✘] %t", "Volume Proportional to damage done");
234 AddMenuItem(interface_menus, "proptohealth", PROP);
235
236 AddMenuItem(interface_menus, "creds", "Créditos");
237
238 SetMenuTitle(interface_menus, "%t", "Main Menu Title");
239 DisplayMenu(interface_menus, client, MENU_TIME_FOREVER);
240}
241
242public Interface_wepmenu(Handle:interface_menus, MenuAction:action, param1, param2)
243{
244
245 if (action == MenuAction_Select)
246 {
247 if(param1 > 0 && param1 <= MaxClients)
248 {
249 new String:info[38];
250 GetMenuItem(interface_menus, param2, info, sizeof(info));
251 if (StrEqual(info, "changehit")) Menu_Hitsound(param1);
252 else if (StrEqual(info, "volume")) Menu_Volume(param1)
253 else if (StrEqual(info, "creds")) Creditos(param1)
254 else if (StrEqual(info, "proptohealth"))
255 {
256 decl String:query[255], String:steamid[40];
257 GetClientAuthString(param1, steamid, sizeof(steamid))
258 if(User_Bool[param1])
259 {
260 Format(query, sizeof(query), "UPDATE `user_settings` SET `prop` = '0' WHERE `SteamID` = '%s'", steamid);
261 User_Bool[param1] = false;
262 }
263 else
264 {
265 Format(query, sizeof(query), "UPDATE `user_settings` SET `prop` = '1' WHERE `SteamID` = '%s'", steamid);
266 User_Bool[param1] = true;
267 }
268 Open_Interface(param1);
269 }
270 }
271 }
272 if (action == MenuAction_End)
273 {
274 CloseHandle(interface_menus);
275 }
276}
277
278// Menu Hitsound choose
279
280Menu_Hitsound(client)
281{
282 // create menu
283 new Handle:menu_choose_hitsound = CreateMenu(Hitsound_Menu);
284 for(new x = 0; x < MAX_HITSOUNDS-1; x++)
285 {
286 if(Hitsounds[x][Name][0] != '\0')
287 {
288 decl String:tt[5];
289 FormatEx(tt, sizeof(tt), "%i", x);
290 decl String:display[180];
291 if (UserSettings[client][Index] == x) Format(display, sizeof(display), "[E] %s", Hitsounds[x][Name]);
292 else Format(display, sizeof(display), "%s", Hitsounds[x][Name]);
293 AddMenuItem(menu_choose_hitsound, tt, display);
294 }
295 }
296 strcopy(Hitsounds[64][Name], strlen("Nenhum"), "Nenhum");
297 SetMenuTitle(menu_choose_hitsound, "%t", "Choose hitsound");
298 SetMenuExitBackButton(menu_choose_hitsound, true);
299 DisplayMenu(menu_choose_hitsound, client, MENU_TIME_FOREVER);
300}
301
302public Hitsound_Menu(Handle:menura, MenuAction:action, param1, param2)
303{
304
305 if (action == MenuAction_Select)
306 {
307 new String:info[38];
308 GetMenuItem(menura, param2, info, sizeof(info));
309 new intt = StringToInt(info);
310 decl String:steamid[40];
311 GetClientAuthString(param1, steamid, sizeof(steamid))
312 decl String:QuerySQL[256];
313 if (UserSettings[param1][Index] == intt)
314 {
315 UserSettings[param1][Index] = 64;
316 CPrintToChatEx(param1, param1, "%t", "Disable Hitsound");
317 Format(QuerySQL, sizeof(QuerySQL), "UPDATE `user_settings` SET `pindex` = '64' WHERE `SteamID` = '%s'", steamid);
318
319 }
320 else
321 {
322 CPrintToChatEx(param1, param1, "%t", "Changed hitsound", Hitsounds[intt][Name]);
323 UserSettings[param1][Index] = intt;
324 Format(QuerySQL, sizeof(QuerySQL), "UPDATE `user_settings` SET `pindex` = '%i' WHERE `SteamID` = '%s'", intt, steamid);
325 }
326 SQL_TQuery(DB, SQL_DoNothing, QuerySQL);
327 Playhitsound(param1);
328 Menu_Hitsound(param1)
329 }
330 if (action == MenuAction_End)
331 {
332 CloseHandle(menura);
333 }
334 if (action == MenuAction_Cancel)
335 {
336 Open_Interface(param1)
337 }
338}
339
340// Menu Volume
341
342public Menu_Volume(client)
343{
344 new Handle:menu_Choose_volume = CreateMenu(Volume_Menu);
345
346 decl String:temp[80];
347 if(UserSettings[client][Volume] == 101) FormatEx(temp, sizeof(temp), "[E] %t", "Equal to game volume");
348 else FormatEx(temp, sizeof(temp), "%t", "Equal to game volume");
349 AddMenuItem(menu_Choose_volume, "101", temp);
350 for(new x = 100; x != -5; x -= 5)
351 {
352 decl String:cc[10];
353 decl String:tt[10];
354 FormatEx(cc, sizeof(cc), "%i", x);
355 if(UserSettings[client][Volume] == x) FormatEx(tt, sizeof(tt), "[✓] %i", x);
356 else FormatEx(tt, sizeof(tt), "%i", x);
357 AddMenuItem(menu_Choose_volume, cc, tt);
358 }
359 SetMenuTitle(menu_Choose_volume, "%t", "Choose new volume");
360 SetMenuExitBackButton(menu_Choose_volume, true);
361 DisplayMenu(menu_Choose_volume, client, MENU_TIME_FOREVER);
362}
363
364public Volume_Menu(Handle:menura, MenuAction:action, param1, param2)
365{
366
367 if (action == MenuAction_Select)
368 {
369 new String:info[38];
370 GetMenuItem(menura, param2, info, sizeof(info));
371 new volume = StringToInt(info);
372 if(volume <= 100) CPrintToChatEx(param1, param1, "%t", "Changed Volume", volume);
373 else if(volume <= 100) CPrintToChatEx(param1, param1, "%t", "Volume equal to game volume");
374 decl String:steamid[40];
375 GetClientAuthString(param1, steamid, sizeof(steamid))
376 decl String:QuerySQL[256];
377 Format(QuerySQL, sizeof(QuerySQL), "UPDATE `user_settings` SET `volume` = '%i' WHERE `SteamID` = '%s'", volume, steamid);
378 SQL_TQuery(DB, SQL_DoNothing, QuerySQL);
379 UserSettings[param1][Volume] = volume;
380 Playhitsound(param1);
381 Menu_Volume(param1);
382 }
383 if (action == MenuAction_End)
384 {
385 CloseHandle(menura);
386 }
387 if (action == MenuAction_Cancel)
388 {
389 Open_Interface(param1)
390 }
391}
392
393//Menu creds
394Creditos(client)
395{
396 // create menu
397 new Handle:credits_menu = CreateMenu(Credits_Menuu);
398 SetMenuTitle(credits_menu, "Créditos:\n \n");
399 AddMenuItem(credits_menu, "is" , "Desenvolvido por ⇦Dark⇧Panda⇨\npara a comunidade PT'Fun.\nReportar qualquer bug a um admin!\n> ptfun.net" , ITEMDRAW_DISABLED);
400 SetMenuExitBackButton(credits_menu, true);
401 DisplayMenu(credits_menu, client, MENU_TIME_FOREVER);
402}
403
404public Credits_Menuu(Handle:credits_menu, MenuAction:action, param1, param2)
405{
406 if (action == MenuAction_End)
407 {
408 CloseHandle(credits_menu);
409 }
410 if (action == MenuAction_Cancel)
411 {
412 Open_Interface (param1);
413 }
414}
415
416//
417public OnTakeDamagePostCallback(victim, attacker, inflictor, Float:damage, damagetype)
418{
419 if(victim != 0 && attacker != 0 && victim <= MaxClients && attacker <= MaxClients && GetClientTeam(victim) != GetClientTeam(attacker) && UserSettings[attacker][Volume] != 0)
420 {
421 new String:flag[2];
422 GetConVarString(g_AccessFlag, flag, sizeof(flag))
423 if(flag[0] == '\0') Playhitsound(attacker, damage);
424 new chat_flag;
425 chat_flag = ReadFlagString(flag);
426 if(GetUserFlagBits(attacker) & chat_flag) Playhitsound(attacker, damage);
427 }
428}
429
430Playhitsound(client, Float:damage = 100.0)
431{
432 if( UserSettings[client][Index] != 64)
433 {
434 if(User_Bool[client])
435 {
436 new index = UserSettings[client][Index];
437 new Float:volume = damage / 100.0;
438 if( volume > 1.0) volume = 1.0
439 EmitSoundToClientAny(client, Hitsounds[index][Path], SOUND_FROM_PLAYER, SOUND_AMBIENT_CHANNEL,_,_, volume);
440 }
441 else if(UserSettings[client][Volume] > 100)
442 {
443 new index = UserSettings[client][Index];
444 ClientCommand(client, "play %s", Hitsounds[index][Path]);
445 }
446 else
447 {
448 new index = UserSettings[client][Index];
449 new Float:volume = float(UserSettings[client][Volume]) / 100.0;
450 EmitSoundToClientAny(client, Hitsounds[index][Path],_,_,_,_, volume);
451 }
452 }
453}