2017-11-02 18:20:10 -04:00
|
|
|
|
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
|
|
|
|
|
{
|
2017-11-04 19:42:31 -04:00
|
|
|
|
private string[] CommonWords = new string[] { "for",
|
|
|
|
|
"with",
|
|
|
|
|
"from",
|
|
|
|
|
"about",
|
2017-11-13 16:58:23 -05:00
|
|
|
|
"your",
|
|
|
|
|
"just",
|
2017-11-04 19:42:31 -04:00
|
|
|
|
"into",
|
|
|
|
|
"over",
|
|
|
|
|
"after",
|
|
|
|
|
"that",
|
|
|
|
|
"not",
|
|
|
|
|
"you",
|
|
|
|
|
"this",
|
|
|
|
|
"but",
|
|
|
|
|
"his",
|
|
|
|
|
"they",
|
2017-11-13 16:58:23 -05:00
|
|
|
|
"then",
|
2017-11-04 19:42:31 -04:00
|
|
|
|
"her",
|
|
|
|
|
"she",
|
|
|
|
|
"will",
|
|
|
|
|
"one",
|
|
|
|
|
"all",
|
|
|
|
|
"would",
|
|
|
|
|
"there",
|
|
|
|
|
"their",
|
|
|
|
|
"have",
|
|
|
|
|
"say",
|
|
|
|
|
"get",
|
|
|
|
|
"make",
|
|
|
|
|
"know",
|
|
|
|
|
"take",
|
|
|
|
|
"see",
|
|
|
|
|
"come",
|
|
|
|
|
"think",
|
|
|
|
|
"look",
|
|
|
|
|
"want",
|
2017-11-13 16:58:23 -05:00
|
|
|
|
"can",
|
|
|
|
|
"was",
|
2017-11-04 19:42:31 -04:00
|
|
|
|
"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",
|
2017-11-13 16:58:23 -05:00
|
|
|
|
"than",
|
2017-11-04 19:42:31 -04:00
|
|
|
|
"have",
|
|
|
|
|
"this",
|
|
|
|
|
"one",
|
|
|
|
|
"would",
|
|
|
|
|
"yeah",
|
|
|
|
|
"yah",
|
|
|
|
|
"why",
|
|
|
|
|
"who" ,
|
2017-11-13 16:58:23 -05:00
|
|
|
|
"when",
|
|
|
|
|
"where",
|
|
|
|
|
};
|
2017-11-04 19:42:31 -04:00
|
|
|
|
|
2017-11-14 16:36:55 -05:00
|
|
|
|
public ChatDatabase(string FN, SharedLibrary.Interfaces.ILogger logger) : base(FN, logger)
|
2017-11-02 18:20:10 -04:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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<ChatHistory> 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<ChatHistory> GetChatForPlayer(int clientID)
|
|
|
|
|
{
|
|
|
|
|
var queryResult = GetDataTable("CHATHISTORY", new KeyValuePair<string, object>("ClientID", clientID));
|
|
|
|
|
return GetChatHistoryFromQuery(queryResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ChatHistory> GetChatForServer(int serverID)
|
|
|
|
|
{
|
|
|
|
|
var queryResult = GetDataTable("CHATHISTORY", new KeyValuePair<string, object>("ServerID", serverID));
|
|
|
|
|
return GetChatHistoryFromQuery(queryResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddChatHistory(int clientID, int serverID, string message)
|
|
|
|
|
{
|
2017-11-04 19:42:31 -04:00
|
|
|
|
if (message.Length < 3)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-11-02 18:20:10 -04:00
|
|
|
|
var chat = new Dictionary<string, object>()
|
|
|
|
|
{
|
|
|
|
|
{ "ClientID", clientID },
|
|
|
|
|
{ "ServerID", serverID },
|
|
|
|
|
{ "Message", message},
|
|
|
|
|
{ "TimeSent", DateTime.UtcNow }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Insert("CHATHISTORY", chat);
|
|
|
|
|
|
2017-11-04 19:42:31 -04:00
|
|
|
|
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<string, object>() { { "Word", word } }, true);
|
|
|
|
|
UpdateIncrement("WORDSTATS", "Count", new Dictionary<string, object>() { { "Count", 1 } }, new KeyValuePair<string, object>("Word", word));
|
|
|
|
|
}
|
2017-11-02 18:20:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public KeyValuePair<string, int>[] GetWords()
|
|
|
|
|
{
|
2017-11-04 19:42:31 -04:00
|
|
|
|
var result = GetDataTable("SELECT * FROM WORDSTATS ORDER BY Count desc LIMIT 200");
|
2017-11-02 18:20:10 -04:00
|
|
|
|
return result.Select().Select(w => new KeyValuePair<string, int>(w["Word"].ToString(), Convert.ToInt32(w["Count"].ToString()))).ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|