Update src/battle.php
This commit is contained in:
parent
8427292d07
commit
af6859ccd8
@ -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));
|
||||||
|
Loading…
Reference in New Issue
Block a user