From 8427292d0781fa25108b9b0c798b596f91efdee7 Mon Sep 17 00:00:00 2001 From: Ivor Barhansky Date: Fri, 21 Jun 2024 11:20:05 +0000 Subject: [PATCH 1/2] Update src/battle.js --- src/battle.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/battle.js b/src/battle.js index 7730d98..9ecff1a 100644 --- a/src/battle.js +++ b/src/battle.js @@ -69,7 +69,8 @@ function updateGameState(gameState) { const winnerDiv = document.createElement('div'); winnerDiv.innerHTML = `

Battle End! Winner: ${gameState.winner}

`; document.body.appendChild(winnerDiv); - submitButton.disabled = true; + submitButton.style.display = 'none'; + targetSelector.style.display = 'none'; clearInterval(pollInterval); } else { const aliveFighters = gameState.fighters.filter(fighter => fighter.hp > 0); @@ -94,10 +95,14 @@ function updateGameState(gameState) { 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'; + targetSelector.style.display = 'none'; } else { submitButton.style.display = 'block'; + targetSelector.style.display = 'block'; } } } -- 2.45.2 From af6859ccd8bbb8dcee22450f7fc17bd7678e485c Mon Sep 17 00:00:00 2001 From: Ivor Barhansky Date: Fri, 21 Jun 2024 11:20:43 +0000 Subject: [PATCH 2/2] Update src/battle.php --- src/battle.php | 168 ++++++++++++++++++++++++++----------------------- 1 file changed, 88 insertions(+), 80 deletions(-) diff --git a/src/battle.php b/src/battle.php index 5943cb8..cfd59fc 100644 --- a/src/battle.php +++ b/src/battle.php @@ -1,80 +1,88 @@ - [], - 'moves' => [], - 'last_fighter' => null, - 'battle_end' => false - ])); -} - -$battleState = json_decode(file_get_contents($currentBattle), true); - -if ($_SERVER['REQUEST_METHOD'] === 'POST') { - $action = $_POST['action']; - $username = $_POST['username']; - - if ($action === 'join') { - $fighterExists = false; - foreach ($battleState['fighters'] as $fighter) { - if ($username === $fighter['name']) { - $fighterExists = true; - break; - } - } - if (!$fighterExists) { - $battleState['fighters'][] = [ - 'name' => $username, - 'hp' => 100, - 'attack' => 10, - ]; - } - - } elseif ($action === 'move' && !$battleState['battle_end']) { - if ($battleState['last_fighter'] !== $username) { - $move = $_POST['move']; - $target = $_POST['target']; - $battleState['moves'][] = ['fighter' => $username, 'move' => $move, 'target' => $target]; - $battleState['last_fighter'] = $username; - - $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) { - $battleState['fighters'][$targetIndex]['hp'] -= $battleState['fighters'][$attackerIndex]['attack']; - if ($battleState['fighters'][$targetIndex]['hp'] <= 0) { - $battleState['fighters'][$targetIndex]['hp'] = 0; - } - - $aliveFighters = array_filter($battleState['fighters'], function ($fighter){ - return $fighter['hp'] > 0; - }); - - if (count($aliveFighters) === 1) { - $battleState['battle_end'] = true; - $battleState['winner'] = reset($aliveFighters)['name']; - } - } - - } else { - echo json_encode(['error' => 'Not your turn!']); - exit; - } - } - - file_put_contents($currentBattle, json_encode($battleState)); - echo json_encode($battleState); -} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') { - echo json_encode($battleState); -} + [], + 'moves' => [], + 'last_fighter' => null, + 'battle_end' => false + ])); +} + +$battleState = json_decode(file_get_contents($currentBattle), true); + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $action = $_POST['action']; + $username = $_POST['username']; + + if ($action === 'join') { + $fighterExists = false; + foreach ($battleState['fighters'] as $fighter) { + if ($username === $fighter['name']) { + $fighterExists = true; + break; + } + } + if (!$fighterExists) { + $battleState['fighters'][] = [ + 'name' => $username, + 'hp' => 100, + 'attack' => 10, + ]; + } + + } 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) { + $move = $_POST['move']; + $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; + } + } + + if ($targetIndex !== -1) { + $battleState['fighters'][$targetIndex]['hp'] -= $battleState['fighters'][$attackerIndex]['attack']; + if ($battleState['fighters'][$targetIndex]['hp'] <= 0) { + $battleState['fighters'][$targetIndex]['hp'] = 0; + } + + $aliveFighters = array_filter($battleState['fighters'], function ($fighter){ + return $fighter['hp'] > 0; + }); + + if (count($aliveFighters) === 1) { + $battleState['battle_end'] = true; + $battleState['winner'] = reset($aliveFighters)['name']; + } + } + } else { + echo json_encode(['error' => 'Not your turn!']); + exit; + } + } else { + echo json_encode(['error' => 'You are dead!']); + exit(); + } + } + + file_put_contents($currentBattle, json_encode($battleState)); + echo json_encode($battleState); +} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') { + echo json_encode($battleState); +} -- 2.45.2