Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
chore: reduce code footprint for chat messages
Browse files Browse the repository at this point in the history
replace the resource name with an icon
  • Loading branch information
LangLangBart committed Mar 18, 2023
1 parent 5bdcacf commit b8731af
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 183 deletions.
27 changes: 27 additions & 0 deletions gui/common/tooltips~boongui.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,30 @@ function setupStatHUDTreasureInfo(template)
const resourceAmount = resourceName.map(type => resources[type]);
return { resourceName, resourceAmount };
}

/**
* @param {object} resources {wood:330}
* @param {string} color color code, e.g. "200 0 0"
* @return {string} Colored amount and resource icon
*/
function getLocalizedResourceAmounts(resources, color = "255 255 255")
{

color = brightenedColor(color);
const amounts = g_ResourceData.GetCodes()
.filter(type => !!resources[type])
.map(type => sprintf(translate("%(amount)s %(resourceType)s"), {
"amount": coloredText(resources[type], color),
"resourceType": resourceIcon(type)
}));

if (amounts.length < 2)
return amounts.join();

const lastAmount = amounts.pop();
return sprintf(translate("%(previousAmounts)s and %(lastAmount)s"), {
// Translation: This comma is used for separating first to penultimate elements in an enumeration.
"previousAmounts": coloredText(amounts.join(translate(", ")), color),
"lastAmount": coloredText(lastAmount, color)
});
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
ChatMessageEvents.ClientReady.prototype.ReadyMessage = [
translate("--- %(username)s is not ready."),
translate("+++ %(username)s is ready!")
translate("* %(username)s is not ready.").replace(/^\*/, "---"),
translate("* %(username)s is ready!").replace(/^\*/, "+++")
];

ChatMessageEvents.ClientReady.prototype.MessageTags = {
"font": "sans-stroke-14"
};
2 changes: 1 addition & 1 deletion gui/session/boonGUI_XML/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<style
name="chatPanelStyle"
buffer_zone="4"
font="sans-stroke-18"
font="sans-bold-stroke-16"
textcolor="white"
text_align="left"
text_valign="center"
Expand Down
178 changes: 0 additions & 178 deletions gui/session/chat/ChatMessageFormatSimulation.js

This file was deleted.

100 changes: 100 additions & 0 deletions gui/session/chat/ChatMessageFormatSimulation~boongui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Remove the last "period" in some messages, ".slice(0, -1)" would work too, but regex is safer
const Regex_Period = /\.$/;

ChatMessageFormatSimulation.attack.prototype.parse = function(msg)
{
if (msg.player != g_ViewedPlayer)
return "";

const message = msg.targetIsDomesticAnimal ?
translate("%(icon)sYour livestock has been attacked by %(attacker)s!") :
translate("%(icon)sYou have been attacked by %(attacker)s!");

return {
"text": sprintf(message, {
"icon": '[icon="icon_focusattacked" displace="0 1"]',
"attacker": colorizePlayernameByID(msg.attacker)
}),
"callback": ((target, position) => function() {
focusAttack({ target, position });
})(msg.target, msg.position),
"tooltip": translate("Click to focus on the attacked unit.")
};
};

ChatMessageFormatSimulation.phase.prototype.parse = function(msg)
{
const notifyPhase = Engine.ConfigDB_GetValue("user", "gui.session.notifications.phase");
if (notifyPhase == "none" || msg.player != g_ViewedPlayer && !g_IsObserver && !g_Players[msg.player].isMutualAlly[g_ViewedPlayer])
return "";

let message = "";
if (notifyPhase == "all")
{
if (msg.phaseState == "started")
message = translate("%(player)s is advancing to the %(phaseName)s.");
else if (msg.phaseState == "aborted")
message = translate("The %(phaseName)s of %(player)s has been aborted.");
}
if (msg.phaseState == "completed")
message = translate("%(player)s has reached the %(phaseName)s.");

if (!message)
return "";
message = `%(time)s ${message}`;
return {
"text": coloredText(sprintf(message.replace(Regex_Period, ""), {
"time": Engine.FormatMillisecondsIntoDateStringLocal(g_SimState.timeElapsed, translate("m:ss")),
"player": colorizePlayernameByID(msg.player),
"phaseName": getEntityNames(GetTechnologyData(msg.phaseName, g_Players[msg.player]))
}), brightenedColor(rgbToGuiColor(g_DiplomacyColors.displayedPlayerColors[msg.player])))
};
};

ChatMessageFormatSimulation.barter.prototype.parse = function(msg)
{
if (!g_IsObserver || Engine.ConfigDB_GetValue("user", "gui.session.notifications.barter") != "true")
return "";

const amountGiven = {};
amountGiven[msg.resourceGiven] = msg.amountGiven;

const amountGained = {};
amountGained[msg.resourceGained] = msg.amountGained;

return {
"text": sprintf(translate("%(player)s bartered %(amountGiven)s for %(amountGained)s.").replace(Regex_Period, ""), {
"player": colorizePlayernameByID(msg.player),
"amountGiven": getLocalizedResourceAmounts(amountGiven, "200 0 0"),
"amountGained": getLocalizedResourceAmounts(amountGained, "0 200 0")
})
};
};

ChatMessageFormatSimulation.tribute.prototype.parse = function(msg)
{
let message, color;
if (msg.targetPlayer == Engine.GetPlayerID())
{
message = translate("%(player)s has sent you %(amounts)s.");
color = "green";
}
else if (msg.sourcePlayer == Engine.GetPlayerID())
{
message = translate("You have sent %(player2)s %(amounts)s.");
color = "red";
}
else if (Engine.ConfigDB_GetValue("user", "gui.session.notifications.tribute") == "true" &&
(g_IsObserver || g_InitAttributes.settings.LockTeams &&
g_Players[msg.sourcePlayer].isMutualAlly[Engine.GetPlayerID()] &&
g_Players[msg.targetPlayer].isMutualAlly[Engine.GetPlayerID()]))
message = translate("%(player)s has sent %(player2)s %(amounts)s.");

return {
"text": sprintf(message.replace(Regex_Period, ""), {
"player": colorizePlayernameByID(msg.sourcePlayer),
"player2": colorizePlayernameByID(msg.targetPlayer),
"amounts": getLocalizedResourceAmounts(msg.amounts, color)
})
};
};
2 changes: 1 addition & 1 deletion gui/session/messages~boongui.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function colorizePlayernameByIDReturnNick(playerID)
function colorizePlayernameByID(playerID)
{
const username = splitRatingFromNick(g_Players[playerID] && escapeText(g_Players[playerID].name)).nick;
return colorizePlayernameHelper(username, playerID);
Expand Down

0 comments on commit b8731af

Please sign in to comment.