From 9ab51a359937884ec94a1b29f2a0f9bbe228f6f8 Mon Sep 17 00:00:00 2001 From: Donald Burr Date: Wed, 21 Oct 2015 02:00:40 -0700 Subject: [PATCH] Add abort condition to "date when you have X gems" calculator If a user puts in unattainable values (i.e. Tier 1 all the things and doesn't have any gems to start with, i.e. net loss) then this calculation could spin on into infinity, causing browser freak out. We now set an arbitrary limit of 5 years, if the desired amount of gems can't be obtained within that time, we bail. --- web_app/js/sif_tools.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/web_app/js/sif_tools.js b/web_app/js/sif_tools.js index fabbf7d..72d16c7 100644 --- a/web_app/js/sif_tools.js +++ b/web_app/js/sif_tools.js @@ -696,15 +696,20 @@ function calculate_gems() } var now = moment(new Date()); + // arbitrary limit to make sure we don't go wander off into infinity + var cutoff = moment(now).add(5, 'years'); var resultsString = sprintf("Today is %02d/%02d/%04d and you currently have %d love gems.
(Assuming you collected any gems you got today and already counted those.)", month(now), day(now), year(now), current_gems); var verboseText = ""; if (calc_daily_quest_gems) { verboseText = "(Including daily 'quest' gems in the calculation. There will not be a separate daily entry for each one.)

"; } + + // make sure we don't go off into infinity (i.e. user gives input that is impossible to calculate) + var abort = false; var gems = current_gems - while (gems < target_gems) { + while (gems < target_gems && !abort) { now = now.add(1, 'days') // is it a login bonus? @@ -788,10 +793,18 @@ function calculate_gems() verboseText += sprintf("That brings you to %d gems!

", gems); } } + + // do we abort? + if (now.isAfter(cutoff)) { + verboseText += sprintf("(Cannot proceed, this appears to be an unattainable amount of gems given your constraints.)"); + abort = true; + } } resultsString = resultsString + sprintf("
You will have %d love gems on %02d/%02d/%04d. Good things come to those who wait!", gems, month(now), day(now), year(now)); - + if (abort) { + resultsString += "
(Note: the calculation was stopped because it appears that you will be unable to attain the desired amount of gems given your constraints.)"; + } $("#gem-result-summary").html(resultsString); if (verbose) { $("#gem-result-verbose-area").html(verboseText);