diff --git a/ag/include/tournament.inc.php b/ag/include/tournament.inc.php
new file mode 100644
index 0000000..0e5bb66
--- /dev/null
+++ b/ag/include/tournament.inc.php
@@ -0,0 +1,280 @@
+ 0) {
+ $subselect = 'SELECT ec.char_id from event_chars ec inner join tournament t on ec.event_id = t.event_id WHERE t.type = '.$tournament_type['id'].' GROUP by ec.char_id HAVING count(*) >= '.$exclude_fusi_count;
+ $exclusions[] = '( '.$prefix.'fusion = \'nein\' OR '.$prefix.'id NOT IN ( '.$subselect.' ) )';
+ }
+
+ // Es sollen keine NPC mitmachen :) und nicht die Spezial Wanted NPCs
+
+ $exclude_race_ids = array();
+
+ $npc_race = getRaceTypeName('NPC');
+ $races = getRacesByType($npc_race); // remove all NPC
+ foreach($races as $race) {
+ $exclude_race_ids[] = '\''.$race['id'].'\'';
+ }
+
+ if($special != 1){
+ $races = getSpecialRaceIds();
+ $npc_race = getRaceTypeName('NPC'); // do not remove npc (there is a separate setting for this)
+
+ foreach($races as $race) {
+ if($race['type'] != $npc_race['id']) {
+ $exclude_race_ids[] = '\''.$race['id'].'\'';
+ }
+ }
+ }
+
+ if(count($exclude_race_ids)) {
+ $exclusions[] = $prefix.'rasse NOT IN('.implode(',',$exclude_race_ids).') ';
+ }
+
+ if(is_numeric($minlevel)){
+ $exclusions[] = $prefix.'level >= '.$minlevel;
+ }
+
+ if(is_numeric($maxlevel)){
+ $exclusions[] = $prefix.'level <= '.$maxlevel;
+ }
+
+ if ($exclude_winners == 1) {
+ $exclusions[] = $prefix.'id NOT IN( SELECT charid FROM highscore WHERE art = "'.$name.'" AND charid IS NOT NULL) ';
+ }
+
+ if ( count($exclusions ) > 0 ) {
+ return ' '.join(' AND ', $exclusions).' ';
+ }
+ return '1';
+
+}
+
+function retrieveParticipants($tournament_type) {
+ $filter_sql = getTournamentCharExclusionSQL($tournament_type, 'c');
+
+ $name = $tournament_type['name'];
+
+ $anzahl = $tournament_type['competitors']; // Anzahl (benötigt)
+ $gain = $tournament_type['gain']; // ('Anmeldung', 'PL', 'Level')
+ $itemless = !$_GET['without_equip']; // gibt an ob das turnier die items ingorieren soll (standard sind items aktiv)
+
+ $sql = 'SELECT count(*) as anzahl from chars c where '.$filter_sql;
+
+ echo $sql.'
';
+ $qry = db_query($sql);
+ $row = mysqli_fetch_assoc($qry);
+
+ if($row['anzahl'] < $anzahl){
+ echo $name.' konnte nicht mit '.$anzahl.' Chars gestartet werden, da es nur '.$row['anzahl'].' von '.$anzahl.' nötigen Chars gibt!
';
+ $anzahl = pow(2, floor(log($row['anzahl'], 2)));
+ echo $name.' wird nun mit '.$anzahl.' Chars gestartet!
';
+ }
+
+ // Erstma nur die id auslesen (danach wird eh nochmal gemischt ;))
+ $cid = array();
+ if($gain == TOURNAMENT_GAIN_ANMELDUNG){
+ $sql = 'SELECT c.id FROM tournament_registration tr inner join chars as c ON tr.charakter = c.id WHERE tr.type='.$tournament_type['id'].' AND '.$filter_sql;
+ $qry = db_query($sql);
+ while($row = mysqli_fetch_assoc($qry) && count($cid) < $anzahl){
+ $cid[] = $row['id'];
+ }
+ $sql = 'DELETE FROM tournament_registration WHERE type = '.$tournament_type['id'];
+ // echo $sql .'
';
+ db_query($sql); // Lösche die Anmeldungsliste
+
+ // In case the tournament is not full
+ $sql = 'SELECT id FROM chars c where '.$filter_sql.' ORDER BY RAND()';
+ } else if($gain == TOURNAMENT_GAIN_PL){
+ // Die Buffs werden nicht zur PL gezählt ;)
+ $sql = 'SELECT c.id FROM chars c where '.$filter_sql.' ORDER BY starke+verteidigung+speed+ausdauer+glueck desc';
+ } else if($gain == TOURNAMENT_GAIN_LEVEL){
+ $sql = 'SELECT c.id FROM chars c where '.$filter_sql.' ORDER BY level desc';
+ } else{
+ echo 'Parameter gain war weder Anmeldung, PL noch Level
';
+ return array();
+ }
+
+ $qry = db_query($sql);
+
+ while( ($row = mysqli_fetch_assoc($qry)) && count($cid) < $anzahl) {
+ $cid[] = $row['id'];
+ }
+
+ // echo $sql.'
';
+ $char_array = array();
+ foreach ($cid as $char_id) {
+ $qry = db_query($sql);
+ // echo $row['id'].'
';
+ if($itemless == 1) {
+ $char_array[] = getChar($char_id); // ohne Equip für Turniere!!
+ } else {
+ $char_array[] = getCharWithBuffs($char_id); // Equip für Turniere!!
+ }
+ }
+
+ return $char_array;
+}
+
+function canEditTournament($tournament_type) {
+ $sql = 'SELECT count(id) FROM tournament WHERE ausgewertet = FALSE AND type = '.$tournament_type['id'];
+ $count = mysqli_fetch_row(db_query($sql))[0];
+ return $count == 0;
+}
+
+function canStartTournament($tournament_type) {
+ return canEditTournament($tournament_type);
+}
+
+/**
+ * Calling this method will run the given tournament
+ * @param mixed $tournament_type integer => id, string => name, array fetched from getTournamentType(s)
+ */
+function runTournament($tournament_type) {
+ if (is_numeric($tournament_type)) {
+ $tournament_type = getTournamentType($tournament_type);
+ } else if (is_string($tournament_type)) {
+ $tournament_type= mysqli_fetch_assoc( db_query( 'SELECT * from tournament_types WHERE name = \'' . $tournament_type.'\'' ));
+ }
+
+ if (!canStartTournament($tournament_type)) {
+ echo 'Unable to start Tournament as tournament of the same type is already running!';
+ return;
+ }
+
+ $participants = retrieveParticipants($tournament_type);
+ if ($participants !== NULL) {
+ $tournament = createTournament($tournament_type);
+ calculateTournament($tournament, $participants);
+ }
+}
+
+function createTournament($tournament_type) {
+ $event_id= createEvent(EVENT_TURNIER);
+ db_query('INSERT INTO tournament(type, event_id) values('.$tournament_type['id'].','.$event_id.')');
+ return mysqli_fetch_assoc(db_query( 'SELECT * FROM tournament WHERE event_id = ' . $event_id));
+}
+
+function calculateTournament($tournament, $participants) {
+ shuffle($participants);
+
+ $event_id = $tournament['event_id'];
+
+ foreach ($participants as $participant) {
+ addParticipant($event_id, $participant);
+ }
+
+ // Hier Gruppenphase berechnen und die Chars für die Endrunde in $chars speichern
+ $start_time = time();
+ $counter = 1;
+ $round = 1;
+ $duration = TOURNAMENT_FIGHT_DURATION * 60;
+
+ $char_array = $participants;
+
+ // Beginn der Endrunde
+ while(count($char_array) > 1){ // Solange bis nur ein Char übrig bleibt
+ $n_chars = array();
+ for($i=0;$i $round));
+ if ($winner['id'] == $char_array[$i]['id']) {
+ $n_chars[] = $char_array[$i];
+ } else {
+ $n_chars[] = $char_array[$i+1];
+ }
+ // echo 'Berechne fight zwischen '.$char_array[$i].'und '.$char_array[$i+1].'('.$i.'/'.count($char_array).')
';
+ $counter++;
+ }
+ // echo 'nxt round
';
+ $round++;
+ if($tournament['randomize'] == 1) {
+ shuffle($n_chars);
+ }
+ $char_array = $n_chars;
+ }
+
+ $end_time = $start_time + $counter * $duration;
+ $end_time_str = date("Y-m-d H:i:s",$end_time);
+ db_query('UPDATE event_chars SET block_begin = \''.$end_time_str.'\', block_end = \''.$end_time_str.'\' WHERE event_id = '.$event_id);
+
+ // Seems that we have a winner :D
+ $fruit_item_str = getFruitItem($tournament['fruit_type'], $tournament['fruit_chance']);
+
+ if ($fruit_item_str !== NULL) {
+ // add item in event_char_metadata
+ db_query('INSERT INTO event_char_metadata(event_id, event_char_id, `key`, `value`) values('.$event_id.','.$char_array[0]['id'].',\''.KEY_ITM_CHAR1.'\',\''.$fruit_item_str.'\')');
+ }
+
+}