11#include < amxmodx>
22#include < amxmisc>
3+ #include < sqlx>
34
45#include < ChatAdditions>
56
@@ -20,6 +21,10 @@ enum {
2021 ITEM_MUTE_ALL = - 1
2122}
2223
24+ const QUERY_LENGTH = 4096
25+ new const g_mute_table[] = " ca_players_mute"
26+ new Handle: g_tuple = Empty_Handle
27+
2328public stock const PluginName[] = " CA: Mute"
2429public stock const PluginVersion[] = CA_VERSION
2530public stock const PluginAuthor[] = " Sergey Shorokhov"
@@ -48,6 +53,8 @@ public plugin_init() {
4853
4954 AutoExecConfig (true , " CA_Mute" , " ChatAdditions" )
5055
56+ Storage_Init ()
57+
5158 CA_Log (logLevel_Debug, " [CA]: Mute initialized!" )
5259}
5360
@@ -182,6 +189,7 @@ public MenuHandler_PlayersList(const id, const menu, const item) {
182189 }
183190
184191 g_playersMute[id][player] ^= true
192+ Storage_Update (id, player)
185193
186194 client_print_color (id, print_team_default, " % L % L \3 %n \1 " , id, " Mute_prefix" ,
187195 id, g_playersMute[id][player] ? " Mute_YouMutePlayer" : " Mute_YouUnmutePlayer" , player
@@ -202,8 +210,13 @@ public client_disconnected(id) {
202210 g_globalMute[id] = false
203211 g_nextUse[id] = 0 .0
204212
205- for (new i; i < sizeof (g_playersMute[]); i++ )
213+ for (new i; i < sizeof (g_playersMute[]); i++ ) {
214+ if (! g_playersMute[i][id])
215+ continue
216+
217+ // Storage_Update(i, id)
206218 g_playersMute[i][id] = false
219+ }
207220}
208221
209222public CA_Client_Voice (const listener, const sender) {
@@ -221,3 +234,128 @@ public CA_Client_Voice(const listener, const sender) {
221234
222235 return CA_CONTINUE
223236}
237+
238+ Storage_Init () {
239+ if (! SQL_SetAffinity (" sqlite" )) {
240+ set_fail_state (" Can't user 'SQLite'. Check modules.ini" )
241+ }
242+
243+ g_tuple = SQL_MakeDbTuple (" " , " " , " " , g_mute_table)
244+
245+ Storage_Create ()
246+ }
247+
248+ Storage_Create () {
249+ new query[QUERY_LENGTH / 2 ]
250+
251+ formatex (query, charsmax (query), " CREATE TABLE IF NOT EXISTS %s " , g_mute_table); {
252+ strcat (query, " ( id INTEGER PRIMARY KEY AUTOINCREMENT," , charsmax (query))
253+ strcat (query, " authid VARCHAR NOT NULL," , charsmax (query))
254+ strcat (query, " authid_target VARCHAR NOT NULL); " , charsmax (query))
255+ strcat (query, fmt (" CREATE UNIQUE INDEX IF NOT EXISTS authid_target_idx1 ON %s (authid, authid_target)" , g_mute_table), charsmax (query))
256+ }
257+
258+ SQL_ThreadQuery (g_tuple, " handle_StorageCreated" , query)
259+ }
260+
261+ public handle_StorageCreated (failstate, Handle: query, error[], errnum, data[], size, Float: queuetime) {
262+ if (IsSQLQueryFailed (failstate, query, error, errnum)) {
263+ return
264+ }
265+
266+ CA_Log (logLevel_Debug, " Table '%s ' created! (queryTime: '%.3f ' sec)" , g_mute_table, queuetime)
267+ }
268+
269+ public client_putinserver (player) {
270+ Storage_Load (player)
271+ }
272+
273+ Storage_Update (const player, const target) {
274+ if (! is_user_connected (target))
275+ return
276+
277+ new authId[MAX_AUTHID_LENGTH], authId_target[MAX_AUTHID_LENGTH]
278+ get_user_authid (player, authId, charsmax (authId))
279+ get_user_authid (target, authId_target, charsmax (authId_target))
280+
281+ new query[QUERY_LENGTH / 2 ]
282+
283+ if (g_playersMute[player][target]) {
284+ formatex (query, charsmax (query), " INSERT INTO %s (authid, authid_target)" , g_mute_table)
285+ strcat (query, fmt (" VALUES ('%s ', '%s ') ON CONFLICT IGNORE" , authId, authId_target), charsmax (query))
286+ } else {
287+ formatex (query, charsmax (query), " DELETE FROM %s " , g_mute_table)
288+ strcat (query, fmt (" WHERE authid='%s ' AND authid_target = '%s '" , authId, authId_target), charsmax (query))
289+ }
290+
291+ SQL_ThreadQuery (g_tuple, " handle_Saved" , query)
292+ }
293+
294+ public handle_Saved (failstate, Handle: query, error[], errnum, data[], size, Float: queuetime) {
295+ if (IsSQLQueryFailed (failstate, query, error, errnum)) {
296+ return
297+ }
298+ }
299+
300+ Storage_Load (const player) {
301+ new authId[MAX_AUTHID_LENGTH]
302+ get_user_authid (player, authId, charsmax (authId))
303+
304+ new query[QUERY_LENGTH / 2 ]
305+ formatex (query, charsmax (query), " SELECT authid, authid_target FROM %s " , g_mute_table)
306+ strcat (query, fmt (" WHERE authid='%s ' OR authid_target = '%s '" , authId, authId), charsmax (query))
307+
308+ SQL_ThreadQuery (g_tuple, " handle_LoadedMute" , query)
309+ }
310+
311+ public handle_LoadedMute (failstate, Handle: query, error[], errnum, data[], size, Float: queuetime) {
312+ if (IsSQLQueryFailed (failstate, query, error, errnum)) {
313+ return
314+ }
315+
316+ if (! SQL_NumResults (query))
317+ return
318+
319+ while (SQL_MoreResults (query)) {
320+ new authId[MAX_AUTHID_LENGTH], authId_target[MAX_AUTHID_LENGTH]
321+ SQL_ReadResult (query, 0 , authId, charsmax (authId))
322+ SQL_ReadResult (query, 1 , authId_target, charsmax (authId_target))
323+
324+ new player = find_player_ex (FindPlayer_MatchAuthId, authId)
325+ if (player == 0 ) {
326+ goto next
327+ }
328+
329+ new target = find_player_ex (FindPlayer_MatchAuthId, authId_target)
330+ if (target == 0 ) {
331+ goto next
332+ }
333+
334+ g_playersMute[player][target] = true
335+
336+ next:
337+ SQL_NextRow (query)
338+ }
339+ }
340+
341+ static stock bool: IsSQLQueryFailed (const failstate, const Handle: query, const error[], const errNum) {
342+ switch (failstate) {
343+ case TQUERY_CONNECT_FAILED: {
344+ log_amx (" SQL: connection failed [%i ] `%s `" , errNum, error)
345+ return true
346+ }
347+ case TQUERY_QUERY_FAILED: {
348+ log_amx (" SQL: query failed [%i ] %s " , errNum, error);
349+
350+ server_print (" ^n^n ===> Query:" )
351+ new buffer[1024 ]; SQL_GetQueryString (query, buffer, charsmax (buffer));
352+ for (new i, len = strlen (buffer); i < len; i+= 255 ) {
353+ server_print (fmt (" %-255s " , buffer[i]));
354+ }
355+
356+ return true
357+ }
358+ }
359+
360+ return false
361+ }
0 commit comments