<?php
function add_icon ($icon) {
  $path   = "./icons/$icon.png";
  $size   = getimagesize($path);
  $width  = $size[0];
  $height = $size[1];
  return "<img src=\"$path\" width=\"$width\" height=\"$height\" alt=\"$icon\" border=\"0\">";
}

function print_percent ($percent) {
  while (strlen($percent)<2) $percent="0".$percent;
  $return=($percent) ? substr($percent,0,strlen($percent)-1).",".substr($percent,strlen($percent)-1,1)."%" : "0&euro;";
  return ($return);
}

function print_bill ($bill) {
  while (strlen($bill)<3) $bill="0".$bill;
  $return=($bill) ? substr($bill,0,strlen($bill)-2).",".substr($bill,strlen($bill)-2,2)."&euro;" : "0&euro;";
  return ($return);
}

function money_to_bill($money) {
  $money=str_replace("€","",$money);
  $money=str_replace(",",".",$money);
  return ($money*100);
}

function user_id_to_alias($id) {
  global $db;
  $query=mysqli_query($db, "SELECT * FROM `player` WHERE `id` = $id LIMIT 0 , 30");
  $data=mysqli_fetch_array($query);
  if ($data === FALSE) return NULL;
  else if ($data === NULL) return NULL;
  else return $data['alias'];
}

function count_games () {
  global $db;
  $query=mysqli_query($db, "SELECT `id` FROM `games` WHERE `scoregame` = 'TRUE'");
  return (mysqli_num_rows($query));
}

function user_count_last_missed_games($id)
{
  global $db;
  $games = 0;
  $query = mysqli_query($db, "SELECT `id` FROM `games` WHERE `scoregame` = 'TRUE' ORDER BY `datetime` DESC");
  while ($data=mysqli_fetch_array($query)) {
    $player_array = array();
    $player_array = player_of_game($data['id']);
    $deposition=0;
    foreach ($player_array as $player)
      if ($player==$id) $deposition++;
    if (!$deposition) $games++;
    else break;
  }
  return ($games);
}

function user_count_games($id) { //counts the games wich are played by the user $id
  global $db;
  $games = 0;
  $query = mysqli_query($db, "SELECT `id` FROM `games`");
  while ($data=mysqli_fetch_array($query)) {
    $player_array = array();
    $player_array = player_of_game($data['id']);
    $deposition   = 0;
    foreach ($player_array as $player)
      if ($player==$id) $deposition++;
    if ($deposition) $games++;
  }
  return ($games);
}

function user_count_point_games($id) { //counts the games wich are played by the user $id
  global $db;
  $games = 0;
  $query = mysqli_query($db, "SELECT `id` FROM `games` WHERE `scoregame` = 'TRUE'");
  while ($data=mysqli_fetch_array($query)) {
    $player_array = array();
    $player_array = player_of_game($data['id']);
    $deposition   = 0;
    foreach ($player_array as $player)
      if ($player==$id) $deposition++;
    if ($deposition) $games++;
  }
  return ($games);
}

function user_count_place($id,$place) { //counts how often a user ($id) has made the place ($place)
  global $db;
  $games = 0;
  $query = mysqli_query($db, "SELECT `".$place."_id` FROM `games` WHERE `scoregame` = 'TRUE'");
  while ($data=mysqli_fetch_row($query)) {
    if ($data[0]==$id) $games++;
  }
  return ($games);
}

function user_min_bill() {
  global $db;
  $minbill=0;
  $query=mysqli_query($db, "SELECT `id` FROM `player`");
  while ($data=mysqli_fetch_row($query)){
    $helpvar=user_count_bill($data[0]);
    if ($helpvar<$minbill) $minbill=$helpvar;
  }
  return ($minbill);
}

function user_max_bill() {
  global $db;
  $maxbill=0;
  $query=mysqli_query($db, "SELECT `id` FROM `player`");
  while ($data=mysqli_fetch_row($query)){
    $helpvar=user_count_bill($data[0]);
    if ($helpvar>$maxbill) $maxbill=$helpvar;
  }
  return ($maxbill);
}

function user_count_bill($id) {
  global $db;
  $bill=0;
  $query=mysqli_query($db, "SELECT * FROM `games` WHERE `scoregame` = 'TRUE'");
  while ($data=mysqli_fetch_array($query)) {
    $playerlist=player_of_game($data['id']);
    foreach ($playerlist as $player) {
      if ($player==$id) $bill-=$data['base_bill'];
    }
    if ($data['first_id']==$id)  $bill+=$data['first_bill'];
    if ($data['second_id']==$id) $bill+=$data['second_bill'];
    if ($data['third_id']==$id)  $bill+=$data['third_bill'];
  }
  return ($bill);
}

function player_of_game($game_id) {
  global $db;
  $player_array=array();

  $query =mysqli_query($db, "SELECT `player` FROM `games` WHERE `id` = $game_id");
  $data  =mysqli_fetch_array($query);
  $player=$data['player'];

  while (strlen($player)>0) {
    while (substr($player,0,1)==",") $player=substr($player,1,strlen($player)-1); //delete leading commata
    $position=0;
    $deposition="";
    for ($position=0;substr($player,$position,1)!="," && $position<strlen($player);$position++);
    $deposition=trim(substr($player,0,$position));
    if ($deposition) $player_array[count($player_array)]=$deposition;
    $player=((strlen($player)-$position-1)>0) ? substr($player,$position,strlen($player)-$position):"";
  }
  return ($player_array);
}

function gibmirzeit_von_datetime($format,$datetime) {
  $yyyy=  substr($datetime,0,4);
  $yy  =  substr($datetime,2,2);
  $mm  =  substr($datetime,5,2);
  $dd  =  substr($datetime,8,2);
  $HH  =  substr($datetime,11,2);
  $MM  =  substr($datetime,14,2);
  $SS  =  substr($datetime,17,2);

  $format=str_replace("yyyy",$yyyy,$format);
  $format=str_replace("jjjj",$yyyy,$format);
  $format=str_replace("yy"  ,$yy  ,$format);
  $format=str_replace("jj"  ,$yy  ,$format);
  $format=str_replace("mm"  ,$mm  ,$format);
  $format=str_replace("dd"  ,$dd  ,$format);
  $format=str_replace("tt"  ,$dd  ,$format);
  $format=str_replace("HH"  ,$HH  ,$format);
  $format=str_replace("MM"  ,$MM  ,$format);
  $format=str_replace("SS"  ,$SS  ,$format);

  return ($format);
}
?>
