我有一个 PHP 代码,可以用战斗结果更新 mysql 表。 当您按下战斗按钮时,您将执行以下操作:
if(isset($_POST['attack_creature'])){
if($monnewhealth > "0"){ //if Monster exists
if($charhealth > "0"){ //if Character is alive go to fight
$fightcreature = "UPDATE user_character SET current_action_points = current_action_points-$fight_action_points WHERE ID = $currentUser AND current_action_points>$fight_action_points";
$stmt = $con->prepare($fightcreature);
$stmt->execute();
if($totalinflicteddamagetocreature > "0") {
$monnewhealth = $monnewhealth - $totalinflicteddamagetocreature;
if($monnewhealth < "0") {
$monnewhealth = 0;
}
$updatenmonewhealth = "UPDATE user_character SET fight_creature_new_health = $monnewhealth WHERE ID = $currentUser";
$stmt = $con->prepare($updatenmonewhealth);
$stmt->execute();
}
if($monnewhealth <= "0"){
$lastFight = $now_time;
$updatecharlastfightkills = "UPDATE user_character SET character_last_fight = $now_time, character_kills = $charkills+1, character_gold = $chargold+$mongoldreward, character_current_xp = $charexp+$monxpreward, current_xp_reward = $monxpreward, current_gold_reward = $mongoldreward WHERE ID = $currentUser";
$stmt = $con->prepare($updatecharlastfightkills);
$stmt->execute();
$insertbattlelog1 = "INSERT INTO battle_log (ID, battle_log_date, battle_log_result, battle_log_enemy_name, battle_log_enemy_lvl, battle_log_gold, battle_log_xp, battle_log_event) VALUES ('$currentUser', '$now_time', '1', '$monname', '$monlvl', '$charlastgoldreward', '$charlastxpreward', 'You have Destroyed Level $monlvl $monname and earned $monxpreward XP and $mongoldreward')";
mysqli_query($con, $insertbattlelog1);
}
if($monnewhealth > "0"){ //if Monster still alive
if($totalinflicteddamagetocharacter > "0") {
$charhealth = $charhealth - $totalinflicteddamagetocharacter;
if($charhealth < "0") {
$charhealth = 0;
}
$updatecharnewhealth = "UPDATE user_character SET current_health = $charhealth WHERE ID = $currentUser";
$stmt = $con->prepare($updatecharnewhealth);
$stmt->execute();
}
if($charhealth <= "0"){
$updatecharlastfightdeaths = "UPDATE user_character SET character_last_fight = $now_time, character_deaths = $chardeaths+1 WHERE ID = $currentUser";
$stmt = $con->prepare($updatecharlastfightdeaths);
$stmt->execute();
$insertbattlelog2 = "INSERT INTO battle_log (ID, battle_log_date, battle_log_result, battle_log_enemy_name, battle_log_enemy_lvl, battle_log_event) VALUES ('$currentUser', '$now_time', '2', '$monname', '$monlvl', '$charlastgoldreward', '$charlastxpreward', 'You have been killed by Level $monlvl $monname')";
mysqli_query($con, $insertbattlelog2);
}
}
}
}
header('Location: hunt.php');
}
我不知道如何重复此过程,直到 monhealth 或 charhealth 达到 0 零。 另外我想记录达到 0 零所需的回合数并记录每轮的总伤害。
非常感谢您, 克里斯
最佳答案
您可以编写一个函数并根据条件递归调用它。
function doIt() {
if($condition > 0) {
doIt();
}
}
doIt();
关于php - 自动重复发布项目直至满足要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57573348/