<?php
Aseco::registerEvent('onStartup', 'countdown_startup');
Aseco::registerEvent('onEverySecond', 'countdown_update');
Aseco::addChatCommand('countdown', 'provides chat commands to countdown');

global $countdown;

function countdown_startup($aseco, $command) {
    global $countdown;
    $countdown = new ServerCountdown($aseco);
}

function countdown_update($aseco, $command) {
    global $countdown;
    $countdown->updateTime();
}

function chat_countdown($aseco, $command) {
    global $countdown;
    $admin = $command['author'];
    $args = explode(' ', $command['params']);
    $countdown->chatcommand1($args, $admin);    
}


class ServerCountdown {

    private $connection;
    private $enabled = false;
    private $countdown = 0;
    private $serverName = "";
    private $aseco;
    
    function ServerCountdown($aseco) {
        $this->aseco = $aseco;        
        $this->connection = new countdown_connection($aseco);
    }

    function updateTime() {
        if (!$this->enabled)
            return;

        if ((time() - $this->countdown) < 0 && (time() - $this->countdown) % 300 == 0) {
            $time = ($this->countdown - time());
            $this->connection->setServerName($this->serverName . '$fff (' . date("i", $time) . 'min to go!)');
            $this->connection->chatSendServerMessage("Server name set to:" . $this->serverName . '$fff (' . date("i", $time) . 'min to go!)');
        }

        if (time() - $this->countdown > 0) {
            $this->connection->setServerName($this->serverName);
            $this->connection->chatSendServerMessage("Countdown ended!");
            $this->connection->chatSendServerMessage("Server name set to:" . $this->serverName);
            $this->enabled = false;
        }
    }

    function chatcommand1($command, $login) {        
        if ($this->aseco->isAnyAdmin($login)) {
            if ($command[0] == "off") {
                $this->enabled = false;
                   $this->connection->chatSendServerMessage("Countdown disabled!");
                if (!empty($this->serverName)) {
                    $this->connection->chatSendServerMessage("Server name set to:" . $this->serverName);
                    $this->connection->setServerName($this->serverName);
                }
            }
            if (is_numeric($command[0])) {
                $this->serverName = $this->connection->getServerName();
                $this->countdown = time() + ((int) ($command[0]) * 60);
                $this->connection->chatSendServerMessage("Countdown is now start!");
                $this->connection->setServerName($this->serverName . '$fff (' . $command[0] . 'min to go!)');                
                $this->connection->chatSendServerMessage($this->serverName . '$fff (' . $command[0] . 'min to go!)');
                $this->enabled = true;
                
            }
        }
    }

}

class countdown_connection {

    private $aseco;

    function __construct($aseco) {
        $this->aseco = $aseco;
    }

    public function __call($name, $e) {
        $count = count($e);
        switch ($count) {
            case 0:
                $this->aseco->client->query(ucfirst($name));
                break;
            case 1:
                $this->aseco->client->query(ucfirst($name), $e[0]);
                break;
            case 2:
                $this->aseco->client->query(ucfirst($name), $e[0], $e[1]);
                break;
            case 3:
                $this->aseco->client->query(ucfirst($name), $e[0], $e[1], $e[2]);
                break;
            case 4:
                $this->aseco->client->query(ucfirst($name), $e[0], $e[1], $e[2], $e[3]);
                break;
        }

        $result = $this->aseco->client->getResponse();
        if (is_array($result) && isset($result['faultString']))
            throw new Exception($result['faultString'], $result['faultCode']);

        return $result;
    }

}

?>