<?php
    include("config.php");

    $_GET['player'] = substr($_GET['player'],0,30);

    /* Determine if a Player was entered. If not, redirect. */
    if ($_GET['player']=="") header('Location: http://'.$_SERVER['SERVER_NAME'].
        ($_SERVER['SERVER_PORT']!=80?':'.$_SERVER['SERVER_PORT']:'').$BASEURL.
        'players.php');
    
    $irpg_page_title = "Joueur: " . htmlentities($_GET['player']);
    $showmap = isset($_GET['showmap'])?$_GET['showmap']:false;
    $allmods = isset($_GET['allmods'])?$_GET['allmods']:false;
    
    include("header.php");
    include("commonfunctions.php");
    echo "<h1>Fiche de personnage</h1>";
    $file = fopen($irpg_db,"r");
    fgets($file,1024); // skip top comment
    $found=0;
    while ($line=fgets($file,1024)) {
        if (substr($line,0,strlen($_GET['player'])+1) == $_GET['player']."\t") {
            list($user,,$isadmin,$level,$class,$secs,,$uhost,$online,$idled,
                 $x,$y,
                 $pen['mesg'],
                 $pen['nick'],
                 $pen['part'],
                 $pen['kick'],
                 $pen['quit'],
                 $pen['quest'],
                 $pen['logout'],
                 $created,
                 $lastlogin,
                 $item['amulet'],
                 $item['charm'],
                 $item['helm'],
                 $item['boots'],
                 $item['gloves'],
                 $item['ring'],
                 $item['leggings'],
                 $item['shield'],
                 $item['tunic'],
                 $item['weapon'],
                 $alignment,
            ) = explode("\t",trim($line));
            $found=1;
            break;
        }
    }
    if (!$found) echo "<h1>Erreur</h1><p><b>Utilisateur inconnu.</b></p>\n";
    else {
        $class=htmlentities($class);
        /* if we htmlentities($user), then we cannot use links with it. */
        echo "      <p><b>Pseudo :</b> ".htmlentities($user)."<br />\n".
             "      <b>Profession :</b> $class<br />\n".
             "      <b>Admin ? :</b> ".($isadmin?"Oui":"Non")."<br />\n".
             "      <b>Niveau :</b> $level<br />\n".
             "      <b>Prochain niveau :</b> ".duration($secs)."<br />\n".
             "      <b>Statut :</b> ".($online?"C":"D&eacute;c")."onnect&eacute;<br />\n".
             "      <b>Host :</b> ".($uhost?$uhost:"Unknown")."<br />\n".
             "      <b>Date de cr&eacute;ation :</b> ".htmlentities(strftime($dateformat,$created))."<br />\n".
             "      <b>Derni&egrave;re connexion :</b> ".htmlentities(strftime($dateformat,$lastlogin))."<br />\n".
             "      <b>Temps d'inactivit&eacute; total:</b> ".duration($idled)."<br />\n".
             "      <b>Position :</b> [$x,$y]<br />\n".
             "      <b>Cot&eacute; :</b> ".($alignment=='e'?"Sadique":($alignment=='n'?"Nain-d&eacute;ci":"Brave"))."<br />\n".
             "      <b>XML :</b> [<a href=\"xml.php?player=".urlencode($user)."\">link</a>]</p>\n".
             "    <h2>Carte</h2>\n".
             "    ".($showmap?"<div id=\"map\"><img src=\"makemap.php?player=".urlencode($user)."\"></div>\n\n":"<p><a href=\"?player=".urlencode($user)."&showmap=1\">Voir la carte</a></p>\n\n")."".
             "    <h2>Equipement</h2>\n<p>";
        ksort($item);
        $sum = 0;
        foreach ($item_order as $key) {
        		$val = $item[$key];
            $uniquecolor="#be9256";
            if ($key == "helm" && substr($val,-1,1) == "a") {
                $val = intval($val)." [<font color=\"$uniquecolor\">".htmlentities(ucfirst(gettext("Mattt's Omniscience Grand Crown")))."</font>]";
            }
            if ($key == "tunic" && substr($val,-1,1) == "b") {
                $val = intval($val)." [<font color=\"$uniquecolor\">".htmlentities(ucfirst(gettext("Res0's Protectorate Plate Mail")))."</font>]";
            }
            if ($key == "amulet" && substr($val,-1,1) == "c") {
                $val = intval($val)." [<font color=\"$uniquecolor\">".htmlentities(ucfirst(gettext("Dwyn's Storm Magic Amulet")))."</font>]";
            }
            if ($key == "weapon" && substr($val,-1,1) == "d") {
                $val = intval($val)." [<font color=\"$uniquecolor\">".htmlentities(ucfirst(gettext("Jotun's Fury Colossal Sword")))."</font>]";
            }
            if ($key == "weapon" && substr($val,-1,1) == "e") {
                $val = intval($val)." [<font color=\"$uniquecolor\">".htmlentities(ucfirst(gettext("Drdink's Cane of Blind Rage")))."</font>]";
            }
            if ($key == "boots" && substr($val,-1,1) == "f") {
                $val = intval($val)." [<font color=\"$uniquecolor\">".htmlentities(ucfirst(gettext("Mrquick's Magical Boots of Swiftness")))."</font>]";
            }
            if ($key == "weapon" && substr($val,-1,1) == "g") {
                $val = intval($val)." [<font color=\"$uniquecolor\">".htmlentities(ucfirst(gettext("Jeff's Cluehammer of Doom")))."</font>]";
            }
            if ($key == "ring" && substr($val,-1,1) == "h") {
                $val = intval($val)." [<font color=\"$uniquecolor\">".htmlentities(ucfirst(gettext("Juliet's Glorious Ring of Sparkliness")))."</font>]";
            }
            echo "      ".htmlentities(ucfirst(gettext($key)))." : $val<br />\n";
            $sum += $val;
        }
        echo "      <br />\n      Somme : $sum<br />\n".
             "    </p>".
             "    <h2>P&eacute;nalit&eacute;s</h2>\n".
             "    <p>\n";

        ksort($pen);
        $sum = 0;
        foreach ($pen as $key => $val) {
            echo "      ".ucfirst($key)." : ".duration($val)."<br />\n";
            $sum += $val;
        }
        echo "      <br />\n      Total : ".duration($sum)."</p>\n";

        $file = fopen($irpg_mod,"r");
        $temp = array();
        while ($line=fgets($file,1024)) {
            if (strstr($line," ".$_GET['player']." ")          ||
                strstr($line," ".$_GET['player'].", ")         ||
                substr($line,0,strlen($_GET['player'])+1) ==
                       $_GET['player']." "                        ||
                substr($line,0,strlen($_GET['player'])+3) ==
                       $_GET['player']."'s ") {
                array_push($temp,$line);
            }
        }
        fclose($file);
        if (!is_null($temp) && count($temp)) {
            echo('<h2>');
            echo (!$allmods)?"Derni&egrave;res ":"";
            echo('Modifications d\'Horlorge</h2><p>');
            if ($allmods == 1 || count($temp) < 6) {
                foreach ($temp as $line) {
                    $line=htmlentities(trim($line));
                    echo "      $line<br />\n";
                }
                echo "      <br />\n";
            }
            else {
                end($temp);
                for ($i=0;$i<4;++$i) prev($temp);
                for ($line=trim(current($temp));$line;$line=trim(next($temp))) {
                    $line=htmlentities(trim($line));
                    echo "      $line<br />\n";
                }
            }
        }
        if ($allmods != 1 && count($temp) > 5) {
?>
      <br />
      [<a href="<?php echo $_SERVER['PHP_SELF']."?player=".urlencode($user);?>&amp;allmods=1">Voir toutes les modifications</a> (<?php echo count($temp)?>)]
      </p>
<?php
        }
    }
    include("footer.php");
?>

