Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you're jumping through some hoops you don't need to, but a couple of notes first. </p> <p>In the <code>movie_level</code> function you're checking if the level needs increased, it only makes sense to do that check when the associated variables have changed -- i.e. make the change when you update <code>movie_level</code> or <code>ratings_score</code>. Also you return <code>movie_level</code>, but that doesn't reflect the <code>+ 1</code> if you've added it, so you always return the old score (as you use <code>new_level</code> to store the new score). That might be what you intend, but I suspect it's not. </p> <p>The key question you're trying to answer is -- how far to the next level? For blocks of 25 the next level is <code>(movie_level * 25)</code> and the amount to the next level is <code>(movie_level * 25) - rating_score</code>. As a percentage it's <code>((movie_level * 25) - rating_score) / 25</code> (maybe <code>* 100</code> if you want). </p> <p>That works with fixed blocks, for exponential blocks you could explicitly store next target as a variable. But you can apply the same logic as above, assume that the function <code>level_target(x)</code> returns the target score for level <code>x</code>. Then you can work out how far to go with <code>level_target(movie_level) - rating_score</code>, but you need to know the size of that level which is <code>level_target(movie_level) - level_target(movie_level - 1)</code>. </p> <p>So to get the percentage do: <code>(level_target(movie_level) - rating_score)/(level_target(movie_level) - level_target(movie_level - 1))</code></p> <p>You can implement <code>level_target</code> as function, or work out what you want and incorporate it directly which would allow you to simplify the equation. </p> <p>Exponential level increases are going to get very big rapidly, so you might want a maximum on there. But if you want </p> <pre><code> 0, 25, 50, 100, 200, 400 ... </code></pre> <p>you can note that it's equivalent to </p> <pre><code> 25 * 0, 25 * 1, 25 * 2, 25 * 4, 25 * 8, 25 * 16 ... </code></pre> <p>and then note that the sequence 0, 1, 2, 4, 8, 16 ... are just increasing powers of two. So <code>level_target(x) = 25 * pow(2,x)</code>. </p> <p>The reason it might be more useful to store the next target level as a separate variable is that you can just double the old target level and avoid ever having to do the powers of 2 thing (and you can work out the difference it levels because it'll always be exactly half the level target).</p> <p><strong>EDIT</strong> </p> <p>To clarify my field change comment. You only need to recalculate movie_level after rating_score changes (or vice versa), so typically you would only recalculate the variables when one changes. </p> <p>I don't know your application, but assume that there's only one page where the values of movie_level and/or rating_score can change (an edit score page, say) but there are dozens of other pages that all show the progress bar. If you check <code>($movie_percentage_check == 100)</code> only on the page where the values change then you don't need to check anywhere else (because the values cannot change anywhere else so you can know it's not true). </p> <p>It's not necessarily bad to check that on all the view pages, particularly for a simple site. And there are cases where you'd need to (if some other part of the system can update the values somehow). In a more complex system you want to avoid doing work you don't need to so only 'edit' pages should change the values, 'view' pages should just read/use the values. </p> <p>I've maybe not explained that well, hopefully that helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload