From bf8985c22ea383feb0bfbfb327c6561703aa633a Mon Sep 17 00:00:00 2001 From: Chadwick McHenry Date: Sun, 29 Jul 2018 13:46:30 -0400 Subject: [PATCH] Fix advancement descriptions for edge case --- .../src/main/webapp/javascript/tech.js | 62 ++++++++----------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/freeciv-web/src/main/webapp/javascript/tech.js b/freeciv-web/src/main/webapp/javascript/tech.js index 3a43dfb3a..46b26b280 100644 --- a/freeciv-web/src/main/webapp/javascript/tech.js +++ b/freeciv-web/src/main/webapp/javascript/tech.js @@ -410,45 +410,35 @@ function update_tech_screen() } /************************************************************************** - Returns for example "Bronze working allows building phalax". + Returns for example "Bronze working allows building phalanx." **************************************************************************/ function get_advances_text(tech_id) { - var ptech = techs[tech_id]; - - var adv_txt = "" + ptech['name'] + " (" + Math.floor(ptech['cost']) + ") allows "; - var prunits = get_units_from_tech(tech_id); - var pimprovements = get_improvements_from_tech(tech_id); - - for (var i = 0; i < prunits.length; i++) { - if (i == 0) adv_txt += "building unit "; - var punit = prunits[i]; - adv_txt += "" - + punit['name'] + ", "; - } - - for (var i = 0; i < pimprovements.length; i++) { - if (i == 0) adv_txt += "building "; - var pimpr = pimprovements[i]; - adv_txt += "" + pimpr['name'] + ", "; - } - - - for (var next_tech_id in techs) { - var ntech = techs[next_tech_id]; - if (!(next_tech_id+'' in reqtree)) continue; - - if (is_tech_req_for_tech(tech_id, next_tech_id)) { - if (adv_txt.indexOf("researching") == -1) adv_txt += "researching "; - adv_txt += "" + ntech['name'] + ", "; - } - } - - if (adv_txt.length > 3) adv_txt = adv_txt.substring(0, adv_txt.length - 2) + "."; - - return adv_txt; + const num = (value) => value === null ? 'null' : value; + const tech_span = (name, unit_id, impr_id, title) => + `${name}`; + + const is_valid_and_required = (next_tech_id) => + reqtree.hasOwnProperty(next_tech_id) && is_tech_req_for_tech(tech_id, next_tech_id); + + const format_list_with_intro = (intro, list) => + (list = list.filter(Boolean)).length ? (intro + ' ' + list.join(', ')) : ''; + + const ptech = techs[tech_id]; + + return tech_span(ptech.name, null, null) + ' (' + Math.floor(ptech.cost) + ')' + + format_list_with_intro(' allows', + [ + format_list_with_intro('building unit', get_units_from_tech(tech_id) + .map(unit => tech_span(unit.name, unit.id, null, unit.helptext))), + format_list_with_intro('building', get_improvements_from_tech(tech_id) + .map(impr => tech_span(impr.name, null, impr.id, impr.helptext))), + format_list_with_intro('researching', Object.keys(techs) + .filter(is_valid_and_required) + .map(tid => techs[tid]) + .map(tech => tech_span(tech.name, null, null))) + ]) + '.'; } /**************************************************************************