lopar-patch-1 #5

Merged
lopar merged 2 commits from lopar-patch-1 into main 2024-06-21 11:24:07 +00:00
Showing only changes of commit af6859ccd8 - Show all commits

View File

@ -1,80 +1,88 @@
<?php <?php
$currentBattle = 'battle_state.json'; $currentBattle = 'battle_state.json';
if (!file_exists($currentBattle)) { if (!file_exists($currentBattle)) {
file_put_contents($currentBattle, json_encode([ file_put_contents($currentBattle, json_encode([
'fighters' => [], 'fighters' => [],
'moves' => [], 'moves' => [],
'last_fighter' => null, 'last_fighter' => null,
'battle_end' => false 'battle_end' => false
])); ]));
} }
$battleState = json_decode(file_get_contents($currentBattle), true); $battleState = json_decode(file_get_contents($currentBattle), true);
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action']; $action = $_POST['action'];
$username = $_POST['username']; $username = $_POST['username'];
if ($action === 'join') { if ($action === 'join') {
$fighterExists = false; $fighterExists = false;
foreach ($battleState['fighters'] as $fighter) { foreach ($battleState['fighters'] as $fighter) {
if ($username === $fighter['name']) { if ($username === $fighter['name']) {
$fighterExists = true; $fighterExists = true;
break; break;
} }
} }
if (!$fighterExists) { if (!$fighterExists) {
$battleState['fighters'][] = [ $battleState['fighters'][] = [
'name' => $username, 'name' => $username,
'hp' => 100, 'hp' => 100,
'attack' => 10, 'attack' => 10,
]; ];
} }
} 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;
if ($attackerIndex !== -1 && $battleState['fighters'][$attackerIndex]['hp'] > 0) {
foreach ($battleState['fighters'] as $index => $fighter) { if ($battleState['last_fighter'] !== $username) {
if ($fighter['name'] === $username) { $move = $_POST['move'];
$attackerIndex = $index; $target = $_POST['target'];
} $battleState['moves'][] = ['fighter' => $username, 'move' => $move, 'target' => $target];
if ($fighter['name'] === $target) { $battleState['last_fighter'] = $username;
$targetIndex = $index;
} $targetIndex = -1;
} foreach ($battleState['fighters'] as $index => $fighter) {
if ($fighter['name'] === $target) {
if ($attackerIndex !== -1 && $targetIndex !== -1) { $targetIndex = $index;
$battleState['fighters'][$targetIndex]['hp'] -= $battleState['fighters'][$attackerIndex]['attack']; break;
if ($battleState['fighters'][$targetIndex]['hp'] <= 0) { }
$battleState['fighters'][$targetIndex]['hp'] = 0; }
}
if ($targetIndex !== -1) {
$aliveFighters = array_filter($battleState['fighters'], function ($fighter){ $battleState['fighters'][$targetIndex]['hp'] -= $battleState['fighters'][$attackerIndex]['attack'];
return $fighter['hp'] > 0; if ($battleState['fighters'][$targetIndex]['hp'] <= 0) {
}); $battleState['fighters'][$targetIndex]['hp'] = 0;
}
if (count($aliveFighters) === 1) {
$battleState['battle_end'] = true; $aliveFighters = array_filter($battleState['fighters'], function ($fighter){
$battleState['winner'] = reset($aliveFighters)['name']; return $fighter['hp'] > 0;
} });
}
if (count($aliveFighters) === 1) {
} else { $battleState['battle_end'] = true;
echo json_encode(['error' => 'Not your turn!']); $battleState['winner'] = reset($aliveFighters)['name'];
exit; }
} }
} } else {
echo json_encode(['error' => 'Not your turn!']);
file_put_contents($currentBattle, json_encode($battleState)); exit;
echo json_encode($battleState); }
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') { } else {
echo json_encode($battleState); 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);
}