Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PlayersDB 1.2 by Savage
- {LIBDB}
- // =====================================
- // C O N S T A N T S
- // =====================================
- { Database plugins enumeration
- for the DB_Open() function }
- Const DB_Plugin_ODBC = 1;
- Const DB_Plugin_SQLite = 2;
- Const DB_Plugin_PostgreSQL = 3;
- { Database column types enumeration
- for the DB_ColumnType() function }
- Const DB_Type_Double = 1;
- Const DB_Type_Float = 2;
- Const DB_Type_Long = 3;
- Const DB_Type_String = 4;
- // =====================================
- // D E C L A R A T I O N S
- // =====================================
- Procedure DB_Close(DatabaseID: Integer);
- External 'DB_Close@libdb-0.2.dll cdecl';
- Function DB_ColumnName(DatabaseID, Column: Integer): PChar;
- External 'DB_ColumnName@libdb-0.2.dll cdecl';
- Function DB_ColumnSize(DatabaseID, Column: Integer): Integer;
- External 'DB_ColumnSize@libdb-0.2.dll cdecl';
- Function DB_ColumnType(DatabaseID, Column: Integer): Integer;
- External 'DB_ColumnType@libdb-0.2.dll cdecl';
- Function DB_Columns(DatabaseID: Integer): Integer;
- External 'DB_Columns@libdb-0.2.dll cdecl';
- Function DB_Error(): PChar;
- External 'DB_Error@libdb-0.2.dll cdecl';
- Function DB_Query(DatabaseID: Integer; Query: PChar): Integer;
- External 'DB_Query@libdb-0.2.dll cdecl';
- Function DB_Update(DatabaseID: Integer; Query: PChar): Integer;
- External 'DB_Update@libdb-0.2.dll cdecl';
- Procedure DB_FinishQuery(DatabaseID: Integer);
- External 'DB_FinishQuery@libdb-0.2.dll cdecl';
- Function DB_FirstRow(DatabaseID: Integer): Integer;
- External 'DB_FirstRow@libdb-0.2.dll cdecl';
- Function DB_GetDouble(DatabaseID, Column: Integer): Double;
- External 'DB_GetDouble@libdb-0.2.dll cdecl';
- Function DB_GetFloat(DatabaseID, Column: Integer): Single;
- External 'DB_GetFloat@libdb-0.2.dll cdecl';
- Function DB_GetLong(DatabaseID, Column: Integer): LongInt;
- External 'DB_GetLong@libdb-0.2.dll cdecl';
- Function DB_GetString(DatabaseID, Column: Integer): PChar;
- External 'DB_GetString@libdb-0.2.dll cdecl';
- Function DB_IsDatabase(DatabaseID: Integer): Integer;
- External 'DB_IsDatabase@libdb-0.2.dll cdecl';
- Function DB_NextRow(DatabaseID: Integer): Integer;
- External 'DB_NextRow@libdb-0.2.dll cdecl';
- Function DB_Open(DatabaseID: Integer; DatabaseName, User, Password: PChar; Plugin: Integer): Integer;
- External 'DB_Open@libdb-0.2.dll cdecl';
- Function DB_ExamineDrivers(): Integer;
- External 'DB_ExamineDrivers@libdb-0.2.dll cdecl';
- Function DB_NextDriver(): Integer;
- External 'DB_NextDriver@libdb-0.2.dll cdecl';
- Function DB_DriverDescription(): PChar;
- External 'DB_DriverDescription@libdb-0.2.dll cdecl';
- Function DB_DriverName(): PChar;
- External 'DB_DriverName@libdb-0.2.dll cdecl';
- Function DB_GetVersion(): Integer;
- External 'DB_GetVersion@libdb-0.2.dll cdecl';
- const
- DB_ID = 0;
- DB_NAME = 'PlayersDB.db';
- MSG_COLOR = $6495ed;
- function EscapeApostrophe(Source: String): String;
- begin
- Result := ReplaceRegExpr('''', Source, '''''', False);
- end;
- function GetWord(Source: String; WordNumber: Integer): String;
- var
- WordCounter, WordIndex, i: Integer;
- begin
- if WordNumber > 0 then begin
- for i := 1 to Length(Source) do
- if WordIndex <> 0 then begin
- if Source[i] = ' ' then begin
- Inc(WordCounter, 1);
- if WordNumber = WordCounter then begin
- Result := Copy(Source, WordIndex, i-WordIndex);
- break;
- end else
- WordIndex := 0;
- end else
- if i = Length(Source) then begin
- Inc(WordCounter, 1);
- if WordNumber = WordCounter then
- Result := Copy(Source, WordIndex, i-WordIndex+1);
- end;
- end else
- if Source[i] = ' ' then
- continue
- else
- if i = Length(Source) then begin
- Inc(WordCounter, 1);
- if WordNumber = WordCounter then
- Result := Source[i];
- end else
- WordIndex := i;
- end else
- WriteLn('Warning: Function "GetWord" - Second parameter has to be higher than "0"');
- end;
- procedure OnJoin(Player: TActivePlayer; Team: TTeam);
- begin
- if Player.Human then begin
- if DB_Query(DB_ID, 'SELECT Name, Hwid FROM Players WHERE Name = '''+EscapeApostrophe(Player.Name)+''' AND Hwid = '''+EscapeApostrophe(Player.HWID)+''' LIMIT 1;') = 0 then
- WriteLn('PlayersDB Error4: '+DB_Error)
- else begin
- if DB_FirstRow(DB_ID) = 0 then begin
- if DB_Update(DB_ID, 'INSERT INTO Players(Name, Hwid, Entry) VALUES('''+EscapeApostrophe(Player.Name)+''', '''+EscapeApostrophe(Player.HWID)+''', 1);') = 0 then
- WriteLn('PlayersDB Error5: '+DB_Error);
- end else
- if DB_Update(DB_ID, 'UPDATE Players SET Entry = Entry +1 WHERE Name = '''+EscapeApostrophe(Player.Name)+''' AND Hwid = '''+EscapeApostrophe(Player.HWID)+''';') = 0 then
- WriteLn('PlayersDB Error6: '+DB_Error);
- DB_FinishQuery(DB_ID);
- end;
- end;
- end;
- function OnAdminCommand(Player: TActivePlayer; Command: string): boolean;
- var
- TempID: Byte;
- begin
- Result := False;
- if (GetWord(Command, 1) = '/checkhw') and (GetWord(Command, 2) <> nil) then
- if DB_Query(DB_ID, 'SELECT Name, Entry FROM Players WHERE Hwid = '''+EscapeApostrophe(GetWord(Command, 2))+''' ORDER BY Entry;') <> 0 then begin
- While DB_NextRow(DB_ID) <> 0 Do
- Player.WriteConsole(DB_GetString(DB_ID, 0)+': '+DB_GetString(DB_ID, 1), MSG_COLOR);
- DB_FinishQuery(DB_ID);
- Player.WriteConsole('Result for HWID "'+GetWord(Command, 2)+'" has been sorted by entries in ascending order', MSG_COLOR);
- end else
- Player.WriteConsole('PlayersDB Error7: '+DB_Error, MSG_COLOR);
- if (Copy(Command, 1, 11) = '/checknick ') and (Copy(Command, 12, Length(Command)) <> nil) then
- if DB_Query(DB_ID, 'SELECT Hwid, Entry FROM Players WHERE Name = '''+EscapeApostrophe(Copy(Command, 12, Length(Command)))+''' ORDER BY Entry;') <> 0 then begin
- While DB_NextRow(DB_ID) <> 0 Do
- Player.WriteConsole(DB_GetString(DB_ID, 0)+': '+DB_GetString(DB_ID, 1), MSG_COLOR);
- DB_FinishQuery(DB_ID);
- Player.WriteConsole('Result for Nick "'+Copy(Command, 12, Length(Command))+'" has been sorted by entries in ascending order', MSG_COLOR);
- end else
- Player.WriteConsole('PlayersDB Error8: '+DB_Error, MSG_COLOR);
- if (GetWord(Command, 1) = '/checkid') and (GetWord(Command, 2) <> nil) then begin
- try
- TempID := StrToInt(GetWord(Command, 2));
- except
- Player.WriteConsole('"'+GetWord(Command, 2)+'" is invalid integer', MSG_COLOR);
- end;
- if (TempID > 0) and (TempID < 33) then begin
- if DB_Query(DB_ID, 'SELECT Name, Entry FROM Players WHERE Hwid = '''+EscapeApostrophe(Players[TempID].HWID)+''' ORDER BY Entry;') <> 0 then begin
- While DB_NextRow(DB_ID) <> 0 Do
- Player.WriteConsole(DB_GetString(DB_ID, 0)+': '+DB_GetString(DB_ID, 1), MSG_COLOR);
- DB_FinishQuery(DB_ID);
- Player.WriteConsole('Result for HWID "'+Players[TempID].HWID+'" has been sorted by entries in ascending order', MSG_COLOR);
- end else
- Player.WriteConsole('PlayersDB Error9: '+DB_Error, MSG_COLOR);
- end else
- Player.WriteConsole('ID has to be from 1 to 32', MSG_COLOR);
- end;
- end;
- function OnTCPCommand(Ip: string; Port: Word; Command: string): Boolean;
- var
- TempID: Byte;
- begin
- Result := False;
- if (GetWord(Command, 1) = '/checkhw') and (GetWord(Command, 2) <> nil) then
- if DB_Query(DB_ID, 'SELECT Name, Entry FROM Players WHERE Hwid = '''+EscapeApostrophe(GetWord(Command, 2))+''' ORDER BY Entry;') <> 0 then begin
- While DB_NextRow(DB_ID) <> 0 Do
- WriteLn(DB_GetString(DB_ID, 0)+': '+DB_GetString(DB_ID, 1));
- DB_FinishQuery(DB_ID);
- WriteLn('Result for HWID "'+GetWord(Command, 2)+'" has been sorted by entries in ascending order');
- end else
- WriteLn('PlayersDB Error10: '+DB_Error);
- if (Copy(Command, 1, 11) = '/checknick ') and (Copy(Command, 12, Length(Command)) <> nil) then
- if DB_Query(DB_ID, 'SELECT Hwid, Entry FROM Players WHERE Name = '''+EscapeApostrophe(Copy(Command, 12, Length(Command)))+''' ORDER BY Entry;') <> 0 then begin
- While DB_NextRow(DB_ID) <> 0 Do
- WriteLn(DB_GetString(DB_ID, 0)+': '+DB_GetString(DB_ID, 1));
- DB_FinishQuery(DB_ID);
- WriteLn('Result for Nick "'+Copy(Command, 12, Length(Command))+'" has been sorted by entries in ascending order');
- end else
- WriteLn('PlayersDB Error11: '+DB_Error);
- if (GetWord(Command, 1) = '/checkid') and (GetWord(Command, 2) <> nil) then begin
- try
- TempID := StrToInt(GetWord(Command, 2));
- except
- WriteLn('"'+GetWord(Command, 2)+'" is invalid integer');
- end;
- if (TempID > 0) and (TempID < 33) then begin
- if DB_Query(DB_ID, 'SELECT Name, Entry FROM Players WHERE Hwid = '''+EscapeApostrophe(Players[TempID].HWID)+''' ORDER BY Entry;') <> 0 then begin
- While DB_NextRow(DB_ID) <> 0 Do
- WriteLn(DB_GetString(DB_ID, 0)+': '+DB_GetString(DB_ID, 1));
- DB_FinishQuery(DB_ID);
- WriteLn('Result for HWID "'+Players[TempID].HWID+'" has been sorted by entries in ascending order');
- end else
- WriteLn('PlayersDB Error12: '+DB_Error);
- end else
- WriteLn('ID has to be from 1 to 32');
- end;
- end;
- procedure Init;
- var
- DBFile: TFileStream;
- begin
- if not File.Exists(DB_NAME) then begin
- DBFile := File.CreateFileStream;
- DBFile.SaveToFile(DB_NAME);
- DBFile.Free;
- WriteLn('Database "'+DB_NAME+'" has been created');
- if DB_Open(DB_ID, DB_NAME, '', '', DB_Plugin_SQLite) <> 0 then begin
- if DB_Update(DB_ID, 'CREATE TABLE Players(Id INTEGER PRIMARY KEY, Name TEXT, Hwid TEXT, Entry INTEGER);') = 0 then
- WriteLn('PlayersDB Error2: '+DB_Error);
- end else
- WriteLn('PlayersDB Error1: '+DB_Error);
- end else
- if DB_Open(DB_ID, DB_NAME, '', '', DB_Plugin_SQLite) = 0 then
- WriteLn('PlayersDB Error3: '+DB_Error);
- Game.OnJoin := @OnJoin;
- Game.OnAdminCommand := @OnAdminCommand;
- Game.OnTCPCommand := @OnTCPCommand;
- end;
- begin
- Init;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement