2015-08-20 01:06:44 -04:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Data.SQLite ;
using System.Data ;
using System.IO ;
namespace SharedLibrary
{
public abstract class Database
{
public Database ( String FN )
{
FileName = FN ;
DBCon = String . Format ( "Data Source={0}" , FN ) ;
try
{
Con = new SQLiteConnection ( DBCon ) ;
}
2017-05-27 00:22:50 -04:00
catch ( DllNotFoundException )
2015-08-20 01:06:44 -04:00
{
2015-08-20 13:52:30 -04:00
Console . WriteLine ( "Fatal Error: could not locate the SQLite DLL(s)!\nEnsure they are located in the 'lib' folder" ) ;
2015-08-20 01:06:44 -04:00
Utilities . Wait ( 5 ) ;
2017-05-26 18:49:27 -04:00
Environment . Exit ( 0 ) ;
2015-08-20 01:06:44 -04:00
}
2017-05-27 00:22:50 -04:00
2015-08-20 01:06:44 -04:00
Open = false ;
Init ( ) ;
}
abstract public void Init ( ) ;
protected bool Insert ( String tableName , Dictionary < String , object > data )
{
2017-05-26 18:49:27 -04:00
string names = "" ;
string parameters = "" ;
foreach ( string key in data . Keys )
2015-08-20 01:06:44 -04:00
{
2017-05-26 18:49:27 -04:00
names + = key + ',' ;
parameters + = '@' + key + ',' ;
2015-08-20 01:06:44 -04:00
}
2017-05-26 18:49:27 -04:00
names = names . Substring ( 0 , names . Length - 1 ) ;
parameters = parameters . Substring ( 0 , parameters . Length - 1 ) ;
SQLiteCommand insertcmd = new SQLiteCommand ( ) ;
insertcmd . Connection = this . Con ;
insertcmd . CommandText = String . Format ( "INSERT INTO `{0}` ({1}) VALUES ({2});" , tableName , names , parameters ) ;
foreach ( string key in data . Keys )
{
insertcmd . Parameters . AddWithValue ( '@' + key , data [ key ] ) ;
}
2015-08-20 01:06:44 -04:00
try
{
2017-05-26 18:49:27 -04:00
Con . Open ( ) ;
insertcmd . ExecuteNonQuery ( ) ;
Con . Close ( ) ;
return true ;
2015-08-20 01:06:44 -04:00
}
2017-05-26 18:49:27 -04:00
catch ( Exception )
2015-08-20 01:06:44 -04:00
{
2017-05-26 18:49:27 -04:00
//LOGME
return false ;
2015-08-20 01:06:44 -04:00
}
2017-05-26 18:49:27 -04:00
2015-08-20 01:06:44 -04:00
}
2017-05-26 18:49:27 -04:00
protected bool Update ( String tableName , Dictionary < String , object > data , KeyValuePair < string , object > where )
2015-08-20 01:06:44 -04:00
{
2017-05-26 18:49:27 -04:00
string parameters = "" ;
foreach ( string key in data . Keys )
2015-08-20 01:06:44 -04:00
{
2017-05-26 18:49:27 -04:00
parameters + = key + '=' + '@' + key + ',' ;
}
parameters = parameters . Substring ( 0 , parameters . Length - 1 ) ;
SQLiteCommand updatecmd = new SQLiteCommand ( ) ;
updatecmd . Connection = this . Con ;
updatecmd . CommandText = String . Format ( "UPDATE `{0}` SET {1} WHERE {2}=@{2}" , tableName , parameters , where . Key ) ;
foreach ( string key in data . Keys )
{
updatecmd . Parameters . AddWithValue ( '@' + key , data [ key ] ) ;
2015-08-20 01:06:44 -04:00
}
2017-05-26 18:49:27 -04:00
updatecmd . Parameters . AddWithValue ( '@' + where . Key , where . Value ) ;
2015-08-20 01:06:44 -04:00
try
{
2017-05-26 18:49:27 -04:00
Con . Open ( ) ;
updatecmd . ExecuteNonQuery ( ) ;
Con . Close ( ) ;
return true ;
2015-08-20 01:06:44 -04:00
}
2017-05-26 18:49:27 -04:00
catch ( Exception e )
2015-08-20 01:06:44 -04:00
{
2017-05-26 18:49:27 -04:00
//LOGME
return false ;
2015-08-20 01:06:44 -04:00
}
}
protected DataRow getDataRow ( String Q )
{
DataRow Result = GetDataTable ( Q ) . Rows [ 0 ] ;
return Result ;
}
protected int ExecuteNonQuery ( String Request )
{
waitForClose ( ) ;
int rowsUpdated = 0 ;
2017-05-27 00:22:50 -04:00
Request = Request . Replace ( "!'" , "" ) . Replace ( "!" , "" ) ;
2017-05-26 18:49:27 -04:00
try
{
lock ( Con )
{
Con . Open ( ) ;
SQLiteCommand CMD = new SQLiteCommand ( Con ) ;
CMD . CommandText = Request ;
rowsUpdated = CMD . ExecuteNonQuery ( ) ;
Con . Close ( ) ;
}
return rowsUpdated ;
}
2015-08-20 01:06:44 -04:00
2017-05-26 18:49:27 -04:00
catch ( Exception E )
{
Console . WriteLine ( E . Message ) ;
Console . WriteLine ( E . StackTrace ) ;
Console . WriteLine ( Request ) ;
return 0 ;
2017-05-27 00:22:50 -04:00
}
2017-05-26 18:49:27 -04:00
}
protected DataTable GetDataTable ( string tableName , KeyValuePair < string , object > where )
{
DataTable dt = new DataTable ( ) ;
SQLiteCommand updatecmd = new SQLiteCommand ( ) ;
updatecmd . Connection = this . Con ;
updatecmd . CommandText = String . Format ( "SELECT * FROM {0} WHERE `{1}`=@{1};" , tableName , where . Key ) ;
updatecmd . Parameters . AddWithValue ( '@' + where . Key , where . Value ) ;
try
2015-08-20 01:06:44 -04:00
{
Con . Open ( ) ;
2017-05-26 18:49:27 -04:00
SQLiteDataReader reader = updatecmd . ExecuteReader ( ) ;
dt . Load ( reader ) ;
reader . Close ( ) ;
2015-08-20 01:06:44 -04:00
Con . Close ( ) ;
}
2017-05-27 00:22:50 -04:00
catch ( Exception e )
{
//LOGME
2017-05-27 18:08:04 -04:00
Console . WriteLine ( $"Line 160: {e.Message}" ) ;
2017-05-27 00:22:50 -04:00
}
return dt ;
}
protected DataTable GetDataTable ( SQLiteCommand cmd )
{
DataTable dt = new DataTable ( ) ;
try
{
Con . Open ( ) ;
SQLiteDataReader reader = cmd . ExecuteReader ( ) ;
dt . Load ( reader ) ;
reader . Close ( ) ;
Con . Close ( ) ;
}
2017-05-26 18:49:27 -04:00
catch ( Exception e )
{
//LOGME
2017-05-27 18:08:04 -04:00
Console . Write ( $"Line 181: {e.Message}" ) ;
2017-05-26 18:49:27 -04:00
}
return dt ;
2015-08-20 01:06:44 -04:00
}
protected DataTable GetDataTable ( String sql )
{
DataTable dt = new DataTable ( ) ;
2017-05-27 00:22:50 -04:00
2015-08-20 01:06:44 -04:00
try
{
waitForClose ( ) ;
lock ( Con )
{
Con . Open ( ) ;
SQLiteCommand mycommand = new SQLiteCommand ( Con ) ;
mycommand . CommandText = sql ;
SQLiteDataReader reader = mycommand . ExecuteReader ( ) ;
dt . Load ( reader ) ;
reader . Close ( ) ;
Con . Close ( ) ;
}
}
catch ( Exception e )
{
2017-05-26 18:49:27 -04:00
Console . WriteLine ( e . Message + " GetDataTable" ) ;
2015-08-20 17:54:38 -04:00
return new DataTable ( ) ;
2015-08-20 01:06:44 -04:00
}
return dt ;
}
protected void waitForClose ( )
{
while ( Con . State = = ConnectionState . Open )
{
Utilities . Wait ( 0.01 ) ;
}
return ;
}
protected String FileName ;
protected String DBCon ;
protected SQLiteConnection Con ;
protected bool Open ;
}
public class ClientsDB : Database
{
public ClientsDB ( String FN ) : base ( FN ) { }
public override void Init ( )
{
if ( ! File . Exists ( FileName ) )
{
2017-05-27 00:22:50 -04:00
String Create = "CREATE TABLE [CLIENTS] ( [Name] TEXT NULL, [npID] TEXT NULL, [Number] INTEGER PRIMARY KEY AUTOINCREMENT, [Level] INT DEFAULT 0 NULL, [LastOffense] TEXT NULL, [Connections] INT DEFAULT 1 NULL, [IP] TEXT NULL, [LastConnection] TEXT NULL, [UID] TEXT NULL, [Masked] INT DEFAULT 0, [Reserved] INT DEFAULT 0);" ;
2015-08-20 01:06:44 -04:00
ExecuteNonQuery ( Create ) ;
2016-01-15 17:15:39 -05:00
Create = "CREATE TABLE [BANS] ( [TYPE] TEXT NULL, [Reason] TEXT NULL, [npID] TEXT NULL, [bannedByID] TEXT NULL, [IP] TEXT NULL, [TIME] TEXT NULL);" ;
2015-08-20 01:06:44 -04:00
ExecuteNonQuery ( Create ) ;
}
}
//Returns a single player object with matching GUID, false if no matches
public Player getPlayer ( String ID , int cNum )
{
2017-05-27 00:22:50 -04:00
DataTable Result = GetDataTable ( "CLIENTS" , new KeyValuePair < string , object > ( "npID" , ID ) ) ;
2015-08-20 01:06:44 -04:00
if ( Result ! = null & & Result . Rows . Count > 0 )
{
DataRow ResponseRow = Result . Rows [ 0 ] ;
2015-08-21 21:11:35 -04:00
DateTime lastCon = DateTime . MinValue ;
DateTime . TryParse ( ResponseRow [ "LastConnection" ] . ToString ( ) , out lastCon ) ;
2015-08-20 01:06:44 -04:00
2017-05-26 18:49:27 -04:00
return new Player ( ResponseRow [ "Name" ] . ToString ( ) , ResponseRow [ "npID" ] . ToString ( ) , cNum , ( Player . Permission ) ( ResponseRow [ "Level" ] ) , Convert . ToInt32 ( ResponseRow [ "Number" ] ) , ResponseRow [ "LastOffense" ] . ToString ( ) , ( int ) ResponseRow [ "Connections" ] , ResponseRow [ "IP" ] . ToString ( ) , lastCon , ResponseRow [ "UID" ] . ToString ( ) , ResponseRow [ "Masked" ] . ToString ( ) = = "1" ) ;
2015-08-21 21:11:35 -04:00
}
else
return null ;
}
2017-05-26 18:49:27 -04:00
public List < Player > getRecentPlayers ( )
{
List < Player > returnssss = new List < Player > ( ) ;
2017-05-27 18:08:04 -04:00
String Query = String . Format ( $"SELECT * FROM CLIENTS LIMIT 15 OFFSET (SELECT COUNT(*) FROM CLIENTS)-15" ) ;
2017-05-26 18:49:27 -04:00
DataTable Result = GetDataTable ( Query ) ;
if ( Result ! = null & & Result . Rows . Count > 0 )
{
foreach ( DataRow ResponseRow in Result . Rows )
{
DateTime lastCon = DateTime . MinValue ;
DateTime . TryParse ( ResponseRow [ "LastConnection" ] . ToString ( ) , out lastCon ) ;
returnssss . Add ( new Player ( ResponseRow [ "Name" ] . ToString ( ) , ResponseRow [ "npID" ] . ToString ( ) , - 1 , ( Player . Permission ) ( ResponseRow [ "Level" ] ) , Convert . ToInt32 ( ResponseRow [ "Number" ] ) , ResponseRow [ "LastOffense" ] . ToString ( ) , ( int ) ResponseRow [ "Connections" ] , ResponseRow [ "IP" ] . ToString ( ) , lastCon , ResponseRow [ "UID" ] . ToString ( ) , ResponseRow [ "Masked" ] . ToString ( ) = = "1" ) ) ;
}
}
2017-05-27 18:08:04 -04:00
return returnssss . OrderByDescending ( p = > p . LastConnection ) . ToList ( ) ; ;
2017-05-26 18:49:27 -04:00
}
2015-08-21 21:11:35 -04:00
public List < Player > getPlayers ( List < String > npIDs )
{
List < Player > returnssss = new List < Player > ( ) ;
String test = String . Join ( "' OR npID = '" , npIDs ) ;
2017-05-27 00:22:50 -04:00
2015-08-21 21:11:35 -04:00
String Query = String . Format ( "SELECT * FROM CLIENTS WHERE npID = '{0}'" , test ) ;
DataTable Result = GetDataTable ( Query ) ;
if ( Result ! = null & & Result . Rows . Count > 0 )
{
foreach ( DataRow ResponseRow in Result . Rows )
2015-08-20 01:06:44 -04:00
{
2015-08-21 21:11:35 -04:00
DateTime lastCon = DateTime . MinValue ;
DateTime . TryParse ( ResponseRow [ "LastConnection" ] . ToString ( ) , out lastCon ) ;
2017-05-26 18:49:27 -04:00
returnssss . Add ( new Player ( ResponseRow [ "Name" ] . ToString ( ) , ResponseRow [ "npID" ] . ToString ( ) , - 1 , ( Player . Permission ) ( ResponseRow [ "Level" ] ) , Convert . ToInt32 ( ResponseRow [ "Number" ] ) , ResponseRow [ "LastOffense" ] . ToString ( ) , ( int ) ResponseRow [ "Connections" ] , ResponseRow [ "IP" ] . ToString ( ) , lastCon , ResponseRow [ "UID" ] . ToString ( ) , ResponseRow [ "Masked" ] . ToString ( ) = = "1" ) ) ;
2015-08-20 01:06:44 -04:00
}
2015-08-21 21:11:35 -04:00
}
return returnssss ;
}
public List < Player > getPlayers ( List < int > databaseIDs )
{
List < Player > returnssss = new List < Player > ( ) ;
String test = String . Join ( "' OR Number = '" , databaseIDs ) ;
String Query = String . Format ( "SELECT * FROM CLIENTS WHERE Number = '{0}'" , test ) ;
DataTable Result = GetDataTable ( Query ) ;
if ( Result ! = null & & Result . Rows . Count > 0 )
{
foreach ( DataRow ResponseRow in Result . Rows )
2015-08-20 01:06:44 -04:00
{
2015-08-21 21:11:35 -04:00
DateTime lastCon = DateTime . MinValue ;
DateTime . TryParse ( ResponseRow [ "LastConnection" ] . ToString ( ) , out lastCon ) ;
2015-08-20 01:06:44 -04:00
2017-05-26 18:49:27 -04:00
returnssss . Add ( new Player ( ResponseRow [ "Name" ] . ToString ( ) , ResponseRow [ "npID" ] . ToString ( ) , - 1 , ( Player . Permission ) ( ResponseRow [ "Level" ] ) , Convert . ToInt32 ( ResponseRow [ "Number" ] ) , ResponseRow [ "LastOffense" ] . ToString ( ) , ( int ) ResponseRow [ "Connections" ] , ResponseRow [ "IP" ] . ToString ( ) , lastCon , ResponseRow [ "UID" ] . ToString ( ) , ResponseRow [ "Masked" ] . ToString ( ) = = "1" ) ) ;
2015-08-21 21:11:35 -04:00
}
2015-08-20 01:06:44 -04:00
}
2015-08-21 21:11:35 -04:00
return returnssss ;
2015-08-20 01:06:44 -04:00
}
//Overloaded method for getPlayer, returns Client with matching DBIndex, null if none found
public Player getPlayer ( int dbIndex )
{
2017-05-27 00:22:50 -04:00
DataTable Result = GetDataTable ( "CLIENTS" , new KeyValuePair < string , object > ( "Number" , dbIndex ) ) ;
2015-08-20 01:06:44 -04:00
if ( Result ! = null & & Result . Rows . Count > 0 )
{
DataRow p = Result . Rows [ 0 ] ;
DateTime LC ;
try
{
LC = DateTime . Parse ( p [ "LastConnection" ] . ToString ( ) ) ;
}
catch ( Exception )
{
2015-08-21 21:11:35 -04:00
LC = DateTime . MinValue ;
2015-08-20 01:06:44 -04:00
}
2017-05-26 18:49:27 -04:00
return new Player ( p [ "Name" ] . ToString ( ) , p [ "npID" ] . ToString ( ) , - 1 , ( Player . Permission ) ( p [ "Level" ] ) , Convert . ToInt32 ( p [ "Number" ] ) , p [ "LastOffense" ] . ToString ( ) , Convert . ToInt32 ( p [ "Connections" ] ) , p [ "IP" ] . ToString ( ) , LC , p [ "UID" ] . ToString ( ) , p [ "Masked" ] . ToString ( ) = = "1" ) ;
2015-08-20 01:06:44 -04:00
}
else
return null ;
}
//get player by ip, (used for webfront)
public Player getPlayer ( String IP )
{
2017-05-27 00:22:50 -04:00
DataTable Result = GetDataTable ( "CLIENTS" , new KeyValuePair < string , object > ( "IP" , IP ) ) ;
2015-08-20 01:06:44 -04:00
if ( Result ! = null & & Result . Rows . Count > 0 )
{
List < Player > lastKnown = new List < Player > ( ) ;
foreach ( DataRow p in Result . Rows )
{
DateTime LC ;
try
{
LC = DateTime . Parse ( p [ "LastConnection" ] . ToString ( ) ) ;
2017-05-26 18:49:27 -04:00
lastKnown . Add ( new Player ( p [ "Name" ] . ToString ( ) , p [ "npID" ] . ToString ( ) , - 1 , ( Player . Permission ) ( p [ "Level" ] ) , Convert . ToInt32 ( p [ "Number" ] ) , p [ "LastOffense" ] . ToString ( ) , Convert . ToInt32 ( ( DateTime . Now - LC ) . TotalSeconds ) , p [ "IP" ] . ToString ( ) , LC , p [ "UID" ] . ToString ( ) , p [ "Masked" ] . ToString ( ) = = "1" ) ) ;
2015-08-20 01:06:44 -04:00
}
catch ( Exception )
{
continue ;
}
}
if ( lastKnown . Count > 0 )
{
List < Player > Returning = lastKnown . OrderBy ( t = > t . Connections ) . ToList ( ) ;
return Returning [ 0 ] ;
}
else
return null ;
}
else
return null ;
}
//Returns a list of players matching name parameter, null if no players found matching
public List < Player > findPlayers ( String name )
{
2017-05-27 00:22:50 -04:00
SQLiteCommand cmd = new SQLiteCommand ( Con ) ;
cmd . CommandText = "SELECT * FROM CLIENTS WHERE Name LIKE @Name LIMIT 32" ;
cmd . Parameters . AddWithValue ( "@Name" , '%' + name + '%' ) ;
var Result = GetDataTable ( cmd ) ;
2015-08-20 01:06:44 -04:00
List < Player > Players = new List < Player > ( ) ;
if ( Result ! = null & & Result . Rows . Count > 0 )
{
foreach ( DataRow p in Result . Rows )
{
DateTime LC ;
2017-05-26 18:49:27 -04:00
string Masked = null ;
2015-08-20 01:06:44 -04:00
try
{
LC = DateTime . Parse ( p [ "LastConnection" ] . ToString ( ) ) ;
2017-05-26 18:49:27 -04:00
Masked = p [ "Masked" ] . ToString ( ) ;
2015-08-20 01:06:44 -04:00
}
catch ( Exception )
{
2017-05-26 18:49:27 -04:00
if ( Masked = = null )
Masked = "0" ;
2015-08-21 21:11:35 -04:00
LC = DateTime . MinValue ;
2015-08-20 01:06:44 -04:00
}
2017-05-26 18:49:27 -04:00
Players . Add ( new Player ( p [ "Name" ] . ToString ( ) , p [ "npID" ] . ToString ( ) , - 1 , ( Player . Permission ) ( p [ "Level" ] ) , Convert . ToInt32 ( p [ "Number" ] ) , p [ "LastOffense" ] . ToString ( ) , Convert . ToInt32 ( p [ "Connections" ] ) , p [ "IP" ] . ToString ( ) , LC , p [ "IP" ] . ToString ( ) , Masked = = "1" ) ) ;
2015-08-20 01:06:44 -04:00
}
return Players ;
}
else
return null ;
}
//Returns any player with level 4 permissions, null if no owner found
public Player getOwner ( )
{
String Query = String . Format ( "SELECT * FROM CLIENTS WHERE Level > '{0}'" , 4 ) ;
DataTable Result = GetDataTable ( Query ) ;
if ( Result ! = null & & Result . Rows . Count > 0 )
{
DataRow ResponseRow = Result . Rows [ 0 ] ;
if ( ResponseRow [ "IP" ] . ToString ( ) . Length < 6 )
ResponseRow [ "IP" ] = "0" ;
return new Player ( ResponseRow [ "Name" ] . ToString ( ) , ResponseRow [ "npID" ] . ToString ( ) , - 1 , ( Player . Permission ) ( ResponseRow [ "Level" ] ) , Convert . ToInt32 ( ResponseRow [ "Number" ] ) , null , 0 , ResponseRow [ "IP" ] . ToString ( ) ) ;
}
else
return null ;
}
2017-05-27 18:08:04 -04:00
public List < Penalty > GetClientPenalties ( Player P )
{
List < Penalty > ClientPenalties = new List < Penalty > ( ) ;
String Query = $"SELECT * FROM `BANS` WHERE `npID` = '{P.npID}' OR `IP` = '{P.IP}'" ;
DataTable Result = GetDataTable ( Query ) ;
foreach ( DataRow Row in Result . Rows )
{
if ( Row [ "TIME" ] . ToString ( ) . Length < 2 ) //compatibility with my old database
Row [ "TIME" ] = DateTime . Now . ToString ( ) ;
SharedLibrary . Penalty . Type BanType = Penalty . Type . Ban ;
if ( Row [ "TYPE" ] . ToString ( ) . Length ! = 0 )
BanType = ( Penalty . Type ) Enum . Parse ( typeof ( Penalty . Type ) , Row [ "TYPE" ] . ToString ( ) ) ;
ClientPenalties . Add ( new Penalty ( BanType , Row [ "Reason" ] . ToString ( ) . Trim ( ) , Row [ "npID" ] . ToString ( ) , Row [ "bannedByID" ] . ToString ( ) , DateTime . Parse ( Row [ "TIME" ] . ToString ( ) ) , Row [ "IP" ] . ToString ( ) ) ) ;
}
return ClientPenalties ;
}
public List < Penalty > GetPenaltiesChronologically ( int offset , int count )
2015-08-20 01:06:44 -04:00
{
2017-05-27 18:08:04 -04:00
List < Penalty > ClientPenalties = new List < Penalty > ( ) ;
DataTable Result = GetDataTable ( $"SELECT * FROM BANS LIMIT {count} OFFSET (SELECT COUNT(*) FROM BANS)-{offset + 10}" ) ;
2015-08-20 01:06:44 -04:00
foreach ( DataRow Row in Result . Rows )
{
if ( Row [ "TIME" ] . ToString ( ) . Length < 2 ) //compatibility with my old database
Row [ "TIME" ] = DateTime . Now . ToString ( ) ;
2016-01-15 17:15:39 -05:00
SharedLibrary . Penalty . Type BanType = Penalty . Type . Ban ;
if ( Row [ "TYPE" ] . ToString ( ) . Length ! = 0 )
BanType = ( Penalty . Type ) Enum . Parse ( typeof ( Penalty . Type ) , Row [ "TYPE" ] . ToString ( ) ) ;
2017-05-27 18:08:04 -04:00
ClientPenalties . Add ( new Penalty ( BanType , Row [ "Reason" ] . ToString ( ) . Trim ( ) , Row [ "npID" ] . ToString ( ) , Row [ "bannedByID" ] . ToString ( ) , DateTime . Parse ( Row [ "TIME" ] . ToString ( ) ) , Row [ "IP" ] . ToString ( ) ) ) ;
2017-05-27 00:22:50 -04:00
2015-08-20 01:06:44 -04:00
}
2017-05-27 18:08:04 -04:00
return ClientPenalties ;
2015-08-20 01:06:44 -04:00
}
//Returns all players with level > Flagged
public List < Player > getAdmins ( )
{
List < Player > Admins = new List < Player > ( ) ;
2017-05-26 18:49:27 -04:00
String Query = String . Format ( "SELECT * FROM CLIENTS WHERE Level >= '{0}'" , ( int ) Player . Permission . Moderator ) ;
2015-08-20 01:06:44 -04:00
DataTable Result = GetDataTable ( Query ) ;
foreach ( DataRow P in Result . Rows )
2017-05-26 18:49:27 -04:00
Admins . Add ( new Player ( P [ "Name" ] . ToString ( ) , P [ "npID" ] . ToString ( ) , ( Player . Permission ) P [ "Level" ] , P [ "IP" ] . ToString ( ) , P [ "UID" ] . ToString ( ) ) ) ;
2015-08-20 01:06:44 -04:00
return Admins ;
}
//Returns total number of player entries in database
public int totalPlayers ( )
{
DataTable Result = GetDataTable ( "SELECT * from CLIENTS ORDER BY Number DESC LIMIT 1" ) ;
if ( Result . Rows . Count > 0 )
return Convert . ToInt32 ( Result . Rows [ 0 ] [ "Number" ] ) ;
else
return 0 ;
}
//Add specified player to database
public void addPlayer ( Player P )
{
Dictionary < String , object > newPlayer = new Dictionary < String , object > ( ) ;
newPlayer . Add ( "Name" , Utilities . removeNastyChars ( P . Name ) ) ;
newPlayer . Add ( "npID" , P . npID ) ;
newPlayer . Add ( "Level" , ( int ) P . Level ) ;
newPlayer . Add ( "LastOffense" , "" ) ;
newPlayer . Add ( "Connections" , 1 ) ;
newPlayer . Add ( "IP" , P . IP ) ;
newPlayer . Add ( "LastConnection" , Utilities . DateTimeSQLite ( DateTime . Now ) ) ;
2017-05-26 18:49:27 -04:00
newPlayer . Add ( "UID" , P . UID ) ;
newPlayer . Add ( "Masked" , Convert . ToInt32 ( P . Masked ) ) ;
2015-08-20 01:06:44 -04:00
Insert ( "CLIENTS" , newPlayer ) ;
}
///Update information of specified player
public void updatePlayer ( Player P )
{
Dictionary < String , Object > updatedPlayer = new Dictionary < String , Object > ( ) ;
updatedPlayer . Add ( "Name" , P . Name ) ;
updatedPlayer . Add ( "npID" , P . npID ) ;
updatedPlayer . Add ( "Level" , ( int ) P . Level ) ;
updatedPlayer . Add ( "LastOffense" , P . lastOffense ) ;
updatedPlayer . Add ( "Connections" , P . Connections ) ;
updatedPlayer . Add ( "IP" , P . IP ) ;
updatedPlayer . Add ( "LastConnection" , Utilities . DateTimeSQLite ( DateTime . Now ) ) ;
2017-05-26 18:49:27 -04:00
updatedPlayer . Add ( "UID" , P . UID ) ;
updatedPlayer . Add ( "Masked" , Convert . ToInt32 ( P . Masked ) ) ;
2015-08-20 01:06:44 -04:00
2017-05-27 00:22:50 -04:00
Update ( "CLIENTS" , updatedPlayer , new KeyValuePair < string , object > ( "npID" , P . npID ) ) ;
2015-08-20 01:06:44 -04:00
}
//Add specified ban to database
2016-01-15 17:15:39 -05:00
public void addBan ( Penalty B )
2015-08-20 01:06:44 -04:00
{
Dictionary < String , object > newBan = new Dictionary < String , object > ( ) ;
2017-05-26 18:49:27 -04:00
newBan . Add ( "Reason" , Utilities . removeNastyChars ( B . Reason ) ) ;
2015-08-20 01:06:44 -04:00
newBan . Add ( "npID" , B . npID ) ;
newBan . Add ( "bannedByID" , B . bannedByID ) ;
newBan . Add ( "IP" , B . IP ) ;
newBan . Add ( "TIME" , Utilities . DateTimeSQLite ( DateTime . Now ) ) ;
2016-01-15 17:15:39 -05:00
newBan . Add ( "TYPE" , B . BType ) ;
2015-08-20 01:06:44 -04:00
Insert ( "BANS" , newBan ) ;
}
//Deletes ban with matching GUID
public void removeBan ( String GUID )
{
String Query = String . Format ( "DELETE FROM BANS WHERE npID = '{0}'" , GUID ) ;
ExecuteNonQuery ( Query ) ;
}
public void removeBan ( String GUID , String IP )
{
2017-05-27 00:22:50 -04:00
String Query = String . Format ( "DELETE FROM BANS WHERE npID = '{0}' or IP = '{1}'" , GUID , IP ) ;
2015-08-20 01:06:44 -04:00
ExecuteNonQuery ( Query ) ;
}
}
public class AliasesDB : Database
{
public AliasesDB ( String FN ) : base ( FN ) { }
public override void Init ( )
{
if ( ! File . Exists ( FileName ) )
{
String Create = "CREATE TABLE [ALIASES] ( [Number] INTEGER, [NAMES] TEXT NULL, [IPS] TEXTNULL );" ;
ExecuteNonQuery ( Create ) ;
}
}
public Aliases getPlayer ( int dbIndex )
{
String Query = String . Format ( "SELECT * FROM ALIASES WHERE Number = '{0}' LIMIT 1" , dbIndex ) ;
DataTable Result = GetDataTable ( Query ) ;
if ( Result ! = null & & Result . Rows . Count > 0 )
{
DataRow p = Result . Rows [ 0 ] ;
return new Aliases ( Convert . ToInt32 ( p [ "Number" ] ) , p [ "NAMES" ] . ToString ( ) , p [ "IPS" ] . ToString ( ) ) ;
}
else
return null ;
}
public List < Aliases > getPlayer ( String IP )
{
2017-05-27 00:22:50 -04:00
SQLiteCommand cmd = new SQLiteCommand ( Con ) ;
cmd . CommandText = "SELECT * FROM ALIASES WHERE IPS LIKE @IP" ;
cmd . Parameters . AddWithValue ( "@IP" , IP ) ;
var Result = GetDataTable ( cmd ) ;
2015-08-20 01:06:44 -04:00
List < Aliases > players = new List < Aliases > ( ) ;
if ( Result ! = null & & Result . Rows . Count > 0 )
{
foreach ( DataRow p in Result . Rows )
players . Add ( new Aliases ( Convert . ToInt32 ( p [ "Number" ] ) , p [ "NAMES" ] . ToString ( ) , p [ "IPS" ] . ToString ( ) ) ) ;
}
return players ;
}
public List < Aliases > findPlayers ( String name )
{
2015-10-10 19:44:01 -04:00
name = name . Replace ( "'" , "" ) ;
2015-08-20 01:06:44 -04:00
String [ ] EyePee = name . Split ( '.' ) ;
String Penor = "THISISNOTANIP" ;
if ( EyePee . Length > 1 )
Penor = ( EyePee [ 0 ] + '.' + EyePee [ 1 ] + '.' ) ;
2017-05-27 00:22:50 -04:00
SQLiteCommand cmd = new SQLiteCommand ( Con ) ;
cmd . CommandText = "SELECT * FROM ALIASES WHERE NAMES LIKE @name OR IPS LIKE @ip LIMIT 15" ;
cmd . Parameters . AddWithValue ( "@name" , name ) ;
cmd . Parameters . AddWithValue ( "@ip" , Penor ) ;
var Result = GetDataTable ( cmd ) ;
2015-08-20 01:06:44 -04:00
List < Aliases > players = new List < Aliases > ( ) ;
if ( Result ! = null & & Result . Rows . Count > 0 )
{
foreach ( DataRow p in Result . Rows )
players . Add ( new Aliases ( Convert . ToInt32 ( p [ "Number" ] ) , p [ "NAMES" ] . ToString ( ) , p [ "IPS" ] . ToString ( ) ) ) ;
}
return players ;
}
public void addPlayer ( Aliases Alias )
{
Dictionary < String , object > newPlayer = new Dictionary < String , object > ( ) ;
2015-08-21 21:11:35 -04:00
newPlayer . Add ( "Number" , Alias . Number ) ;
2017-05-26 18:49:27 -04:00
newPlayer . Add ( "NAMES" , Utilities . removeNastyChars ( String . Join ( ";" , Alias . Names ) ) ) ;
2015-08-21 21:11:35 -04:00
newPlayer . Add ( "IPS" , String . Join ( ";" , Alias . IPS ) ) ;
2015-08-20 01:06:44 -04:00
Insert ( "ALIASES" , newPlayer ) ;
}
public void updatePlayer ( Aliases Alias )
{
Dictionary < String , object > updatedPlayer = new Dictionary < String , object > ( ) ;
2015-08-21 21:11:35 -04:00
updatedPlayer . Add ( "Number" , Alias . Number ) ;
updatedPlayer . Add ( "NAMES" , String . Join ( ";" , Alias . Names ) ) ;
updatedPlayer . Add ( "IPS" , String . Join ( ";" , Alias . IPS ) ) ;
2015-08-20 01:06:44 -04:00
2017-05-26 18:49:27 -04:00
Update ( "ALIASES" , updatedPlayer , new KeyValuePair < string , object > ( "Number" , Alias . Number ) ) ;
2015-08-20 01:06:44 -04:00
}
}
}