duanmeng7865 2013-10-14 06:58
浏览 55
已采纳

简单的PHP Level-up进度计算

I am creating a Level up System for a wordpress blog. I believe that this issue is more towards Php and maths rather than wordpress which is why im asking the question here.

I want users to gain 'levels' when the ratings (or you can call it xp) custom field gets to a multiple of 25 ( Although I would prefer to have the xp requirement increase exponentially, please look at request #2 below).

I have this in my functions.php file.

function movie_level() {
   if ( is_single() ) {
      global $post;
      $movie_level = get_post_meta( $post->ID, "movie_level", true);
      if ( !isset( $movie_level ) OR empty( $movie_level ) OR !is_numeric( $movie_level ) ) {
     $movie_level = 1;
     add_post_meta( $post->ID, "movie_level", $movie_level );
      }
  $vmeta = get_post_meta( get_the_ID(), 'ratings_score', true );
  $movie_percentage_check = $vmeta / ( 25*$movie_level ) * 100;
  if ( $movie_percentage_check == 100 ) {
         $new_level = $movie_level + 1;
         update_post_meta( $post->ID, "movie_level", $new_level );
         $update_rating = $vmeta + 1;
         update_post_meta( $post->ID, "ratings_score", $update_rating );
      } return $movie_level;
    }
 }

What it does is add +1 to movie_movie level when ratings reach 25.

In my single.php file i have the following:

 $meta = get_post_meta( get_the_ID(), 'ratings_score', true );
 $level= movie_level();
 $perc = $meta / ( 25 * $level ) * 100;

 echo '<div class="progress large-6 small-12 columns "><span class="meter" style="width:'.$perc.'%"><span></span></span></div>';

 echo movie_level();

This displays the progress to the next level using a progress bar css component.

Requests

  1. How can I make $perc start at 0% again when an new level is reached? With my current equation the progress bar will always show progress starting from level 1 - level 3(for example), rather than level 2 - level 3.

  2. How can I update my code so that the ratings requirement increases exponentially for each level and still achieve what I wanted in request #1

  • 写回答

1条回答 默认 最新

  • douxiongye5779 2013-10-14 08:03
    关注

    I think you're jumping through some hoops you don't need to, but a couple of notes first.

    In the movie_level 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 movie_level or ratings_score. Also you return movie_level, but that doesn't reflect the + 1 if you've added it, so you always return the old score (as you use new_level to store the new score). That might be what you intend, but I suspect it's not.

    The key question you're trying to answer is -- how far to the next level? For blocks of 25 the next level is (movie_level * 25) and the amount to the next level is (movie_level * 25) - rating_score. As a percentage it's ((movie_level * 25) - rating_score) / 25 (maybe * 100 if you want).

    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 level_target(x) returns the target score for level x. Then you can work out how far to go with level_target(movie_level) - rating_score, but you need to know the size of that level which is level_target(movie_level) - level_target(movie_level - 1).

    So to get the percentage do: (level_target(movie_level) - rating_score)/(level_target(movie_level) - level_target(movie_level - 1))

    You can implement level_target as function, or work out what you want and incorporate it directly which would allow you to simplify the equation.

    Exponential level increases are going to get very big rapidly, so you might want a maximum on there. But if you want

     0, 25, 50, 100, 200, 400 ...
    

    you can note that it's equivalent to

     25 * 0, 25 * 1, 25 * 2, 25 * 4, 25 * 8, 25 * 16 ...
    

    and then note that the sequence 0, 1, 2, 4, 8, 16 ... are just increasing powers of two. So level_target(x) = 25 * pow(2,x).

    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).

    EDIT

    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.

    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 ($movie_percentage_check == 100) 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).

    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.

    I've maybe not explained that well, hopefully that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来