我试图在php中为我的用户构建一个关卡系统。到目前为止,我只有一个mysql表...
id / xp (default 0) / xp_needed (default 5) / level (default 1)
我的用户面板中还有一个栏,显示需要多少xp,$ details是mysql表列的数组。
$xp_needed = ($details['xp'] / $details ['xp_needed']) * 100;
echo'<p>XP Needed:</p><div class="bar Tooltip" title="' . $details['xp'] . '/' .
$details['xp_needed'] . '"><span style="width: ' . $xp_needed . '%;"></span></div>';
我无法解决的是检查xp是否超过所需xp的函数,如果是这样,将级别更新为下一个整数,将xp更新为5 *级别并将剩余部分更改为余数(如果有的话) xp_needed到新的xp_needed?
感谢您的耐心
答案 0 :(得分:3)
编写一个函数xpIncrease($amount)
,并在增加用户xp的任何地方调用它。
它应该是这样的:
function xpIncrease($amount) {
if ($amount>=$details['xp_needed']) {
$amount-=$details['xp_needed'];
$details['xp_needed']=$newValue; //user leveled up, update xp_needed
xpIcrease($amount); //call it again because user may level up twice or more...
} else {
//just add it with SQL
}
}
答案 1 :(得分:1)
我这样做了..
<?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
{
}
?>
答案 2 :(得分:0)
每次experience
获得的时间超过experienceneeded
时,人员级别上升,然后设置experienceneeded * 1,2
:
<?php include 'Style/database.php'; ?>
<?php include 'Style/functions.php'; ?>
<?php include'logged_in.php'; ?>
<?php $randomnummer=rand(0,100); ?>
<?php $sql = "SELECT * FROM users WHERE id = '$users[id]' LIMIT 1";
if ($result = $mysqli->query($sql)) {$user = $result->fetch_array();} ?>
<?php if(isset($_POST['train']))
{ $msg="You are training!<br>" ;
$allowed = mysqli_query($mysqli," UPDATE users SET experience = experience+$randomnummer WHERE username = '$users[username]' ");
$level_up = mysqli_query($mysqli," UPDATE users SET experience = experience-experienceneeded ,level=level+1 WHERE username =
$users[username]' ");
if ($users['experience'] >= $users['experienceneeded'])
{
$res = mysqli_query($mysqli,"UPDATE users SET level = (level + 1) WHERE id = '$users[id]' LIMIT 1");
$res = mysqli_query($mysqli,"UPDATE users SET experienceneeded = experienceneeded * 1.2 WHERE id = '$users[id]'LIMIT 1");
$res = mysqli_query($mysqli,"UPDATE users SET credit = credit + 3 WHERE id = '$users[id]'LIMIT 1");
}
if ($users['experience'] >= $users['experienceneeded'])
{ echo '<div class="Leveled">'.
'You sucessfully leveled up to '
. $level_up . '!'.
' As a reward you were given 3 credit points!'
.'</div>';}}
?>
<div class="content">
<center>
<form method="post" action="user_train.php">
<?php echo "$msg" ; ?>
<button type="submit" name="train" id="train">train for <?php echo $users['username'] ; ?></button>
</form></center>
</div>
答案 3 :(得分:0)
这很简单:
while(currentExp >= expNeededNextLevel) {
currentExp -= expNeededNextLevel
levelUp();
}