Upload files to "src"

Signed-off-by: Ivor Barhansky <lopar@noreply.lopar.us>
This commit is contained in:
Ivor Barhansky 2024-06-20 12:17:27 +00:00
commit 6c0a75d48a
4 changed files with 213 additions and 0 deletions

105
src/battle.js Normal file
View File

@ -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 += `<p>Players: ${gameState.fighters.map(f => f.name).join(', ')}</p>`;
gameState.moves.forEach(move => {
gameStateDiv.innerHTML += `<p>${move.fighter}: ${move.move} -> ${move.target}</p>`;
});
const fightersDiv = document.getElementById('fighters');
fightersDiv.innerHTML = '';
gameState.fighters.forEach(fighter => {
fightersDiv.innerHTML += `<p>${fighter.name} - HP: ${fighter.hp}, ATT: ${fighter.attack}</p>`;
});
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 = `<p>Battle End! Winner: ${gameState.winner}</p>`;
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);
});
}

80
src/battle.php Normal file
View File

@ -0,0 +1,80 @@
<?php
$currentBattle = 'battle_state.json';
if (!file_exists($currentBattle)) {
file_put_contents($currentBattle, json_encode([
'fighters' => [],
'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);
}

6
src/battle_state.json Normal file
View File

@ -0,0 +1,6 @@
{
"fighters": [],
"moves": [],
"last_fighter": null,
"battle_end": false
}

22
src/index.php Normal file
View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Battle</title>
</head>
<body>
<h1>Battle</h1>
<form id="joinForm">
<input type="text" id="username" placeholder="Enter your name" required>
<button type="submit">Join Game</button>
</form>
<div id="gameContainer" style="display: none;">
<div id="gameState"></div>
<div id="fighters"></div>
<select id="targetPlayer"></select>
<button value="basic_attack" id="submitMove">Basic Attack</button>
</div>
<script src="battle.js"></script>
</body>
</html>