2010-08-12 17:44:03 +02:00
|
|
|
// Chat data
|
2010-08-13 18:05:06 +02:00
|
|
|
const CHAT_TIMEOUT = 30000;
|
2010-08-12 17:44:03 +02:00
|
|
|
const MAX_NUM_CHAT_LINES = 20;
|
|
|
|
var chatMessages = [];
|
|
|
|
var chatTimers = [];
|
|
|
|
|
2010-08-14 00:02:27 +02:00
|
|
|
// Notification Data
|
|
|
|
const NOTIFICATION_TIMEOUT = 10000;
|
|
|
|
const MAX_NUM_NOTIFICATION_LINES = 3;
|
|
|
|
var notifications = [];
|
|
|
|
var notificationsTimers = [];
|
|
|
|
|
|
|
|
// Notifications
|
|
|
|
function handleNotifications()
|
|
|
|
{
|
|
|
|
var notification = Engine.GuiInterfaceCall("PopNotification");
|
|
|
|
var timerExpiredFunction = function () { removeOldNotifications(); }
|
|
|
|
|
|
|
|
if (notification)
|
|
|
|
{
|
|
|
|
notifications.push(notification);
|
|
|
|
notificationsTimers.push(setTimeout(timerExpiredFunction, NOTIFICATION_TIMEOUT));
|
|
|
|
|
|
|
|
if (notifications.length <= MAX_NUM_NOTIFICATION_LINES)
|
|
|
|
getGUIObjectByName("notificationText").caption = notifications.join("\n");
|
|
|
|
else
|
|
|
|
removeOldNotifications();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeOldNotifications()
|
|
|
|
{
|
|
|
|
clearTimeout(notificationsTimers[0]); // The timer only needs to be cleared when new notifications bump old notifications off
|
|
|
|
notificationsTimers.shift();
|
|
|
|
notifications.shift();
|
|
|
|
getGUIObjectByName("notificationText").caption = notifications.join("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Messages
|
2010-08-12 17:44:03 +02:00
|
|
|
function handleNetMessage(message)
|
|
|
|
{
|
|
|
|
log("Net message: "+uneval(message));
|
|
|
|
|
|
|
|
switch (message.type)
|
|
|
|
{
|
|
|
|
case "netstatus":
|
|
|
|
var obj = getGUIObjectByName("netStatus");
|
|
|
|
switch (message.status)
|
|
|
|
{
|
|
|
|
case "waiting_for_players":
|
|
|
|
obj.caption = "Waiting for other players to connect";
|
|
|
|
obj.hidden = false;
|
|
|
|
break;
|
|
|
|
case "active":
|
|
|
|
obj.caption = "";
|
|
|
|
obj.hidden = true;
|
|
|
|
break;
|
|
|
|
case "disconnected":
|
|
|
|
obj.caption = "Connection to the server has been lost";
|
|
|
|
obj.hidden = false;
|
2010-08-13 01:15:01 +02:00
|
|
|
getGUIObjectByName("disconnectedExitButton").hidden = false;
|
2010-08-12 17:44:03 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unrecognised netstatus type "+message.status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2010-08-12 21:31:17 +02:00
|
|
|
case "players":
|
|
|
|
// Find and report all leavings
|
|
|
|
for (var host in g_PlayerAssignments)
|
2010-08-13 01:15:01 +02:00
|
|
|
{
|
2010-08-12 21:31:17 +02:00
|
|
|
if (! message.hosts[host])
|
2010-08-13 01:15:01 +02:00
|
|
|
{
|
|
|
|
var obj = getGUIObjectByName("netStatus");
|
|
|
|
obj.caption = g_PlayerAssignments[host].name + " has left\n\nConnection to the server has been lost";
|
|
|
|
obj.hidden = false;
|
|
|
|
getGUIObjectByName("disconnectedExitButton").hidden = false;
|
|
|
|
}
|
|
|
|
}
|
2010-08-12 21:31:17 +02:00
|
|
|
break;
|
2010-08-12 17:44:03 +02:00
|
|
|
case "chat":
|
|
|
|
addChatMessage({ "type": "message", "username": message.username, "text": message.text });
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unrecognised net message type "+message.type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function submitChatInput()
|
|
|
|
{
|
|
|
|
var input = getGUIObjectByName("chatInput");
|
|
|
|
var text = input.caption;
|
|
|
|
if (text.length)
|
|
|
|
{
|
|
|
|
if (g_IsNetworked)
|
|
|
|
Engine.SendNetworkChat(text);
|
|
|
|
else
|
|
|
|
addChatMessage({ "type": "message", "username": g_Players[1].name, "text": text });
|
|
|
|
|
|
|
|
input.caption = "";
|
2010-08-13 15:50:03 +02:00
|
|
|
|
|
|
|
// Remove focus
|
|
|
|
input.blur();
|
2010-08-12 17:44:03 +02:00
|
|
|
}
|
2010-08-13 18:05:06 +02:00
|
|
|
|
|
|
|
toggleChatWindow();
|
2010-08-12 17:44:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function addChatMessage(msg)
|
|
|
|
{
|
|
|
|
// TODO: we ought to escape all values before displaying them,
|
|
|
|
// to prevent people inserting colours and newlines etc
|
|
|
|
|
|
|
|
var playerColor = getColorByPlayerName(msg.username);
|
|
|
|
var formatted;
|
|
|
|
|
|
|
|
switch (msg.type)
|
|
|
|
{
|
|
|
|
case "disconnect":
|
2010-08-14 00:02:27 +02:00
|
|
|
formatted = "<[color=\"" + playerColor + "\"]" + msg.username + "[/color]> has left";
|
2010-08-12 17:44:03 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "message":
|
2010-08-13 18:05:06 +02:00
|
|
|
console.write("<" + msg.username + "> " + msg.text);
|
|
|
|
formatted = "<[color=\"" + playerColor + "\"]" + msg.username + "[/color]> " + msg.text;
|
2010-08-12 17:44:03 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("Invalid chat message '" + uneval(msg) + "'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var timerExpiredFunction = function () { removeOldChatMessages(); }
|
|
|
|
|
|
|
|
chatMessages.push(formatted);
|
|
|
|
chatTimers.push(setTimeout(timerExpiredFunction, CHAT_TIMEOUT));
|
|
|
|
|
2010-08-14 00:02:27 +02:00
|
|
|
if (chatMessages.length <= MAX_NUM_CHAT_LINES)
|
2010-08-12 17:44:03 +02:00
|
|
|
getGUIObjectByName("chatText").caption = chatMessages.join("\n");
|
|
|
|
else
|
|
|
|
removeOldChatMessages();
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeOldChatMessages()
|
|
|
|
{
|
2010-08-12 21:31:17 +02:00
|
|
|
clearTimeout(chatTimers[0]); // The timer only needs to be cleared when new messages bump old messages off
|
2010-08-12 17:44:03 +02:00
|
|
|
chatTimers.shift();
|
|
|
|
chatMessages.shift();
|
|
|
|
getGUIObjectByName("chatText").caption = chatMessages.join("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
function getColorByPlayerName(playerName)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < g_Players.length; i++)
|
|
|
|
if (playerName == g_Players[i].name)
|
|
|
|
return g_Players[i].color.r + " " + g_Players[i].color.g + " " + g_Players[i].color.b;
|
|
|
|
|
|
|
|
return "255 255 255";
|
|
|
|
}
|