<?php

namespace Robert\Sumo\Arena;
use pocketmine\Server;
class Arena
{

    private $name, $spawn1, $spawn2;
    private $players = [
        // player name => player name
    ];

    private $hasStarted = false;

    public function __construct($name, $spawn1, $spawn2)
    {
        $this->name = $name;
        $this->spawn1 = $spawn1;
        $this->spawn2 = $spawn2;
    }
    public function getName(): string
    {
        return $this->name;
    }
    public function getSpawn1()
    {
        return $this->spawn1;
    }
    public function getSpawn2()
    {
        return $this->spawn2;
    }
    public function getPlayerCount()
    {
        return count($this->players);
    }
    public function registerPlayer($name)
    {
        $this->players[$name] = $name;
    }
    public function unregisterPlayer($name)
    {
       unset($this->players[$name]);
       $this->broadcastMessage($name. "§cleft the game.");
    }
    public function getPlayers(): array
    {
        return $this->players;
    }
    public function setStarted(bool $value) {
        $this->hasStarted = $value;
    }
    public function hasStarted() : bool {
        return $this->hasStarted;
    }

    public function broadcastMessage(string $msg) {
        foreach ($this->getPlayers() as $name) {
            $p = Server::getInstance()->getPlayerExact($name);
            if ($p == null) {
                $this->unregisterPlayer($name);
                return;
            }
            
            $p->sendMessage($msg);
        }
    }
    public function broadcastTip(string $tip) {
        foreach ($this->getPlayers() as $name) {
            $p = Server::getInstance()->getPlayerExact($name);
            if ($p == null) {
                $this->unregisterPlayer($name);
                return;
            }
            
            $p->sendTip($tip);
        }
    }
}
