commit 6c0a75d48aee4322c8d82da92a103108c201a009 Author: Ivor Barhansky Date: Thu Jun 20 12:17:27 2024 +0000 Upload files to "src" Signed-off-by: Ivor Barhansky diff --git a/src/battle.js b/src/battle.js new file mode 100644 index 0000000..6e6ce69 --- /dev/null +++ b/src/battle.js @@ -0,0 +1,105 @@ +document.getElementById('joinForm').addEventListener('submit', function (event) { + event.preventDefault(); + const username = document.getElementById('username').value; + joinGame(username); +}); + +document.getElementById('submitMove').addEventListener('click', function () { + const username = document.getElementById('username').value; + const move = document.getElementById('submitMove').value; + const target = document.getElementById('targetPlayer').value; + submitMove(username, move, target); +}); + +let pollInterval; + +function joinGame(username) { + fetch('battle.php', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + }, + body: `action=join&username=${encodeURIComponent(username)}` + }).then(response => response.json()) + .then(data => { + document.getElementById('joinForm').style.display = 'none'; + document.getElementById('gameContainer').style.display = 'block'; + updateGameState(data); + pollInterval = setInterval(pollGameState, 1000); + }); +} + +function submitMove(username, move, target) { + fetch('battle.php', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + }, + body: `action=move&username=${encodeURIComponent(username)}&move=${encodeURIComponent(move)}&target=${encodeURIComponent(target)}` + }).then(response => response.json()) + .then(data => { + if (data.error) { + alert(data.error); + } else { + updateGameState(data); + } + }); +} + +function updateGameState(gameState) { + const gameStateDiv = document.getElementById('gameState'); + gameStateDiv.innerHTML = ''; + gameStateDiv.innerHTML += `

Players: ${gameState.fighters.map(f => f.name).join(', ')}

`; + gameState.moves.forEach(move => { + gameStateDiv.innerHTML += `

${move.fighter}: ${move.move} -> ${move.target}

`; + }); + + const fightersDiv = document.getElementById('fighters'); + fightersDiv.innerHTML = ''; + gameState.fighters.forEach(fighter => { + fightersDiv.innerHTML += `

${fighter.name} - HP: ${fighter.hp}, ATT: ${fighter.attack}

`; + }); + + const username = document.getElementById('username').value; + const submitButton = document.getElementById('submitMove'); + const targetSelector = document.getElementById('targetPlayer'); + + if (gameState.battle_end) { + const winnerDiv = document.createElement('div'); + winnerDiv.innerHTML = `

Battle End! Winner: ${gameState.winner}

`; + document.body.appendChild(winnerDiv); + submitButton.disabled = true; + clearInterval(pollInterval); + } else { + const aliveFighters = gameState.fighters.filter(fighter => fighter.hp > 0); + targetSelector.innerHTML = ''; + + if (aliveFighters.length > 2) { + targetSelector.style.display = 'block'; + aliveFighters.forEach(fighter => { + if (fighter.name !== username) { + const option = document.createElement('option'); + option.value = fighter.name; + option.textContent = fighter.name; + targetSelector.appendChild(option); + } + }); + } else { + targetSelector.style.display = 'none'; + } + + if (gameState.last_fighter === username) { + submitButton.style.display = 'none'; + } else { + submitButton.style.display = 'block'; + } + } +} + +function pollGameState() { + fetch('battle.php') + .then(response => response.json()) + .then(data => { + updateGameState(data); + }); +} diff --git a/src/battle.php b/src/battle.php new file mode 100644 index 0000000..5943cb8 --- /dev/null +++ b/src/battle.php @@ -0,0 +1,80 @@ + [], + '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); +} diff --git a/src/battle_state.json b/src/battle_state.json new file mode 100644 index 0000000..99251a3 --- /dev/null +++ b/src/battle_state.json @@ -0,0 +1,6 @@ +{ + "fighters": [], + "moves": [], + "last_fighter": null, + "battle_end": false +} \ No newline at end of file diff --git a/src/index.php b/src/index.php new file mode 100644 index 0000000..838b13c --- /dev/null +++ b/src/index.php @@ -0,0 +1,22 @@ + + + + + Battle + + +

Battle

+
+ + +
+ + + + + \ No newline at end of file