简体   繁体   English

高效的找平系统

[英]Efficient Leveling System

I'm making a leveling system based on experience you have on the site. 我正在根据您在该站点上的经验制作一个水准测量系统。 I already have all the experience stuff figured out, and how I want to do the leveling, but I need a more efficient way to do it. 我已经弄清了所有的经验,以及如何进行调平,但是我需要一种更有效的方法。 I know this would probably would be achieved using an array, but I don't really know how to go about doing that. 我知道使用数组可能会实现,但是我真的不知道该怎么做。 Enough blabbering, though, this is what I'm trying to do... 不过,这很b昧,这就是我想要做的...

Level 1 will be anything under 150 experience Then I'm going to multiply that by 1.5, so Level 2 will be anything under 225 Level 3 will be anything under 337.5 and so on. 1级将是150经验以下的任何东西,然后我将其乘以1.5,因此2级将是225以下的任何东西。3级将是337.5以下的任何东西,依此类推。 This is the inefficient way that I was going to do. 这是我要做的低效方式。

if($xp < 150){
$level = "1";
}elseif($xp < 225){
$level = "2";
}elseif($xp < 337.5){
$level = "3";
}

I could use a variable for the number and multiple by 1.5 ( $number*1.5 ), but like I said before I don't really know how that'd work. 我可以为数字使用一个变量,然后乘以1.5( $number*1.5 ),但是就像我之前所说的,我真的不知道该怎么做。

*Some more info.. I have a session file included on every page and I have queries that would check every time there is new experience earned and this code would be used to update the level on the database automatically. *更多信息..我在每个页面上都包含一个会话文件,并且每次查询都会获得新的经验时都会进行查询,并且此代码将用于自动更新数据库级别。

Try 尝试

$level = min(max((int)(log($xp / 100, 1.5) + 1), 1), $maxLevel);

That takes the logarithm base 1.5 of xp/100 (ie the number of times you'd have to multiply 100 by to get $xp), then adds one (since log($x, 1.5) is less than one if $x is less than 1.5). 这需要xp / 100的对数以1.5 (即,您必须乘以100以获得$ xp的次数),然后加一(因为log($ x,1.5)如果$ x为,则小于1小于1.5)。 The min(max(..., minLevel), maxLevel) construct lets you clamp the level to lie between 1 and $maxLevel , also avoiding any issues with negative levels (if $xp is sufficiently less than 150). min(max(..., minLevel), maxLevel)构造使您可以将级别限制在1到$maxLevel之间,还可以避免出现任何负级别的问题(如果$xp足够小于150)。

Here's how I did my level system. 这是我完成关卡系统的方式。 It's a little more advanced featuring skill points.. 它具有一些高级技能点。

    <?php
    $level_up = ($level + 1);
if ($exp >= $max_exp)
    {
    $sql = "UPDATE users SET level=(level + 1) , max_exp=(exp * 1.05) , skill_points=(skill_points + 3) WHERE id='".$id."' LIMIT 1";
    $res = mysql_query($sql);
if ($exp >= $max_exp)
        echo '<div class="Leveled">' . 'You sucessfully leveled up to ' . $level_up . '!' . ' As a reward you were given 3 skill points!' . '</div>';
    }
    else
    {
    } 
    ?>

I'd agree. 我同意 either objects or arrays would be best. 对象或数组都是最好的。

Something like: 就像是:

$array = array(
    array(
        'xp_required' => 150,
        'level' => 1
    ),
    array(
        'xp_required' => 225,
        'level' => 2
    ),
    array(
        'xp_required' => 337.5,
        'level' => 3
    )
);
$current_xp = 155;
foreach( $array as $reqs ){
    if( $current_xp > $reqs['xp_required'] ){
        $level = $reqs['level'];
    }
}
echo $level';

well at the moment once you grab the users exp you can have that code block as a function to constantly check for variations in exp levels, but use a ranged statement 现在,当您抓住用户exp时,您可以将该代码块作为函数来不断检查exp级别的变化,但可以使用范围声明

 if($xp < 150){
     $level = "1";
  }elseif($xp > 150 && $xp < 225 ){
    $level = "2";
  }elseif($xp > 225 && $xp < 337.5){
    $level = "3";
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM