// while
            if ($game2join == '') {
                echo "<b>Tournament full</b>";
                exit;
            }
            // Now we got the game 2 join, yeah, now it's gettin' way easier
            $field = '';
            for ($i = 1; $i < 5; $i++) {
                if ($game2join['player' . $i] == 0 && $field == '') {
                    $field = 'player' . $i;
                }
            }
            mysql_query("UPDATE tournaments set " . $field . " = '" . $_SESSION['playerID'] . "' WHERE id = '" . $game2join['id'] . "'");
            // That was the easy part, now create the games
            if ($field == 'player4') {
                create_games($game2join['id']);
            }
            // That was the las one, the tournament CAN start
            if ($game2jojn['gamenum'] == 4) {
                // Mail to all players
                // Problem: All other Group Games (1-3) already started
            }
            // That's it we've joined the tournament
        }
        ?>

<b><?php 
        echo $MSG_LANG["tournamentsuccess"];
        ?>
 <a href="tournaments.php?action=view&id=<?php 
        echo $id;
function save_tournament($id, $winner="") {

//echo get_winner($id); exit;

// Holy shiat :D

$query = "SELECT * FROM {$db_prefix}games WHERE game_id = '$id'";
$sql = mysql_query($query);
$g = mysql_fetch_array($sql);

if (!$g['tournament'])
{
         //echo "// Game not part of a tournament";
         return false;
}

else
{
    // Okay, we have a tournament Game, get the data
         $t = mysql_query("SELECT * FROM tournaments WHERE id = '".$g['tournament']."'");
    $t = mysql_fetch_array($t);

         if ($t['big'] == 0)
         {
                 // Small tournament, no danger
                 $lastgame = true;
                 $gs = mysql_query("SELECT * FROM {$db_prefix}games WHERE tournament = '".$t['id']."'");
                 while ($game = mysql_fetch_array($gs))
                 {
                    if ($game['gameMessage'] == '') $lastgame = false;
                 }

                 if ($lastgame)
                 {
                         $twinner = calculate_winner($t['id']);
                         mysql_query("UPDATE tournaments SET winner = '$twinner' WHERE id = '".$t['id']."'");
                         giveMedalt('tournament4',$twinner,'');
                 }
         }
         else
         {
            // Oh my god - we have a BIG tournament
                 // first, we have to see, if this game IS the LAST game of this group

                 //echo "big tournament ".$t['big'];

                 // Get the finalgame

                 $query = "SELECT id FROM tournaments WHERE big='".$t['big']."' AND name like '%Finals'";
            $finalgame = mysql_fetch_array(mysql_query($query));
                 $finalgame = $finalgame['id'];

                 $lastgame = true;

                 $gs = mysql_query("SELECT * FROM {$db_prefix}games WHERE tournament = '".$t['id']."'");
                 while ($game = mysql_fetch_array($gs))
                 {
                    if ($game['gameMessage'] == '') $lastgame = false;
                 }

                 if (($lastgame == true) && ($t['id'] != $finalgame))
                 {
                    // It was the lastgame, now we want to have the group winner
                         //echo "lastgame<br>";
                         // Put the winner in the finals group
                         // see if we ware fourth player
                         // start finals

                         // Okay, lets get the 4 Group Games

                $game2join = '';
                         $query = "SELECT * FROM tournaments WHERE big='".$t['big']."' AND name like '%Finals'";
                         //echo "<br>";
                $b = mysql_query($query);
                $game2join = mysql_fetch_array($b);
                         //echo mysql_error();
                         //echo "join finals " . $game2join['id'] . "<br>";
                // Now we got the game 2 join, yeah, now it's gettin' way easier

                $field = '';

                for ($i = 1; $i < 5; $i++)
                {       echo "checking " . $game2join['player'.$i] . "<br>";
                    if ($game2join['player'.$i] == 0 && $field == '')
                        {
                            $field = 'player'.$i; echo $field."<br>";
                        }
            }

                         //echo "Join Finals as " . $field . "<br>";

                         // Now let's get the winner
                         $twinner = calculate_winner($t['id']);
                         //echo "Winner ".$twinner."<br>";

                         mysql_query("UPDATE tournaments SET winner = '$twinner' WHERE id = '".$t['id']."'");
                mysql_query("UPDATE tournaments set ".$field." = '".$twinner."' WHERE id = '".$game2join['id']."'");

                // That was the easy part, now create the games

                if ($field == 'player4')
                {
                                 echo "Create games<br>";
                                 create_games($game2join['id']);
                                 send_mails($game2join['id']);
                }

                         if ($twinner == $_SESSION['playerID'])
                         {
                            // I am the Group winner and am going to play in the finals :D
                                 //mail()
                         }
                 }

                 elseif (($lastgame == true) && ($t['id'] == $finalgame))
                 {
                    $twinner = calculate_winner($t['id']);
                         mysql_query("UPDATE tournaments SET winner = '$twinner' WHERE id = '".$t['id']."'");
                         giveMedalt('tournament16',$twinner,'');
                 }
         }


}

}