Advertisement
Guest User

PlayersDB

a guest
Nov 22nd, 2017
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 12.59 KB | None | 0 0
  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   }
  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. // WINDOWS
  21. // =====================================
  22. //        D E C L A R A T I O N S
  23. // =====================================
  24. Procedure DB_Close(DatabaseID: Integer);
  25. External 'DB_Close@libdb-0.2.dll cdecl';
  26.  
  27. Function DB_ColumnName(DatabaseID, Column: Integer): PChar;
  28. External 'DB_ColumnName@libdb-0.2.dll cdecl';
  29.  
  30. Function DB_ColumnSize(DatabaseID, Column: Integer): Integer;
  31. External 'DB_ColumnSize@libdb-0.2.dll cdecl';
  32.  
  33. Function DB_ColumnType(DatabaseID, Column: Integer): Integer;
  34. External 'DB_ColumnType@libdb-0.2.dll cdecl';
  35.  
  36. Function DB_Columns(DatabaseID: Integer): Integer;
  37. External 'DB_Columns@libdb-0.2.dll cdecl';
  38.  
  39. Function DB_Error(): PChar;
  40. External 'DB_Error@libdb-0.2.dll cdecl';
  41.  
  42. Function DB_Query(DatabaseID: Integer; Query: PChar): Integer;
  43. External 'DB_Query@libdb-0.2.dll cdecl';
  44.  
  45. Function DB_Update(DatabaseID: Integer; Query: PChar): Integer;
  46. External 'DB_Update@libdb-0.2.dll cdecl';
  47.  
  48. Procedure DB_FinishQuery(DatabaseID: Integer);
  49. External 'DB_FinishQuery@libdb-0.2.dll cdecl';
  50.  
  51. Function DB_FirstRow(DatabaseID: Integer): Integer;
  52. External 'DB_FirstRow@libdb-0.2.dll cdecl';
  53.  
  54. Function DB_GetDouble(DatabaseID, Column: Integer): Double;
  55. External 'DB_GetDouble@libdb-0.2.dll cdecl';
  56.  
  57. Function DB_GetFloat(DatabaseID, Column: Integer): Single;
  58. External 'DB_GetFloat@libdb-0.2.dll cdecl';
  59.  
  60. Function DB_GetLong(DatabaseID, Column: Integer): LongInt;
  61. External 'DB_GetLong@libdb-0.2.dll cdecl';
  62.  
  63. Function DB_GetString(DatabaseID, Column: Integer): PChar;
  64. External 'DB_GetString@libdb-0.2.dll cdecl';
  65.  
  66. Function DB_IsDatabase(DatabaseID: Integer): Integer;
  67. External 'DB_IsDatabase@libdb-0.2.dll cdecl';
  68.  
  69. Function DB_NextRow(DatabaseID: Integer): Integer;
  70. External 'DB_NextRow@libdb-0.2.dll cdecl';
  71.  
  72. Function DB_Open(DatabaseID: Integer; DatabaseName, User, Password: PChar; Plugin: Integer): Integer;
  73. External 'DB_Open@libdb-0.2.dll cdecl';
  74.  
  75. Function DB_ExamineDrivers(): Integer;
  76. External 'DB_ExamineDrivers@libdb-0.2.dll cdecl';
  77.  
  78. Function DB_NextDriver(): Integer;
  79. External 'DB_NextDriver@libdb-0.2.dll cdecl';
  80.  
  81. Function DB_DriverDescription(): PChar;
  82. External 'DB_DriverDescription@libdb-0.2.dll cdecl';
  83.  
  84. Function DB_DriverName(): PChar;
  85. External 'DB_DriverName@libdb-0.2.dll cdecl';
  86.  
  87. Function DB_GetVersion(): Integer;
  88. External '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. // =====================================
  94. Procedure DB_Close(DatabaseID: Integer);
  95. External 'DB_Close@libdb-0.2.so cdecl';
  96.  
  97. Function DB_ColumnName(DatabaseID, Column: Integer): PChar;
  98. External 'DB_ColumnName@libdb-0.2.so cdecl';
  99.  
  100. Function DB_ColumnSize(DatabaseID, Column: Integer): Integer;
  101. External 'DB_ColumnSize@libdb-0.2.so cdecl';
  102.  
  103. Function DB_ColumnType(DatabaseID, Column: Integer): Integer;
  104. External 'DB_ColumnType@libdb-0.2.so cdecl';
  105.  
  106. Function DB_Columns(DatabaseID: Integer): Integer;
  107. External 'DB_Columns@libdb-0.2.so cdecl';
  108.  
  109. Function DB_Error(): PChar;
  110. External 'DB_Error@libdb-0.2.so cdecl';
  111.  
  112. Function DB_Query(DatabaseID: Integer; Query: PChar): Integer;
  113. External 'DB_Query@libdb-0.2.so cdecl';
  114.  
  115. Function DB_Update(DatabaseID: Integer; Query: PChar): Integer;
  116. External 'DB_Update@libdb-0.2.so cdecl';
  117.  
  118. Procedure DB_FinishQuery(DatabaseID: Integer);
  119. External 'DB_FinishQuery@libdb-0.2.so cdecl';
  120.  
  121. Function DB_FirstRow(DatabaseID: Integer): Integer;
  122. External 'DB_FirstRow@libdb-0.2.so cdecl';
  123.  
  124. Function DB_GetDouble(DatabaseID, Column: Integer): Double;
  125. External 'DB_GetDouble@libdb-0.2.so cdecl';
  126.  
  127. Function DB_GetFloat(DatabaseID, Column: Integer): Single;
  128. External 'DB_GetFloat@libdb-0.2.so cdecl';
  129.  
  130. Function DB_GetLong(DatabaseID, Column: Integer): LongInt;
  131. External 'DB_GetLong@libdb-0.2.so cdecl';
  132.  
  133. Function DB_GetString(DatabaseID, Column: Integer): PChar;
  134. External 'DB_GetString@libdb-0.2.so cdecl';
  135.  
  136. Function DB_IsDatabase(DatabaseID: Integer): Integer;
  137. External 'DB_IsDatabase@libdb-0.2.so cdecl';
  138.  
  139. Function DB_NextRow(DatabaseID: Integer): Integer;
  140. External 'DB_NextRow@libdb-0.2.so cdecl';
  141.  
  142. Function DB_Open(DatabaseID: Integer; DatabaseName, User, Password: PChar; Plugin: Integer): Integer;
  143. External 'DB_Open@libdb-0.2.so cdecl';
  144.  
  145. Function DB_ExamineDrivers(): Integer;
  146. External 'DB_ExamineDrivers@libdb-0.2.so cdecl';
  147.  
  148. Function DB_NextDriver(): Integer;
  149. External 'DB_NextDriver@libdb-0.2.so cdecl';
  150.  
  151. Function DB_DriverDescription(): PChar;
  152. External 'DB_DriverDescription@libdb-0.2.so cdecl';
  153.  
  154. Function DB_DriverName(): PChar;
  155. External 'DB_DriverName@libdb-0.2.so cdecl';
  156.  
  157. Function DB_GetVersion(): Integer;
  158. External 'DB_GetVersion@libdb-0.2.so cdecl';
  159. }
  160.  
  161. const
  162.     DB_NAME = 'PlayersDB.db';
  163.     MSG_COLOR = $6495ed;
  164.    
  165. function EscapeApostrophe(Source: String): String;
  166. begin
  167.     Result := ReplaceRegExpr('''', Source, '''''', False);
  168. end;
  169.  
  170. function GetWord(Source: String; WordNumber: Integer): String;
  171. var
  172.     WordCounter, WordIndex, i: Integer;
  173. begin
  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"');
  202. end;
  203.  
  204. procedure OnJoin(Player: TActivePlayer; Team: TTeam);
  205. begin
  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;
  225. end;
  226.  
  227. function OnAdminCommand(Player: TActivePlayer; Command: string): boolean;
  228. var
  229.     TempID: Byte;
  230. begin
  231. Result := 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;
  277. end;
  278.  
  279. function OnTCPCommand(Ip: string; Port: Word; Command: string): Boolean;
  280. var
  281.     TempID: Byte;
  282. begin
  283. Result := 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;
  329. end;
  330.  
  331. procedure Init;
  332. var
  333.     DBFile: TFileStream;
  334. begin
  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;
  359. end;
  360.  
  361. begin
  362.     Init;
  363. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement