<?php
   /*
    * phpUA
    * United Admins © 2004
    * http://www.unitedadmins.com/
    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * as published by the Free Software Foundation; either version 2
    * of the License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    *
    */
    
    // $Id: plugin_q3_mode_watch.inc,v 1.3 2004/02/26 10:15:18 mad-x Exp $
    // Created by Hendrik Leppkes (punisher@unitedadmins.com)
    
    pluginRegisterMode("Q3", "watch", $phpUA["LANGUAGE"]["Watch"], "plugin_q3_mode_watch", 1, 1, 0, "plugin_q3_mode_watch.inc");
    
    function plugin_q3_mode_watch($id, $ip, $port)
    {
        startBenchmark(__FILE__, __FUNCTION__);
        
        global $phpUA;
        require_once(ABSOLUTEPATH . "plugins/shared/plugin_q3_query.inc");
        $server_query = new plugin_q3_query($ip, $port);
        
        if ($info = $server_query->getinfo()) {
            $status = $server_query->getstatus();
            $phpUA["TEMPLATE"]->assign("SERVER_ID", $id);
            $phpUA["TEMPLATE"]->assign("SERVER_IP", $ip);
            $phpUA["TEMPLATE"]->assign("SERVER_PORT", $port);
            
            $phpUA["TEMPLATE"]->assign("SERVER_HOSTNAME", $server_query->stripColorCodes($info["hostname"]));
            $phpUA["TEMPLATE"]->assign("SERVER_MAPNAME", "<span id=\"server_mapname\">&nbsp</span>");
            $phpUA["TEMPLATE"]->assign("SERVER_NUMPLAYERS", "<span id=\"server_numplayers\">&nbsp</span>");
            $phpUA["TEMPLATE"]->assign("SERVER_MAXPLAYERS", "<span id=\"server_maxplayers\">&nbsp</span>");
            
            for($i=0; $i<$status["sv_maxclients"]; $i++) {
                $players_array[$i] = array("name"=>"<span id=\"player_" . $i . "_name\">&nbsp</span>",
                                           "frags"=>"<span id=\"player_" . $i . "_frags\">&nbsp</span>",
                                           "ping"=>"<span id=\"player_" . $i . "_ping\">&nbsp</span>");
            }
            $phpUA["TEMPLATE"]->assign("SERVER_PLAYERS", $players_array);
            
            $phpUA["TEMPLATE"]->display($phpUA["STYLE"] . "/plugin_q3/watch.tpl");
            $phpUA["TEMPLATE"]->display($phpUA["STYLE"] . "/body_footer.tpl");
            
            endBenchmark(__FILE__, __FUNCTION__);
            printBenchmark();
            
            $startTimeLimit = time();
            while (!connection_aborted()) {
                if (!$info = $server_query->getinfo()) displayErrorMessage($phpUA["LANGUAGE"]["ConnectionTimeout"] . "<br>(" . basename(__FILE__) . ":" . __LINE__ . ")");
                $players = $server_query->getplayers();
                echo "<script language=\"JavaScript\" type=\"text/javascript\">\n";
                plugin_q3_mode_watch_updateserver($info["mapname"], $info["clients"], $status["sv_maxclients"]);
                $players_sorted = array();
                for($i=0; $i<$info["clients"]; $i++) {
                    $players_sorted[] = array("name"=>$players[$i]["name"],
                                              "frags"=>$players[$i]["frags"],
                                              "ping"=>$players[$i]["ping"]);
                }
                foreach ($players_sorted as $key => $row) {
                    $frags[$key] = $row["frags"];
                    $ping[$key] = $row["ping"];
                    $name[$key] = $row["name"];
                }
                @array_multisort($frags, SORT_DESC, $ping, SORT_ASC, $name, SORT_ASC,  $players_sorted);
                for ($i=0; $i<count($players_sorted); $i++) {
                    plugin_q3_mode_watch_updateplayer($i, $players_sorted[$i]["name"], $players_sorted[$i]["frags"], $players_sorted[$i]["ping"]);
                }
                plugin_q3_mode_watch_clearplayer($info["clients"]);
                echo "</script>\n";
                if (time() >= ($phpUA["ENGINE"]["TIME"] + $phpUA["CONFIG"]["TIMELIMIT"] - $phpUA["CONFIG"]["TIMEOUT"])) {
                    echo "<script language=\"JavaScript\" type=\"text/javascript\">\n";
                    echo "if ( confirm('" . $phpUA["LANGUAGE"]["ERROR_TIMELIMITREACHED"] . "') ) {\n";
                    echo "  document.location.href = '" . INDEXFILE . "?mode=watch&server=" . $id . "';\n";
                    echo "} else {\n";
                    echo "  document.location.href = '" . INDEXFILE . "?mode=index';\n";
                    echo "}\n";
                    echo "</script>\n";
                    $phpUA["TEMPLATE"]->display($phpUA["STYLE"] . "/html_footer.tpl");
                    exit;
                }
                flush();
                sleep($phpUA["CONFIG"]["INTERVAL"]);
                if (!ini_get("safe_mode")) set_time_limit(ini_get("max_execution_time"));
            }
        }
        else {
            displayErrorMessage($phpUA["LANGUAGE"]["ConnectionTimeout"] . "<br>(" . basename(__FILE__) . ":" . __LINE__ . ")");
        }
    }
    
    function plugin_q3_mode_watch_updateplayer($id, $name, $frags, $ping )
    {
        // p(id, name, frags, deaths)
        echo "p('" . $id . "', '" . htmlentities($name, ENT_QUOTES) . "', '" . $frags . "', '" . $ping . "');\n";

    }
    
    function plugin_q3_mode_watch_clearplayer($count)
    {
        // c(count)
        echo "c('" . $count . "');\n";
    }
    
    function plugin_q3_mode_watch_updateserver($mapname, $numplayers, $maxplayers)
    {
        // s(mapname, numplayers, maxplayers)
        echo "s('" . $mapname . "', '" . $numplayers . "', '" . $maxplayers . "');\n";
    }
?>