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
{
2017-11-14 16:36:55 -05:00
private Interfaces . ILogger Logger ;
public Database ( String FN , Interfaces . ILogger logger )
2015-08-20 01:06:44 -04:00
{
FileName = FN ;
2017-11-14 16:36:55 -05:00
Logger = logger ;
2015-08-20 01:06:44 -04:00
Init ( ) ;
}
2017-05-29 22:25:49 -04:00
protected SQLiteConnection GetNewConnection ( )
{
return new SQLiteConnection ( $"Data Source={FileName}" ) ;
}
2015-08-20 01:06:44 -04:00
abstract public void Init ( ) ;
2017-11-02 18:20:10 -04:00
protected bool Insert ( String tableName , Dictionary < String , object > data , bool ignore = false )
2015-08-20 01:06:44 -04:00
{
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 ) ;
2017-05-29 22:25:49 -04:00
var Con = GetNewConnection ( ) ;
2017-05-26 18:49:27 -04:00
2017-11-02 18:20:10 -04:00
string ignoreCmd = ignore ? " OR IGNORE " : " " ;
2017-05-29 22:25:49 -04:00
SQLiteCommand insertcmd = new SQLiteCommand ( )
{
Connection = Con ,
2017-11-02 18:20:10 -04:00
CommandText = String . Format ( "INSERT{0}INTO `{1}` ({2}) VALUES ({3});" , ignoreCmd , tableName , names , parameters )
2017-05-29 22:25:49 -04:00
} ;
2017-05-26 18:49:27 -04:00
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
2017-05-29 22:25:49 -04:00
catch ( Exception E )
2015-08-20 01:06:44 -04:00
{
2017-11-14 16:36:55 -05:00
Logger . WriteWarning ( $"Database Insert failed" ) ;
Logger . WriteDebug ( $"Exception Message: {E.Message}" ) ;
Logger . WriteDebug ( $"SQL command: {insertcmd.CommandText}" ) ;
Logger . WriteDebug ( $"Database File: {FileName}" ) ;
2017-05-26 18:49:27 -04:00
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-11-04 19:42:31 -04:00
protected void UpdateIncrement ( String tableName , string columnName , Dictionary < String , object > data , KeyValuePair < string , object > where )
{
string parameters = "" ;
foreach ( string key in data . Keys )
{
parameters + = $"{key}={key}+1," ;
}
parameters = parameters . Substring ( 0 , parameters . Length - 1 ) ;
var Con = GetNewConnection ( ) ;
SQLiteCommand updatecmd = new SQLiteCommand ( )
{
Connection = Con ,
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 ] ) ;
}
updatecmd . Parameters . AddWithValue ( '@' + where . Key , where . Value ) ;
try
{
Con . Open ( ) ;
updatecmd . ExecuteNonQuery ( ) ;
Con . Close ( ) ;
}
catch ( Exception E )
{
2017-11-14 16:36:55 -05:00
Logger . WriteWarning ( $"Database UpdateIncrement failed" ) ;
Logger . WriteDebug ( $"Exception Message: {E.Message}" ) ;
Logger . WriteDebug ( $"SQL command: {updatecmd?.CommandText}" ) ;
Logger . WriteDebug ( $"Database File: {FileName}" ) ;
2017-11-04 19:42:31 -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 ) ;
2017-05-29 22:25:49 -04:00
var Con = GetNewConnection ( ) ;
2017-05-26 18:49:27 -04:00
2017-05-29 22:25:49 -04:00
SQLiteCommand updatecmd = new SQLiteCommand ( )
{
Connection = Con ,
CommandText = String . Format ( "UPDATE `{0}` SET {1} WHERE {2}=@{2}" , tableName , parameters , where . Key )
} ;
2017-05-26 18:49:27 -04:00
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
2017-05-29 22:25:49 -04:00
catch ( Exception E )
2015-08-20 01:06:44 -04:00
{
2017-11-14 16:36:55 -05:00
Logger . WriteWarning ( $"Database update failed" ) ;
Logger . WriteDebug ( $"Exception Message: {E.Message}" ) ;
Logger . WriteDebug ( $"SQL Query: {updatecmd.CommandText}" ) ;
Logger . WriteDebug ( $"Database File: {FileName}" ) ;
2017-05-26 18:49:27 -04:00
return false ;
2015-08-20 01:06:44 -04:00
}
}
2017-05-29 22:25:49 -04:00
protected DataRow GetDataRow ( String Q )
2015-08-20 01:06:44 -04:00
{
DataRow Result = GetDataTable ( Q ) . Rows [ 0 ] ;
return Result ;
}
protected int ExecuteNonQuery ( String Request )
{
int rowsUpdated = 0 ;
2017-05-27 00:22:50 -04:00
Request = Request . Replace ( "!'" , "" ) . Replace ( "!" , "" ) ;
2017-05-29 22:25:49 -04:00
var Con = GetNewConnection ( ) ;
2017-11-14 16:36:55 -05:00
SQLiteCommand CMD = null ;
2017-05-26 18:49:27 -04:00
try
{
2017-05-29 22:25:49 -04:00
2017-11-14 16:36:55 -05:00
Con . Open ( ) ;
CMD = new SQLiteCommand ( Con )
2017-05-29 22:25:49 -04:00
{
CommandText = Request
} ;
rowsUpdated = CMD . ExecuteNonQuery ( ) ;
2017-11-14 16:36:55 -05:00
Con . Close ( ) ;
2017-05-26 18:49:27 -04:00
return rowsUpdated ;
}
2015-08-20 01:06:44 -04:00
2017-05-26 18:49:27 -04:00
catch ( Exception E )
{
2017-11-14 16:36:55 -05:00
Logger . WriteWarning ( $"Database command failed" ) ;
Logger . WriteDebug ( $"Exception Message: {E.Message}" ) ;
Logger . WriteDebug ( $"SQL command: {CMD?.CommandText}" ) ;
Logger . WriteDebug ( $"Database File: {FileName}" ) ;
2017-05-26 18:49:27 -04:00
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 ( ) ;
2017-05-29 22:25:49 -04:00
SQLiteCommand updatecmd = new SQLiteCommand ( )
{
CommandText = String . Format ( "SELECT * FROM {0} WHERE `{1}`=@{1};" , tableName , where . Key )
} ;
var Con = GetNewConnection ( ) ;
2017-05-30 17:23:31 -04:00
updatecmd . Parameters . AddWithValue ( '@' + where . Key , where . Value ) ;
updatecmd . Connection = Con ;
2017-11-14 16:36:55 -05:00
2017-05-26 18:49:27 -04:00
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-11-14 16:36:55 -05:00
catch ( Exception E )
2017-05-27 00:22:50 -04:00
{
2017-11-14 16:36:55 -05:00
Logger . WriteWarning ( $"Database GetDataTable failed" ) ;
Logger . WriteDebug ( $"Exception Message: {E.Message}" ) ;
Logger . WriteDebug ( $"SQL command: {updatecmd.CommandText}" ) ;
Logger . WriteDebug ( $"Database File: {FileName}" ) ;
2017-05-27 00:22:50 -04:00
}
return dt ;
}
protected DataTable GetDataTable ( SQLiteCommand cmd )
{
DataTable dt = new DataTable ( ) ;
2017-05-29 22:25:49 -04:00
var Con = GetNewConnection ( ) ;
2017-05-30 17:23:31 -04:00
cmd . Connection = Con ;
2017-05-27 00:22:50 -04:00
try
{
Con . Open ( ) ;
SQLiteDataReader reader = cmd . ExecuteReader ( ) ;
dt . Load ( reader ) ;
reader . Close ( ) ;
Con . Close ( ) ;
}
2017-11-14 16:36:55 -05:00
catch ( Exception E )
2017-05-26 18:49:27 -04:00
{
2017-11-14 16:36:55 -05:00
Logger . WriteWarning ( $"Database GetDataTable failed" ) ;
Logger . WriteDebug ( $"Exception Message: {E.Message}" ) ;
Logger . WriteDebug ( $"SQL command: {cmd.CommandText}" ) ;
Logger . WriteDebug ( $"Database File: {FileName}" ) ;
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-29 22:25:49 -04:00
var Con = GetNewConnection ( ) ;
2017-11-14 16:36:55 -05:00
SQLiteCommand cmd = null ;
2017-05-27 00:22:50 -04:00
2015-08-20 01:06:44 -04:00
try
{
2017-05-29 22:25:49 -04:00
2017-11-14 16:36:55 -05:00
Con . Open ( ) ;
cmd = new SQLiteCommand ( Con )
2017-05-29 22:25:49 -04:00
{
CommandText = sql
} ;
2017-11-14 16:36:55 -05:00
SQLiteDataReader reader = cmd . ExecuteReader ( ) ;
dt . Load ( reader ) ;
reader . Close ( ) ;
Con . Close ( ) ;
2015-08-20 01:06:44 -04:00
}
2017-11-14 16:36:55 -05:00
catch ( Exception E )
2015-08-20 01:06:44 -04:00
{
2017-11-14 16:36:55 -05:00
Logger . WriteWarning ( $"Database GetDataTable failed" ) ;
Logger . WriteDebug ( $"Exception Message: {E.Message}" ) ;
Logger . WriteDebug ( $"SQL command: {cmd?.CommandText}" ) ;
Logger . WriteDebug ( $"Database File: {FileName}" ) ;
2015-08-20 17:54:38 -04:00
return new DataTable ( ) ;
2015-08-20 01:06:44 -04:00
}
return dt ;
}
protected String FileName ;
}
public class ClientsDB : Database
{
2017-11-14 16:36:55 -05:00
public ClientsDB ( String FN , Interfaces . ILogger logger ) : base ( FN , logger ) { }
2015-08-20 01:06:44 -04:00
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 ) ;
2017-08-23 18:29:48 -04:00
Create = "CREATE TABLE [BANS] ( [TYPE] TEXT NULL, [Reason] TEXT NULL, [npID] TEXT NULL, [bannedByID] TEXT NULL, [IP] TEXT NULL, [TIME] TEXT NULL, [EXPIRES] TEXT);" ;
2015-08-20 01:06:44 -04:00
ExecuteNonQuery ( Create ) ;
}
}
2017-05-29 22:25:49 -04:00
public List < Player > GetRecentPlayers ( )
2017-05-26 18:49:27 -04:00
{
List < Player > returnssss = new List < Player > ( ) ;
2017-06-01 13:42:28 -04:00
//String Query = String.Format($"SELECT * FROM CLIENTS LIMIT 15 OFFSET (SELECT COUNT(*) FROM CLIENTS)-15");
String Query = "SELECT * FROM CLIENTS ORDER BY LastConnection DESC LIMIT 25" ;
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
}
2017-05-29 22:25:49 -04:00
public List < Player > GetPlayers ( List < String > npIDs )
2015-08-21 21:11:35 -04:00
{
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 ;
}
2017-05-29 22:25:49 -04:00
public List < Player > GetPlayers ( List < int > databaseIDs )
2015-08-21 21:11:35 -04:00
{
List < Player > returnssss = new List < Player > ( ) ;
2017-05-29 22:25:49 -04:00
String Condition = String . Join ( "' OR Number = '" , databaseIDs ) ;
2015-08-21 21:11:35 -04:00
2017-05-29 22:25:49 -04:00
String Query = String . Format ( "SELECT * FROM CLIENTS WHERE Number = '{0}'" , Condition ) ;
2015-08-21 21:11:35 -04:00
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
2017-05-29 22:25:49 -04:00
public Player GetPlayer ( int dbIndex )
2015-08-20 01:06:44 -04:00
{
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)
2017-05-29 22:25:49 -04:00
public Player GetPlayer ( String IP )
2015-08-20 01:06:44 -04:00
{
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 ;
}
2017-05-29 22:25:49 -04:00
//Returns a single player object with matching GUID, false if no matches
public Player GetPlayer ( String ID , int cNum )
{
DataTable Result = GetDataTable ( "CLIENTS" , new KeyValuePair < string , object > ( "npID" , ID ) ) ;
if ( Result ! = null & & Result . Rows . Count > 0 )
{
DataRow ResponseRow = Result . Rows [ 0 ] ;
DateTime lastCon = DateTime . MinValue ;
DateTime . TryParse ( ResponseRow [ "LastConnection" ] . ToString ( ) , out lastCon ) ;
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" ) ;
}
else
return null ;
}
2015-08-20 01:06:44 -04:00
//Returns a list of players matching name parameter, null if no players found matching
2017-05-29 22:25:49 -04:00
public List < Player > FindPlayers ( String name )
2015-08-20 01:06:44 -04:00
{
2017-05-29 22:25:49 -04:00
var Con = GetNewConnection ( ) ;
SQLiteCommand cmd = new SQLiteCommand ( Con )
{
2017-06-07 20:59:59 -04:00
CommandText = "SELECT * FROM CLIENTS WHERE Name LIKE @Name"
2017-05-29 22:25:49 -04:00
} ;
2017-05-27 00:22:50 -04:00
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
2017-05-29 22:25:49 -04:00
public Player GetOwner ( )
2015-08-20 01:06:44 -04:00
{
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 > ( ) ;
2017-05-31 01:31:56 -04:00
String Query = $"SELECT * FROM `BANS` WHERE `npID` = '{P.NetworkID}' OR `IP` = '{P.IP}'" ;
2017-05-27 18:08:04 -04:00
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 ( ) ;
2017-05-29 22:25:49 -04:00
Penalty . Type BanType = Penalty . Type . Ban ;
2017-05-27 18:08:04 -04:00
if ( Row [ "TYPE" ] . ToString ( ) . Length ! = 0 )
BanType = ( Penalty . Type ) Enum . Parse ( typeof ( Penalty . Type ) , Row [ "TYPE" ] . ToString ( ) ) ;
2017-08-23 18:29:48 -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 ( ) , DateTime . Parse ( Row [ "EXPIRES" ] . ToString ( ) ) ) ) ;
2017-05-27 18:08:04 -04:00
}
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 ( ) ;
2017-11-14 16:36:55 -05:00
var BanType = ( Penalty . Type ) Enum . Parse ( typeof ( Penalty . Type ) , Row [ "TYPE" ] . ToString ( ) ) ;
2017-08-23 18:29:48 -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 ( ) , DateTime . Parse ( Row [ "EXPIRES" ] . ToString ( ) ) ) ) ;
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
2017-05-29 22:25:49 -04:00
public List < Player > GetAdmins ( )
2015-08-20 01:06:44 -04:00
{
List < Player > Admins = new List < Player > ( ) ;
2017-08-17 19:28:08 -04:00
String Query = String . Format ( "SELECT * FROM CLIENTS WHERE Level >= '{0}' ORDER BY Name" , ( int ) Player . Permission . Trusted ) ;
2015-08-20 01:06:44 -04:00
DataTable Result = GetDataTable ( Query ) ;
foreach ( DataRow P in Result . Rows )
2017-08-17 19:28:08 -04:00
Admins . Add ( new Player ( P [ "Name" ] . ToString ( ) , P [ "npID" ] . ToString ( ) , ( Player . Permission ) P [ "Level" ] , P [ "IP" ] . ToString ( ) , P [ "UID" ] . ToString ( ) , Convert . ToInt32 ( P [ "Number" ] . ToString ( ) ) ) ) ;
2015-08-20 01:06:44 -04:00
return Admins ;
}
//Returns total number of player entries in database
2017-05-29 22:25:49 -04:00
public int TotalPlayers ( )
2015-08-20 01:06:44 -04:00
{
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
2017-05-29 22:25:49 -04:00
public void AddPlayer ( Player P )
{
Dictionary < String , object > newPlayer = new Dictionary < String , object >
{
2017-06-12 17:47:31 -04:00
{ "Name" , Utilities . StripIllegalCharacters ( P . Name ) } ,
2017-05-31 01:31:56 -04:00
{ "npID" , P . NetworkID } ,
2017-05-29 22:25:49 -04:00
{ "Level" , ( int ) P . Level } ,
{ "LastOffense" , "" } ,
{ "Connections" , 1 } ,
{ "IP" , P . IP } ,
{ "LastConnection" , Utilities . DateTimeSQLite ( DateTime . Now ) } ,
{ "UID" , P . UID } ,
{ "Masked" , Convert . ToInt32 ( P . Masked ) }
} ;
2015-08-20 01:06:44 -04:00
Insert ( "CLIENTS" , newPlayer ) ;
}
///Update information of specified player
2017-05-29 22:25:49 -04:00
public void UpdatePlayer ( Player P )
{
Dictionary < String , Object > updatedPlayer = new Dictionary < String , Object >
{
{ "Name" , P . Name } ,
2017-05-31 01:31:56 -04:00
{ "npID" , P . NetworkID } ,
2017-05-29 22:25:49 -04:00
{ "Level" , ( int ) P . Level } ,
{ "LastOffense" , P . lastOffense } ,
{ "Connections" , P . Connections } ,
{ "IP" , P . IP } ,
{ "LastConnection" , Utilities . DateTimeSQLite ( DateTime . Now ) } ,
{ "UID" , P . UID } ,
{ "Masked" , Convert . ToInt32 ( P . Masked ) }
} ;
2017-05-31 01:31:56 -04:00
Update ( "CLIENTS" , updatedPlayer , new KeyValuePair < string , object > ( "npID" , P . NetworkID ) ) ;
2015-08-20 01:06:44 -04:00
}
//Add specified ban to database
2017-08-23 18:29:48 -04:00
public void AddPenalty ( Penalty B )
2015-08-20 01:06:44 -04:00
{
2017-05-29 22:25:49 -04:00
Dictionary < String , object > newBan = new Dictionary < String , object >
{
2017-06-12 17:47:31 -04:00
{ "Reason" , Utilities . StripIllegalCharacters ( B . Reason ) } ,
2017-05-31 01:31:56 -04:00
{ "npID" , B . OffenderID } ,
{ "bannedByID" , B . PenaltyOriginID } ,
2017-05-29 22:25:49 -04:00
{ "IP" , B . IP } ,
{ "TIME" , Utilities . DateTimeSQLite ( DateTime . Now ) } ,
2017-08-23 18:29:48 -04:00
{ "TYPE" , B . BType } ,
{ "EXPIRES" , B . Expires }
2017-05-29 22:25:49 -04:00
} ;
2015-08-20 01:06:44 -04:00
Insert ( "BANS" , newBan ) ;
}
//Deletes ban with matching GUID
2017-05-29 22:25:49 -04:00
public void RemoveBan ( String GUID )
2015-08-20 01:06:44 -04:00
{
String Query = String . Format ( "DELETE FROM BANS WHERE npID = '{0}'" , GUID ) ;
ExecuteNonQuery ( Query ) ;
}
2017-05-29 22:25:49 -04:00
public void RemoveBan ( String GUID , String IP )
2015-08-20 01:06:44 -04:00
{
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
{
2017-11-14 16:36:55 -05:00
public AliasesDB ( String FN , Interfaces . ILogger logger ) : base ( FN , logger ) { }
2015-08-20 01:06:44 -04:00
public override void Init ( )
{
if ( ! File . Exists ( FileName ) )
{
String Create = "CREATE TABLE [ALIASES] ( [Number] INTEGER, [NAMES] TEXT NULL, [IPS] TEXTNULL );" ;
ExecuteNonQuery ( Create ) ;
}
}
2017-05-29 22:25:49 -04:00
public Aliases GetPlayerAliases ( int dbIndex )
2015-08-20 01:06:44 -04:00
{
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 ;
}
2017-05-29 22:25:49 -04:00
public List < Aliases > GetPlayerAliases ( String IP )
2015-08-20 01:06:44 -04:00
{
2017-05-29 22:25:49 -04:00
var Con = GetNewConnection ( ) ;
SQLiteCommand cmd = new SQLiteCommand ( Con )
{
CommandText = "SELECT * FROM ALIASES WHERE IPS LIKE @IP"
} ;
2017-05-27 00:22:50 -04:00
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 ;
}
2017-05-29 22:25:49 -04:00
public List < Aliases > FindPlayerAliases ( String name )
2015-08-20 01:06:44 -04:00
{
2015-10-10 19:44:01 -04:00
name = name . Replace ( "'" , "" ) ;
2017-05-29 22:25:49 -04:00
String [ ] IP = name . Split ( '.' ) ;
String DefaultIP = "LEGACY_INVALID_IP" ;
if ( IP . Length > 1 )
DefaultIP = ( IP [ 0 ] + '.' + IP [ 1 ] + '.' ) ;
var Con = GetNewConnection ( ) ;
2015-08-20 01:06:44 -04:00
2017-05-29 22:25:49 -04:00
SQLiteCommand cmd = new SQLiteCommand ( Con )
{
CommandText = "SELECT * FROM ALIASES WHERE NAMES LIKE @name OR IPS LIKE @ip LIMIT 15"
} ;
2017-08-17 19:28:08 -04:00
cmd . Parameters . AddWithValue ( "@name" , '%' + name + '%' ) ;
cmd . Parameters . AddWithValue ( "@ip" , '%' + DefaultIP + '%' ) ;
2017-05-27 00:22:50 -04:00
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 ;
}
2017-05-29 22:25:49 -04:00
public void AddPlayerAliases ( Aliases Alias )
2015-08-20 01:06:44 -04:00
{
2017-05-29 22:25:49 -04:00
Dictionary < String , object > newPlayer = new Dictionary < String , object >
{
{ "Number" , Alias . Number } ,
2017-06-12 17:47:31 -04:00
{ "NAMES" , Utilities . StripIllegalCharacters ( String . Join ( ";" , Alias . Names ) ) } ,
2017-05-29 22:25:49 -04:00
{ "IPS" , String . Join ( ";" , Alias . IPS ) }
} ;
2015-08-20 01:06:44 -04:00
Insert ( "ALIASES" , newPlayer ) ;
}
2017-05-29 22:25:49 -04:00
public void UpdatePlayerAliases ( Aliases Alias )
2015-08-20 01:06:44 -04:00
{
2017-05-29 22:25:49 -04:00
Dictionary < String , object > updatedPlayer = new Dictionary < String , object >
{
{ "Number" , Alias . Number } ,
{ "NAMES" , String . Join ( ";" , Alias . Names ) } ,
{ "IPS" , String . Join ( ";" , Alias . IPS ) }
} ;
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
}
}
}