lopar-patch-1 #5

Merged
lopar merged 2 commits from lopar-patch-1 into main 2024-06-21 11:24:07 +00:00
2 changed files with 95 additions and 82 deletions

View File

@ -69,7 +69,8 @@ function updateGameState(gameState) {
const winnerDiv = document.createElement('div'); const winnerDiv = document.createElement('div');
winnerDiv.innerHTML = `<p>Battle End! Winner: ${gameState.winner}</p>`; winnerDiv.innerHTML = `<p>Battle End! Winner: ${gameState.winner}</p>`;
document.body.appendChild(winnerDiv); document.body.appendChild(winnerDiv);
submitButton.disabled = true; submitButton.style.display = 'none';
targetSelector.style.display = 'none';
clearInterval(pollInterval); clearInterval(pollInterval);
} else { } else {
const aliveFighters = gameState.fighters.filter(fighter => fighter.hp > 0); const aliveFighters = gameState.fighters.filter(fighter => fighter.hp > 0);
@ -94,10 +95,14 @@ function updateGameState(gameState) {
targetSelector.style.display = 'none'; targetSelector.style.display = 'none';
} }
if (gameState.last_fighter === username) { const currentFighter = gameState.fighters.find(fighter => fighter.name === username);
if (gameState.last_fighter === username || currentFighter.hp <= 0) {
submitButton.style.display = 'none'; submitButton.style.display = 'none';
targetSelector.style.display = 'none';
} else { } else {
submitButton.style.display = 'block'; submitButton.style.display = 'block';
targetSelector.style.display = 'block';
} }
} }
} }

View File

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