using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SharedLibrary; using System.IO; using System.Data; namespace StatsPlugin { public class ChatDatabase : Database { private string[] CommonWords = new string[] { "for", "with", "from", "about", "your", "just", "into", "over", "after", "that", "not", "you", "this", "but", "his", "they", "then", "her", "she", "will", "one", "all", "would", "there", "their", "have", "say", "get", "make", "know", "take", "see", "come", "think", "look", "want", "can", "was", "give", "use", "find", "tell", "ask", "work", "seem", "feel", "try", "leave", "call", "good", "new", "first", "last", "long", "great", "little", "own", "other", "old", "right", "big", "high", "small", "large", "next", "early", "young", "important", "few", "public", "same", "able", "the", "and", "that", "than", "have", "this", "one", "would", "yeah", "yah", "why", "who" , "when", "where", }; public ChatDatabase(string FN) : base(FN) { } public override void Init() { if (!File.Exists(FileName)) { string createChatHistory = @"CREATE TABLE `CHATHISTORY` ( `ClientID` INTEGER NOT NULL, `Message` TEXT NOT NULL, `ServerID` INTEGER NOT NULL, `TimeSent` TEXT NOT NULL );"; ExecuteNonQuery(createChatHistory); string createChatStats = @"CREATE TABLE `WORDSTATS` ( `Word` TEXT NOT NULL, `Count` INTEGER NOT NULL DEFAULT 1, PRIMARY KEY(`Word`) );"; ExecuteNonQuery(createChatStats); } } private List GetChatHistoryFromQuery(DataTable dt) { return dt.Select().Select(q => new ChatHistory() { ClientID = Convert.ToInt32(q["ClientID"].ToString()), Message = q["Message"].ToString(), ServerID = Convert.ToInt32(q["ServerID"].ToString()), TimeSent = DateTime.Parse(q["TimeSent"].ToString()) }) .ToList(); } public List GetChatForPlayer(int clientID) { var queryResult = GetDataTable("CHATHISTORY", new KeyValuePair("ClientID", clientID)); return GetChatHistoryFromQuery(queryResult); } public List GetChatForServer(int serverID) { var queryResult = GetDataTable("CHATHISTORY", new KeyValuePair("ServerID", serverID)); return GetChatHistoryFromQuery(queryResult); } public void AddChatHistory(int clientID, int serverID, string message) { if (message.Length < 3) return; var chat = new Dictionary() { { "ClientID", clientID }, { "ServerID", serverID }, { "Message", message}, { "TimeSent", DateTime.UtcNow } }; Insert("CHATHISTORY", chat); var eachWord = message.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Where (word => word.Length >= 3) .Where(word => CommonWords.FirstOrDefault(c => c == word.ToLower()) == null) .ToList(); foreach (string _word in eachWord) { string word = _word.ToLower(); Insert("WORDSTATS", new Dictionary() { { "Word", word } }, true); UpdateIncrement("WORDSTATS", "Count", new Dictionary() { { "Count", 1 } }, new KeyValuePair("Word", word)); } } public KeyValuePair[] GetWords() { var result = GetDataTable("SELECT * FROM WORDSTATS ORDER BY Count desc LIMIT 200"); return result.Select().Select(w => new KeyValuePair(w["Word"].ToString(), Convert.ToInt32(w["Count"].ToString()))).ToArray(); } } }