From 4e49bbc70620df674001773eff3bcc635330dfe2 Mon Sep 17 00:00:00 2001 From: ezsh <ezsh.junk@gmail.com> Date: Mon, 17 Jan 2022 01:45:07 +0100 Subject: [PATCH] Move rating utilities from slaveSummary to free function --- js/002-config/fc-js-init.js | 1 + js/003-data/slaveSummaryData.js | 4 +- js/ratings.js | 41 ++++++++++++++++ src/js/slaveSummaryHelpers.js | 84 +++++++++------------------------ src/js/slaveSummaryWidgets.js | 18 +++---- 5 files changed, 74 insertions(+), 74 deletions(-) create mode 100644 js/ratings.js diff --git a/js/002-config/fc-js-init.js b/js/002-config/fc-js-init.js index dde6cc0b140..12eccefaacb 100644 --- a/js/002-config/fc-js-init.js +++ b/js/002-config/fc-js-init.js @@ -67,6 +67,7 @@ App.Medicine.Surgery = {}; App.Medicine.Surgery.Procedures = {}; App.Medicine.Surgery.Reactions = {}; App.RA = {}; +App.Ratings = {}; App.SF = {}; App.SecExp = {}; App.SlaveAssignment = {}; diff --git a/js/003-data/slaveSummaryData.js b/js/003-data/slaveSummaryData.js index 78f1c569fae..9015f388967 100644 --- a/js/003-data/slaveSummaryData.js +++ b/js/003-data/slaveSummaryData.js @@ -1,5 +1,5 @@ -// these ratings tables are consumed by App.UI.SlaveSummaryImpl.helpers. getNumericRating() -// this function takes a value for rating and iterates a table (in the declaration order) +// these ratings tables are consumed by App.Ratings.xxx() +// those function takes a value for rating and iterates a table (in the declaration order) // until it finds a key greater or equal to the value; the found record is returned from the function // Briefly, each dictionary entry key -> value is read as "the highest rating that still suits -> value" or // "up to, including" diff --git a/js/ratings.js b/js/ratings.js new file mode 100644 index 00000000000..3e0671dfa50 --- /dev/null +++ b/js/ratings.js @@ -0,0 +1,41 @@ + +/** + * @template {PropertyKey} K + * @template {*} T + * @param {Partial<Record<K, T>>} dict + * @param {K} value + * @param {T} [defaultValue] + * @returns {T|null} + */ +App.Ratings.exact = function(dict, value, defaultValue = null) { + const res = dict[value]; + return res ? res : defaultValue; +}; + +/** + * @template {*} T + * @param {Record<number, T>} ratings + * @param {number} value + * @returns {T|null} + */ +App.Ratings.numeric = function(ratings, value) { + for (const key in ratings) { + if (parseInt(key) >= value) { + return ratings[key]; + } + } + return null; +}; + +/** + * @param {object} ratings + * @param {number[]} values + * @returns {*|null} + */ +App.Ratings.multiNumeric = function(ratings, values) { + const firstRating = App.Ratings.numeric(ratings, values[0]); + if (firstRating === null || typeof firstRating === "string" || firstRating.hasOwnProperty("desc")) { + return firstRating; + } + return App.Ratings.multiNumeric(firstRating, values.slice(1)); +}; diff --git a/src/js/slaveSummaryHelpers.js b/src/js/slaveSummaryHelpers.js index 77ec72c4062..bf70a2bee02 100644 --- a/src/js/slaveSummaryHelpers.js +++ b/src/js/slaveSummaryHelpers.js @@ -84,51 +84,13 @@ App.UI.SlaveSummaryImpl = function() { return r; } - /** - * @param {object} dict - * @param {*} value - * @param {*} [defaultValue] - * @returns {*|null} - */ - function getExactRating(dict, value, defaultValue = null) { - const res = dict[value]; - return res ? res : defaultValue; - } - - /** - * @param {object} ratings - * @param {number} value - * @returns {*|null} - */ - function getNumericRating(ratings, value) { - for (const key in ratings) { - if (parseInt(key) >= value) { - return ratings[key]; - } - } - return null; - } - - /** - * @param {object} ratings - * @param {number[]} values - * @returns {*|null} - */ - function getMultiNumericRating(ratings, values) { - const firstRating = getNumericRating(ratings, values[0]); - if (firstRating === null || typeof firstRating === "string" || firstRating.hasOwnProperty("desc")) { - return firstRating; - } - return getMultiNumericRating(firstRating, values.slice(1)); - } - /** * @typedef {object} StyledDesc * @property {string} desc * @property {string|string[]} [style] */ - /** @typedef {Object.<string, StyledDesc>} StyledRatings */ + /** @typedef {Object.<number, StyledDesc>} StyledRatings */ /** * @param {Node} container * @param {StyledRatings} ratings @@ -137,8 +99,7 @@ App.UI.SlaveSummaryImpl = function() { * @param {boolean} [stdDecor=false] */ function makeRatedStyledSpan(container, ratings, value, offset = 0, stdDecor = false) { - /** @type {StyledDesc} */ - const d = getNumericRating(ratings, value + offset); + const d = App.Ratings.numeric(ratings, value + offset); if (d) { makeSpan(container, d.desc, d.style, stdDecor, value); } @@ -393,9 +354,6 @@ App.UI.SlaveSummaryImpl = function() { makeSpan, makeBlock, makeParagraph, - getExactRating, - getNumericRating, - getMultiNumericRating, makeStyledSpan, makeRatedStyledSpan, makeMappedStyledSpan, @@ -630,7 +588,7 @@ App.UI.SlaveSummaryImpl = function() { */ function long_age(slave, c) { const style = "pink"; - makeSpan(c, V.showAgeDetail ? `Age ${slave.actualAge}` : helpers.getNumericRating(data.long.body.age, slave.actualAge), style, true); + makeSpan(c, V.showAgeDetail ? `Age ${slave.actualAge}` : App.Ratings.numeric(data.long.body.age, slave.actualAge), style, true); /* ** No NCS, then do the standard, However because of the wrinkles of Incubators, as long as visual age is greater ** than or equal to physical age, we do the old physical body/Looks for fresh out of the can NCS slaves. @@ -668,7 +626,7 @@ App.UI.SlaveSummaryImpl = function() { * @returns {void} */ function long_face(slave, c) { - const r = helpers.getNumericRating(data.long.body.face, slave.face + 100); + const r = App.Ratings.numeric(data.long.body.face, slave.face + 100); makeSpan(c, `${r.desc} ${slave.faceShape} face`, r.style, true, slave.face); } @@ -723,7 +681,7 @@ App.UI.SlaveSummaryImpl = function() { */ function long_muscles(slave, c) { const h = helpers; - h.makeStyledSpan(c, h.getMultiNumericRating(data.long.body.muscles, [slave.muscles + 100, h.FSData.policy.PhysicalIdealist.active]), slave.muscles, true); + h.makeStyledSpan(c, App.Ratings.multiNumeric(data.long.body.muscles, [slave.muscles + 100, h.FSData.policy.PhysicalIdealist.active]), slave.muscles, true); } /** @@ -747,7 +705,7 @@ App.UI.SlaveSummaryImpl = function() { function long_tits_ass(slave, c) { const h = helpers; h.makeStyledSpan(c, - h.getMultiNumericRating(data.long.body.titsAss, + App.Ratings.multiNumeric(data.long.body.titsAss, [slave.boobs, slave.butt, h.FSData.policy.AssetExpansionist.active, slave.weight + 100, slave.muscles + 100])); } @@ -841,7 +799,7 @@ App.UI.SlaveSummaryImpl = function() { if (V.showAgeDetail === 1) { r.textContent += slave.actualAge.toString(); } else if (slave.actualAge >= 18) { - r.textContent += helpers.getNumericRating(data.short.body.age, slave.actualAge); + r.textContent += App.Ratings.numeric(data.short.body.age, slave.actualAge); } if (slave.actualAge !== slave.physicalAge) { @@ -913,7 +871,7 @@ App.UI.SlaveSummaryImpl = function() { */ function short_muscles(slave, c) { const h = helpers; - h.makeStyledSpan(c, h.getMultiNumericRating(data.short.body.muscles, [slave.muscles + 100, h.FSData.policy.PhysicalIdealist.active]), slave.muscles, true); + h.makeStyledSpan(c, App.Ratings.multiNumeric(data.short.body.muscles, [slave.muscles + 100, h.FSData.policy.PhysicalIdealist.active]), slave.muscles, true); } /** @@ -937,7 +895,7 @@ App.UI.SlaveSummaryImpl = function() { function short_tits_ass(slave, c) { const h = helpers; h.makeStyledSpan(c, - h.getMultiNumericRating(data.short.body.titsAss, + App.Ratings.multiNumeric(data.short.body.titsAss, [slave.boobs, slave.butt, h.FSData.policy.AssetExpansionist.active, slave.weight + 100, slave.muscles + 100])); } @@ -1019,8 +977,8 @@ App.UI.SlaveSummaryImpl = function() { return; } const intelligence = slave.intelligence + slave.intelligenceImplant; - const educationStr = helpers.getNumericRating(data.short.mental.education, slave.intelligenceImplant + 15); - const intelligenceRating = helpers.getNumericRating(data.short.mental.intelligence, intelligence + 100); + const educationStr = App.Ratings.numeric(data.short.mental.education, slave.intelligenceImplant + 15); + const intelligenceRating = App.Ratings.numeric(data.short.mental.intelligence, intelligence + 100); makeSpan(c, `${intelligenceRating.desc}${educationStr}`, intelligenceRating.style, true, intelligence); } @@ -1036,7 +994,7 @@ App.UI.SlaveSummaryImpl = function() { helpers.makeStyledSpan(c, sd.mss); } else { SSkills += slave.skill.vaginal; - helpers.makeStyledSpan(c, helpers.getMultiNumericRating(sd.sex, [SSkills, slave.vagina >= 0 ? 1 : 0]), Math.trunc(SSkills), true); + helpers.makeStyledSpan(c, App.Ratings.multiNumeric(sd.sex, [SSkills, slave.vagina >= 0 ? 1 : 0]), Math.trunc(SSkills), true); helpers.makeRatedStyledSpan(c, sd.whoring, slave.skill.whoring, 0, true); helpers.makeRatedStyledSpan(c, sd.entertainment, slave.skill.entertainment, 0, true); } @@ -1073,8 +1031,8 @@ App.UI.SlaveSummaryImpl = function() { return; } const intelligence = slave.intelligence + slave.intelligenceImplant; - const educationStr = helpers.getNumericRating(data.long.mental.education, slave.intelligenceImplant + 15); - const intelligenceRating = helpers.getNumericRating(data.long.mental.intelligence, intelligence + 100); + const educationStr = App.Ratings.numeric(data.long.mental.education, slave.intelligenceImplant + 15); + const intelligenceRating = App.Ratings.numeric(data.long.mental.intelligence, intelligence + 100); makeSpan(c, `${intelligenceRating.desc}${educationStr}`, intelligenceRating.style, true, intelligence); } @@ -1090,7 +1048,7 @@ App.UI.SlaveSummaryImpl = function() { helpers.makeStyledSpan(c, sd.mss); } else { SSkills += slave.skill.vaginal; - helpers.makeStyledSpan(c, helpers.getMultiNumericRating(sd.sex, [SSkills, slave.vagina >= 0 ? 1 : 0]), Math.trunc(SSkills), true); + helpers.makeStyledSpan(c, App.Ratings.multiNumeric(sd.sex, [SSkills, slave.vagina >= 0 ? 1 : 0]), Math.trunc(SSkills), true); helpers.makeRatedStyledSpan(c, sd.whoring, slave.skill.whoring, 0, true); helpers.makeRatedStyledSpan(c, sd.entertainment, slave.skill.entertainment, 0, true); } @@ -1123,7 +1081,7 @@ App.UI.SlaveSummaryImpl = function() { * @returns {void} */ function long_clothes(slave, c) { - makeSpan(c, helpers.getExactRating(data.long.clothes, slave.clothes, 'Naked.')); + makeSpan(c, App.Ratings.exact(data.long.clothes, slave.clothes, 'Naked.')); } /** @@ -1280,7 +1238,7 @@ App.UI.SlaveSummaryImpl = function() { if (!tbl || typeof tbl === 'string') { return tbl; } - return helpers.getNumericRating(tbl, slave.fetishStrength); + return App.Ratings.numeric(tbl, slave.fetishStrength); } const fStr = fetishStr(slave); @@ -1358,20 +1316,20 @@ App.UI.SlaveSummaryImpl = function() { } /** - * @param {App.Entity.SlaveState} slave + * @param {FC.SlaveState} slave * @param {Node} c * @returns {void} */ function long_fetish(slave, c) { - function fetishStr(slave) { + function fetishStr() { const tbl = data.long.fetish[slave.fetish]; if (!tbl || typeof tbl === 'string') { return tbl; } - return helpers.getNumericRating(tbl, slave.fetishStrength); + return App.Ratings.numeric(tbl, slave.fetishStrength); } - const fStr = fetishStr(slave); + const fStr = fetishStr(); if (fStr) { makeSpan(c, fStr, "lightcoral", true, slave.fetishStrength); } diff --git a/src/js/slaveSummaryWidgets.js b/src/js/slaveSummaryWidgets.js index cc56af0532c..2f7dac4fbec 100644 --- a/src/js/slaveSummaryWidgets.js +++ b/src/js/slaveSummaryWidgets.js @@ -14,7 +14,7 @@ App.UI.SlaveSummaryRenderers = function() { makeSpan(c, "MB", "mindbroken"); } else { helpers.makeRatedStyledSpan(c, data.short.mental.devotion, slave.devotion, 100, true); - helpers.makeStyledSpan(c, helpers.getMultiNumericRating(data.short.mental.trust, [slave.trust + 100, slave.devotion + 100]), + helpers.makeStyledSpan(c, App.Ratings.multiNumeric(data.short.mental.trust, [slave.trust + 100, slave.devotion + 100]), slave.trust, true); } }, @@ -114,7 +114,7 @@ App.UI.SlaveSummaryRenderers = function() { * @returns {void} */ weight: function(slave, c) { - helpers.makeStyledSpan(c, helpers.getMultiNumericRating(data.short.body.weight, + helpers.makeStyledSpan(c, App.Ratings.multiNumeric(data.short.body.weight, [slave.weight + 100, helpers.FSData.policy.HedonisticDecadence.active, slave.hips + 2]), slave.weight, true); }, @@ -127,7 +127,7 @@ App.UI.SlaveSummaryRenderers = function() { const makeSpan = helpers.makeSpan; if (slave.dick > 0) { let dickDesc = slave.balls === 0 ? "Geld" : ""; - const dickBallsDesc = helpers.getMultiNumericRating(data.short.body.genitalia.dickBalls, [slave.dick, slave.balls]); + const dickBallsDesc = App.Ratings.multiNumeric(data.short.body.genitalia.dickBalls, [slave.dick, slave.balls]); if (dickBallsDesc) { dickDesc += ` ${dickBallsDesc}`; } @@ -143,7 +143,7 @@ App.UI.SlaveSummaryRenderers = function() { if (slave.anus === 0) { makeSpan(c, "AV", "lime"); } - const holesDesc = helpers.getMultiNumericRating(data.long.body.genitalia.holes, [slave.vagina, slave.anus]); + const holesDesc = App.Ratings.multiNumeric(data.long.body.genitalia.holes, [slave.vagina, slave.anus]); if (holesDesc) { makeSpan(c, holesDesc, "pink"); } @@ -398,7 +398,7 @@ App.UI.SlaveSummaryRenderers = function() { makeSpan(c, "Mindbroken.", "mindbroken"); } else { helpers.makeRatedStyledSpan(c, data.long.mental.devotion, slave.devotion, 100, true); - helpers.makeStyledSpan(c, helpers.getMultiNumericRating(data.long.mental.trust, [slave.trust + 100, slave.devotion + 100]), + helpers.makeStyledSpan(c, App.Ratings.multiNumeric(data.long.mental.trust, [slave.trust + 100, slave.devotion + 100]), slave.trust, true); } }, @@ -420,7 +420,7 @@ App.UI.SlaveSummaryRenderers = function() { * @returns {void} */ weight: function(slave, c) { - helpers.makeStyledSpan(c, helpers.getMultiNumericRating(data.long.body.weight, + helpers.makeStyledSpan(c, App.Ratings.multiNumeric(data.long.body.weight, [slave.weight + 100, helpers.FSData.policy.HedonisticDecadence.active, slave.hips + 2]), slave.weight, true); }, @@ -449,7 +449,7 @@ App.UI.SlaveSummaryRenderers = function() { genitalia: function(slave, c) { if (slave.dick > 0) { let dickDesc = slave.balls === 0 ? "Gelded." : ""; - const dickBallsDesc = helpers.getMultiNumericRating(data.long.body.genitalia.dickBalls, [slave.dick, slave.balls]); + const dickBallsDesc = App.Ratings.multiNumeric(data.long.body.genitalia.dickBalls, [slave.dick, slave.balls]); if (dickBallsDesc) { dickDesc += ` ${dickBallsDesc}`; } @@ -465,7 +465,7 @@ App.UI.SlaveSummaryRenderers = function() { if (slave.anus === 0) { helpers.makeSpan(c, "Anal virgin.", "lime"); } - const holesDesc = helpers.getMultiNumericRating(data.long.body.genitalia.holes, [slave.vagina, slave.anus]); + const holesDesc = App.Ratings.multiNumeric(data.long.body.genitalia.holes, [slave.vagina, slave.anus]); if (holesDesc) { helpers.makeSpan(c, holesDesc, "pink"); } @@ -632,7 +632,7 @@ App.UI.SlaveSummaryRenderers = function() { */ hormoneBalance: function(slave, c) { const colorClass = slave.hormoneBalance <= -21 ? "deepskyblue" : "pink"; - const desc = helpers.getNumericRating(data.long.hormoneBalance, slave.hormoneBalance + 500); + const desc = App.Ratings.numeric(data.long.hormoneBalance, slave.hormoneBalance + 500); helpers.makeSpan(c, desc + " hormone balance.", colorClass); }, -- GitLab