1

假设我有这个简单的功能

<?php

function levels($theExp) {

          if ($theExp < 52) { $theLevel = 2;}
          else if ($theExp < 110) { $theLevel = 3;}
          else if ($theExp < 175) { $theLevel = 4;}
          else if ($theExp < 246) { $theLevel = 5;}
          else if ($theExp < 325) { $theLevel = 6;}
          else if ($theExp < 413) { $theLevel = 7;}
          else if ($theExp < 510) { $theLevel = 8;}
          else if ($theExp < 616) { $theLevel = 9;}
          else if ($theExp < 734) { $theLevel = 10;}
          else if ($theExp < 865) { $theLevel = 11;}
          else if ($theExp < 1008) { $theLevel = 12;}
          else if ($theExp < 1167) { $theLevel = 13;}
          else if ($theExp < 1342) { $theLevel = 14;}
          else if ($theExp < 1536) { $theLevel = 15;}
          else if ($theExp < 1749) { $theLevel = 16;}
          else if ($theExp < 1985) { $theLevel = 17;}
          else if ($theExp < 2245) { $theLevel = 18;}
          else if ($theExp < 2531) { $theLevel = 19;}
          else if ($theExp < 2848) { $theLevel = 20;}
          else if ($theExp < 3197) { $theLevel = 21;}
          else if ($theExp < 3582) { $theLevel = 22;}
          else if ($theExp < 4007) { $theLevel = 23;}
          else if ($theExp < 4476) { $theLevel = 24;}
          else if ($theExp < 4993) { $theLevel = 25;}
          else if ($theExp < 5564) { $theLevel = 26;}
          else if ($theExp < 6193) { $theLevel = 27;}
          else if ($theExp < 6888) { $theLevel = 28;}
          else if ($theExp < 7655) { $theLevel = 29;}
          else if ($theExp < 8502) { $theLevel = 30;}
          else if ($theExp < 9435) { $theLevel = 31;}
          else if ($theExp < 10466) { $theLevel = 32;}
          else if ($theExp < 11603) { $theLevel = 33;}
          else if ($theExp < 12858) { $theLevel = 34;}
          else if ($theExp < 14243) { $theLevel = 35;}
          else if ($theExp < 15772) { $theLevel = 36;}
          else if ($theExp < 17459) { $theLevel = 37;}
          else if ($theExp < 19321) { $theLevel = 38;}
          else if ($theExp < 21376) { $theLevel = 39;}
          else if ($theExp < 23644) { $theLevel = 40;}
          else if ($theExp < 26148) { $theLevel = 41;}
          else if ($theExp < 28912) { $theLevel = 42;}
          else if ($theExp < 31962) { $theLevel = 43;}
          else if ($theExp < 35329) { $theLevel = 44;}
          else if ($theExp < 39046) { $theLevel = 45;}
          else if ($theExp < 43149) { $theLevel = 46;}
          else if ($theExp < 47677) { $theLevel = 47;}
          else if ($theExp < 52677) { $theLevel = 48;}
          else if ($theExp < 58195) { $theLevel = 49;}
          else if ($theExp < 64288) { $theLevel = 50;}
          else if ($theExp < 71013) { $theLevel = 51;}
          else if ($theExp < 78437) { $theLevel = 52;}
          else if ($theExp < 86633) { $theLevel = 53;}
          else if ($theExp < 95681) { $theLevel = 54;}
          else if ($theExp < 105670) { $theLevel = 55;}
          else if ($theExp < 116697) { $theLevel = 56;}
          else if ($theExp < 128871) { $theLevel = 57;}
          else if ($theExp < 142311) { $theLevel = 58;}
          else if ($theExp < 157148) { $theLevel = 59;}
          else if ($theExp < 173529) { $theLevel = 60;}
          else if ($theExp < 191614) { $theLevel = 61;}
          else if ($theExp < 211579) { $theLevel = 62;}
          else if ($theExp < 233622) { $theLevel = 63;}
          else if ($theExp < 257958) { $theLevel = 64;}
          else if ($theExp < 284825) { $theLevel = 65;}
          else if ($theExp < 314487) { $theLevel = 66;}
          else if ($theExp < 347236) { $theLevel = 67;}
          else if ($theExp < 383392) { $theLevel = 68;}
          else if ($theExp < 423310) { $theLevel = 69;}
          else if ($theExp < 467382) { $theLevel = 70;}
          else if ($theExp < 516039) { $theLevel = 71;}
          else if ($theExp < 569760) { $theLevel = 72;}
          else if ($theExp < 629070) { $theLevel = 73;}
          else if ($theExp < 694553) { $theLevel = 74;}
          else if ($theExp < 766850) { $theLevel = 75;}
          else if ($theExp < 846671) { $theLevel = 76;}
          else if ($theExp < 934799) { $theLevel = 77;}
          else if ($theExp < 1032098) { $theLevel = 78;}
          else if ($theExp < 1139523) { $theLevel = 79;}
          else if ($theExp < 1258129) { $theLevel = 80;}
          else if ($theExp < 1389078) { $theLevel = 81;}
          else if ($theExp < 1533655) { $theLevel = 82;}
          else if ($theExp < 1693280) { $theLevel = 83;}
          else if ($theExp < 1869518) { $theLevel = 84;}
          else if ($theExp < 2064099) { $theLevel = 85;}
          else if ($theExp < 2278932) { $theLevel = 86;}
          else if ($theExp < 2516125) { $theLevel = 87;}
          else if ($theExp < 2778005) { $theLevel = 88;}
          else if ($theExp < 3067142) { $theLevel = 89;}
          else if ($theExp < 3386373) { $theLevel = 90;}
          else if ($theExp < 3738830) { $theLevel = 91;}
          else if ($theExp < 4127973) { $theLevel = 92;}
          else if ($theExp < 4557619) { $theLevel = 93;}
          else if ($theExp < 5031985) { $theLevel = 94;}
          else if ($theExp < 5555725) { $theLevel = 95;}
          else if ($theExp < 6133979) { $theLevel = 96;}
          else if ($theExp < 6772421) { $theLevel = 97;}
          else if ($theExp < 7477315) { $theLevel = 98;}
          elseif ($theExp < 8255580) { $theLevel = 99;}

          return $theLevel;

      }

      ?>

你输入你的经验,它会给你你的等级。这两个数字是从 MySQL 数据库中提取的......

所以,假设我们有$level$exp

$level鉴于我的 is1$expis ,我将如何找到剩下的经验以达到下一个级别0

4

4 回答 4

1

我相信你应该为两者创建一个数组explevel它会更容易操作,例如:

function levels($theExp, $next = null) {
    $levels = array(
            2 => 52,
            3 => 110,
            4 => 175
           );
    foreach($levels as $level => $exp){
        if($exp > $theExp){
            return ($exp-$theExp);
        }
    }
    return 'MAX LEVEL';
}
$thisLevel = levels(63); // 47
于 2012-09-17T08:18:09.580 回答
1

您应该使用循环而不是编写那么多条件,甚至是一个 switch 语句。无论如何,为了找到剩下的 exp,你必须计算即将到来的级别 exp 和当前 exp 之间的差异:

echo 'Remaining exp to level 3:' , 110 - $theExp;
于 2012-09-17T08:06:06.787 回答
1

制作一个包含经验限制的级别数组,即

$levels = array(
  1 => 0,
  2 => 52,
  3 => 110,
  ...
);

然后,当您知道当前的 exp 时,您可以遍历级别数组

function getNextLevel(&$levels,$experience) {
    $levelscount = count(array_keys($levels));
    $currentlevel = 0;
    for ($a = 1; $a <= $levelscount; $a++) {
        if ($levels[$a] > $experience) {
            $for_next_level = $levels[$a]-$experience;
            return array('level'=>$a-1, 'for_next_level'=>$for_next_level);
        } else if ($levels[$a] == $experience) {
            $for_next_level = $levels[$a+1]-$experience;
            return array('level'=>$a, 'for_next_level'=>$for_next_level);   
        }
    }
    return array('level'=>$levelscount, 'for_next_level'=>null); // if maxed the experience
}


getNextLevel($levels,50); // Array ( [level] => 1 [for_next_level] => 2 )
getNextLevel($levels,52); // Array ( [level] => 2 [for_next_level] => 58 )
于 2012-09-17T08:19:42.180 回答
1

另一种方法是编写一个数学函数;水平作为经验的函数。如果你绘制你的水平近似值,你会得到一个看起来像这样的图表:

在此处输入图像描述

这可以用这个表达式非常准确地近似(相关误差为 0.99968917):

在此处输入图像描述

function levels($exp) {
    if( $exp < 52 ) return( 2 );
    if( $exp > 8255580 ) return( 99 );

    $exp2 = pow($exp, 2);
    $exp3 = pow($exp, 3);
    $exp4 = pow($exp, 4);

    $part_a = 
        (-7083090.21554248 * $exp) -
        (2660.03667387467 * $exp2) -
        (0.00893506217859665 * $exp3);

    $part_b =
        (9.21358754706152e-13*$exp4) -
        246214420.822165 -
        (511314.94011467 * $exp) -
        (52.7886099211375 * $exp2) -
        (9.50051322037161e-5*$exp3);

    return( intval($part_a / $part_b) );
}

这显然是对等式的粗略估计,但它在没有意大利面条代码的情况下工作得很好。它在 100% 公差范围内通过了您的二进制剪辑测试,只有大约 40/96 次,但是当它关​​闭时,它会偏离头发长度。请参阅此图以进行拟合比较:http: //i.imgur.com/DHfJE.png

编辑:回答你原来的问题,“经验离开到一个新的水平”现在由数学计算。由于这种方法是基于数学的,方​​程是可逆的,所以当你求解经验时,你可以写一个反函数(注意这也是一个粗略的估计,并且在下限中出错,所以你需要为 exp 改进它-水平 < 2500):

function experience( $level ) {
    if( $level < 20 ) {
        return( intval(
            52.4648221531097*$level + 
            0.441063182292944*pow($level,3) + 
            0.00123813768395424*pow($level,5) - 
            57.3134003957015 - 
            0.0279703679732544*pow($level,4) - 
            1.81609930709489e-5*pow($level,6)
        ) );
    }

    return( intval(
        2520.69249517965 + 
        (5.46368150243298e-6*pow($level,6)) + 
        (1.30082368317228e-13*pow($level,10)) - 
        (9.417224294e-10*pow($level,8))
    ));
}

因此,要通过这种方法计算下一级所需的经验:$needed = experience($current_level + 1) - experience($current_level);

于 2012-09-18T04:36:53.847 回答