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