lopar-patch-1 #5

Merged
lopar merged 2 commits from lopar-patch-1 into main 2024-06-21 11:24:07 +00:00
Showing only changes of commit af6859ccd8 - Show all commits

View File

@ -33,25 +33,30 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
} }
} elseif ($action === 'move' && !$battleState['battle_end']) { } elseif ($action === 'move' && !$battleState['battle_end']) {
$attackerIndex = -1;
foreach ($battleState['fighters'] as $index => $fighter) {
if ($fighter['name'] === $username) {
$attackerIndex = $index;
break;
}
}
if ($attackerIndex !== -1 && $battleState['fighters'][$attackerIndex]['hp'] > 0) {
if ($battleState['last_fighter'] !== $username) { if ($battleState['last_fighter'] !== $username) {
$move = $_POST['move']; $move = $_POST['move'];
$target = $_POST['target']; $target = $_POST['target'];
$battleState['moves'][] = ['fighter' => $username, 'move' => $move, 'target' => $target]; $battleState['moves'][] = ['fighter' => $username, 'move' => $move, 'target' => $target];
$battleState['last_fighter'] = $username; $battleState['last_fighter'] = $username;
$attackerIndex = -1;
$targetIndex = -1; $targetIndex = -1;
foreach ($battleState['fighters'] as $index => $fighter) { foreach ($battleState['fighters'] as $index => $fighter) {
if ($fighter['name'] === $username) {
$attackerIndex = $index;
}
if ($fighter['name'] === $target) { if ($fighter['name'] === $target) {
$targetIndex = $index; $targetIndex = $index;
break;
} }
} }
if ($attackerIndex !== -1 && $targetIndex !== -1) { if ($targetIndex !== -1) {
$battleState['fighters'][$targetIndex]['hp'] -= $battleState['fighters'][$attackerIndex]['attack']; $battleState['fighters'][$targetIndex]['hp'] -= $battleState['fighters'][$attackerIndex]['attack'];
if ($battleState['fighters'][$targetIndex]['hp'] <= 0) { if ($battleState['fighters'][$targetIndex]['hp'] <= 0) {
$battleState['fighters'][$targetIndex]['hp'] = 0; $battleState['fighters'][$targetIndex]['hp'] = 0;
@ -66,11 +71,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$battleState['winner'] = reset($aliveFighters)['name']; $battleState['winner'] = reset($aliveFighters)['name'];
} }
} }
} else { } else {
echo json_encode(['error' => 'Not your turn!']); echo json_encode(['error' => 'Not your turn!']);
exit; exit;
} }
} else {
echo json_encode(['error' => 'You are dead!']);
exit();
}
} }
file_put_contents($currentBattle, json_encode($battleState)); file_put_contents($currentBattle, json_encode($battleState));