| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace TournamentGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Tournament | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	public $name = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	private $categories = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	private $rounds = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	private $teams = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	private $expectedPlay = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	private $expectedGameWait = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	private $expectedRoundWait = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	private $expectedCategoryWait = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	private $allowSkip = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	function __construct(string $name = ''){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		$this->name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	public function __toString() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		return $this->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	public function setPlay(int $play) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		$this->expectedPlay = $play; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	public function getPlay() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		return $this->expectedPlay; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	public function setGameWait(int $wait) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$this->expectedGameWait = $wait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	public function getGameWait() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		return $this->expectedGameWait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	public function setRoundWait(int $wait) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$this->expectedRoundWait = $wait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	public function getRoundWait() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		return $this->expectedRoundWait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	public function setCategoryWait(int $wait) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$this->expectedCategoryWait = $wait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	public function getCategoryWait() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		return $this->expectedCategoryWait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	public function getTournamentTime(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$games = count($this->getGames()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		return $games*$this->expectedPlay+$games*$this->expectedGameWait+count($this->getRounds())*$this->expectedRoundWait+count($this->getCategories())*$this->expectedCategoryWait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	public function allowSkip(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		$this->allowSkip = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	public function disallowSkip(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$this->allowSkip = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	public function setSkip(bool $skip) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		$this->allowSkip = $skip; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	public function getSkip() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		return $this->allowSkip; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	public function addCategory(Category ...$categories){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		foreach ($categories as $category) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			if ($category instanceof Category) $this->categories[] = $category; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			else throw new \Exception('Trying to add category which is not an instance of the Category class.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	public function category(string $name = '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		$c = new Category($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$this->categories[] = $c->setSkip($this->allowSkip); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		return $c; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	public function getCategories() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		return $this->categories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	public function addRound(Round ...$rounds) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		foreach ($rounds as $round) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			if ($round instanceof Round) $this->rounds[] = $round; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			else throw new \Exception('Trying to add round which is not an instance of the Round class.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	public function round(string $name = '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		$r = new Round($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$this->rounds[] = $r->setSkip($this->allowSkip); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		return $r; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	public function getRounds() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		if (count($this->categories) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			$rounds = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			foreach ($this->categories as $category) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 				$rounds = array_merge($rounds, $category->getRounds()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 			return $rounds; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		return $this->rounds; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  | 	public function addTeam(...$teams) { | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  | 		foreach ($teams as $team) { | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  | 			if ($team instanceof Team)  { | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  | 				$this->teams[] = $team; | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  | 				continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  | 			elseif (gettype($team) === 'array') { | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  | 				array_merge($teams, array_filter($team, function($a) { | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  | 					return ($a instanceof Team); | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  | 				})); | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  | 				continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  | 			throw new \Exception('Trying to add team which is not an instance of Team class'); | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	public function team(string $name = '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		$t = new Team($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		$this->teams[] = $t; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		return $t; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	public function getTeams(bool $ordered = false, $ordering = \POINTS) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		if (count($this->teams) === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 			$teams = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 			foreach ($this->categories as $category) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 				$teams = array_merge($teams, $category->getTeams()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			foreach ($this->rounds as $round) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 				$teams = array_merge($teams, $round->getTeams()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 			$this->teams = $teams; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		if ($ordered) $this->sortTeams($ordering); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		return $this->teams; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	public function sortTeams($ordering = \POINTS) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 		$teams = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		for ($i = count($this->rounds)-1; $i >= 0; $i--) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 			$rTeams = array_filter($this->rounds[$i]->getTeams(true, $ordering), function($a) use ($teams) { return !in_array($a, $teams); }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 			$teams = array_merge($teams, $rTeams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		$this->teams = $teams; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		return $this->teams; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	public function getGames() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		$games = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		foreach ($this->getRounds() as $round) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 			$games = array_merge($games, $round->getGames()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		return $games; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	public function splitTeams(Round ...$wheres) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		if (count($wheres) === 0) $wheres = $this->getRounds(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 		foreach ($wheres as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 			if (gettype($value) === 'array') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 				unset($wheres[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 				$wheres = array_merge($wheres, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 				continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 		$teams = $this->getTeams(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 		shuffle($teams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 		while (count($teams) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 			foreach ($wheres as $where) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 				if (count($teams) > 0) $where->addTeam(array_shift($teams)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 		foreach ($wheres as $where) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 			$where->splitTeams(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 	public function genGamesSimulate(bool $returnTime = false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 		$games = Utilis\Simulator::simulateTournament($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 		if ($returnTime) return $this->getTournamentTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 		return $games; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 	public function genGamesSimulateReal(bool $returnTime = false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | 		$games = Utilis\Simulator::simulateTournamentReal($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  | 		if ($returnTime) return $this->getTournamentTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | 		return $games; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 210 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 211 |  |  |  |