309 lines
12 KiB
C#
309 lines
12 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using MySql.Data.MySqlClient;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Collections.Specialized;
|
|
using System.Windows.Forms;
|
|
using System.Drawing.Text;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace PokerStarsBotClientv2
|
|
{
|
|
class HelperMethods
|
|
{
|
|
// Methods
|
|
public static void InitCustomFont(byte[] font, PrivateFontCollection FontCollection)
|
|
{
|
|
int fontLength = font.Length;
|
|
byte[] fontdata = font; // create a buffer to read in to
|
|
System.IntPtr data = Marshal.AllocCoTaskMem(fontLength); // create an unsafe memory block for the font data
|
|
Marshal.Copy(fontdata, 0, data, fontLength); // copy the bytes to the unsafe memory block
|
|
FontCollection.AddMemoryFont(data, fontLength); // pass the font to the font collection
|
|
}
|
|
|
|
public static void UpdateInsertSQL(string query)
|
|
{
|
|
try
|
|
{
|
|
var dbCon = DBConnection.Instance();
|
|
if (dbCon.IsConnect())
|
|
{
|
|
// Execute Query
|
|
|
|
using (var cmd = new MySqlCommand(query, dbCon.Connection))
|
|
{
|
|
cmd.ExecuteReader();
|
|
}
|
|
dbCon.Close();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Error: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public static void SQLStreamDelay(decimal delay)
|
|
{
|
|
int UserID = Globals.UserID;
|
|
string InsertQuery = "INSERT INTO `pkrstarsbot`.`config_user` (`UserID` ,`Key`) SELECT " + UserID + ", 'DelaySeconds' FROM dual WHERE NOT EXISTS (SELECT 1 FROM `pkrstarsbot`.`config_user` WHERE UserID = " + UserID + " AND `Key` = 'DelaySeconds')";
|
|
UpdateInsertSQL(InsertQuery);
|
|
string query = "UPDATE `pkrstarsbot`.`config_user` SET `Value` = '" + delay + "' WHERE `config_user`.`UserID` = " + UserID + ";";
|
|
UpdateInsertSQL(query);
|
|
}
|
|
|
|
public static DataTable GetSQLDataTable(string query)
|
|
{
|
|
DataTable dt = new DataTable();
|
|
try
|
|
{
|
|
var dbCon = DBConnection.Instance();
|
|
if (dbCon.IsConnect())
|
|
{
|
|
// Execute Query
|
|
var cmd = new MySqlCommand(query, dbCon.Connection);
|
|
using (MySqlDataReader dr = cmd.ExecuteReader())
|
|
{
|
|
dt.Load(dr);
|
|
}
|
|
dbCon.Close();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Error: " + ex.Message);
|
|
}
|
|
return dt;
|
|
}
|
|
|
|
public static void GetUserConfigFromDB()
|
|
{
|
|
// Get user config from table config
|
|
string query = "SELECT * FROM `config`";
|
|
DataTable dt = HelperMethods.GetSQLDataTable(query);
|
|
int FileNotOlderThanMin = dt.Rows[0].Field<Int32>("Value");
|
|
string RecentVersion = dt.Rows[1].Field<string>("Value_String");
|
|
|
|
// Get user ID from table reg_users
|
|
query = "SELECT ID FROM `reg_users` WHERE `EMail` = '" + Properties.Settings.Default.UserName + "'";
|
|
DataTable dtID = HelperMethods.GetSQLDataTable(query);
|
|
int UserID = dtID.Rows[0].Field<Int32>("ID");
|
|
|
|
// Globals
|
|
Globals.UserID = UserID;
|
|
Globals.FileNotOlderThanMin = FileNotOlderThanMin;
|
|
Globals.RecentVersion = RecentVersion;
|
|
Console.WriteLine("UserID (global): " + Globals.UserID);
|
|
Console.WriteLine("FileNotOlderThanMin (global): " + Globals.FileNotOlderThanMin);
|
|
Console.WriteLine("RecentVersion (global): " + Globals.RecentVersion);
|
|
}
|
|
|
|
public static void GetConfigFromDB()
|
|
{
|
|
// Get user config from table config
|
|
string query = "SELECT * FROM `config`";
|
|
DataTable dt = HelperMethods.GetSQLDataTable(query);
|
|
int FileNotOlderThanMin = dt.Rows[0].Field<Int32>("Value");
|
|
string RecentVersion = dt.Rows[1].Field<string>("Value_String");
|
|
|
|
// Globals
|
|
Globals.FileNotOlderThanMin = FileNotOlderThanMin;
|
|
Globals.RecentVersion = RecentVersion;
|
|
Console.WriteLine("FileNotOlderThanMin (global): " + Globals.FileNotOlderThanMin);
|
|
Console.WriteLine("RecentVersion (global): " + Globals.RecentVersion);
|
|
}
|
|
|
|
public static void DirectoryListing(string dir, Dictionary<string, DateTime> dictionary, List<FileInfo> UploadList)
|
|
{
|
|
DirectoryInfo d = new DirectoryInfo(dir);
|
|
FileInfo[] Files = d.GetFiles("*.txt");
|
|
foreach (FileInfo file in Files)
|
|
{
|
|
System.Console.WriteLine(file);
|
|
// Files in range of FileNotOlderThanMin
|
|
|
|
if (file.LastWriteTime < DateTime.Now.AddMinutes(-Globals.FileNotOlderThanMin))
|
|
{
|
|
System.Console.WriteLine("\t-> not recent: Older than " + Globals.FileNotOlderThanMin + " minutes");
|
|
}
|
|
else
|
|
{
|
|
// Recent files modtime check
|
|
System.Console.WriteLine("\t-> is recent");
|
|
|
|
if (dictionary.ContainsKey(file.Name) == true)
|
|
{
|
|
DateTime OldModTime = dictionary[file.Name];
|
|
DateTime NewModTime = file.LastWriteTime;
|
|
int result = DateTime.Compare(OldModTime, NewModTime);
|
|
if (result == 0)
|
|
{
|
|
System.Console.WriteLine("\t-> already uploaded");
|
|
System.Console.WriteLine("\t-> unchanged | LastWriteTime: " + file.LastWriteTime);
|
|
}
|
|
else
|
|
{
|
|
System.Console.WriteLine("\t-> changed: ModTime has changed, adding to upload list...");
|
|
dictionary[file.Name] = NewModTime;
|
|
UploadList.Add(file);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
System.Console.WriteLine("\t-> adding file to dictionary.");
|
|
dictionary.Add(file.Name, file.LastWriteTime);
|
|
UploadList.Add(file);
|
|
}
|
|
}
|
|
}
|
|
//return (UploadList, dictionary);
|
|
}
|
|
|
|
public static void UploadFiles(List<FileInfo> UploadList)
|
|
{
|
|
bool Uploading = false;
|
|
|
|
Console.WriteLine("\nUploadList:");
|
|
bool isEmpty = !UploadList.Any();
|
|
if (isEmpty)
|
|
{
|
|
System.Console.WriteLine("\t-> is empty.");
|
|
}
|
|
else if (Uploading)
|
|
{
|
|
System.Console.WriteLine("\t-> currently uploading...");
|
|
}
|
|
else
|
|
{
|
|
System.Console.WriteLine("\t-> contains elements. Starting upload...");
|
|
Uploading = true;
|
|
|
|
WebClient client = new WebClient();
|
|
client.Encoding = Encoding.UTF8;
|
|
client.Credentials = CredentialCache.DefaultCredentials;
|
|
NameValueCollection parameters = new NameValueCollection();
|
|
parameters.Add("UserID", Globals.UserID.ToString());
|
|
parameters.Add("ClientSecret", Globals.ClientSecret);
|
|
client.QueryString = parameters;
|
|
|
|
for (int i = UploadList.Count - 1; i >= 0; i--)
|
|
{
|
|
// Current fileinfo object
|
|
FileInfo file = UploadList[i];
|
|
|
|
if (file.Length <= Globals.UploadFileSizeLimitBytes) // Check if file is too large
|
|
{
|
|
// Upload via webclient post with parameters
|
|
try
|
|
{
|
|
var responseBytes = client.UploadFile(Globals.uploadURL, "POST", file.FullName);
|
|
string Httpresponse = Encoding.ASCII.GetString(responseBytes);
|
|
System.Threading.Thread.Sleep(500);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Error: " + ex.Message);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
System.Console.WriteLine("\t-> File is too large to upload.");
|
|
}
|
|
|
|
// Remove uploaded item
|
|
UploadList.RemoveAt(i);
|
|
}
|
|
client.Dispose();
|
|
Uploading = false;
|
|
}
|
|
|
|
//return UploadList;
|
|
}
|
|
|
|
public static bool is_valid_run_dir(System.Windows.Forms.TextBox textbox)
|
|
{
|
|
var path = textbox.Text;
|
|
if (Directory.Exists(path))
|
|
{
|
|
System.Console.WriteLine("Directory exists: " + path);
|
|
Properties.Settings.Default.HandhistoryDir = path;
|
|
Properties.Settings.Default.Save();
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
System.Console.WriteLine("Directory does not exist: " + path);
|
|
MessageBox.Show("Selected handhistory directory does not exist.\nPlease select a valid directory.", "Directory not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
textbox.Clear();
|
|
Properties.Settings.Default.HandhistoryDir = string.Empty;
|
|
Properties.Settings.Default.Save();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static void MinimizeToTray(System.Windows.Forms.Form form) {
|
|
System.IO.Stream st;
|
|
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
|
|
st = a.GetManifestResourceStream("PokerStarsBotClientv2.Properties.pkrstarsbot.ico");
|
|
System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
|
|
ni.Icon = new System.Drawing.Icon(st);
|
|
ni.Visible = true;
|
|
ni.DoubleClick +=
|
|
delegate (object sender, EventArgs args)
|
|
{
|
|
form.WindowState = FormWindowState.Minimized;
|
|
form.Show();
|
|
form.WindowState = System.Windows.Forms.FormWindowState.Normal;
|
|
};
|
|
}
|
|
|
|
}
|
|
|
|
class DBConnection
|
|
{
|
|
|
|
private MySqlConnection connection = null;
|
|
public MySqlConnection Connection
|
|
{
|
|
get { return connection; }
|
|
}
|
|
|
|
private static DBConnection _instance = null;
|
|
public static DBConnection Instance()
|
|
{
|
|
if (_instance == null)
|
|
_instance = new DBConnection();
|
|
return _instance;
|
|
}
|
|
|
|
public bool IsConnect()
|
|
{
|
|
bool result = true;
|
|
if (Connection == null)
|
|
{
|
|
if ((Globals.MySQLDatabase ?? Globals.MySQLPassword ?? Globals.MySQLServer ?? Globals.MySQLUser) == null)
|
|
{
|
|
return false;
|
|
}
|
|
string connstring = string.Format(@"server={0};user id={1};persistsecurityinfo=True;database={2};password={3}", Globals.MySQLServer, Globals.MySQLUser, Globals.MySQLDatabase, Globals.MySQLPassword);
|
|
connection = new MySqlConnection(connstring);
|
|
connection.Open();
|
|
result = true;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
connection.Close();
|
|
_instance = null;
|
|
}
|
|
}
|
|
}
|