<?php
/*
 *
 * @copyright (c) 2010 animegame.eu
 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public Licence
 *
 */
/* Funktionen fuer die Clanfights */

function getStatus($charid, $buffer_enabled = true) {
	// Prioritaet, erst Turnier, dann Schatzsuche, dann Kampf
	if ($GLOBALS['char_status'][$charid] != null && $buffer_enabled) {
		return $GLOBALS['char_status'][$charid];
	}

	$sql = 'SELECT count(*) as anzahl FROM turnier_kampf t where art != \'gross\' and (win = ' . $charid . ' or lose = ' . $charid . ') and dauer > now()';
	$row = mysql_fetch_assoc(mysql_query($sql));
	if ($row['anzahl'] > 0) {
		$GLOBALS['char_status'][$charid] = 'Turnier';
		return 'Turnier';
	}
	$sql = 'SELECT count(*) as anzahl from quests where charid = ' . $charid;
	$row = mysql_fetch_assoc(mysql_query($sql));
	if ($row['anzahl'] > 0) {
		$GLOBALS['char_status'][$charid] = 'Schatz Suche';
		return 'Schatz Suche';
	}
	$sql = 'SELECT count(*) as anzahl from kampf where db_satz = ' . $charid;
	$row = mysql_fetch_assoc(mysql_query($sql));
	if ($row['anzahl'] > 0) {
		$GLOBALS['char_status'][$charid] = 'Kampf';
		return 'Kampf';
	}
	
	$sql = 'SELECT count(*) as anzahl FROM kampf_list where charakter = ' . $charid;
	$row = mysql_fetch_assoc(mysql_query($sql));
	if ($row['anzahl'] > 0) {
		$GLOBALS['char_status'][$charid] = 'Kampf erstellt';
		return 'Kampf erstellt';
	}
	$sql = 'SELECT count(*) as anzahl FROM turnier1 where charakter = ' . $charid;
	$row = mysql_fetch_assoc(mysql_query($sql));
	if ($row['anzahl'] > 0) {
		$GLOBALS['char_status'][$charid] = 'Angemeldet';
		return 'Angemeldet';
	}
	$GLOBALS['char_status'][$charid] = 'Frei';
	return 'Frei';
}

function getPicture($char){
	// Sichergehen, dass Char geladen wurde
	if($char['bild'] == null || $char['bild'] == ''){
		return 'design/bilder/avatare/noavart.gif';
	}
	return $char['bild'];
}

function getChar2($charid) {
	// Fehlerkontrolle
	if(!is_numeric($charid)){
		return null;
	} else {
		$char = mysql_fetch_assoc(mysql_query('Select * from chars where id = '.$charid));
		if(!$char){return null;}
//		Speichere in $char['stauts'] den aktuellen Status des Chars (buffer-Strategie ist hierbei die selbe)
		$char['status'] = getStatus($char['id'],$buffer_enabled);
		$char['bild'] = getPicture($char);
	}
	return $char;
}

function starteClanFight($chara_1, $chara_2, $clanfight_id, $runde, $kampf){
	global $sieger, $verlierer;
	include 'kampf_clan.php';
}

function starteClanFight_survival($chara_1, $fighter_a, $chara_2, $fighter_b, $clanfight_id, $runde, $kampf){
	global $sieger, $verlierer, $fighter_team_a_array, $fighter_team_b_array;
	include 'kampf_clan1.php';
}

function clanFightExecute($id) {
	$pkt_a_satz= 0;
  $pkt_b_satz= 0;
  $pkt_runde_a= 0;
  $pkt_runde_b= 0;
  $rundenzaehler= 1;
  $kampfzaehler= 1;
  
  $clanfight_daten= mysql_fetch_assoc(mysql_query('SELECT fighter, gfighter, numberoffighter FROM clan_fight_list WHERE id='.$id.' LIMIT 1'));
  $fighter_a_array= explode(",",$clanfight_daten['fighter']);
  $fighter_b_array= explode(",",$clanfight_daten['gfighter']);
  
  for ($x=1;;$x++) {
    /*if ($rundenzaehler == 7) {
    	break;
    }*/
    
  	$fighter_a= mt_rand(0, count($fighter_a_array)-1);
  	$fighter_b= mt_rand(0, count($fighter_b_array)-1);
  	
  	$char_a= getChar2($fighter_a_array[$fighter_a]);
  	$char_b= getChar2($fighter_b_array[$fighter_b]);	  	
  	  	
  	starteClanFight($char_a, $char_b, $id, $rundenzaehler, $kampfzaehler);
  	
  	$kampfzaehler++;
    
    if ($fighter_a_array[$fighter_a] == $sieger) {
    	$pkt_a_satz++;
    } else {
    	$pkt_b_satz++;
    }
    
  	unset($fighter_a_array[$fighter_a]);
  	unset($fighter_b_array[$fighter_b]);
    sort($fighter_a_array);
    sort($fighter_b_array);
          
    #if ($pkt_a_satz == 3 OR $pkt_b_satz == 3) {
    if ($pkt_a_satz + $pkt_b_satz == $clanfight_daten['numberoffighter']) {
    	if ($pkt_a_satz > $pkt_b_satz) {
    		$pkt_runde_a++;
    	} elseif ($pkt_a_satz < $pkt_b_satz) {
    		$pkt_runde_b++;
    	}
    	
    	$fighter_a_array= explode(",",$clanfight_daten['fighter']);
      $fighter_b_array= explode(",",$clanfight_daten['gfighter']);
    	
    	$rundenzaehler++;
    	$kampfzaehler= 1;
    	$pkt_a_satz= 0;
    	$pkt_b_satz= 0;
    	if ($pkt_runde_a + $pkt_runde_b == 6) {
    		break;
    	}
    }
  }
}

function selectFighterOfTeams($fighter_team_a_array,$fighter_team_b_array,$sieger='') {
	global $fighter_a, $fighter_b, $sieger;
	if (empty($sieger)) {
	  $fighter_a= mt_rand(0, count($fighter_team_a_array)-1);
	  $fighter_b= mt_rand(0, count($fighter_team_b_array)-1);
	} else {
		for ($y=0;$y<count($fighter_team_a_array);$y++) {
			if ($fighter_team_a_array[$y][id] == $sieger) {
				$siegerteam= 'a';
			}
		}
		
		if ($siegerteam == 'a') {
			for ($y=0;$y<count($fighter_team_a_array);$y++) {
				if ($fighter_team_a_array[$y][id] == $sieger) {
					$fighter_a= $y;
					$fighter_b= mt_rand(0, count($fighter_team_b_array)-1);
				}
			}
		} else {
			for ($y=0;$y<count($fighter_team_b_array);$y++) {
				if ($fighter_team_b_array[$y][id] == $sieger) {
					$fighter_b= $y;
					$fighter_a= mt_rand(0, count($fighter_team_a_array)-1);
				}
			}
		}		
	}
}

function clanFightExecute_survival($id) {
	global $fighter_a, $fighter_b, $sieger, $fighter_team_a_array, $fighter_team_b_array;
	$pkt_a_satz= 0;
  $pkt_b_satz= 0;
  $kampfzaehler= 1;
  $rundenzaehler= 1;
  
  $clanfight_daten= mysql_fetch_assoc(mysql_query('SELECT fighter, gfighter, numberoffighter FROM clan_fight_list WHERE id='.$id.' LIMIT 1'));
  $fighter_a_array= explode(",",$clanfight_daten['fighter']);
  $fighter_b_array= explode(",",$clanfight_daten['gfighter']);
  
  for ($x=0;$x<count($fighter_a_array);$x++) {
  	$fighter_team_a_array[$x]= getChar2($fighter_a_array[$x]);
  }
  for ($x=0;$x<count($fighter_b_array);$x++) {
  	$fighter_team_b_array[$x]= getChar2($fighter_b_array[$x]);
  }  
  
  for ($x=1;;$x++) {

    selectFighterOfTeams($fighter_team_a_array,$fighter_team_b_array,$sieger);
  	starteClanFight_survival($fighter_team_a_array[$fighter_a], $fighter_a, $fighter_team_b_array[$fighter_b], $fighter_b, $id, $rundenzaehler, $kampfzaehler);
  	
  	$kampfzaehler++;
    
    if ($fighter_team_a_array[$fighter_a][id] == $sieger) {
    	/* Status des Fighters aus Teams B auf "besiegt" setzen */
    	unset($fighter_team_b_array[$fighter_b]);
    	$fighter_team_b_array= array_values($fighter_team_b_array); 
    	/* Die Statuswerte St�rke, Verteidigung und Geschwindigkeit um 10% verringern */
    	$fighter_team_a_array[$fighter_a][starke]= round($fighter_team_a_array[$fighter_a][starke]*0.9);
    	$fighter_team_a_array[$fighter_a][speed]= round($fighter_team_a_array[$fighter_a][speed]*0.9);
    	$fighter_team_a_array[$fighter_a][verteidigung]= round($fighter_team_a_array[$fighter_a][verteidigung]*0.9);
    	/* Punkt f�r Team A hinzuf�gen */
    	$pkt_a_satz++;
    } else {
    	/* Status des Fighters aus Teams A auf "besiegt" setzen */
    	unset($fighter_team_a_array[$fighter_a]);
    	$fighter_team_a_array= array_values($fighter_team_a_array);
    	/* Die Statuswerte St�rke, Verteidigung und Geschwindigkeit um 10% verringern */
    	$fighter_team_b_array[$fighter_b][starke]= round($fighter_team_b_array[$fighter_b][starke]*0.9);
    	$fighter_team_b_array[$fighter_b][speed]= round($fighter_team_b_array[$fighter_b][speed]*0.9);
    	$fighter_team_b_array[$fighter_b][verteidigung]= round($fighter_team_b_array[$fighter_b][verteidigung]*0.9);
    	/* Punkt f�r Team B hinzuf�gen */
    	$pkt_b_satz++;
    }
          
    if (count($fighter_team_a_array) == 0 OR count($fighter_team_b_array) == 0) {
    	break;
    }
  }
}
?>