diff --git a/bot/bot/Bot.cs b/bot/bot/Bot.cs index 7724d8c..d4702ae 100644 --- a/bot/bot/Bot.cs +++ b/bot/bot/Bot.cs @@ -14,25 +14,59 @@ using MySql.Data.MySqlClient; using System.Threading; namespace bot { - class Bot : StringSanitizer { - public static MySqlConnection conn; + class Bot { static string tmp; + static List responseList = new List(); + + public static void loadResponseList() { + responseList = new List(); + var r = Query.Reader("SELECT * FROM `responses`", _G.conn); + while(r.Read()) { + responseList.Add(new Response( + r.GetString("conditions"), + r.GetInt32("respid"), + r.GetString("parameters"), + r.GetInt32("cooldown"))); + } + r.Close(); + } + static void Main(string[] args) { - conn = new MySqlConnection("SERVER="+ _G.serveraddr +";DATABASE="+ _G.dbname +";UID="+ _G.dbuser +";PASSWORD="+ _G.dbpass +";"); - conn.Open(); + _G.conn = new MySqlConnection("SERVER="+ _G.serveraddr +";DATABASE="+ _G.dbname +";UID="+ _G.dbuser +";PASSWORD="+ _G.dbpass +";"); + _G.conn.Open(); + + _G.loadConfig(); tmp = "DELETE FROM resptypes WHERE "; foreach(Type t in ResponseCaller.getResponseTypes()) { string[] typeInfo = (string[])t.GetMethod("getInfo").Invoke(null, null); - tmp += "name<>'"+ typeInfo[0] +"' AND "; - Query.Quiet("UPDATE `resptypes` SET friendlyname='" + Sanitize(typeInfo[1]) + "',description='" + Sanitize(typeInfo[2]) + "' WHERE name='" + typeInfo[0] + "'", conn); - Query.Quiet("INSERT INTO `resptypes` (name,friendlyname,description) VALUES ('" + typeInfo[0] + "','" + Sanitize(typeInfo[1]) + "','" + Sanitize(typeInfo[2]) + "')", conn); + tmp += "name<>'" + typeInfo[0] + "' AND "; + if((Int64)Query.Scalar("SELECT COUNT(*) FROM `resptypes` WHERE `name`='" + typeInfo[0] + "'", _G.conn) > 0) + Query.Quiet("UPDATE `resptypes` SET friendlyname='" + Sanitizer.Sanitize(typeInfo[1]) + "',description='" + Sanitizer.Sanitize(typeInfo[2]) + "' WHERE name='" + typeInfo[0] + "'", _G.conn); + else + Query.Quiet("INSERT INTO `resptypes` (name,friendlyname,description) VALUES ('" + typeInfo[0] + "','" + Sanitizer.Sanitize(typeInfo[1]) + "','" + Sanitizer.Sanitize(typeInfo[2]) + "')", _G.conn); } - - tmp = tmp.Substring(0, tmp.Length - 5); - Query.Quiet(tmp, conn); + Query.Quiet(tmp, _G.conn); + + tmp = "DELETE FROM conditions WHERE "; + foreach(Type t in ConditionChecker.getConditions()) { + string[] typeInfo = (string[])t.GetMethod("getInfo").Invoke(null, null); + tmp += "name<>'" + typeInfo[0] + "' AND "; + if((Int64)Query.Scalar("SELECT COUNT(*) FROM `conditions` WHERE `name`='" + typeInfo[0] + "'", _G.conn) > 0) + Query.Quiet("UPDATE `conditions` SET friendlyname='" + Sanitizer.Sanitize(typeInfo[1]) + "' WHERE name='" + typeInfo[0] + "'", _G.conn); + else + Query.Quiet("INSERT INTO `conditions` (name,friendlyname) VALUES ('" + typeInfo[0] + "','" + Sanitizer.Sanitize(typeInfo[1]) + "')", _G.conn); + } + tmp = tmp.Substring(0, tmp.Length - 5); + Query.Quiet(tmp, _G.conn); + + _G.updateHeartbeat(); + + _G.startThread(_G.pulseThread); + + Console.WriteLine(_G.propername +" has started successfully."); while(true) ; } diff --git a/bot/bot/ConditionChecker.cs b/bot/bot/ConditionChecker.cs new file mode 100644 index 0000000..020469e --- /dev/null +++ b/bot/bot/ConditionChecker.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Reflection; + +namespace bot { + class ConditionChecker { + static Type[] conditionTypes = null; + + static void loadConditionTypes() { + if(conditionTypes == null) + conditionTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => String.Equals(t.Namespace, "bot.conditions", StringComparison.Ordinal)).ToArray(); + } + + public static bool checkCondition(String conditionName, string check, string parameter) { + loadConditionTypes(); + return (bool)conditionTypes.First(t => t.Name == conditionName).GetMethod("performCheck").Invoke(null, new Object[] { (object)parameter }); + } + + public static Type[] getConditions() { + loadConditionTypes(); + return conditionTypes; + } + } +} diff --git a/bot/bot/Response.cs b/bot/bot/Response.cs index f09111c..28d5cfe 100644 --- a/bot/bot/Response.cs +++ b/bot/bot/Response.cs @@ -24,7 +24,7 @@ namespace bot { public Response(string conditions, int responseId, string parameters, int cooldown) { this.conditions = conditions; - string typeName = (string)(new MySqlCommand("SELECT `name` FROM `resptypes` WHERE `id`=" + responseId, Bot.conn)).ExecuteScalar(); + string typeName = (string)(new MySqlCommand("SELECT `name` FROM `resptypes` WHERE `id`=" + responseId, _G.conn)).ExecuteScalar(); this.responseType = Assembly.GetExecutingAssembly().GetTypes().Where(t => String.Equals(t.Namespace, "bot.responses", StringComparison.Ordinal) && String.Equals(t.Name, typeName, StringComparison.Ordinal)).ToArray()[0]; this.parameters = parameters; this.cooldown = cooldown; diff --git a/bot/bot/StringSanitizer.cs b/bot/bot/Sanitizer.cs similarity index 89% rename from bot/bot/StringSanitizer.cs rename to bot/bot/Sanitizer.cs index 9078808..dbd82f6 100644 --- a/bot/bot/StringSanitizer.cs +++ b/bot/bot/Sanitizer.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; namespace bot { - class StringSanitizer { + class Sanitizer { public static String Sanitize(String str) { return str.Replace("'", "\\'"); } diff --git a/bot/bot/bot.csproj b/bot/bot/bot.csproj index f11bed8..dabbf4c 100644 --- a/bot/bot/bot.csproj +++ b/bot/bot/bot.csproj @@ -91,10 +91,12 @@ + + - + @@ -106,9 +108,7 @@ - - - +