Added a basic notification system

Currently, it only reports insufficient resource notifications

This was SVN commit r7939.
This commit is contained in:
WhiteTreePaladin 2010-08-13 22:02:27 +00:00
parent 8367b73087
commit 9a1e450031
7 changed files with 97 additions and 17 deletions

View File

@ -4,6 +4,39 @@ const MAX_NUM_CHAT_LINES = 20;
var chatMessages = [];
var chatTimers = [];
// 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
function handleNetMessage(message)
{
log("Net message: "+uneval(message));
@ -84,7 +117,7 @@ function addChatMessage(msg)
switch (msg.type)
{
case "disconnect":
formatted = '<[font=\"serif-stroke-14\"][color="' + playerColor + '"]' + msg.username + '[/color][/font]> has left';
formatted = "<[color=\"" + playerColor + "\"]" + msg.username + "[/color]> has left";
break;
case "message":
@ -102,7 +135,7 @@ function addChatMessage(msg)
chatMessages.push(formatted);
chatTimers.push(setTimeout(timerExpiredFunction, CHAT_TIMEOUT));
if (chatMessages.length < MAX_NUM_CHAT_LINES)
if (chatMessages.length <= MAX_NUM_CHAT_LINES)
getGUIObjectByName("chatText").caption = chatMessages.join("\n");
else
removeOldChatMessages();
@ -124,9 +157,3 @@ function getColorByPlayerName(playerName)
return "255 255 255";
}
function clearChatInput()
{
getGUIObjectByName("chatInput").caption = "";
}

View File

@ -23,7 +23,6 @@ var g_IsNetworked = false;
// Cache the basic player data (name, civ, color)
var g_Players = [];
var g_PlayerAssignments = {};
// Cache dev-mode settings that are frequently or widely used
@ -110,6 +109,10 @@ function onSimulationUpdate()
if (!simState)
return;
handleNotifications();
updateDebug(simState);
updatePlayerDisplay(simState);
updateSelectionDetails();

View File

@ -131,6 +131,13 @@
<action on="Press">togglePause();</action>
</object>
<!-- ================================ ================================ -->
<!-- Notification Area -->
<!-- ================================ ================================ -->
<object name="notificationPanel" type="image" size="217 100%-240 650 100%-180" ghost="true">
<object name="notificationText" size="0 0 100% 100%" type="text" style="notificationPanel" ghost="true"/>
</object>
<!-- ================================ ================================ -->
<!-- Chat -->
<!-- ================================ ================================ -->

View File

@ -154,5 +154,14 @@
text_align="left"
text_valign="top"
/>
<style name="notificationPanel"
buffer_zone="5"
font="serif-bold-stroke-14"
textcolor="red"
textcolor_selected="white"
text_align="center"
text_valign="top"
/>
</styles>

View File

@ -1,4 +1,7 @@
function GuiInterface() {}
function GuiInterface()
{
this.notifications = [];
}
GuiInterface.prototype.Schema =
"<a:component type='system'/><empty/>";
@ -14,6 +17,21 @@ GuiInterface.prototype.Init = function()
this.rallyPoints = undefined;
};
GuiInterface.prototype.PushNotification = function(notification)
{
this.notifications.push(notification);
};
GuiInterface.prototype.PopNotification = function()
{
//warn(uneval(this.notifications));
if (this.notifications.length)
return this.notifications.pop();
else
return "";
};
GuiInterface.prototype.GetSimulationState = function(player)
{
var ret = {
@ -367,6 +385,7 @@ GuiInterface.prototype.SetRangeDebugOverlay = function(player, enabled)
// trusted and indicates the player associated with the current client; no data should
// be returned unless this player is meant to be able to see it.)
var exposedFunctions = {
"PopNotification": 1,
"GetSimulationState": 1,
"GetEntityState": 1,
"GetTemplateData": 1,

View File

@ -82,14 +82,29 @@ Player.prototype.AddResources = function(amounts)
Player.prototype.TrySubtractResources = function(amounts)
{
// Check we can afford it all
// Check if we can afford it all
var amountsNeeded = {};
for (var type in amounts)
if (amounts[type] > this.resourceCount[type])
return false;
// Subtract the resources
for (var type in amounts)
this.resourceCount[type] -= amounts[type];
amountsNeeded[type] = amounts[type] - this.resourceCount[type];
var formattedAmountsNeeded = [];
for (var type in amountsNeeded)
formattedAmountsNeeded.push(type + ": " + amountsNeeded[type]);
// If we don't have enough resources, send a notification to the player
if (formattedAmountsNeeded.length)
{
var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
cmpGUIInterface.PushNotification("Resources needed: " + formattedAmountsNeeded.join(", "));
return false;
}
else
{
// Subtract the resources
for (var type in amounts)
his.resourceCount[type] -= amounts[type];
}
return true;
};

View File

@ -81,7 +81,7 @@ TrainingQueue.prototype.AddBatch = function(player, templateName, count)
if (!cmpPlayer.TrySubtractResources(costs))
{
// TODO: report error to player (they ran out of resources)
// TrySubtractResources should report error to player (they ran out of resources)
return;
}