<?php
    include("config.php");
    $irpg_page_title = "Les qu&ecirc;tes";
    include("header.php");
    echo "        <h1>Qu&ecirc;te en cours</h1>\n";
    include("commonfunctions.php");
    $file = fopen($irpg_qfile,"r");
    $type=0;
    while ($line=fgets($file,1024)) {
        $arg = explode(" ",trim($line));
        if ($arg[0] == "T") {
            unset($arg[0]);
            $text = implode(" ",$arg);
        }
        elseif ($arg[0] == "Y") {
            $type = $arg[1];
        }
        elseif ($arg[0] == "P") {
            $p1[0] = $arg[1];
            $p1[1] = $arg[2];
            $p2[0] = $arg[3];
            $p2[1] = $arg[4];
        }
        elseif ($arg[0] == "S") {
            if ($type == 1) $time = $arg[1];
            elseif ($type == 2) $stage = $arg[1];
        }
        elseif ($arg[0] == "P1") {
            $player[1]['name'] = $arg[1];
            if ($type == 2) {
                $player[1]['x'] = $arg[2];
                $player[1]['y'] = $arg[3];
            }
        }
        elseif ($arg[0] == "P2") {
            $player[2]['name'] = $arg[1];
            if ($type == 2) {
                $player[2]['x'] = $arg[2];
                $player[2]['y'] = $arg[3];
            }
        }
        elseif ($arg[0] == "P3") {
            $player[3]['name'] = $arg[1];
            if ($type == 2) {
                $player[3]['x'] = $arg[2];
                $player[3]['y'] = $arg[3];
            }
        }
        elseif ($arg[0] == "P4") {
            $player[4]['name'] = $arg[1];
            if ($type == 2) {
                $player[4]['x'] = $arg[2];
                $player[4]['y'] = $arg[3];
            }
        }
    }
    if (!$type) {
        echo "        <p>D&eacute;sol&eacute;, il n'y a aucune qu&ecirc;te actuellement.</p>\n";
    }
    else {
        echo "        <p><b>Qu&ecirc;te :</b> ".htmlentities(ucfirst($text)).".</p>\n";
        if ($type == 1) {
            echo "        <p><b>Temps restant :</b> ".duration($time-time()).
                 "</p>\n";
        }
        elseif ($type == 2) {
            if ($stage == 1) {
                echo "        <p><b>Destination actuelle :</b> [$p1[0],$p1[1]]</p>\n";
            }
            else {
                echo "        <p><b>Destination actuelle :</b> [$p2[0],$p2[1]]</p>\n";
            }
        }
        for ($i=1;$i<=4;$i++) {
        	echo "        <p><b>Participant $i :</b> <a href=\"playerview.php?player=".
             	 urlencode($player[$i]['name'])."\">".htmlentities($player[$i]['name']).
               "</a><br />\n";
        	if ($type == 2) {
             	echo "        <b>Position :</b> [".$player[$i]['x'].",".$player[$i]['y']."]</p>\n";
        	}
        	else echo    "<br />\n";
       	}
        if ($type == 2) {
        	echo "        <h2>Carte :</h2>\n".
          	   "        <p>[Les participant sont en bleu et leur destination en rouge]</p>\n".
            	 "        <div id=\"map\"><img src=\"makequestmap.php\" alt=\"Carte de la qu&ecirc;te Idle RPG\" usemap=\"#quest\" border=\"0\" /></div>\n".
            	 "        <map id=\"quest\" name=\"quest\">\n";
                  
        	for ($i=1;$i<=4;$i++) {
        		echo "            <area shape=\"circle\" coords=\"".$player[$i]['x'].",".$player[$i]['y'].",6\" alt=\"".htmlentities($player[$i]['name']).
            	   "\" href=\"playerview.php?player=".urlencode($player[$i]['name'])."\" title=\"".htmlentities($player[$i]['name'])."\" />\n";
        	}
          echo "        </map>\n";
        }
        else echo    "<br />\n";
    }
    echo "        <br />\n";
    include("footer.php");
?>
