· 8 years ago · Nov 22, 2017, 04:06 PM
1//PlayersDB 1.0 by Savage
2
3{LIBDB}
4// =====================================
5// C O N S T A N T S
6// =====================================
7{ Database plugins enumeration
8 for the DB_Open() function }
9Const DB_Plugin_ODBC = 1;
10Const DB_Plugin_SQLite = 2;
11Const DB_Plugin_PostgreSQL = 3;
12
13{ Database column types enumeration
14 for the DB_ColumnType() function }
15Const DB_Type_Double = 1;
16Const DB_Type_Float = 2;
17Const DB_Type_Long = 3;
18Const DB_Type_String = 4;
19
20// WINDOWS
21// =====================================
22// D E C L A R A T I O N S
23// =====================================
24Procedure DB_Close(DatabaseID: Integer);
25External 'DB_Close@libdb-0.2.dll cdecl';
26
27Function DB_ColumnName(DatabaseID, Column: Integer): PChar;
28External 'DB_ColumnName@libdb-0.2.dll cdecl';
29
30Function DB_ColumnSize(DatabaseID, Column: Integer): Integer;
31External 'DB_ColumnSize@libdb-0.2.dll cdecl';
32
33Function DB_ColumnType(DatabaseID, Column: Integer): Integer;
34External 'DB_ColumnType@libdb-0.2.dll cdecl';
35
36Function DB_Columns(DatabaseID: Integer): Integer;
37External 'DB_Columns@libdb-0.2.dll cdecl';
38
39Function DB_Error(): PChar;
40External 'DB_Error@libdb-0.2.dll cdecl';
41
42Function DB_Query(DatabaseID: Integer; Query: PChar): Integer;
43External 'DB_Query@libdb-0.2.dll cdecl';
44
45Function DB_Update(DatabaseID: Integer; Query: PChar): Integer;
46External 'DB_Update@libdb-0.2.dll cdecl';
47
48Procedure DB_FinishQuery(DatabaseID: Integer);
49External 'DB_FinishQuery@libdb-0.2.dll cdecl';
50
51Function DB_FirstRow(DatabaseID: Integer): Integer;
52External 'DB_FirstRow@libdb-0.2.dll cdecl';
53
54Function DB_GetDouble(DatabaseID, Column: Integer): Double;
55External 'DB_GetDouble@libdb-0.2.dll cdecl';
56
57Function DB_GetFloat(DatabaseID, Column: Integer): Single;
58External 'DB_GetFloat@libdb-0.2.dll cdecl';
59
60Function DB_GetLong(DatabaseID, Column: Integer): LongInt;
61External 'DB_GetLong@libdb-0.2.dll cdecl';
62
63Function DB_GetString(DatabaseID, Column: Integer): PChar;
64External 'DB_GetString@libdb-0.2.dll cdecl';
65
66Function DB_IsDatabase(DatabaseID: Integer): Integer;
67External 'DB_IsDatabase@libdb-0.2.dll cdecl';
68
69Function DB_NextRow(DatabaseID: Integer): Integer;
70External 'DB_NextRow@libdb-0.2.dll cdecl';
71
72Function DB_Open(DatabaseID: Integer; DatabaseName, User, Password: PChar; Plugin: Integer): Integer;
73External 'DB_Open@libdb-0.2.dll cdecl';
74
75Function DB_ExamineDrivers(): Integer;
76External 'DB_ExamineDrivers@libdb-0.2.dll cdecl';
77
78Function DB_NextDriver(): Integer;
79External 'DB_NextDriver@libdb-0.2.dll cdecl';
80
81Function DB_DriverDescription(): PChar;
82External 'DB_DriverDescription@libdb-0.2.dll cdecl';
83
84Function DB_DriverName(): PChar;
85External 'DB_DriverName@libdb-0.2.dll cdecl';
86
87Function DB_GetVersion(): Integer;
88External 'DB_GetVersion@libdb-0.2.dll cdecl';
89{
90// LINUX
91// =====================================
92// D E C L A R A T I O N S
93// =====================================
94Procedure DB_Close(DatabaseID: Integer);
95External 'DB_Close@libdb-0.2.so cdecl';
96
97Function DB_ColumnName(DatabaseID, Column: Integer): PChar;
98External 'DB_ColumnName@libdb-0.2.so cdecl';
99
100Function DB_ColumnSize(DatabaseID, Column: Integer): Integer;
101External 'DB_ColumnSize@libdb-0.2.so cdecl';
102
103Function DB_ColumnType(DatabaseID, Column: Integer): Integer;
104External 'DB_ColumnType@libdb-0.2.so cdecl';
105
106Function DB_Columns(DatabaseID: Integer): Integer;
107External 'DB_Columns@libdb-0.2.so cdecl';
108
109Function DB_Error(): PChar;
110External 'DB_Error@libdb-0.2.so cdecl';
111
112Function DB_Query(DatabaseID: Integer; Query: PChar): Integer;
113External 'DB_Query@libdb-0.2.so cdecl';
114
115Function DB_Update(DatabaseID: Integer; Query: PChar): Integer;
116External 'DB_Update@libdb-0.2.so cdecl';
117
118Procedure DB_FinishQuery(DatabaseID: Integer);
119External 'DB_FinishQuery@libdb-0.2.so cdecl';
120
121Function DB_FirstRow(DatabaseID: Integer): Integer;
122External 'DB_FirstRow@libdb-0.2.so cdecl';
123
124Function DB_GetDouble(DatabaseID, Column: Integer): Double;
125External 'DB_GetDouble@libdb-0.2.so cdecl';
126
127Function DB_GetFloat(DatabaseID, Column: Integer): Single;
128External 'DB_GetFloat@libdb-0.2.so cdecl';
129
130Function DB_GetLong(DatabaseID, Column: Integer): LongInt;
131External 'DB_GetLong@libdb-0.2.so cdecl';
132
133Function DB_GetString(DatabaseID, Column: Integer): PChar;
134External 'DB_GetString@libdb-0.2.so cdecl';
135
136Function DB_IsDatabase(DatabaseID: Integer): Integer;
137External 'DB_IsDatabase@libdb-0.2.so cdecl';
138
139Function DB_NextRow(DatabaseID: Integer): Integer;
140External 'DB_NextRow@libdb-0.2.so cdecl';
141
142Function DB_Open(DatabaseID: Integer; DatabaseName, User, Password: PChar; Plugin: Integer): Integer;
143External 'DB_Open@libdb-0.2.so cdecl';
144
145Function DB_ExamineDrivers(): Integer;
146External 'DB_ExamineDrivers@libdb-0.2.so cdecl';
147
148Function DB_NextDriver(): Integer;
149External 'DB_NextDriver@libdb-0.2.so cdecl';
150
151Function DB_DriverDescription(): PChar;
152External 'DB_DriverDescription@libdb-0.2.so cdecl';
153
154Function DB_DriverName(): PChar;
155External 'DB_DriverName@libdb-0.2.so cdecl';
156
157Function DB_GetVersion(): Integer;
158External 'DB_GetVersion@libdb-0.2.so cdecl';
159}
160
161const
162 DB_NAME = 'PlayersDB.db';
163 MSG_COLOR = $6495ed;
164
165function EscapeApostrophe(Source: String): String;
166begin
167 Result := ReplaceRegExpr('''', Source, '''''', False);
168end;
169
170function GetWord(Source: String; WordNumber: Integer): String;
171var
172 WordCounter, WordIndex, i: Integer;
173begin
174 if WordNumber > 0 then begin
175 for i := 1 to Length(Source) do
176 if WordIndex <> 0 then begin
177 if Source[i] = ' ' then begin
178 Inc(WordCounter, 1);
179 if WordNumber = WordCounter then begin
180 Result := Copy(Source, WordIndex, i-WordIndex);
181 break;
182 end else
183 WordIndex := 0;
184 end else
185 if i = Length(Source) then begin
186 Inc(WordCounter, 1);
187 if WordNumber = WordCounter then
188 Result := Copy(Source, WordIndex, i-WordIndex+1);
189 end;
190 end else
191 if Source[i] = ' ' then
192 continue
193 else
194 if i = Length(Source) then begin
195 Inc(WordCounter, 1);
196 if WordNumber = WordCounter then
197 Result := Source[i];
198 end else
199 WordIndex := i;
200 end else
201 WriteLn('Warning: Function "GetWord" - Second parameter has to be higher than "0"');
202end;
203
204procedure OnJoin(Player: TActivePlayer; Team: TTeam);
205begin
206 if Player.Human then begin
207 if DB_Query(0, 'SELECT Name, Hwid FROM Players WHERE Name = '''+EscapeApostrophe(Player.Name)+''' AND Hwid = '''+EscapeApostrophe(Player.HWID)+''' LIMIT 1;') = 0 then begin
208 WriteLn('Couldn''t receive record from "Players" table');
209 WriteLn('Error: '+DB_Error);
210 end else
211 begin
212 if DB_FirstRow(0) = 0 then begin
213 if DB_Update(0, 'INSERT INTO Players(Name, Hwid, Entry) VALUES('''+EscapeApostrophe(Player.Name)+''', '''+EscapeApostrophe(Player.HWID)+''', 1);') = 0 then begin
214 WriteLn('Couldn''t insert values into "Players" table');
215 WriteLn('Error: '+DB_Error);
216 end;
217 end else
218 if DB_Update(0, 'UPDATE Players SET Entry = Entry +1 WHERE Name = '''+EscapeApostrophe(Player.Name)+''' AND Hwid = '''+EscapeApostrophe(Player.HWID)+''';') = 0 then begin
219 WriteLn('Couldn''t update values in "Players" table');
220 WriteLn('Error: '+DB_Error);
221 end;
222 DB_FinishQuery(0);
223 end;
224 end;
225end;
226
227function OnAdminCommand(Player: TActivePlayer; Command: string): boolean;
228var
229 TempID: Byte;
230begin
231Result := False;
232
233 if (GetWord(Command, 1) = '/checkhw') and (GetWord(Command, 2) <> nil) then
234 if DB_Query(0, 'SELECT Name, Entry FROM Players WHERE Hwid = '''+EscapeApostrophe(GetWord(Command, 2))+''' ORDER BY Entry;') <> 0 then begin
235 While DB_NextRow(0) <> 0 Do
236 Player.WriteConsole(DB_GetString(0, 0)+': '+DB_GetString(0, 1), MSG_COLOR);
237 DB_FinishQuery(0);
238 Player.WriteConsole('Result for HWID "'+GetWord(Command, 2)+'" has been sorted by entries in ascending order', MSG_COLOR);
239 end else
240 begin
241 Player.WriteConsole('Couldn''t receive records from "Players" table', MSG_COLOR);
242 Player.WriteConsole('Error: '+DB_Error, MSG_COLOR);
243 end;
244
245 if (GetWord(Command, 1) = '/checknick') and (GetWord(Command, 2) <> nil) then
246 if DB_Query(0, 'SELECT Hwid, Entry FROM Players WHERE Name = '''+EscapeApostrophe(GetWord(Command, 2))+''' ORDER BY Entry;') <> 0 then begin
247 While DB_NextRow(0) <> 0 Do
248 Player.WriteConsole(DB_GetString(0, 0)+': '+DB_GetString(0, 1), MSG_COLOR);
249 DB_FinishQuery(0);
250 Player.WriteConsole('Result for Nick "'+GetWord(Command, 2)+'" has been sorted by entries in ascending order', MSG_COLOR);
251 end else
252 begin
253 Player.WriteConsole('Couldn''t receive records from "Players" table', MSG_COLOR);
254 Player.WriteConsole('Error: '+DB_Error, MSG_COLOR);
255 end;
256
257 if (GetWord(Command, 1) = '/checkid') and (GetWord(Command, 2) <> nil) then begin
258 try
259 TempID := StrToInt(GetWord(Command, 2));
260 except
261 Player.WriteConsole('"'+GetWord(Command, 2)+'" is invalid integer', MSG_COLOR);
262 end;
263 if (TempID > 0) and (TempID < 33) then begin
264 if DB_Query(0, 'SELECT Name, Entry FROM Players WHERE Hwid = '''+EscapeApostrophe(Players[TempID].HWID)+''' ORDER BY Entry;') <> 0 then begin
265 While DB_NextRow(0) <> 0 Do
266 Player.WriteConsole(DB_GetString(0, 0)+': '+DB_GetString(0, 1), MSG_COLOR);
267 DB_FinishQuery(0);
268 Player.WriteConsole('Result for HWID "'+Players[TempID].HWID+'" has been sorted by entries in ascending order', MSG_COLOR);
269 end else
270 begin
271 Player.WriteConsole('Couldn''t receive records from "Players" table', MSG_COLOR);
272 Player.WriteConsole('Error: '+DB_Error, MSG_COLOR);
273 end;
274 end else
275 Player.WriteConsole('ID has to be from 1 to 32', MSG_COLOR);
276 end;
277end;
278
279function OnTCPCommand(Ip: string; Port: Word; Command: string): Boolean;
280var
281 TempID: Byte;
282begin
283Result := False;
284
285 if (GetWord(Command, 1) = '/checkhw') and (GetWord(Command, 2) <> nil) then
286 if DB_Query(0, 'SELECT Name, Entry FROM Players WHERE Hwid = '''+EscapeApostrophe(GetWord(Command, 2))+''' ORDER BY Entry;') <> 0 then begin
287 While DB_NextRow(0) <> 0 Do
288 WriteLn(DB_GetString(0, 0)+': '+DB_GetString(0, 1));
289 DB_FinishQuery(0);
290 WriteLn('Result for HWID "'+GetWord(Command, 2)+'" has been sorted by entries in ascending order');
291 end else
292 begin
293 WriteLn('Couldn''t receive records from "Players" table');
294 WriteLn('Error: '+DB_Error);
295 end;
296
297 if (GetWord(Command, 1) = '/checknick') and (GetWord(Command, 2) <> nil) then
298 if DB_Query(0, 'SELECT Hwid, Entry FROM Players WHERE Name = '''+EscapeApostrophe(GetWord(Command, 2))+''' ORDER BY Entry;') <> 0 then begin
299 While DB_NextRow(0) <> 0 Do
300 WriteLn(DB_GetString(0, 0)+': '+DB_GetString(0, 1));
301 DB_FinishQuery(0);
302 WriteLn('Result for Nick "'+GetWord(Command, 2)+'" has been sorted by entries in ascending order');
303 end else
304 begin
305 WriteLn('Couldn''t receive records from "Players" table');
306 WriteLn('Error: '+DB_Error);
307 end;
308
309 if (GetWord(Command, 1) = '/checkid') and (GetWord(Command, 2) <> nil) then begin
310 try
311 TempID := StrToInt(GetWord(Command, 2));
312 except
313 WriteLn('"'+GetWord(Command, 2)+'" is invalid integer');
314 end;
315 if (TempID > 0) and (TempID < 33) then begin
316 if DB_Query(0, 'SELECT Name, Entry FROM Players WHERE Hwid = '''+EscapeApostrophe(Players[TempID].HWID)+''' ORDER BY Entry;') <> 0 then begin
317 While DB_NextRow(0) <> 0 Do
318 WriteLn(DB_GetString(0, 0)+': '+DB_GetString(0, 1));
319 DB_FinishQuery(0);
320 WriteLn('Result for HWID "'+Players[TempID].HWID+'" has been sorted by entries in ascending order');
321 end else
322 begin
323 WriteLn('Couldn''t receive records from "Players" table');
324 WriteLn('Error: '+DB_Error);
325 end;
326 end else
327 WriteLn('ID has to be from 1 to 32');
328 end;
329end;
330
331procedure Init;
332var
333 DBFile: TFileStream;
334begin
335 if not File.Exists(DB_NAME) then begin
336 DBFile := File.CreateFileStream;
337 DBFile.SaveToFile(DB_NAME);
338 DBFile.Free;
339 WriteLn('Database "'+DB_NAME+'" has been created');
340 if DB_Open(0, DB_NAME, '', '', DB_Plugin_SQLite) <> 0 then begin
341 if DB_Update(0, 'CREATE TABLE IF NOT EXISTS Players(Id INTEGER PRIMARY KEY, Name TEXT, Hwid TEXT, Entry INTEGER);') = 0 then begin
342 WriteLn('Couldn''t create "Players" table');
343 WriteLn('Error: '+DB_Error);
344 end;
345 end else
346 begin
347 WriteLn('Couldn''t open Database "'+DB_NAME+'"');
348 WriteLn('Error: '+DB_Error);
349 end;
350 end else
351 if DB_Open(0, DB_NAME, '', '', DB_Plugin_SQLite) = 0 then begin
352 WriteLn('Couldn''t open Database "'+DB_NAME+'"');
353 WriteLn('Error: '+DB_Error);
354 end;
355
356 Game.OnJoin := @OnJoin;
357 Game.OnAdminCommand := @OnAdminCommand;
358 Game.OnTCPCommand := @OnTCPCommand;
359end;
360
361begin
362 Init;
363end.