Test Failed
Push — master ( 9aea1c...7949ab )
by Tomáš
02:12
created

Category::genGamesSimulateReal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TournamentGenerator;
4
5
/**
6
 *
7
 */
8
class Category extends Base implements WithSkipSetters, WithTeams, WithRounds
9
{
10
11
	private $rounds = [];
12
	private $teams = [];
13
	private $allowSkip = false;
14
15
	public function __construct(string $name = '', $id = null) {
16
		$this->setName($name);
17 5
		$this->setId(isset($id) ? $id : uniqid());
18 5
	}
19 5
20 5
	public function addRound(Round ...$rounds){
21
		foreach ($rounds as $round) {
22
			$this->rounds[] = $round;
23
		}
24
		return $this;
25
	}
26
	public function round(string $name = '') {
27
		$r = new Round($name);
28
		$this->rounds[] = $r->setSkip($this->allowSkip);
29 1
		return $r;
30 1
	}
31 1
	public function getRounds(){
32 1
		return $this->rounds;
33
	}
34 2
	public function getGroups() {
35 2
		$groups = [];
36
		foreach ($this->getRounds() as $round) {
37
			$groups = array_merge($groups, $round->getGroups());
38
		}
39
		return $groups;
40
	}
41
42
	public function allowSkip(){
43
		$this->allowSkip = true;
44
		return $this;
45
	}
46 4
	public function disallowSkip(){
47 4
		$this->allowSkip = false;
48 4
		return $this;
49
	}
50 1
	public function setSkip(bool $skip) {
51 1
		$this->allowSkip = $skip;
52
		return $this;
53
	}
54 1
	public function getSkip() {
55 1
		return $this->allowSkip;
56 1
	}
57
58 1
	public function addTeam(Team ...$teams) {
59
		foreach ($teams as $team) {
60
			$this->teams[] = $team;
61
		}
62
		return $this;
63
	}
64
	public function team(string $name = '', $id = null) {
65 1
		$t = new Team($name, $id);
66 1
		$this->teams[] = $t;
67
		return $t;
68
	}
69
	public function getTeams(bool $ordered = false, $ordering = \TournamentGenerator\Constants::POINTS, array $filters = []) {
70
		$teams = $this->teams;
71
		foreach ($this->rounds as $round) {
72
			$teams = \array_merge($teams, $round->getTeams());
73
		}
74
		$teams = \array_unique($teams);
75
		$this->teams = $teams;
76
		if ($ordered) $teams = $this->sortTeams($ordering);
77
78
		// APPLY FILTERS
79
		$filter = new Filter($this->getGroups(), $filters);
80
		$filter->filter($teams);
81
82
		return $teams;
83
	}
84
	public function sortTeams($ordering = \TournamentGenerator\Constants::POINTS, array $filters = []) {
85
		$teams = [];
86
		for ($i = count($this->rounds)-1; $i >= 0; $i--) {
87
			$rTeams = array_filter($this->rounds[$i]->getTeams(true, $ordering), function($a) use ($teams) { return !in_array($a, $teams); });
88
			$teams = array_merge($teams, $rTeams);
89
		}
90
		$this->teams = $teams;
91
92
		// APPLY FILTERS
93
		$filter = new Filter($this->getGroups(), $filters);
94
		$filter->filter($teams);
95
96
		return $teams;
97
	}
98
99
	public function getGames() {
100
		$games = [];
101
		foreach ($this->getRounds() as $round) {
102
			$games = array_merge($games, $round->getGames());
103
		}
104
		return $games;
105
	}
106
107
	public function splitTeams(Round ...$wheres) {
108
109
		if (count($wheres) === 0) $wheres = $this->getRounds();
110
111
		$teams = $this->getTeams();
112
		shuffle($teams);
113
114
		while (count($teams) > 0) {
115
			foreach ($wheres as $where) {
116
				if (count($teams) > 0) $where->addTeam(array_shift($teams));
117
			}
118
		}
119
		foreach ($wheres as $where) {
120
			$where->splitTeams();
121
		}
122
		return $this;
123
	}
124
125
	public function genGamesSimulate() {
126
		$games = Utilis\Simulator::simulateCategory($this);
127
		return $games;
128
	}
129
	public function genGamesSimulateReal() {
130
		$games = Utilis\Simulator::simulateCategoryReal($this);
131
		return $games;
132
	}
133
}
134