'0') { while ($rowtournament = mysql_fetch_array($resulttournament)) { # get tournament details $tnid = $rowtournament["Id"]; $tnentriesmin = $rowtournament["EntriesMin"]; $tntable = "Tournament".$tnid; # table name for registered characters echo $tntable."



"; $resulttn = mysql_query("SELECT * FROM $tntable"); $nbregis = mysql_num_rows($resulttn); if ($nbregis < $tnentriesmin) { echo "tournament cancelled
"; } if (($nbregis >= $tnentriesmin) and ($nbregis > 4)) { # calculate number of entrant surviving pools $nbkeep = 1; while ($nbkeep < $nbregis) { $nbkeep = $nbkeep * 2; } $nbkeep = (int)($nbkeep / 4); # calculate number of pools and survivors per pool $nbpool = $nbkeep; $found = false; while (($nbpool > 1) and (!$found)) { $nbkeeppool = $nbkeep / $nbpool; if (max($nbkeep, $nbkeeppool) / min($nbkeep, $nbkeeppool) <= 4) { $found = true; } if (!$found) { $nbpool = (int)($nbpool / 2); } } echo "Of ".$nbregis." registered, ".$nbkeep." will get out of pools
"; echo "Will make ".$nbpool." pools with ".$nbkeeppool." winners per pool
"; # calculate how many entrants per pool there is, and alter table $resulttn = mysql_query("ALTER TABLE $tntable ADD Pool TINYINT"); $resulttn = mysql_query("ALTER TABLE $tntable ADD Fighted TINYINT"); $normalinpool = (int)($nbregis / $nbpool); $maxinpool = $nbregis / $nbpool; if ($maxinpool <> $normalinpool) { $maxinpool = (int)($maxinpool) + 1; } echo "Each pool contains ".$normalinpool." or ".$maxinpool." entrants
"; for ($i=1; $i<=$maxinpool; $i++) { $colname = "P".$i; $resulttn = mysql_query("ALTER TABLE $tntable ADD $colname INT DEFAULT '0'"); } for ($i=1; $i<=$nbpool; $i++) { $nbinpool[$i] = 0; } # move entrants into each pool srand(time()); for ($i=1; $i<=$normalinpool * $nbpool; $i++) { do { $moveinpool = Dice($nbpool); } while ($nbinpool[$moveinpool] >= $normalinpool); $nbinpool[$moveinpool]++; $result = mysql_query("UPDATE $tntable SET Pool=$moveinpool WHERE Id=$i"); } if ($maxinpool <> $normalinpool) { for ($i=($normalinpool * $nbpool) + 1; $i<=$nbregis; $i++) { do { $moveinpool = Dice($nbpool); } while ($nbinpool[$moveinpool] >= $maxinpool); $nbinpool[$moveinpool]++; $result = mysql_query("UPDATE $tntable SET Pool=$moveinpool WHERE Id=$i"); } } # next phase of the tournament is entering pool mode $result = mysql_query("UPDATE Tournaments SET Pool=$maxinpool WHERE Id=$tnid"); $result = mysql_query("UPDATE Tournaments SET NbPool=$nbpool WHERE Id=$tnid"); $result = mysql_query("UPDATE Tournaments SET SelPool=$nbkeeppool WHERE Id=$tnid"); } } } # grabs all tournaments that have a phase happening this tick $resulttournament = mysql_query("SELECT * FROM Tournaments WHERE NextTick = $turn"); if (mysql_num_rows($resulttournament) <> '0') { while ($rowtournament = mysql_fetch_array($resulttournament)) { # get tournament details $tnid = $rowtournament["Id"]; $tournamentname = $row["Name"]; $tnpool = $rowtournament["Pool"]; $tnnbpool = $rowtournament["NbPool"]; $tnfinals = $rowtournament["Finals"]; # in finals phase or not (actually, semi-finals) $tnpool = $rowtournament["Pool"]; # in pool phase or not $tnP1 = $rowtournament["P1"]; $tntable = "Tournament".$tnid; # table name for registered characters echo $tntable."



"; $isintournament = "true"; if ($tnpool == 0) { # ELIMINATION MODE # number of battles to be calculated in this phase $result = mysql_query("SELECT * FROM $tntable"); $battles = (int)(mysql_num_rows($result) / 2); # echo "battles ".$battles."
"; for ($tnphase=1; $tnphase<=$battles; $tnphase++) { # gets the odd and even ID of characters for each battle # specific for semi-finals : play the losers' final before the winners' if (($battles == 2) and ($tnfinals == 1)) { $tnphase = 2; } $numadv1 = ($tnphase - 1) * 2 + 1; $numadv2 = $numadv1 + 1; # echo "Playing ".$tournamentid."
"; # echo "tnphase ".$tnphase."
"; # echo "adv".$numadv1." vs adv".$numadv2."
"; # fetch ID of both characters $result = mysql_query("SELECT * FROM $tntable WHERE Id = $numadv1"); $row = mysql_fetch_array($result); $entry1 = $row["Entry"]; $result = mysql_query("SELECT * FROM Characters WHERE Id = $entry1"); $row = mysql_fetch_array($result); $name = $row["Name"]; echo $name." vs "; $result = mysql_query("SELECT * FROM $tntable WHERE Id = $numadv2"); $row = mysql_fetch_array($result); $entry2 = $row["Entry"]; $result = mysql_query("SELECT * FROM Characters WHERE Id = $entry2"); $row = mysql_fetch_array($result); $adv = $row["Name"]; echo $adv."
"; # actually do the fight $idadv[1] = $entry1; $idadv[2] = $entry2; include Inc("dofight.inc"); $result = mysql_query("SELECT * FROM Characters WHERE Id=$loserref"); $report = "Eliminated from the tournament...
"; AddToStory($loserref, $report); if ($battles <> 1) { $result = mysql_query("SELECT * FROM Characters WHERE Id=$winnerref"); $report = "You continue onto the tournaments next phase !
"; AddToStory($winnerref, $report); } # handles the specific treatment of the semi-finals (when only four characters are left) if ($battles == 2) { if ($tnfinals == 0) { if ($tnphase == 1) { $loserrefone = $loserref; # if first battle of semi-finals, write down the loser's ID } else { # if second battle of semi-finals, exchange characters to have winners in the top 2 and losers at place 3 and 4 $result = mysql_query("SELECT * FROM $tntable WHERE Entry=$winnerref"); $row = mysql_fetch_array($result); $idw = $row["Id"]; $result = mysql_query("UPDATE $tntable SET Entry=$winnerref WHERE Entry=$loserrefone"); $result = mysql_query("UPDATE $tntable SET Entry=$loserrefone WHERE Id=$idw"); } } if ($tnfinals == 1) { # after losers' finals we have third and fourth of the tournament $result = mysql_query("UPDATE Tournaments SET P3=$winnerref WHERE Id=$tnid"); $result = mysql_query("UPDATE Tournaments SET P4=$loserref WHERE Id=$tnid"); } } else { if ($battles == 1) { # if only one battle was left, it was the final $result = mysql_query("UPDATE Tournaments SET P2=$loserref WHERE Id=$tnid"); $result = mysql_query("UPDATE Tournaments SET P1=$winnerref WHERE Id=$tnid"); } # in all cases apart 4 players left, writes the winner as the first entrant in the battle (for later gathering) $result = mysql_query("UPDATE $tntable SET Entry=$winnerref WHERE Id=$numadv1"); } } # gather all remaining fighters in the first half of the table... if ($battles <> 2) { for ($tnphase=2; $tnphase<=$battles; $tnphase++) { $numadv1 = ($tnphase - 1) * 2 + 1; $numadv2 = $numadv1 - 1; $result = mysql_query("SELECT * FROM $tntable WHERE Id=$numadv1"); $row = mysql_fetch_array($result); $entry = $row["Entry"]; $result = mysql_query("UPDATE $tntable SET Entry=$entry WHERE Id=$numadv2"); } } # ... then cut down the second half if (($battles <> 2) or (($battles == 2) and ($tnfinals == 1))){ $result = mysql_query("DELETE FROM $tntable WHERE Id>$battles"); } # if there has been two battles switch to finals mode if ($battles == 2) { $result = mysql_query("UPDATE Tournaments SET Finals=1 WHERE Id=$tnid"); } } else { # POOL MODE $result = mysql_query("UPDATE $tntable SET Fighted=0"); # initialize nb of players in each pool for ($i=1; $i<=$tnnbpool; $i++) { $nbinpool[$i] = 0; } # get details for each entrant (ie, in which pool he is) $resulttn = mysql_query("SELECT * FROM $tntable"); while ($rowtn = mysql_fetch_array($resulttn)) { $idintn = $rowtn["Id"]; $charintn = $rowtn["Entry"]; $pool = $rowtn["Pool"]; $nbinpool[$pool]++; $inpool[$pool][$nbinpool[$pool]] = $charintn; } # fight each pool for ($pool=1; $pool<=$tnnbpool; $pool++) { # for each entrant in the pool, search an opponent echo "pool ".$pool."
"; for ($pl1=1; $pl1<=$nbinpool[$pool]; $pl1++) { # echo "pl1=".$pl1."
"; $hasfighted = false; for ($pl2=1; $pl2<=$nbinpool[$pool]; $pl2++) { if (($pl1 <> $pl2) and (!$hasfighted)) { # get their actual character id $pl1id = $inpool[$pool][$pl1]; $pl2id = $inpool[$pool][$pl2]; $result = mysql_query("SELECT * FROM $tntable WHERE Entry=$pl1id"); $colname = "P".$pl2; $played1 = mysql_result($result,0,$colname); $fighted1 = mysql_result($result,0,"Fighted"); # echo "played1=".$played1.", fighted1=".$fighted1."
"; $result = mysql_query("SELECT * FROM $tntable WHERE Entry=$pl2id"); $colname = "P".$pl1; $played2 = mysql_result($result,0,$colname); $fighted2 = mysql_result($result,0,"Fighted"); # echo "played2=".$played2.", fighted2=".$fighted2."
"; # checked that they havent already played against each other if (($played1 == 0) and ($played2 == 0) and ($fighted1 == 0) and ($fighted2 == 0)) { # actually do the fight $idadvid[1] = $pl1; $idadvid[2] = $pl2; $idadv[1] = $pl1id; $idadv[2] = $pl2id; $result = mysql_query("SELECT * FROM Characters WHERE Id = $pl1id"); $row = mysql_fetch_array($result); $name = $row["Name"]; echo $name." vs "; $result = mysql_query("SELECT * FROM Characters WHERE Id = $pl2id"); $row = mysql_fetch_array($result); $adv = $row["Name"]; echo $adv."
"; include Inc("dofight.inc"); $hasfighted = true; # enter the xp gain in the winner's column $colname = "P".$idadvid[$loserrefid]; $gain = $XPgain[$winnerrefid]; $gain = 50; $result = mysql_query("UPDATE $tntable SET $colname=$gain WHERE Entry=$winnerref"); $result = mysql_query("UPDATE $tntable SET Fighted=1 WHERE Entry=$winnerref"); $result = mysql_query("UPDATE $tntable SET Fighted=1 WHERE Entry=$loserref"); } } } } } } # end of one tournament fighting cycle # when its over, distribute prizes if ($tnP1 <> 0) { # gather all winners $resulttn = mysql_query("SELECT * FROM Tournaments WHERE Id=$tnid"); $rowtn = mysql_fetch_array($resulttn); $placed[1] = $rowtn["P1"]; $placed[2] = $rowtn["P2"]; $placed[3] = $rowtn["P3"]; $placed[4] = $rowtn["P4"]; $places = $rowtn["Places"]; $prize = $rowtn["Prize"]; # distribute prizes for ($i=1; $i <= $places; $i++) { $pl = $placed[$i]; $result = mysql_query("SELECT * FROM Characters WHERE Id=$pl"); $row = mysql_fetch_array($result); $silver = $row["Silver"]; $prizewon = (int)($prize * $Twinplace[$places][$i] / 100); $silver = $silver + $prizewon; $result = mysql_query("UPDATE Characters SET Silver=$silver WHERE Id=$pl"); $report = "You place ".$Tplaced[$i]." in the tournament, and win ".$prizewon." sp !!
"; AddToStory($pl, $report); $result = mysql_query("DROP TABLE $tntable"); } # the host gest the entrance fees $host = $rowtn["Host"]; $cost = $rowtn["Cost"]; $entries = $rowtn["Entries"]; $result = mysql_query("SELECT * FROM Characters WHERE Id=$host"); $row = mysql_fetch_array($result); $silver = $row["Silver"] + $entries * $cost; $result = mysql_query("UPDATE Characters SET Silver=$silver WHERE Id=$host"); $report = "As host of the tournament you gain ".($entries * $cost)." sp.
"; AddToStory($host, $report); } else { # writes down when the next phase of the tournament will happen $tndelay = $rowtournament["Phase"]; # delay between two phases of the tournament $nexttick = $turn+$tndelay; $result = mysql_query("UPDATE Tournaments SET NextTick=$nexttick WHERE Id=$tnid"); # writes down the next phase in plannings $resultp = mysql_query("SELECT * FROM $tntable"); while ($rowp = mysql_fetch_array($resultp)) { $id = $rowp["Entry"]; $turntoact = $nexttick; $delay = 1; # TO BE MODIFIED !? $action = 4; $param = $tnid; include Inc("setplanning.inc"); } } } # end of one tournament treatment } # end of all tournaments for this turn # check each tournament if the pool phase didnt end just now $resulttournament = mysql_query("SELECT * FROM Tournaments WHERE Pool<>0"); if (mysql_num_rows($resulttournament) <> '0') { while ($rowtournament = mysql_fetch_array($resulttournament)) { # get tournament details $tnid = $rowtournament["Id"]; $tntick = $rowtournament["Tick"]; $tnphase = $rowtournament["Phase"]; $tnmaxinpool = $rowtournament["Pool"]; $tnnbpool = $rowtournament["NbPool"]; $tnnbkeeppool = $rowtournament["SelPool"]; $tnnexttick = $rowtournament["NextTick"]; $tnentries = $rowtournament["Entries"]; $tntable = "Tournament".$tnid; # table name for registered characters # nb of tournament phases plyaed since the start $nbphases = (int)(($tnnexttick - $tntick) / $tnphase); if ($nbphases >= $tnmaxinpool) { # end of the pool phase for this tournament # initialize nb of entrants in each pool, and their final score for ($i=1; $i<=$tnnbpool; $i++) { $nbinpool[$i] = 0; for ($j=1; $j<=$tnmaxinpool; $j++) { $charres[$i][$j] = 0; } } # gather all pools and entrants results $resulttn = mysql_query("SELECT * FROM $tntable"); while ($rowtn = mysql_fetch_array($resulttn)) { $idintn = $rowtn["Id"]; $charintn = $rowtn["Entry"]; $pool = $rowtn["Pool"]; $nbinpool[$pool]++; $n = $nbinpool[$pool]; $inpool[$pool][$nbinpool[$pool]] = $charintn; for ($i=1; $i<=$tnnbpool; $i++) { $colname = "P".$i; $pf = $rowtn[$colname]; if ($pf <> 0) { # calculate score = nb of wins + xp gains $charres[$pool][$n] = $charres[$pool][$n]*10000 + $pf; } } } # search winners of each pool for ($pool=1; $pool<=$tnnbpool; $pool++) { for ($sel=1; $sel<=$tnnbkeeppool; $sel++) { $poolmax[$pool] = 0; for ($pl=1; $pl<=$tnmaxinpool; $pl++) { if ($charres[$pool][$pl] > $poolmax[$pool]) { $poolmax[$pool] = $charres[$pool][$pl]; $poolwinner[$pool] = $pl; } } $p = $poolwinner[$pool]; $charres[$pool][$p] = 0; $charid = $inpool[$pool][$p]; $winpool[$pool][$sel] = $charid; $result = mysql_query("UPDATE $tntable SET P1=1 WHERE Entry=$charid"); echo "char ".$charid." places ".$sel." of pool ".$pool."
"; if ($pool/2 == (int)($pool/2)) { $newid = ($pool-2)*$sel + ($tnnbkeeppool-$sel+1)*2; } else { $newid = ($pool-1)*$sel + $sel*2-1; } $result = mysql_query("UPDATE $tntable SET P2=$newid WHERE Entry=$charid"); } } # suppress all pool losers from the table $result = mysql_query("DELETE FROM $tntable WHERE P1<>1"); $result = mysql_query("UPDATE $tntable SET Id=P2"); echo "end of pool phase for tournament".$tnid."
"; $result = mysql_query("UPDATE Tournaments SET Pool=0 WHERE Id=$tnid"); } } } ?>