Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions attack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
require_once __DIR__ . '/vendor/autoload.php';
// require './index.php';
require './classes/player.class.php';
session_start();
if (isset($_SESSION['player1'])) {
$playerOne = $_SESSION['player1'];
$playerTwo = $_SESSION['player2'];
if (($playerOne->health < 0) || ($playerTwo->health < 0)) {
echo "end of game";
header('Location: ./resultat.php');
}
}
if (($_SERVER['REQUEST_METHOD'] == 'POST') && !isset($_POST['attaque']) && !isset($_POST['soin'])) {

$playerOne = new Player($_POST['player-name'], $_POST['player-attaque'], $_POST['player-mana'], $_POST['player-sante']);
$playerTwo = new Player($_POST['adversaire-name'], $_POST['adversaire-attaque'], $_POST['adversaire-mana'], $_POST['adversaire-sante']);
$_SESSION['player1'] = $playerOne;
$_SESSION['player2'] = $playerTwo;
}
// $_SESSION['fight'] = true;
if (($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['attaque'])) {
$playerOne = $_SESSION['player1'];
$playerTwo = $_SESSION['player2'];
// while ($playerOne->getLifeStatus() && $playerTwo->getLifeStatus())
$playerOne->attack($playerTwo);
$_SESSION['player2'] = $playerTwo;

if ($playerTwo->health < 0) {
echo "end of game";
header('Location: ./resultat.php');
}
if ($playerTwo->health < 30) {
$iscured = $playerTwo->cure();
if ($iscured === false) {
$playerTwo->attack();
}
} else {
$playerTwo->attack($playerOne);
$_SESSION['player1'] = $playerOne;
if ($playerOne->health < 0) {
echo "end of game";
header('Location: ./resultat.php');
}
}
}
if (($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['soin'])) {
$playerOne = $_SESSION['player1'];
$playerTwo = $_SESSION['player2'];
$isCured = $playerOne->cure();
if ($isCured === false) {
return;
} else {
$playerTwo->attack($playerOne);
$_SESSION['player1'] = $playerOne;

if ($playerOne->health < 0) {
echo "end of game";
header('Location: ./resultat.php');
}
}
}
?>

<div id="match" class="row gx-5">
<h2>Match</h2>
<div class="col-6 ">
<div class="position-relative float-end">
<img id="player" src="https://api.dicebear.com/6.x/lorelei/svg?flip=false&seed=test" alt="Avatar" class="avatar float-end">
<span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">

</span>
<ul>
<li>Name : <?= $playerOne->name ?></li>
<li>Attaque : <?= $playerOne->power ?></li>
<li>Mana : <?= $playerOne->mana ?></li>
<li>Vie : <?= $playerOne->health ?></li>
</ul>
</div>
</div>
<div class="col-6" id="adversaire">
<div class="position-relative float-start">
<img src="https://api.dicebear.com/6.x/lorelei/svg?flip=true&seed=test2" alt="Avatar" class="avatar">
<span class="position-absolute top-0 start-0 translate-middle badge rounded-pill bg-danger">

</span>
<ul>
<li>Name : <?= $playerTwo->name ?></li>
<li>Attaque : <?= $playerTwo->power ?></li>
<li>Mana : <?= $playerTwo->mana ?></li>
<li>Vie : <?= $playerTwo->health ?></li>
</ul>
</div>
</div>
<div id="combats">
<h2>Combat</h2>
<ul>


<?php if (isset($playerOne->comment)) { ?>
<li><i class="fa-solid fa-khanda p-1"><?= $playerOne->comment ?></i></li><?php } ?>
<?php if (isset($playerTwo->comment)) { ?>
<li><i class="fa-solid fa-khanda p-1"><?= $playerTwo->comment ?></i></li><?php } ?>
</li>

</ul>
<form id='actionForm' action="attack.php" method="post">
<div class="d-flex justify-content-center">
<input id="attaque" name="attaque" type="submit" value="Attaquer">
<input name="soin" type="submit" value="Se soigner">
</div>
<div class="d-flex justify-content-center">
<input id="restart" name="restart" type="submit" value="Stopper le combat">
</div>
</form>
</div>
<style>
.avatar {
vertical-align: middle;
width: 100px;
border-radius: 50%;
}
</style>
<!-- <script>
document.addEventListener("DOMContentLoaded", function() {


let submitAttaque = document.querySelector("#attaque");
let alreadyPlaySong = false;
if (submitAttaque) {
submitAttaque.addEventListener("click", function(event) {
if (alreadyPlaySong)
return true;
event.preventDefault();
let player = document.querySelector("#player")
player.classList.add("animate__animated");
player.classList.add("animate__rubberBand");
submitAttaque.classList.add("animate__animated");
submitAttaque.classList.add("animate__rubberBand");
setTimeout(function() {
submitAttaque.classList.remove("animate__rubberBand");
player.classList.remove("animate__rubberBand");
}, 1000);
let hadouken_song = document.getElementById("hadoudken-song");
hadouken_song.play();
alreadyPlaySong = true;
setTimeout(function() {
submitAttaque.click();
}, 1000);
})
}


});
</script> -->
59 changes: 59 additions & 0 deletions classes/player.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
class Player
{
public string $name;
public float $power;
public float $mana;
public float $health;
public string $comment;

public function __construct(string $name, int $power, int $mana, int $health)
{
$this->name = $name;
$this->power = $power;
$this->mana = $mana;
$this->health = $health;
}
public function attack(Player $player)
{
$damage = $this->power / 4;
$player->health -= $damage;
$this->comment = "$this->name attaque $player->name et lui inflige $damage de dégats \r \n";
// $damage = $player->power / 20;
// $this->health -= $damage;
// echo " \r \n $player->name réplique et inflige $damage de dégat à $this->name.";
}


public function cure()
{
if ($this->mana > 0) {

$this->health += (($this->mana / 20) + 10);
$this->mana /= 4;
$this->mana -= 1;
$this->comment = "$this->name s'est soigné";
return $this;
} else {
$this->comment = "$this->name n'a plus assez de mana pour se soigner";
return false;
}
}
}


class Fight
{
public object $player1;
public object $player2;

public function __construct(Player $player1, Player $player2 = null)
{
$this->player1 = $player1;
$this->player2 = $player2;
}

public function stopMatch()
{
}
}
Loading