1
0

Web chat for the WebAdmin :D

git-svn-id: http://mc-server.googlecode.com/svn/trunk@773 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
faketruth 2012-08-22 00:35:06 +00:00
parent 46904e1820
commit b5c4994475
2 changed files with 112 additions and 0 deletions

View File

@ -22,6 +22,7 @@ function Initialize( Plugin )
PluginManager:AddHook(Plugin, cPluginManager.HOOK_BLOCK_DIG)
PluginManager:AddHook(Plugin, cPluginManager.HOOK_KILLED)
PluginManager:AddHook(Plugin, cPluginManager.HOOK_CRAFTING_NO_RECIPE)
PluginManager:AddHook(Plugin, cPluginManager.E_PLUGIN_CHAT) -- used in web_chat.lua
Plugin:AddCommand("/help", " - [Page] Show this message", "core.help")
Plugin:AddCommand("/pluginlist", " - Show list of plugins", "core.pluginlist")
@ -137,6 +138,7 @@ function Initialize( Plugin )
local WebPlugin = Plugin:CreateWebPlugin()
WebPlugin:SetName( Plugin:GetName() )
WebPlugin:AddTab( "Server Settings", HandleRequest_ServerSettings )
WebPlugin:AddTab( "Chat", HandleRequest_Chat )
WebPlugin:AddTab( "Playerlist", HandleRequest_PlayerList )
WebPlugin:AddTab( "Whitelist", HandleRequest_WhiteList )
WebPlugin:AddTab( "Permissions", HandleRequest_Permissions )

View File

@ -0,0 +1,110 @@
local JavaScript = [[
<script type="text/javascript">
function createXHR()
{
var request = false;
try {
request = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (err2) {
try {
request = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (err3) {
try {
request = new XMLHttpRequest();
}
catch (err1)
{
request = false;
}
}
}
return request;
}
function loadWholePage( url )
{
var storage = document.getElementById('ChatDiv');
var xhr = createXHR();
xhr.onreadystatechange=function()
{
if(xhr.readyState == 4)
{
//alert( xhr.status + " " + xhr.statusText );
//if(xhr.status == 200)
{
storage.innerHTML = xhr.responseText;//getBody(xhr.responseText);
storage.scrollTop = storage.scrollHeight;
}
}
};
xhr.open("GET", url , true);
xhr.send(null);
return false;
}
function SendChatMessage()
{
var MessageContainer = document.getElementById('ChatMessage');
if( MessageContainer.value == "" ) return;
var xhr = createXHR();
xhr.onreadystatechange=function()
{
if(xhr.readyState == 4)
{
//alert( xhr.status + " " + xhr.statusText );
RefreshChat();
}
};
xhr.open("GET", "/~webadmin/Core/Chat/?ChatMessage=" + MessageContainer.value, true);
xhr.send(null);
MessageContainer.value = "";
}
function RefreshChat()
{
loadWholePage('/~webadmin/Core/Chat/?JustChat=true');
}
setInterval(RefreshChat, 1000);
window.onload = RefreshChat;
</script>
]]
local ChatLogMessages = {}
function AddMessage( PlayerName, Message )
table.insert( ChatLogMessages, { name = PlayerName, message = Message } )
end
function OnChat( Player, Message )
AddMessage( Player:GetName(), Message )
end
function HandleRequest_Chat( Request )
if( Request.Params["JustChat"] ~= nil ) then
local Content = ""
for key, value in pairs(ChatLogMessages) do
Content = Content .. "[" .. value.name .. "]: " .. value.message .. "<br>"
end
return Content
end
if( Request.Params["ChatMessage"] ~= nil ) then
local Message = "[WebAdmin]: " .. Request.Params["ChatMessage"]
cRoot:Get():GetServer():SendMessage( Message )
AddMessage("WebAdmin", Request.Params["ChatMessage"] )
return ""
end
local Content = JavaScript
Content = Content .. [[
<div style="font-family: Courier; border: 1px solid #DDD; padding: 10px; width: 97%; height: 200px; overflow: scroll;" id="ChatDiv">Chat messageessss</div>
<input type="text" id="ChatMessage" onKeyPress="if (event.keyCode == 13) { SendChatMessage(); }"><input type="submit" value="Submit" onClick="SendChatMessage();">
]]
return Content
end