· 8 years ago · Jun 13, 2018, 08:34 PM
1#pragma semicolon 1
2
3#define DEBUG
4
5#define PLUGIN_AUTHOR "jenz"
6#define PLUGIN_VERSION "1.00"
7
8#include <sourcemod>
9#include <sdktools>
10#include <colorvariables>
11#include <chat-processor>
12#include <data_string_parameter>
13
14#pragma newdecls required
15
16//global variables
17bool g_bResponseMode[MAXPLAYERS+1];
18bool g_bBotCooldown = true;
19bool g_bGag[MAXPLAYERS+1];
20
21public Plugin myinfo =
22{
23 name = "Artificial Autism",
24 author = PLUGIN_AUTHOR,
25 description = "Client messages for bot",
26 version = PLUGIN_VERSION,
27 url = ""
28};
29
30public void OnPluginStart()
31{
32 SQL_StartConnection();
33 RegConsoleCmd("sm_autism", cmd_Autismblock, "stops the messages");
34}
35
36public void OnMapStart()
37{
38 g_bBotCooldown = true;
39}
40
41//blocking bot response
42public Action cmd_Autismblock(int client, int args)
43{
44 if ((g_bGag[client]) == false)
45 {
46 g_bGag[client] = true;
47 ReplyToCommand(client, "Bot detection disabled");
48 }
49 else
50 {
51 g_bGag[client] = false;
52 ReplyToCommand(client, "Bot detection enabled");
53 }
54}
55
56public void OnClientPostAdminCheck(int client)
57{
58 g_bResponseMode[client] = false;
59}
60
61//post every message
62
63public void CP_OnChatMessagePost(int author, ArrayList recipients, const char[] flagstring, const char[] formatstring, const char[] name, const char[] message, bool processcolors, bool removecolors)
64{
65 if((g_bGag[author]) == true)
66 {
67 return;
68 }
69 //variables
70 DataPack pack = new DataPack();
71 pack.WriteString(name);
72 pack.WriteString(message);
73
74 //Avoid plugin from automatically marking down commands to mysql or empty strings.
75 if(StrContains(message, "/", false) >= 1 || StrContains(message, "!", false) >= 1 || StrEqual(message, "") || strlen(message) < 3)
76 {
77 return;
78 }
79
80 //storing every message to mysql
81 sendMYSQL(message);
82
83 if(!(g_bBotCooldown))
84 {
85 return;
86 }
87
88 //ask a question if no interaction yet
89 if(g_bResponseMode[author] == false && StrContains(message, "?", false) <= 1)
90 {
91 CreateTimer(1.0, Timer_Question, pack, TIMER_FLAG_NO_MAPCHANGE);
92 g_bResponseMode[author] = true;
93 }
94 //send an answer asuming the client writes back
95 else if(g_bResponseMode[author] == true && StrContains(message, "?", false) <= 1)
96 {
97 CreateTimer(1.0, Timer_Answer, pack, TIMER_FLAG_NO_MAPCHANGE);
98 g_bResponseMode[author] = false;
99 }
100
101 //if client asks a question send answer instead
102 if (StrContains(message, "?", false) >= 1)
103 {
104 CreateTimer(1.0, Timer_Answer, pack, TIMER_FLAG_NO_MAPCHANGE);
105 g_bResponseMode[author] = true;
106 }
107 g_bBotCooldown = false;
108 CreateTimer(6.0, Timer_booler, _, TIMER_FLAG_NO_MAPCHANGE);
109
110}
111
112public Action Timer_booler(Handle hTimer, DataPack pack)
113{
114 g_bBotCooldown = true;
115}
116
117//callback
118
119public void DummyCallback(Handle hOwner, Handle hChild, const char[] err, any data)
120{
121 //variable
122 char question[512];
123 char name[512];
124 ReadAnyString(data, name, sizeof(name));
125
126 //validating its not empty
127 if (hOwner == null || hChild == null)
128 {
129 LogError("Query error. (%s)", err);
130 return;
131 }
132
133 if (hChild == INVALID_HANDLE)
134 {
135 return;
136 }
137
138 //fetching and converting question into string
139 if(SQL_FetchRow(hChild))
140 {
141 SQL_FetchString(hChild, 0, question, sizeof(question));
142 }
143
144
145 if (StrContains(question, "bot", false) >= 0)
146 {
147 char namefix[512];
148 Format (namefix, sizeof(namefix), name);
149 int i_ = IsInteger(namefix);
150 Format (namefix, sizeof(namefix), namefix[i_ + 1]);
151 //CPrintToChatAll("namefix value: %s", namefix);
152 ReplaceString(question, sizeof(question), "bot", namefix, false);
153 }
154 //CPrintToChatAll(question);
155
156 //finally printing to chat
157 for (int i = 1; i <= MaxClients; i++)
158 {
159 if (IsClientInGame(i) && IsClientAuthorized(i) && (g_bGag[i]) == false)
160 {
161 CPrintToChat(i, "{default}*UNLOZE* {orchid}[Artificial Autism] : %s %s", name, question);
162 }
163 }
164}
165
166public int IsInteger(const char[] message)
167{
168 int ints = 0;
169 bool nonchar = false;
170 bool ischar = true;
171
172 if (!(nonchar))
173 {
174 do
175 {
176 ++ints;
177 }
178 while (!IsCharNumeric(message[ints]));
179 nonchar = true;
180 ischar = false;
181 }
182
183 if (!(ischar))
184 {
185 while (IsCharNumeric(message[ints]))
186 {
187 ++ints;
188 }
189 ischar = true;
190 }
191 return ints;
192}
193
194
195public Action Timer_Answer(Handle hTimer, DataPack pack)
196{
197 //retrieve client name & message
198 ResetPack(pack);
199 char message[255];
200 char name[255];
201 pack.ReadString(name, sizeof(name));
202 pack.ReadString(message, sizeof(message));
203
204
205 //variables
206 char sQuery[512];
207 char error[255];
208 Database db;
209
210 //initialize db
211 if (SQL_CheckConfig("strings"))
212 {
213 db = SQL_Connect("strings", true, error, sizeof(error));
214 }
215 if (db == null)
216 {
217 CPrintToChatAll("{green}[Unloze] {white}Error! Could not connect to MYSQL-DB!");
218 delete db;
219 }
220
221 //doing the query
222 Format(sQuery, sizeof(sQuery), "SELECT answer FROM (SELECT answer FROM strings_answer ORDER BY levenshtein(answer, '%s', TRUE) ASC LIMIT 5) T1 ORDER BY RAND() LIMIT 1", message);
223 //CPrintToChatAll(sQuery);
224 SQL_TQuery(db, DummyCallback, sQuery, AnyString(name));
225
226 //avoiding memeory leaks probably
227 CloseHandle(pack);
228 delete db;
229}
230
231public Action Timer_Question(Handle hTimer, DataPack pack)
232{
233 //retrieve client name & message
234 ResetPack(pack);
235 char message[255];
236 char name[255];
237 pack.ReadString(name, sizeof(name));
238 pack.ReadString(message, sizeof(message));
239
240 //variables
241 char sQuery[512];
242 char error[255];
243 Database db;
244
245 //initialize db
246 if (SQL_CheckConfig("strings"))
247 {
248 db = SQL_Connect("strings", true, error, sizeof(error));
249 }
250 if (db == null)
251 {
252 CPrintToChatAll("{green}[Unloze] {white}Error! Could not connect to MYSQL-DB!");
253 delete db;
254 }
255
256 //doing the query
257 Format(sQuery, sizeof(sQuery), "SELECT question FROM (SELECT question FROM strings_question ORDER BY levenshtein(question, '%s', TRUE) ASC LIMIT 5) T1 ORDER BY RAND() LIMIT 1", message);
258 SQL_TQuery(db, DummyCallback, sQuery, AnyString(name));
259
260 //avoiding memeory leaks probably
261 CloseHandle(pack);
262 delete db;
263}
264
265public void sendMYSQL(const char[] message)
266{
267 char error[255];
268 Database db;
269 if (SQL_CheckConfig("strings"))
270 {
271 db = SQL_Connect("strings", true, error, sizeof(error));
272 }
273 if (db == null)
274 {
275 CPrintToChatAll("{green}[Unloze] {white}Error! Could not connect to MYSQL-DB!");
276 delete db;
277 }
278
279 //add new strings to database table
280 if (StrContains(message, "?", false) >= 1 && strlen(message) > 1)
281 {
282 char sQueryQuestion[255];
283 Format(sQueryQuestion, sizeof(sQueryQuestion), "INSERT INTO `strings_question` (`question`) VALUES ('%s') ON DUPLICATE KEY UPDATE `question` = '%s'", message, message);
284 SQL_TQuery(db, DummyCallbackSimple, sQueryQuestion);
285 delete db;
286 return;
287 }
288
289 if (strlen(message) > 1)
290 {
291 char sQuery[255];
292 Format(sQuery, sizeof(sQuery), "INSERT INTO `strings_answer` (`answer`) VALUES ('%s') ON DUPLICATE KEY UPDATE `answer` = '%s'", message, message);
293 SQL_TQuery(db, DummyCallbackSimple, sQuery);
294 }
295 delete db;
296}
297
298public void SQL_StartConnection()
299{
300 char error[255];
301 Database db;
302 if (SQL_CheckConfig("strings"))
303 {
304 db = SQL_Connect("strings", true, error, sizeof(error));
305 }
306 if (db == null)
307 {
308 CPrintToChatAll("{green}[Unloze] {white}Error! Could not connect to MYSQL-DB!");
309 delete db;
310 }
311 //create tables
312 char sQuery[255];
313 Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `strings_answer` (`answer` VARCHAR(254) NOT NULL, PRIMARY KEY (`answer`))");
314 SQL_TQuery(db, DummyCallbackSimple, sQuery);
315 Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `strings_question` (`question` VARCHAR(254) NOT NULL, PRIMARY KEY (`question`))");
316 SQL_TQuery(db, DummyCallbackSimple, sQuery);
317 Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `strings_response` (`response` VARCHAR(254) NOT NULL, PRIMARY KEY (`response`))");
318 SQL_TQuery(db, DummyCallbackSimple, sQuery);
319 delete db;
320}
321
322public void OnClientPutInServer(int client)
323{
324 if (!IsValidClient(client))
325 {
326 return;
327 }
328
329}
330
331stock bool IsValidClient(int client, bool alive = false, bool bots = false)
332{
333 if (client > 0 && client <= MaxClients && IsClientInGame(client) && (alive == false || IsPlayerAlive(client)) && (bots == false && !IsFakeClient(client)))
334 {
335 return true;
336 }
337 return false;
338}
339
340
341public void DummyCallbackSimple(Handle hOwner, Handle hChild, const char[] err, DataPack pack1)
342{
343 if (hOwner == null || hChild == null)
344 {
345 LogError("Query error. (%s)", err);
346 }
347}