mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-07-09 13:30:06 -05:00
added chat history stuff
This commit is contained in:
95
Plugins/SimpleStats/Chat/ChatDatabase.cs
Normal file
95
Plugins/SimpleStats/Chat/ChatDatabase.cs
Normal file
@ -0,0 +1,95 @@
|
||||
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
|
||||
{
|
||||
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<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)
|
||||
{
|
||||
var chat = new Dictionary<string, object>()
|
||||
{
|
||||
{ "ClientID", clientID },
|
||||
{ "ServerID", serverID },
|
||||
{ "Message", message},
|
||||
{ "TimeSent", DateTime.UtcNow }
|
||||
};
|
||||
|
||||
Insert("CHATHISTORY", chat);
|
||||
|
||||
message.Split(' ').Where(word => word.Length >= 3).Any(word =>
|
||||
{
|
||||
word = word.ToLower();
|
||||
Insert("WORDSTATS", new Dictionary<string, object>() { { "Word", word } }, true);
|
||||
// shush :^)
|
||||
ExecuteNonQuery($"UPDATE WORDSTATS SET Count = Count + 1 WHERE Word='{word.CleanChars()}'");
|
||||
return true;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public KeyValuePair<string, int>[] GetWords()
|
||||
{
|
||||
var result = GetDataTable("SELECT * FROM WORDSTATS ORDER BY Count desc LIMIT 100");
|
||||
return result.Select().Select(w => new KeyValuePair<string, int>(w["Word"].ToString(), Convert.ToInt32(w["Count"].ToString()))).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
16
Plugins/SimpleStats/Chat/ChatHistory.cs
Normal file
16
Plugins/SimpleStats/Chat/ChatHistory.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StatsPlugin
|
||||
{
|
||||
public class ChatHistory
|
||||
{
|
||||
public int ClientID { get; set; }
|
||||
public string Message { get; set; }
|
||||
public int ServerID { get; set; }
|
||||
public DateTime TimeSent { get; set; }
|
||||
}
|
||||
}
|
82
Plugins/SimpleStats/Chat/ChatHistoryPage.cs
Normal file
82
Plugins/SimpleStats/Chat/ChatHistoryPage.cs
Normal file
@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using SharedLibrary;
|
||||
using System.Collections.Specialized;
|
||||
|
||||
namespace StatsPlugin.Chat
|
||||
{
|
||||
public class ChatPage : HTMLPage
|
||||
{
|
||||
public ChatPage() : base(false) { }
|
||||
|
||||
public override string GetContent(NameValueCollection querySet, IDictionary<string, string> headers)
|
||||
{
|
||||
StringBuilder S = new StringBuilder();
|
||||
S.Append(LoadHeader());
|
||||
|
||||
IFile chat = new IFile("webfront\\chat.html");
|
||||
S.Append(chat.GetText());
|
||||
chat.Close();
|
||||
|
||||
S.Append(LoadFooter());
|
||||
|
||||
return S.ToString();
|
||||
}
|
||||
|
||||
public override string GetName() => "Chat Stats";
|
||||
public override string GetPath() => "/chat";
|
||||
}
|
||||
|
||||
public class WordCloudJSON : IPage
|
||||
{
|
||||
public string GetName() => "Word Cloud JSON";
|
||||
public string GetPath() => "/_words";
|
||||
public string GetContentType() => "application/json";
|
||||
public bool Visible() => false;
|
||||
|
||||
public HttpResponse GetPage(NameValueCollection querySet, IDictionary<string, string> headers)
|
||||
{
|
||||
|
||||
HttpResponse resp = new HttpResponse()
|
||||
{
|
||||
contentType = GetContentType(),
|
||||
content = Stats.ChatDB.GetWords().Select(w => new
|
||||
{
|
||||
Word = w.Key,
|
||||
Count = w.Value
|
||||
})
|
||||
.OrderByDescending(x => x.Count)
|
||||
.ToArray(),
|
||||
|
||||
additionalHeaders = new Dictionary<string, string>()
|
||||
};
|
||||
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
|
||||
public class ClientChatJSON : IPage
|
||||
{
|
||||
public string GetName() => "Client Chat JSON";
|
||||
public string GetPath() => "/_clientchat";
|
||||
public string GetContentType() => "application/json";
|
||||
public bool Visible() => false;
|
||||
|
||||
public HttpResponse GetPage(NameValueCollection querySet, IDictionary<string, string> headers)
|
||||
{
|
||||
|
||||
HttpResponse resp = new HttpResponse()
|
||||
{
|
||||
contentType = GetContentType(),
|
||||
content = Stats.ChatDB.GetChatForPlayer(Convert.ToInt32(querySet["clientid"])).ToArray(),
|
||||
additionalHeaders = new Dictionary<string, string>()
|
||||
};
|
||||
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user