<?php


class hracClass {

	public $oDb, $oDbS, $vekKat;

	function __construct($oDb, $oDbS) {
		$this->oDb = $oDb;
		$this->oDbS = $oDbS;
		$this->vekKat = Array(
				"U11" => 0,
				"U13" => 1,
				"U15" => 2,
				"U17" => 3,
				"U19" => 4,
				"DOS" => 5
		);

	}

	function getHrac($id) {
		$sql= "SELECT *
				FROM `users`
				WHERE id= ?";
		return $this->oDb->obtain($sql, array($id));
	}

	function getTreningy($id, $od ){
		$od*=5;

		$sql='SELECT trening.*
				FROM `ucast`
				LEFT JOIN `trening` on ucast.trening_id = trening.id
				WHERE ucast.hrac_id= ?
				ORDER BY `trening`.`datum`  ASC
				limit '.$od.' , 5';
		return $this->oDb->queryWithBids($sql, array($id));
	}
	
	function getTreningyCount($id ){
	
		$sql=' SELECT count(*) as result
				FROM `ucast`
				LEFT JOIN `trening` on ucast.trening_id = trening.id
				WHERE ucast.hrac_id= ?';
		$q= $this->oDb->queryWithBids($sql, array($id));
		$row= $q->fetch();
		return $row['result'];
	}

	function getKlubInfo($id){
		$sql='	SELECT *
				FROM `klub`
				WHERE id =?
				LIMIT 1 ';
		$q= $this->oDb->queryWithBids($sql, array($id));
		return $q->fetch();

	}


	function ZdrojeGraf($id) {
		/*
		 * kolko zapasov vyhral
		*/
		$sql='SELECT count(*) as result
		  FROM `zapasy_ucast`
		  LEFT JOIN `zapas` on zapas.id = zapasy_ucast.zapas_id
		  where zapas.vytaz=zapasy_ucast.team AND zapasy_ucast.hrac_id= ? ';
		$row = $this->oDb->obtain($sql, array($id));
		$vyhryPocet=$row['result'];

		/*
		 * kolko zapasov prehral
		*/

		$sql='SELECT count(*) as result
		  FROM `zapasy_ucast`
		  LEFT JOIN `zapas` on zapas.id = zapasy_ucast.zapas_id
		  where zapas.vytaz!=zapasy_ucast.team AND zapasy_ucast.hrac_id= ? ';
		$row = $this->oDb->obtain($sql, array($id));
		$prehryPocet=$row['result'];

		$zapasov= $vyhryPocet+$prehryPocet;
		
		return json_encode(array( array('Nazov' => 'Vyhry' , 'Pocet' => ($vyhryPocet*100)/$zapasov  ) ,array( 'Nazov' =>'Prehry' , 'Pocet' =>  ($prehryPocet*100)/$zapasov) ));
	}

	function upravRespektHracovy($id , $delta ){
		$query="UPDATE `users`
				SET `respekt`=respekt+?
				WHERE id = ?";

		if ($this->oDb->queryWithBids($query, array($delta, $id) )) return 1;
		else return -1;
	}

	function zmenaFotky($id, $fotka){
		$query="UPDATE `users`
				SET `foto`= ?
				WHERE id = ?";

		if ($this->oDb->queryWithBids($query, array($fotka, $id) )) return 1;
		else return -1;
	}

	function dajFoto($id) {
		$sql='SELECT foto
				FROM `users`
				where id= ?';
		$row = $this->oDb->obtain($sql, array($id));
		//if (!empty($row['foto'])) return $row['foto'];
		//else return -1;
		return $row['foto'];
	}


}

?>