Advertisement
plirof2

tournament-random-pairing.php

Apr 7th, 2023 (edited)
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Knockdown tournament simple manager PHP
  4. Random pairing
  5.  
  6. Changes
  7. -v230407d - Minor fix
  8. -v230407c - First working version
  9. -
  10.  
  11. https://stackoverflow.com/questions/21232825/randomly-generate-pair-team-not-pairing-with-itself
  12. Note: Online random item list generator : http://www.mynikko.com/tools/tool_incrementstr.html
  13.  
  14. ToDO:
  15. -implement swiss_pairing
  16. - Show game history in the end - maybe with tabs
  17. ok -Front end text area to enter list of teams
  18. ok -List of teams is entered in the terams[] array and then they are paired and returned to frontend
  19. ok -Each paired team has a checkbox for the winner
  20. ok -Submit form get only the winners and puts them again in the teams[] array to do a next random pairing
  21.  
  22.  
  23. */
  24. $debug=false;
  25. if ($debug) {echo "<h3>";print_r($_REQUEST) ;echo " </h3>"; }
  26. $random_pair_for_all_rounds=false; //false for only use random pair in the first round
  27. $swiss_pairing=true; //NOTE swiss_pairing ignores random
  28.  
  29. $round=0;
  30. if(@$_REQUEST['round']==1 ) $round=1;
  31. if(@$_REQUEST['round']>1 ) {
  32.     //if($debug)echo "round".$_REQUEST['round'];
  33.     $round=$_REQUEST['round'];
  34.     //if($debug)echo "round got from form:$round";
  35. }
  36.  
  37. /*
  38. //Filter request : https://stackoverflow.com/questions/69416655/filtering-and-sanitizing-checkboxes-input-with-php
  39. $args=array(
  40.         'onename'   =>  array(
  41.             'filter'    =>  FILTER_SANITIZE_NUMBER_INT,
  42.             'flags'     =>  FILTER_REQUIRE_ARRAY
  43.         )
  44.     );
  45. $_REQUEST=filter_input_array( INPUT_POST, $args );
  46. */
  47. echo "<h3> Round : $round </h3>";
  48.  
  49.  
  50. function name_checkbox_create($teams,$index){
  51.  
  52.     return "<b>".$teams[$index ]."</b> [Won? : <input type='checkbox' name=winner[] value='".$teams[$index ]."''  >]";
  53.  
  54. }
  55.  
  56. function swiss_pair($teams){
  57.     echo "<h3>Using Swiss Pairing! (note: ignores points)</h3>";
  58.     $number_of_teams = count($teams);
  59.     $teams_swiss=array();
  60.     $counter=0;
  61.     for($i=0;$i<($number_of_teams/2);$i++) {
  62.  
  63.         $teams_swiss[$counter]=$teams[$i];
  64.         $counter++;
  65.         $teams_swiss[$counter]=$teams[$i+($number_of_teams/2)];
  66.         $counter++;
  67.  
  68.     }
  69.     return $teams_swiss;
  70.  
  71.  
  72.  
  73.  
  74. }
  75.  
  76.  
  77. function  generate_pairing($teams) {
  78.     global $round,$random_pair_for_all_rounds,$swiss_pairing;
  79.     $number_of_teams = count($teams);
  80.    
  81.     // Shuffle the teams
  82.     if(($round==1 || $random_pair_for_all_rounds ) && !$swiss_pairing ) shuffle($teams);// You get a shuffled array
  83.  
  84.     $nextround=$round+1;
  85.     if($number_of_teams%2==1) {$teams[] = "Bye"; }// Bye is always the last entry echo
  86.     if ($swiss_pairing) $teams=swiss_pair($teams);
  87.  
  88.     //echo '<form action="tournament.php" method=POST id="usrform">
  89.     echo '<form action="" method=POST id="usrform">
  90.  
  91.  <input type="hidden" name=round value='.$nextround.'>';
  92.     // Pair the adjacent teams
  93.     $pairing_table="";
  94.     for ( $index = 0; $index < $number_of_teams; $index +=2) {
  95.         // Pair $teams[$index ] with $teams[$index +1]
  96.         //For RoboJSbattle it could generate a match link for each pair eg ?index.html?bot1=$teams[$index ]&bot2=$teams[$index+1]
  97.         $pairing_table .= name_checkbox_create($teams,$index)  . "-> " . name_checkbox_create($teams,$index+1) ." <br>"; //Print with checkboxes
  98.     } //end of for
  99.     echo $pairing_table;
  100.     echo '<input type="submit">';
  101.  
  102. }
  103.  
  104.  
  105.  
  106. //First page
  107. if($round==0 ){
  108.     echo 'Enter list of names (one name per line). Pairing is random.<br>
  109.    <!-- <form action="tournament.php" method=POST id="usrform"> -->
  110.     <form action="" method=POST id="usrform">
  111.    
  112.      <input type="hidden" name=round value=1>
  113.      <textarea rows=10 name="initialteamlist" form="usrform">
  114. name1
  115. name2
  116. name3
  117. name4
  118. name5
  119. name6
  120. name7
  121. name8
  122.      </textarea>
  123.      <input type="submit">
  124.    </form>
  125.  
  126.  
  127.    ';
  128. }
  129.  
  130.  
  131.  
  132. //$ids = explode(PHP_EOL, $input);
  133. if($round==1 ){
  134.     echo "<h2>First Round</h2>";
  135.     $teamtext_filtered = preg_replace('/\n+/', "\n", trim($_REQUEST['initialteamlist']));
  136.     //$teamtext_filtered = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", trim($_REQUEST['initialteamlist']));
  137.     //$teams = explode(PHP_EOL,$teamtext_filtered );
  138.     $teams = explode("\n",$teamtext_filtered );
  139.     generate_pairing($teams);
  140. }
  141.  
  142.  
  143. if($round>1 ){
  144.     echo "<h2>Next Round</h2>";
  145.     $teams=$_REQUEST['winner'];
  146.     if ($debug) {echo "<h3>ROUND>1 : ";print_r($teams) ;echo " </h3>"; }
  147.     if(count($teams)>1 )generate_pairing($teams); //here we need checkboxes
  148. }
  149. /*
  150. $teams[] = "Austria";
  151. $teams[] = "Hungary";
  152. $teams[] = "Czech Republic";
  153. $teams[] = "New Zealang";
  154. $teams[] = "France";
  155. $teams[] = "Belgium";
  156. $teams[] = "Estonia";
  157. $teams[] = "Iceland";
  158. */
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement