<?php

// Update player statistics (only when someone is logged in)
if (LOGIN!="" && TO_DO == "update_rank") {
  $query = mysqli_query($db, "SELECT * FROM `player`");
  $user_min_bill=-1*user_min_bill();
  while ($data=mysqli_fetch_array($query)) {
    $cnt_games       = user_count_games($data['id']);
    $cnt_point_games = user_count_point_games($data['id']);
    $cnt_first       = user_count_place($data['id'],"first");
    $cnt_second      = user_count_place($data['id'],"second");
    $cnt_third       = user_count_place($data['id'],"third");
    $points          = 3*$cnt_first + 2*$cnt_second + $cnt_third;
    $rate            = ($cnt_games) ? (1000 * $points)/ ($cnt_games):0;
    $bill            = user_count_bill($data['id']);
    $missed_games    = user_count_last_missed_games($data['id']);
    $rank            = calculate_rank($rate,$bill+$user_min_bill,$missed_games);
    mysqli_query($db, "UPDATE `player` SET `rank` = '$rank', `cnt_games` = '$cnt_games', `cnt_first` = '$cnt_first', `cnt_second` = '$cnt_second', `cnt_third` = '$cnt_third', `points` = '$points', `rate` = '$rate', `bill` = '$bill' WHERE `id` =".$data['id']." LIMIT 1 ;");
  }
}

//Determine Sort Order
  if (isset($_GET['sortby']))    $_SESSION['sortby']=$_GET['sortby'];
  if (isset($_GET['sortorder'])) $_SESSION['sortorder']=$_GET['sortorder'];
  $SORTBY     = (isset($_SESSION['sortby'])) ? $_SESSION['sortby']:"rank";
  $SORTORDER  = (isset($_SESSION['sortorder'])) ? $_SESSION['sortorder']:"desc";
  $SORTNOORD  = ($SORTORDER=="asc") ? "desc":"asc";
  $SORTSTRING = "ORDER BY `$SORTBY` ".strtoupper($SORTORDER);


//variablen definieren
    $value_ip_alias   = "";

if (TO_DO=="insert_player" && LOGIN!="") {
  $failure="";
  if (!($alias=$_POST['alias']))     $failure.="Spitznamen angeben!<br>";
  if (!$failure) {
    $query=mysqli_query($db, "SELECT `alias` FROM `player` WHERE `alias` = '$alias' LIMIT 0 , 30");
    $data=mysqli_fetch_array($query);
    if ($data!==NULL && $data!==False && $data['alias']==$alias) $failure.="Spitzname existiert bereits!<br>";
  }
  if (!$failure) {
    mysqli_query($db, "INSERT INTO `player` ( `alias`, `password`, `cnt_games`, `cnt_first`, `cnt_second`, `cnt_third`, `points`, `rate`, `bill`, `rank`) VALUES ( '$alias', '', '0', '0', '0', '0', '0', '0', '0', '0');");
  }
  else {
    $value_ip_alias   = $alias;
  }
  $ERROR.=$failure;
}

if (TO_DO=="save_edit_player" && LOGIN!="" && ($player=$_GET['player'])) {
  $failure="";
  if (!($alias=$_POST['alias']))     $failure.="Spitznamen angeben!<br>";
  if (!$failure) {
    $query=mysqli_query($db, "SELECT `id`,`alias` FROM `player` WHERE `alias` = '$alias' LIMIT 0 , 30");
    while ($data=mysqli_fetch_array($query))
      if ($data['id']!=$player) $failure.="Spitzname existiert bereits!<br>";
  }
  if (!$failure) {
    mysqli_query($db, "UPDATE `player` SET `alias` = '$alias' WHERE `id` = '$player' LIMIT 1 ;");
  }
  $ERROR.=$failure;
}

$BODY.='
       <table class="list">
         <tr>
           <th>#</th>
           <th>&nbsp;</th>
           <th><a href="index.php?page=rank&sortby=alias&sortorder='.$SORTNOORD.'">'.(($SORTBY=="alias") ? add_icon($SORTORDER):"").'Spitzname</a></th>
           <th><a href="index.php?page=rank&sortby=cnt_games&sortorder='.$SORTNOORD.'">'.(($SORTBY=="cnt_games") ? add_icon($SORTORDER):"").'Spiele</a></th>
           <th><a href="index.php?page=rank&sortby=cnt_first&sortorder='.$SORTNOORD.'">'.(($SORTBY=="cnt_first") ? add_icon($SORTORDER):"").'#1</a></th>
           <th><a href="index.php?page=rank&sortby=cnt_second&sortorder='.$SORTNOORD.'">'.(($SORTBY=="cnt_second") ? add_icon($SORTORDER):"").'#2</a></th>
           <th><a href="index.php?page=rank&sortby=cnt_third&sortorder='.$SORTNOORD.'">'.(($SORTBY=="cnt_third") ? add_icon($SORTORDER):"").'#3</a></th>
           <th><a href="index.php?page=rank&sortby=points&sortorder='.$SORTNOORD.'">'.(($SORTBY=="points") ? add_icon($SORTORDER):"").'Punkte</a></th>
           <th><a href="index.php?page=rank&sortby=rate&sortorder='.$SORTNOORD.'">'.(($SORTBY=="rate") ? add_icon($SORTORDER):"").'Quote</a></th>
           <th><a href="index.php?page=rank&sortby=bill&sortorder='.$SORTNOORD.'">'.(($SORTBY=="bill") ? add_icon($SORTORDER):"").'Umsatz</a></th>
           <th><a href="index.php?page=rank&sortby=rank&sortorder='.$SORTNOORD.'">'.(($SORTBY=="rank") ? add_icon($SORTORDER):"").'Rang</a></th>
         </tr>';

$query=mysqli_query($db, "SELECT * FROM `player` $SORTSTRING");
$position = 1;
while ($data=mysqli_fetch_array($query)) {
  $id      = $data['id'];
  $alias   = $data['alias'];
  if (TO_DO=="edit_player" && LOGIN!="" && $_GET['player']==$alias) {
    $BODY.="<form action=\"index.php?page=rank&to_do=save_edit_player&player=$id\" method=\"post\">";
    $BODY.="<tr><td></td><td></td>";
    $BODY.="<td><input type=\"text\" size=\"8\" name=\"alias\" value=\"$alias\"></td>\n";
    $BODY.="<td><input type=\"submit\" value=\"\" class=\"submit_save\"></td>\n";
    $BODY.="</form>";
  }
  else {
    $BODY.="<tr><td>$position</td><td>";
    if (LOGIN!="")
      $BODY.="<a href=\"index.php?page=rank&to_do=edit_player&player=$alias\" target=\"_top\">".add_icon("pencil")."</a>";
    $BODY.="</td><td>$alias</td>";
    $BODY.="<td>".$data['cnt_games']."</td>";
    $BODY.="<td>".$data['cnt_first']."</td>";
    $BODY.="<td>".$data['cnt_second']."</td>";
    $BODY.="<td>".$data['cnt_third']."</td>";
    $BODY.="<td>".$data['points']."</td>";
    $rate=$data['rate'];
    $BODY.="<td>".print_percent($data['rate'])."</td>";
    $BODY.="<td>".print_bill($data['bill'])."</td>";
    $BODY.="<td>".$data['rank']."</td>";
    $BODY.="</tr>\n";
  }

  ++$position;
}

if (LOGIN!="") {
  $BODY.='
          <form action="index.php?page=rank&to_do=insert_player" method="post">
            <tr>
              <td>'.add_icon("add").'</td>
              <td><input type="text" size="8" name="alias" value="'.$value_ip_alias.'"></td>
              <td><input type="submit" value="" class="submit_save"></td>
            </tr>
          </form>
         ';
}

$BODY.='       </table>';

$COMMENT.='
           <b>Punkte</b> F&uuml;r jeden Sieg gibt es drei Punkte, f&uuml;r jeden zweiten Platz zwei Punkte und f&uuml;r jeden dritten Platz einen Punkt.<br>
           <b>Quote:</b> Die Punktequote, wie oft man pro Spiel einen Punkt erreicht. 100% geben an, dass man jedes teilgenomme Spiel einen Punkt gemacht hat. (maximal 300%, wenn man alle Spiele gewonnen hat)<br>
           <b>Rang:</b> Er ist abh&auml;ngig von der Quote, dem Umsatz und der Teilnahmeh&auml;ufigkeit an Rangspielen. Maximal kann ein Rang von 10000 erreicht werden.
          ';
?>
