<?php
namespace App\Entity;
use App\Repository\BattleRepository;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[ORM\Entity(repositoryClass: BattleRepository::class)]
#[Vich\Uploadable]
class Battle
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $battle_ends = null;
#[Vich\UploadableField(mapping: 'battles', fileNameProperty: 'battleFileName', size: 'battleSize')]
public ?File $battleFile = null;
#[ORM\Column(nullable: true)]
private ?string $battleFileName = null;
#[ORM\Column(nullable: true)]
private ?int $battleSize = null;
#[ORM\Column(nullable: true)]
private ?\DateTime $updatedAt = null;
#[ORM\ManyToOne(inversedBy: 'battles_challenger')]
private ?Rapper $challenger = null;
#[ORM\ManyToOne(inversedBy: 'battles_oponnent')]
private ?Rapper $oponnent = null;
#[ORM\Column(nullable: true)]
private ?\DateTime $createdAt = null;
#[ORM\ManyToOne(inversedBy: 'battles')]
private ?Beat $beat = null;
#[ORM\Column]
private ?bool $isConfirmed = null;
#[ORM\Column]
private ?bool $isCompleted = null;
#[ORM\Column]
private ?bool $isCanceled = null;
private ?bool $finished = null;
#[ORM\OneToMany(mappedBy: 'battle', targetEntity: Vote::class)]
private Collection $allVotes;
#[ORM\ManyToOne(inversedBy: 'wonBattles')]
private ?Rapper $winner = null;
#[ORM\OneToMany(mappedBy: 'battle', targetEntity: BattleComment::class)]
private Collection $comments;
#[ORM\ManyToMany(targetEntity: Client::class, mappedBy: 'favoriteBattles')]
private Collection $favoritedClients;
#[ORM\OneToOne(mappedBy: 'battle', cascade: ['persist', 'remove'])]
private ?BattleInvite $battleInvite = null;
public function __construct()
{
$this->allVotes = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->favoritedClients = new ArrayCollection();
}
public function __toString()
{
return (string)$this->getChallenger()->getPseudoname()." vs. ".$this->getOponnent()->getPseudoname();
}
public function getId(): ?int
{
return $this->id;
}
public function getBattleEnds(): ?\DateTimeInterface
{
return $this->battle_ends;
}
public function setBattleEnds(?\DateTimeInterface $battle_ends): static
{
$this->battle_ends = $battle_ends;
return $this;
}
public function setBattleFile(?File $battleFile = null): void
{
$this->battleFile = $battleFile;
if (null !== $battleFile) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTime();
}
}
public function getBattleFile(): ?File
{
return $this->battleFile;
}
public function setBattleFileName(?string $battleFileName): void
{
$this->battleFileName = $battleFileName;
}
public function getBattleFileName(): ?string
{
return $this->battleFileName;
}
public function setBattleSize(?int $battleSize): void
{
$this->battleSize = $battleSize;
}
public function getBattleSize(): ?int
{
return $this->battleSize;
}
public function getUpdatedAt(): ?\DateTime
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTime $updatedAt): static
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getChallenger(): ?Rapper
{
return $this->challenger;
}
public function setChallenger(?Rapper $challenger): static
{
$this->challenger = $challenger;
return $this;
}
public function getOponnent(): ?Rapper
{
return $this->oponnent;
}
public function getOpponent(): ?Rapper
{
return $this->oponnent;
}
public function setOponnent(?Rapper $oponnent): static
{
$this->oponnent = $oponnent;
return $this;
}
public function getCreatedAt(): ?\DateTime
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTime $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getBeat(): ?Beat
{
return $this->beat;
}
public function setBeat(?Beat $beat): static
{
$this->beat = $beat;
return $this;
}
public function isIsConfirmed(): ?bool
{
return $this->isConfirmed;
}
public function setIsConfirmed(bool $isConfirmed): static
{
$this->isConfirmed = $isConfirmed;
return $this;
}
public function isIsCompleted(): ?bool
{
return $this->isCompleted;
}
public function setIsCompleted(bool $isCompleted): static
{
$this->isCompleted = $isCompleted;
return $this;
}
public function isIsCanceled(): ?bool
{
return $this->isCanceled;
}
public function setIsCanceled(bool $isCanceled): static
{
$this->isCanceled = $isCanceled;
return $this;
}
/**
* @return Collection<int, Vote>
*/
public function getAllVotes(): Collection
{
return $this->allVotes;
}
public function addAllVote(Vote $allVote): static
{
if (!$this->allVotes->contains($allVote)) {
$this->allVotes->add($allVote);
$allVote->setBattle($this);
}
return $this;
}
public function removeAllVote(Vote $allVote): static
{
if ($this->allVotes->removeElement($allVote)) {
// set the owning side to null (unless already changed)
if ($allVote->getBattle() === $this) {
$allVote->setBattle(null);
}
}
return $this;
}
public function getWinner(): ?Rapper
{
return $this->winner;
}
public function setWinner(?Rapper $winner): static
{
$this->winner = $winner;
return $this;
}
/**
* @return Collection<int, BattleComment>
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(BattleComment $comment): static
{
if (!$this->comments->contains($comment)) {
$this->comments->add($comment);
$comment->setBattle($this);
}
return $this;
}
public function removeComment(BattleComment $comment): static
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getBattle() === $this) {
$comment->setBattle(null);
}
}
return $this;
}
/**
* @return Collection<int, Client>
*/
public function getFavoritedClients(): Collection
{
return $this->favoritedClients;
}
public function addFavoritedClient(Client $favoritedClient): static
{
if (!$this->favoritedClients->contains($favoritedClient)) {
$this->favoritedClients->add($favoritedClient);
$favoritedClient->addFavoriteBattle($this);
}
return $this;
}
public function removeFavoritedClient(Client $favoritedClient): static
{
if ($this->favoritedClients->removeElement($favoritedClient)) {
$favoritedClient->removeFavoriteBattle($this);
}
return $this;
}
public function getBattleInvite(): ?BattleInvite
{
return $this->battleInvite;
}
public function setBattleInvite(?BattleInvite $battleInvite): static
{
// unset the owning side of the relation if necessary
if ($battleInvite === null && $this->battleInvite !== null) {
$this->battleInvite->setBattle(null);
}
// set the owning side of the relation if necessary
if ($battleInvite !== null && $battleInvite->getBattle() !== $this) {
$battleInvite->setBattle($this);
}
$this->battleInvite = $battleInvite;
return $this;
}
public function isFinished(?bool $_isFinished = false):bool
{
$this->finished = false;
if($_isFinished){
return $this->finished = $_isFinished;
}
if($this->battle_ends < new DateTime("now")){
$this->finished = true;
}
return $this->finished;
}
}