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()
|
|
|
|
{
|
2010-08-14 17:34:36 +02:00
|
|
|
var notification = Engine.GuiInterfaceCall("GetNextNotification");
|
2010-08-14 00:02:27 +02:00
|
|
|
|
2010-08-14 06:34:49 +02:00
|
|
|
if (notification && notification.player == Engine.GetPlayerID())
|
2010-08-14 00:02:27 +02:00
|
|
|
{
|
2010-08-14 06:34:49 +02:00
|
|
|
var timerExpiredFunction = function () { removeOldNotifications(); }
|
|
|
|
|
2010-08-14 00:02:27 +02:00
|
|
|
notifications.push(notification);
|
|
|
|
notificationsTimers.push(setTimeout(timerExpiredFunction, NOTIFICATION_TIMEOUT));
|
|
|
|
|
2010-08-14 06:34:49 +02:00
|
|
|
if (notifications.length > MAX_NUM_NOTIFICATION_LINES)
|
2010-08-14 00:02:27 +02:00
|
|
|
removeOldNotifications();
|
2010-08-14 06:34:49 +02:00
|
|
|
else
|
|
|
|
displayNotifications();
|
2010-08-14 00:02:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeOldNotifications()
|
|
|
|
{
|
|
|
|
clearTimeout(notificationsTimers[0]); // The timer only needs to be cleared when new notifications bump old notifications off
|
|
|
|
notificationsTimers.shift();
|
|
|
|
notifications.shift();
|
2010-08-14 06:34:49 +02:00
|
|
|
displayNotifications();
|
|
|
|
}
|
|
|
|
|
|
|
|
function displayNotifications()
|
|
|
|
{
|
|
|
|
var messages = [];
|
|
|
|
for each (var n in notifications)
|
|
|
|
messages.push(n.message);
|
|
|
|
getGUIObjectByName("notificationText").caption = messages.join("\n");
|
2010-08-14 00:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//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":
|
2010-08-14 21:45:22 +02:00
|
|
|
obj.caption = "Waiting for other players to connect...";
|
2010-08-12 17:44:03 +02:00
|
|
|
obj.hidden = false;
|
|
|
|
break;
|
|
|
|
case "active":
|
|
|
|
obj.caption = "";
|
|
|
|
obj.hidden = true;
|
|
|
|
break;
|
|
|
|
case "disconnected":
|
2010-08-14 21:45:22 +02:00
|
|
|
obj.caption = "Connection to the server has been lost.\n\nThe game has ended.";
|
2010-08-12 17:44:03 +02:00
|
|
|
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");
|
2010-08-14 21:45:22 +02:00
|
|
|
obj.caption = g_PlayerAssignments[host].name + " has disconnected.\n\nThe game has ended.";
|
2010-08-13 01:15:01 +02:00
|
|
|
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":
|
2010-08-14 17:34:36 +02:00
|
|
|
addChatMessage({ "type": "message", "guid": message.guid, "text": message.text });
|
2010-08-12 17:44:03 +02:00
|
|
|
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
|
2010-08-14 21:45:22 +02:00
|
|
|
addChatMessage({ "type": "message", "guid": "local", "text": text });
|
2010-08-12 17:44:03 +02:00
|
|
|
|
2010-08-14 17:34:36 +02:00
|
|
|
input.caption = ""; // Clear chat input
|
2010-08-12 17:44:03 +02:00
|
|
|
}
|
2010-08-15 03:27:52 +02:00
|
|
|
|
|
|
|
input.blur(); // Remove focus
|
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
|
|
|
|
|
2010-08-14 21:45:22 +02:00
|
|
|
var n = g_PlayerAssignments[msg.guid].player;
|
|
|
|
var username = g_PlayerAssignments[msg.guid].name;
|
2010-08-14 17:34:36 +02:00
|
|
|
var playerColor = g_Players[n].color.r + " " + g_Players[n].color.g + " " + g_Players[n].color.b;
|
|
|
|
|
2010-08-12 17:44:03 +02:00
|
|
|
var formatted;
|
|
|
|
|
|
|
|
switch (msg.type)
|
|
|
|
{
|
2010-08-14 17:34:36 +02:00
|
|
|
/*
|
2010-08-12 17:44:03 +02:00
|
|
|
case "disconnect":
|
2010-08-14 17:34:36 +02:00
|
|
|
formatted = "<[color=\"" + playerColor + "\"]" + username + "[/color]> has left";
|
2010-08-12 17:44:03 +02:00
|
|
|
break;
|
2010-08-14 17:34:36 +02:00
|
|
|
*/
|
2010-08-12 17:44:03 +02:00
|
|
|
case "message":
|
2010-08-14 17:34:36 +02:00
|
|
|
console.write("<" + username + "> " + msg.text);
|
|
|
|
formatted = "<[color=\"" + playerColor + "\"]" + 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 06:34:49 +02:00
|
|
|
if (chatMessages.length > MAX_NUM_CHAT_LINES)
|
2010-08-12 17:44:03 +02:00
|
|
|
removeOldChatMessages();
|
2010-08-14 06:34:49 +02:00
|
|
|
else
|
|
|
|
getGUIObjectByName("chatText").caption = chatMessages.join("\n");
|
2010-08-12 17:44:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|