From 5f95bca6b03ed6c9f33efb02bbc10f4fdea4a46e Mon Sep 17 00:00:00 2001 From: elexis Date: Fri, 25 Dec 2015 19:35:14 +0000 Subject: [PATCH] Session cleanup (trading dialog text). Move the code to update the idle-traders text to two new simplified functions. Eliminate all if-statements (including quadruple-nested ones). This was SVN commit r17553. --- binaries/data/mods/public/gui/session/menu.js | 237 ++++++++++-------- 1 file changed, 129 insertions(+), 108 deletions(-) diff --git a/binaries/data/mods/public/gui/session/menu.js b/binaries/data/mods/public/gui/session/menu.js index 9fece37efc..15bc5582f1 100644 --- a/binaries/data/mods/public/gui/session/menu.js +++ b/binaries/data/mods/public/gui/session/menu.js @@ -28,6 +28,9 @@ const RESOURCES = ["food", "wood", "stone", "metal"]; // Trade menu: step for probability changes const STEP = 5; +// Shown in the trade dialog. +const g_IdleTraderTextColor = "orange"; + var g_IsMenuOpen = false; var g_IsDiplomacyOpen = false; @@ -467,118 +470,136 @@ function openTrade() } updateButtons(); - var traderNumber = Engine.GuiInterfaceCall("GetTraderNumber"); - var caption = ""; - if (traderNumber.landTrader.total == 0) - caption = translate("There are no land traders."); - else - { - var inactive = traderNumber.landTrader.total - traderNumber.landTrader.trading - traderNumber.landTrader.garrisoned; - var inactiveString = ""; - if (inactive > 0) - inactiveString = "[color=\"orange\"]" + sprintf(translatePlural("%(numberOfLandTraders)s inactive", "%(numberOfLandTraders)s inactive", inactive), { numberOfLandTraders: inactive }) + "[/color]"; - - if (traderNumber.landTrader.trading > 0) - { - var openingTradingString = sprintf(translatePlural("There is %(numberTrading)s land trader trading", "There are %(numberTrading)s land traders trading", traderNumber.landTrader.trading), { numberTrading: traderNumber.landTrader.trading }); - if (traderNumber.landTrader.garrisoned > 0) - { - var garrisonedString = sprintf(translatePlural("%(numberGarrisoned)s garrisoned on a trading merchant ship", "%(numberGarrisoned)s garrisoned on a trading merchant ship", traderNumber.landTrader.garrisoned), { numberGarrisoned: traderNumber.landTrader.garrisoned }); - if (inactive > 0) - caption = sprintf(translate("%(openingTradingString)s, %(garrisonedString)s, and %(inactiveString)s."), { - openingTradingString: openingTradingString, - garrisonedString: garrisonedString, - inactiveString: inactiveString - }); - else - caption = sprintf(translate("%(openingTradingString)s, and %(garrisonedString)s."), { - openingTradingString: openingTradingString, - garrisonedString: garrisonedString - }); - } - else - { - if (inactive > 0) - caption = sprintf(translate("%(openingTradingString)s, and %(inactiveString)s."), { - openingTradingString: openingTradingString, - inactiveString: inactiveString - }); - else - caption = sprintf(translate("%(openingTradingString)s."), { - openingTradingString: openingTradingString, - }); - } - } - else - { - if (traderNumber.landTrader.garrisoned > 0) - { - var openingGarrisonedString = sprintf(translatePlural("There is %(numberGarrisoned)s land trader garrisoned on a trading merchant ship", "There are %(numberGarrisoned)s land traders garrisoned on a trading merchant ship", traderNumber.landTrader.garrisoned), { numberGarrisoned: traderNumber.landTrader.garrisoned }); - if (inactive > 0) - caption = sprintf(translate("%(openingGarrisonedString)s, and %(inactiveString)s."), { - openingGarrisonedString: openingGarrisonedString, - inactiveString: inactiveString - }); - else - caption = sprintf(translate("%(openingGarrisonedString)s."), { - openingGarrisonedString: openingGarrisonedString - }); - } - else - { - if (inactive > 0) - { - inactiveString = "[color=\"orange\"]" + sprintf(translatePlural("%(numberOfLandTraders)s land trader inactive", "%(numberOfLandTraders)s land traders inactive", inactive), { numberOfLandTraders: inactive }) + "[/color]"; - caption = sprintf(translatePlural("There is %(inactiveString)s.", "There are %(inactiveString)s.", inactive), { - inactiveString: inactiveString - }); - } - // The “else” here is already handled by “if (traderNumber.landTrader.total == 0)” above. - } - } - } - Engine.GetGUIObjectByName("landTraders").caption = caption; - - caption = ""; - if (traderNumber.shipTrader.total == 0) - caption = translate("There are no merchant ships."); - else - { - var inactive = traderNumber.shipTrader.total - traderNumber.shipTrader.trading; - var inactiveString = ""; - if (inactive > 0) - inactiveString = "[color=\"orange\"]" + sprintf(translatePlural("%(numberOfShipTraders)s inactive", "%(numberOfShipTraders)s inactive", inactive), { numberOfShipTraders: inactive }) + "[/color]"; - - if (traderNumber.shipTrader.trading > 0) - { - var openingTradingString = sprintf(translatePlural("There is %(numberTrading)s merchant ship trading", "There are %(numberTrading)s merchant ships trading", traderNumber.shipTrader.trading), { numberTrading: traderNumber.shipTrader.trading }); - if (inactive > 0) - caption = sprintf(translate("%(openingTradingString)s, and %(inactiveString)s."), { - openingTradingString: openingTradingString, - inactiveString: inactiveString - }); - else - caption = sprintf(translate("%(openingTradingString)s."), { - openingTradingString: openingTradingString, - }); - } - else - { - if (inactive > 0) - { - inactiveString = "[color=\"orange\"]" + sprintf(translatePlural("%(numberOfShipTraders)s merchant ship inactive", "%(numberOfShipTraders)s merchant ships inactive", inactive), { numberOfShipTraders: inactive }) + "[/color]"; - caption = sprintf(translatePlural("There is %(inactiveString)s.", "There are %(inactiveString)s.", inactive), { - inactiveString: inactiveString - }); - } - // The “else” here is already handled by “if (traderNumber.shipTrader.total == 0)” above. - } - } - Engine.GetGUIObjectByName("shipTraders").caption = caption; + let traderNumber = Engine.GuiInterfaceCall("GetTraderNumber"); + Engine.GetGUIObjectByName("landTraders").caption = getIdleLandTradersText(traderNumber); + Engine.GetGUIObjectByName("shipTraders").caption = getIdleShipTradersText(traderNumber); Engine.GetGUIObjectByName("tradeDialogPanel").hidden = false; } +function getIdleLandTradersText(traderNumber) +{ + let active = traderNumber.landTrader.trading; + let garrisoned = traderNumber.landTrader.garrisoned; + let inactive = traderNumber.landTrader.total - active - garrisoned; + + let messageTypes = { + "active": { + "garrisoned": { + "no-inactive": translate("%(openingTradingString)s, and %(garrisonedString)s."), + "inactive": translate("%(openingTradingString)s, %(garrisonedString)s, and %(inactiveString)s.") + }, + "no-garrisoned": { + "no-inactive": translate("%(openingTradingString)s."), + "inactive": translate("%(openingTradingString)s, and %(inactiveString)s.") + } + }, + "no-active": { + "garrisoned": { + "no-inactive": translate("%(openingGarrisonedString)s."), + "inactive": translate("%(openingGarrisonedString)s, and %(inactiveString)s.") + }, + "no-garrisoned": { + "inactive": translatePlural("There is %(inactiveString)s.", "There are %(inactiveString)s.", inactive), + "no-inactive": translate("There are no land traders.") + } + } + }; + + let message = messageTypes[active ? "active" : "no-active"][garrisoned ? "garrisoned" : "no-garrisoned"][inactive ? "inactive" : "no-inactive"]; + + let activeString = sprintf( + translatePlural( + "There is %(numberTrading)s land trader trading", + "There are %(numberTrading)s land traders trading", + active + ), + { "numberTrading": active } + ); + + let inactiveString = sprintf(active || garrisoned ? + translatePlural( + "%(numberOfLandTraders)s inactive", + "%(numberOfLandTraders)s inactive", + inactive + ) : + translatePlural( + "%(numberOfLandTraders)s land trader inactive", + "%(numberOfLandTraders)s land traders inactive", + inactive + ), + { "numberOfLandTraders": inactive } + ); + + let garrisonedString = sprintf(active || inactive ? + translatePlural( + "%(numberGarrisoned)s garrisoned on a trading merchant ship", + "%(numberGarrisoned)s garrisoned on a trading merchant ship", + garrisoned + ) : + translatePlural( + "There is %(numberGarrisoned)s land trader garrisoned on a trading merchant ship", + "There are %(numberGarrisoned)s land traders garrisoned on a trading merchant ship", + garrisoned + ), + { "numberGarrisoned": garrisoned } + ); + + return sprintf(message, { + "openingTradingString": activeString, + "openingGarrisonedString": garrisonedString, + "garrisonedString": garrisonedString, + "inactiveString": "[color=\"" + g_IdleTraderTextColor + "\"]" + inactiveString + "[/color]" + }); +} + +function getIdleShipTradersText(traderNumber) +{ + let active = traderNumber.shipTrader.trading; + let inactive = traderNumber.shipTrader.total - active; + + let messageTypes = { + "active": { + "inactive": translate("%(openingTradingString)s, and %(inactiveString)s."), + "no-inactive": translate("%(openingTradingString)s.") + }, + "no-active": { + "inactive": translatePlural("There is %(inactiveString)s.", "There are %(inactiveString)s.", inactive), + "no-inactive": translate("There are no merchant ships.") + } + }; + + let message = messageTypes[active ? "active" : "no-active"][inactive ? "inactive" : "no-inactive"]; + + let activeString = sprintf( + translatePlural( + "There is %(numberTrading)s merchant ship trading", + "There are %(numberTrading)s merchant ships trading", + active + ), + { "numberTrading": active } + ); + + let inactiveString = sprintf(active ? + translatePlural( + "%(numberOfShipTraders)s inactive", + "%(numberOfShipTraders)s inactive", + inactive + ) : + translatePlural( + "%(numberOfShipTraders)s merchant ship inactive", + "%(numberOfShipTraders)s merchant ships inactive", + inactive + ), + { "numberOfShipTraders": inactive } + ); + + return sprintf(message, { + "openingTradingString": activeString, + "inactiveString": "[color=\"" + g_IdleTraderTextColor + "\"]" + inactiveString + "[/color]" + }); +} + function closeTrade() { g_IsTradeOpen = false;