0, 'money' => 0);
			$charids[$fight['char1']] = 1;
		}
		$charArray[$fight['char1']]['exp'] += $fight['exp1'];
		$charArray[$fight['char1']]['money'] += $fight['geld1'];
		if(!isset($charArray[$fight['char2']])) {
			$charArray[$fight['char2']] = array('exp' => 0, 'money' => 0);
			$charids[$fight['char2']] = 1;
		}
		$charArray[$fight['char2']]['exp'] += $fight['exp2'];
		$charArray[$fight['char2']]['money'] += $fight['geld2'];
	}
	$returnArray = array();
	// Now calculate the money and exp the char gets just for proceeding in the tournament (in this case ... order the chars right away ;))
	$qry = mysql_query('Select count(win) as anzahl, win from turnier_kampf where art = \''.$name.'\' group by win order by count(win) desc');
	$maxRank = -1;
	while ($participant = mysql_fetch_assoc($qry)) {
		$charids[$participant['win']] = 0; // okay we already proceeded this one :)
		// okay, first add experience the the $charArray !!
		$charArray[$participant['win']]['exp'] += $additionalExpPerRound * $participant['anzahl'];
		$charArray[$participant['win']]['money'] += $additionalMoneyPerRound * $participant['anzahl'];
		if($maxRank == -1) {
			$maxRank = $participant['anzahl'];
		}
		$returnArray[$maxRank - $participant['anzahl']][] = $participant['win'];
		// add all the experience collected in this tournament at once ;)
		addExpToChar($participant['win'], $charArray[$participant['win']]['exp']);
		// the char is now already buffered, so no problem with that ;)
		$char = getChar($participant['win']);
		$sql = 'UPDATE user set geld = geld + ' . $charArray[$participant['win']]['money'] . ' WHERE id = ' . $char['besitzer'];
		echo $sql . ' 
';
		mysql_query($sql);
	}
	foreach ($charids as $key => $value) {
		if($value === 1) { // we did not have processed this id when processing the winners -> it is a ultimate loser!
			$returnArray[$maxRank][] = $key;
			addExpToChar($key, $charArray[$key]['exp']);
			// the char is now already buffered, so no problem with that ;)
			$char = getChar($key);
			$sql = 'UPDATE user set geld = geld + ' . $charArray[$key]['money'] . ' WHERE id = ' . $char['besitzer'];
			echo $sql . ' 
';
			mysql_query($sql);
		}
	}
	echo 'The results!
';
	print_r($returnArray);
	return $returnArray; // return the array for further investigations like giving out a nature fruit ;), preventing from deletion etc.
}
/**
 * Generates the Rank of a char using the depth of the charid in the ranking array. E.g. 0 is for 1., 1 is for 2., 2 is for 3.-4., ...
 * @param int $depth the depth
 */
function generateRanglistString($depth) {
	if(!is_numeric($depth) || $depth < 0) {
		return 'NaN.';
	}
	if($depth <= 1) {
		return ($depth + 1).'.'; // 1. and 2.
	} else {
		return (pow(2, $depth - 1) + 1) . '. - '.(pow(2, $depth)).'.';
	}
}
function distributeShopPoints(array $prices, array $winners, $reason) {
	$n = min(count($prices), count($winners));
	for($i = 0; $i<$n; $i++) {
		$points = $prices[$i];
		foreach($winners[$i] as $winner) {
//			echo $winner . ' 
';
			$char = getChar($winner); // buffered so will not cause in a sql statement all the time!
			$userid = $char['besitzer'];
			$sql = 'UPDATE user set pkt = pkt + '.$points.' WHERE id = '. $userid;
			echo $sql . ' 
';
			mysql_query($sql);
			insertUserTickerMessage($userid, $points .' IP durch den '.generateRanglistString($i).' Platz im '.$reason.' erhalten!');
		}
	}
}
function distributeUserPrices(array $prices, array $winners, $reason) {
        $n = min(count($prices), count($winners));
        for($i = 0; $i<$n; $i++) {
                $points = $prices[$i];
                foreach($winners[$i] as $winner) {
//                      echo $winner . ' 
';
                        $char = getChar($winner); // buffered so will not cause in a sql statement all the time!
                        $userid = $char['besitzer'];
                        $sql = 'UPDATE user set geld = geld + '.$points.' WHERE id = '. $userid;
                        echo $sql . ' 
';
                        mysql_query($sql);
                        insertUserTickerMessage($userid, $points .' Preisgeld durch den '.generateRanglistString($i).' Platz im '.$reason.' erhalten!');
                }
        }
}
function distributeClanPrices(array $prices, array $results, $reason) {
	$n = min(count($prices), count($results));
	for($i = 0; $i<$n; $i++) {
		$points = $prices[$i];
		foreach($results[$i] as $winner) {
			$char = getChar($winner); // buffered so will not cause in a sql statement all the time!
			$userid = $char['besitzer'];
			$data = mysql_fetch_assoc(mysql_query('Select clan from user where id = ' .$userid ));
			if(is_numeric($data['clan'])) {
				$sql = 'UPDATE clan SET geld = geld + ' . $points . ' WHERE id = ' . $data['clan'];
				echo $sql . ' 
';
				mysql_query($sql);
				insertClanTickerMessage($row['clan'], 'Das Preisgeld von '.$points.' für den '.generateRanglistString($i).' Platz im '.$reason.' erhalten!');
			}
		}
	}
}
?>