From 8967395da6268b905dec1b4bb91e635c84ca7ef1 Mon Sep 17 00:00:00 2001 From: WhiteTreePaladin Date: Sat, 14 Aug 2010 15:34:36 +0000 Subject: [PATCH] Made chat messages send a GUID rather than a user name This was SVN commit r7943. --- .../mods/public/gui/session_new/messages.js | 38 +++++++------------ .../mods/public/gui/session_new/session.js | 2 +- .../simulation/components/GuiInterface.js | 4 +- .../public/simulation/components/Player.js | 4 +- source/network/NetClient.cpp | 2 +- source/network/NetMessages.h | 2 +- source/network/NetServer.cpp | 2 +- 7 files changed, 22 insertions(+), 32 deletions(-) diff --git a/binaries/data/mods/public/gui/session_new/messages.js b/binaries/data/mods/public/gui/session_new/messages.js index 3810cbe82a..002d422895 100644 --- a/binaries/data/mods/public/gui/session_new/messages.js +++ b/binaries/data/mods/public/gui/session_new/messages.js @@ -13,7 +13,7 @@ var notificationsTimers = []; // Notifications function handleNotifications() { - var notification = Engine.GuiInterfaceCall("PopNotification"); + var notification = Engine.GuiInterfaceCall("GetNextNotification"); if (notification && notification.player == Engine.GetPlayerID()) { @@ -88,7 +88,7 @@ function handleNetMessage(message) } break; case "chat": - addChatMessage({ "type": "message", "username": message.username, "text": message.text }); + addChatMessage({ "type": "message", "guid": message.guid, "text": message.text }); break; default: error("Unrecognised net message type "+message.type); @@ -104,12 +104,10 @@ function submitChatInput() if (g_IsNetworked) Engine.SendNetworkChat(text); else - addChatMessage({ "type": "message", "username": g_Players[1].name, "text": text }); + addChatMessage({ "type": "message", "guid": 1, "text": text }); - input.caption = ""; - - // Remove focus - input.blur(); + input.caption = ""; // Clear chat input + input.blur(); // Remove focus } toggleChatWindow(); @@ -120,22 +118,23 @@ function addChatMessage(msg) // TODO: we ought to escape all values before displaying them, // to prevent people inserting colours and newlines etc - //var n = msg.player; - //var playerColor = g_Players[n].color.r + " " + g_Players[n].color.g + " " + g_Players[n].color.b; - var playerColor = getColorByPlayerName(msg.username); + var n = msg.guid; + var username = g_Players[n].name; + var playerColor = g_Players[n].color.r + " " + g_Players[n].color.g + " " + g_Players[n].color.b; + var formatted; switch (msg.type) { + /* case "disconnect": - formatted = "<[color=\"" + playerColor + "\"]" + msg.username + "[/color]> has left"; + formatted = "<[color=\"" + playerColor + "\"]" + username + "[/color]> has left"; break; - + */ case "message": - console.write("<" + msg.username + "> " + msg.text); - formatted = "<[color=\"" + playerColor + "\"]" + msg.username + "[/color]> " + msg.text; + console.write("<" + username + "> " + msg.text); + formatted = "<[color=\"" + playerColor + "\"]" + username + "[/color]> " + msg.text; break; - default: error("Invalid chat message '" + uneval(msg) + "'"); return; @@ -159,12 +158,3 @@ function removeOldChatMessages() 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"; -} diff --git a/binaries/data/mods/public/gui/session_new/session.js b/binaries/data/mods/public/gui/session_new/session.js index 5ceb8ec9f0..1906153785 100644 --- a/binaries/data/mods/public/gui/session_new/session.js +++ b/binaries/data/mods/public/gui/session_new/session.js @@ -23,7 +23,7 @@ var g_IsNetworked = false; // Cache the basic player data (name, civ, color) var g_Players = []; -var g_PlayerAssignments = {}; +var g_PlayerAssignments = { "local": { "name": "You", "player": 1 } }; var g_PlayerID; // Cache dev-mode settings that are frequently or widely used diff --git a/binaries/data/mods/public/simulation/components/GuiInterface.js b/binaries/data/mods/public/simulation/components/GuiInterface.js index 4eff07ef41..fa4e73acc3 100644 --- a/binaries/data/mods/public/simulation/components/GuiInterface.js +++ b/binaries/data/mods/public/simulation/components/GuiInterface.js @@ -181,7 +181,7 @@ GuiInterface.prototype.PushNotification = function(notification) this.notifications.push(notification); }; -GuiInterface.prototype.PopNotification = function() +GuiInterface.prototype.GetNextNotification = function() { if (this.notifications.length) return this.notifications.pop(); @@ -387,7 +387,7 @@ var exposedFunctions = { "GetSimulationState": 1, "GetEntityState": 1, "GetTemplateData": 1, - "PopNotification": 1, + "GetNextNotification": 1, "SetSelectionHighlight": 1, "DisplayRallyPoint": 1, diff --git a/binaries/data/mods/public/simulation/components/Player.js b/binaries/data/mods/public/simulation/components/Player.js index d543211f41..be4fea35f5 100644 --- a/binaries/data/mods/public/simulation/components/Player.js +++ b/binaries/data/mods/public/simulation/components/Player.js @@ -95,7 +95,7 @@ Player.prototype.TrySubtractResources = function(amounts) // If we don't have enough resources, send a notification to the player if (formattedAmountsNeeded.length) { - var notification = {"player": this.playerID, "message": "Resources needed: " + formattedAmountsNeeded.join(", ")}; + var notification = {"player": this.playerID, "message": "Insufficient resources - " + formattedAmountsNeeded.join(", ")}; var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); cmpGUIInterface.PushNotification(notification); return false; @@ -104,7 +104,7 @@ Player.prototype.TrySubtractResources = function(amounts) { // Subtract the resources for (var type in amounts) - this.resourceCount[type] -= amounts[type]; + this.resourceCount[type] -= amounts[type]; } return true; diff --git a/source/network/NetClient.cpp b/source/network/NetClient.cpp index eec42d2612..d06ecf6d7e 100644 --- a/source/network/NetClient.cpp +++ b/source/network/NetClient.cpp @@ -292,7 +292,7 @@ bool CNetClient::OnChat(void* context, CFsmEvent* event) CScriptValRooted msg; client->GetScriptInterface().Eval("({'type':'chat'})", msg); - client->GetScriptInterface().SetProperty(msg.get(), "username", std::wstring(message->m_Sender), false); + client->GetScriptInterface().SetProperty(msg.get(), "guid", std::string(message->m_GUID), false); client->GetScriptInterface().SetProperty(msg.get(), "text", std::wstring(message->m_Message), false); client->PushGuiMessage(msg); diff --git a/source/network/NetMessages.h b/source/network/NetMessages.h index 8765cce32a..7a4d6f68ec 100644 --- a/source/network/NetMessages.h +++ b/source/network/NetMessages.h @@ -107,7 +107,7 @@ START_NMT_CLASS_(AuthenticateResult, NMT_AUTHENTICATE_RESULT) END_NMT_CLASS() START_NMT_CLASS_(Chat, NMT_CHAT) - NMT_FIELD(CStrW, m_Sender) // ignored when client->server, valid when server->client + NMT_FIELD(CStr8, m_GUID) // ignored when client->server, valid when server->client NMT_FIELD(CStrW, m_Message) END_NMT_CLASS() diff --git a/source/network/NetServer.cpp b/source/network/NetServer.cpp index c288195f6e..7b685c6476 100644 --- a/source/network/NetServer.cpp +++ b/source/network/NetServer.cpp @@ -484,7 +484,7 @@ bool CNetServer::OnChat(void* context, CFsmEvent* event) CChatMessage* message = (CChatMessage*)event->GetParamRef(); - message->m_Sender = session->GetUserName(); + message->m_GUID = session->GetHostID(); server.Broadcast(message);