Advertisement
Guest User

PlayersDB 1.2 by Savage

a guest
Dec 1st, 2017
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 9.67 KB | None | 0 0
  1. //PlayersDB 1.2 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   }
  9. Const DB_Plugin_ODBC       = 1;
  10. Const DB_Plugin_SQLite     = 2;
  11. Const DB_Plugin_PostgreSQL = 3;
  12.  
  13. { Database column types enumeration
  14.   for the DB_ColumnType() function  }
  15. Const DB_Type_Double = 1;
  16. Const DB_Type_Float  = 2;
  17. Const DB_Type_Long   = 3;
  18. Const DB_Type_String = 4;
  19.  
  20. // =====================================
  21. //        D E C L A R A T I O N S
  22. // =====================================
  23. Procedure DB_Close(DatabaseID: Integer);
  24. External 'DB_Close@libdb-0.2.dll cdecl';
  25.  
  26. Function DB_ColumnName(DatabaseID, Column: Integer): PChar;
  27. External 'DB_ColumnName@libdb-0.2.dll cdecl';
  28.  
  29. Function DB_ColumnSize(DatabaseID, Column: Integer): Integer;
  30. External 'DB_ColumnSize@libdb-0.2.dll cdecl';
  31.  
  32. Function DB_ColumnType(DatabaseID, Column: Integer): Integer;
  33. External 'DB_ColumnType@libdb-0.2.dll cdecl';
  34.  
  35. Function DB_Columns(DatabaseID: Integer): Integer;
  36. External 'DB_Columns@libdb-0.2.dll cdecl';
  37.  
  38. Function DB_Error(): PChar;
  39. External 'DB_Error@libdb-0.2.dll cdecl';
  40.  
  41. Function DB_Query(DatabaseID: Integer; Query: PChar): Integer;
  42. External 'DB_Query@libdb-0.2.dll cdecl';
  43.  
  44. Function DB_Update(DatabaseID: Integer; Query: PChar): Integer;
  45. External 'DB_Update@libdb-0.2.dll cdecl';
  46.  
  47. Procedure DB_FinishQuery(DatabaseID: Integer);
  48. External 'DB_FinishQuery@libdb-0.2.dll cdecl';
  49.  
  50. Function DB_FirstRow(DatabaseID: Integer): Integer;
  51. External 'DB_FirstRow@libdb-0.2.dll cdecl';
  52.  
  53. Function DB_GetDouble(DatabaseID, Column: Integer): Double;
  54. External 'DB_GetDouble@libdb-0.2.dll cdecl';
  55.  
  56. Function DB_GetFloat(DatabaseID, Column: Integer): Single;
  57. External 'DB_GetFloat@libdb-0.2.dll cdecl';
  58.  
  59. Function DB_GetLong(DatabaseID, Column: Integer): LongInt;
  60. External 'DB_GetLong@libdb-0.2.dll cdecl';
  61.  
  62. Function DB_GetString(DatabaseID, Column: Integer): PChar;
  63. External 'DB_GetString@libdb-0.2.dll cdecl';
  64.  
  65. Function DB_IsDatabase(DatabaseID: Integer): Integer;
  66. External 'DB_IsDatabase@libdb-0.2.dll cdecl';
  67.  
  68. Function DB_NextRow(DatabaseID: Integer): Integer;
  69. External 'DB_NextRow@libdb-0.2.dll cdecl';
  70.  
  71. Function DB_Open(DatabaseID: Integer; DatabaseName, User, Password: PChar; Plugin: Integer): Integer;
  72. External 'DB_Open@libdb-0.2.dll cdecl';
  73.  
  74. Function DB_ExamineDrivers(): Integer;
  75. External 'DB_ExamineDrivers@libdb-0.2.dll cdecl';
  76.  
  77. Function DB_NextDriver(): Integer;
  78. External 'DB_NextDriver@libdb-0.2.dll cdecl';
  79.  
  80. Function DB_DriverDescription(): PChar;
  81. External 'DB_DriverDescription@libdb-0.2.dll cdecl';
  82.  
  83. Function DB_DriverName(): PChar;
  84. External 'DB_DriverName@libdb-0.2.dll cdecl';
  85.  
  86. Function DB_GetVersion(): Integer;
  87. External 'DB_GetVersion@libdb-0.2.dll cdecl';
  88.  
  89. const
  90.     DB_ID = 0;
  91.     DB_NAME = 'PlayersDB.db';
  92.     MSG_COLOR = $6495ed;
  93.    
  94. function EscapeApostrophe(Source: String): String;
  95. begin
  96.     Result := ReplaceRegExpr('''', Source, '''''', False);
  97. end;
  98.  
  99. function GetWord(Source: String; WordNumber: Integer): String;
  100. var
  101.     WordCounter, WordIndex, i: Integer;
  102. begin
  103.     if WordNumber > 0 then begin
  104.         for i := 1 to Length(Source) do
  105.             if WordIndex <> 0 then begin
  106.                 if Source[i] = ' ' then begin
  107.                     Inc(WordCounter, 1);
  108.                     if WordNumber = WordCounter then begin
  109.                         Result := Copy(Source, WordIndex, i-WordIndex);
  110.                         break;
  111.                     end else
  112.                         WordIndex := 0;
  113.                 end else
  114.                     if i = Length(Source) then begin
  115.                         Inc(WordCounter, 1);
  116.                         if WordNumber = WordCounter then
  117.                             Result := Copy(Source, WordIndex, i-WordIndex+1);
  118.                     end;
  119.             end else
  120.                 if Source[i] = ' ' then
  121.                     continue
  122.                 else
  123.                     if i = Length(Source) then begin
  124.                         Inc(WordCounter, 1);
  125.                         if WordNumber = WordCounter then
  126.                             Result := Source[i];
  127.                     end else
  128.                         WordIndex := i;
  129.     end else
  130.         WriteLn('Warning: Function "GetWord" - Second parameter has to be higher than "0"');
  131. end;
  132.  
  133. procedure OnJoin(Player: TActivePlayer; Team: TTeam);
  134. begin
  135.     if Player.Human then begin
  136.         if DB_Query(DB_ID, 'SELECT Name, Hwid FROM Players WHERE Name = '''+EscapeApostrophe(Player.Name)+''' AND Hwid = '''+EscapeApostrophe(Player.HWID)+''' LIMIT 1;') = 0 then
  137.             WriteLn('PlayersDB Error4: '+DB_Error)
  138.         else begin
  139.             if DB_FirstRow(DB_ID) = 0 then begin
  140.                 if DB_Update(DB_ID, 'INSERT INTO Players(Name, Hwid, Entry) VALUES('''+EscapeApostrophe(Player.Name)+''', '''+EscapeApostrophe(Player.HWID)+''', 1);') = 0 then
  141.                     WriteLn('PlayersDB Error5: '+DB_Error);
  142.             end else
  143.                 if DB_Update(DB_ID, 'UPDATE Players SET Entry = Entry +1 WHERE Name = '''+EscapeApostrophe(Player.Name)+''' AND Hwid = '''+EscapeApostrophe(Player.HWID)+''';') = 0 then
  144.                     WriteLn('PlayersDB Error6: '+DB_Error);
  145.                    
  146.             DB_FinishQuery(DB_ID);
  147.         end;
  148.     end;
  149. end;
  150.  
  151. function OnAdminCommand(Player: TActivePlayer; Command: string): boolean;
  152. var
  153.     TempID: Byte;
  154. begin
  155. Result := False;
  156.    
  157.     if (GetWord(Command, 1) = '/checkhw') and (GetWord(Command, 2) <> nil) then
  158.         if DB_Query(DB_ID, 'SELECT Name, Entry FROM Players WHERE Hwid = '''+EscapeApostrophe(GetWord(Command, 2))+''' ORDER BY Entry;') <> 0 then begin
  159.             While DB_NextRow(DB_ID) <> 0 Do
  160.                 Player.WriteConsole(DB_GetString(DB_ID, 0)+': '+DB_GetString(DB_ID, 1), MSG_COLOR);
  161.             DB_FinishQuery(DB_ID);
  162.             Player.WriteConsole('Result for HWID "'+GetWord(Command, 2)+'" has been sorted by entries in ascending order', MSG_COLOR);
  163.         end else
  164.             Player.WriteConsole('PlayersDB Error7: '+DB_Error, MSG_COLOR);
  165.            
  166.     if (Copy(Command, 1, 11) = '/checknick ') and (Copy(Command, 12, Length(Command)) <> nil) then
  167.         if DB_Query(DB_ID, 'SELECT Hwid, Entry FROM Players WHERE Name = '''+EscapeApostrophe(Copy(Command, 12, Length(Command)))+''' ORDER BY Entry;') <> 0 then begin
  168.             While DB_NextRow(DB_ID) <> 0 Do
  169.                 Player.WriteConsole(DB_GetString(DB_ID, 0)+': '+DB_GetString(DB_ID, 1), MSG_COLOR);
  170.             DB_FinishQuery(DB_ID);
  171.             Player.WriteConsole('Result for Nick "'+Copy(Command, 12, Length(Command))+'" has been sorted by entries in ascending order', MSG_COLOR);
  172.         end else
  173.             Player.WriteConsole('PlayersDB Error8: '+DB_Error, MSG_COLOR);
  174.            
  175.     if (GetWord(Command, 1) = '/checkid') and (GetWord(Command, 2) <> nil) then begin
  176.         try
  177.             TempID := StrToInt(GetWord(Command, 2));
  178.         except
  179.             Player.WriteConsole('"'+GetWord(Command, 2)+'" is invalid integer', MSG_COLOR);
  180.         end;
  181.         if (TempID > 0) and (TempID < 33) then begin
  182.             if DB_Query(DB_ID, 'SELECT Name, Entry FROM Players WHERE Hwid = '''+EscapeApostrophe(Players[TempID].HWID)+''' ORDER BY Entry;') <> 0 then begin
  183.                 While DB_NextRow(DB_ID) <> 0 Do
  184.                     Player.WriteConsole(DB_GetString(DB_ID, 0)+': '+DB_GetString(DB_ID, 1), MSG_COLOR);
  185.                 DB_FinishQuery(DB_ID);
  186.                 Player.WriteConsole('Result for HWID "'+Players[TempID].HWID+'" has been sorted by entries in ascending order', MSG_COLOR);
  187.             end else
  188.                 Player.WriteConsole('PlayersDB Error9: '+DB_Error, MSG_COLOR);
  189.         end else
  190.             Player.WriteConsole('ID has to be from 1 to 32', MSG_COLOR);
  191.     end;
  192. end;
  193.  
  194. function OnTCPCommand(Ip: string; Port: Word; Command: string): Boolean;
  195. var
  196.     TempID: Byte;
  197. begin
  198. Result := False;
  199.    
  200.     if (GetWord(Command, 1) = '/checkhw') and (GetWord(Command, 2) <> nil) then
  201.         if DB_Query(DB_ID, 'SELECT Name, Entry FROM Players WHERE Hwid = '''+EscapeApostrophe(GetWord(Command, 2))+''' ORDER BY Entry;') <> 0 then begin
  202.             While DB_NextRow(DB_ID) <> 0 Do
  203.                 WriteLn(DB_GetString(DB_ID, 0)+': '+DB_GetString(DB_ID, 1));
  204.             DB_FinishQuery(DB_ID);
  205.             WriteLn('Result for HWID "'+GetWord(Command, 2)+'" has been sorted by entries in ascending order');
  206.         end else
  207.             WriteLn('PlayersDB Error10: '+DB_Error);
  208.            
  209.     if (Copy(Command, 1, 11) = '/checknick ') and (Copy(Command, 12, Length(Command)) <> nil) then
  210.         if DB_Query(DB_ID, 'SELECT Hwid, Entry FROM Players WHERE Name = '''+EscapeApostrophe(Copy(Command, 12, Length(Command)))+''' ORDER BY Entry;') <> 0 then begin
  211.             While DB_NextRow(DB_ID) <> 0 Do
  212.                 WriteLn(DB_GetString(DB_ID, 0)+': '+DB_GetString(DB_ID, 1));
  213.             DB_FinishQuery(DB_ID);
  214.             WriteLn('Result for Nick "'+Copy(Command, 12, Length(Command))+'" has been sorted by entries in ascending order');
  215.         end else
  216.             WriteLn('PlayersDB Error11: '+DB_Error);
  217.            
  218.     if (GetWord(Command, 1) = '/checkid') and (GetWord(Command, 2) <> nil) then begin
  219.         try
  220.             TempID := StrToInt(GetWord(Command, 2));
  221.         except
  222.             WriteLn('"'+GetWord(Command, 2)+'" is invalid integer');
  223.         end;
  224.         if (TempID > 0) and (TempID < 33) then begin
  225.             if DB_Query(DB_ID, 'SELECT Name, Entry FROM Players WHERE Hwid = '''+EscapeApostrophe(Players[TempID].HWID)+''' ORDER BY Entry;') <> 0 then begin
  226.                 While DB_NextRow(DB_ID) <> 0 Do
  227.                     WriteLn(DB_GetString(DB_ID, 0)+': '+DB_GetString(DB_ID, 1));
  228.                 DB_FinishQuery(DB_ID);
  229.                 WriteLn('Result for HWID "'+Players[TempID].HWID+'" has been sorted by entries in ascending order');
  230.             end else
  231.                 WriteLn('PlayersDB Error12: '+DB_Error);
  232.         end else
  233.             WriteLn('ID has to be from 1 to 32');
  234.     end;
  235. end;
  236.  
  237. procedure Init;
  238. var
  239.     DBFile: TFileStream;
  240. begin
  241.     if not File.Exists(DB_NAME) then begin
  242.         DBFile := File.CreateFileStream;
  243.         DBFile.SaveToFile(DB_NAME);
  244.         DBFile.Free;
  245.         WriteLn('Database "'+DB_NAME+'" has been created');
  246.         if DB_Open(DB_ID, DB_NAME, '', '', DB_Plugin_SQLite) <> 0 then begin
  247.             if DB_Update(DB_ID, 'CREATE TABLE Players(Id INTEGER PRIMARY KEY, Name TEXT, Hwid TEXT, Entry INTEGER);') = 0 then
  248.                 WriteLn('PlayersDB Error2: '+DB_Error);
  249.         end else
  250.             WriteLn('PlayersDB Error1: '+DB_Error);
  251.     end else
  252.         if DB_Open(DB_ID, DB_NAME, '', '', DB_Plugin_SQLite) = 0 then
  253.             WriteLn('PlayersDB Error3: '+DB_Error);
  254.    
  255.     Game.OnJoin := @OnJoin;
  256.     Game.OnAdminCommand := @OnAdminCommand;
  257.     Game.OnTCPCommand := @OnTCPCommand;
  258. end;
  259.  
  260. begin
  261.     Init;
  262. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement