Passed
Push — master ( b9aa7a...67756f )
by Tomáš
02:47
created

Round::addTeam()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 6
nop 1
dl 0
loc 23
rs 9.0777
c 0
b 0
f 0
1
<?php
2
3
namespace TournamentGenerator;
4
5
/**
6
 *
7
 */
8
class Round
9
{
10
11
	public $name = '';
12
	public $id = '';
13
	private $groups = [];
14
	private $games = [];
15
	private $teams = [];
16
	private $allowSkip = false;
17
18
	function __construct(string $name = '') {
19
		$this->id = uniqid();
20
		$this->name = $name;
21
	}
22
	public function __toString() {
23
		return $this->name;
24
	}
25
26
	public function addGroup(Group ...$groups){
27
		foreach ($groups as $group) {
28
			if ($group instanceof Group) $this->groups[] = $group;
29
			else throw new \Exception('Trying to add group which is not an instance of Group class.');
30
		}
31
		return $this;
32
	}
33
	public function group(array $settings = []) {
34
		$g = new Group($settings);
35
		$this->groups[] = $g->setSkip($this->allowSkip);
36
		return $g;
37
	}
38
	public function getGroups(){
39
		$this->orderGroups();
40
		return $this->groups;
41
	}
42
	public function getGroupsIds() {
43
		$this->orderGroups();
44
		return array_map(function($a) { return $a->id; }, $this->groups);
45
	}
46
	public function orderGroups() {
47
		usort($this->groups, function($a, $b){
48
			return $a->order - $b->order;
49
		});
50
	}
51
52
	public function allowSkip(){
53
		$this->allowSkip = true;
54
		return $this;
55
	}
56
	public function disallowSkip(){
57
		$this->allowSkip = false;
58
		return $this;
59
	}
60
	public function setSkip(bool $skip = false) {
61
		$this->allowSkip = $skip;
62
		return $this;
63
	}
64
	public function getSkip() {
65
		return $this->allowSkip;
66
	}
67
68
	public function genGames(){
69
		foreach ($this->groups as $group) {
70
			$group->genGames();
71
			$this->games = array_merge($this->games, $group->orderGames());
72
		}
73
		return $this->games;
74
	}
75
	public function getGames() {
76
		return $this->games;
77
	}
78
	public function isPlayed(){
79
		foreach ($this->groups as $group) {
80
			if (!$group->isPlayed()) return false;
81
		}
82
		return true;
83
	}
84
85
	public function addTeam(...$teams) {
86
		foreach ($teams as $team) {
87
			if ($team instanceof Team)  {
88
				$this->teams[] = $team;
89
			}
90
			elseif (gettype($team) === 'array') {
91
				foreach ($team as $team2) {
92
					if ($team2 instanceof Team) $this->teams[] = $team2;
93
					$team2->groupResults[$this->id] = [
94
						'group' => $this,
95
						'points' => 0,
96
						'score'  => 0,
97
						'wins'   => 0,
98
						'draws'  => 0,
99
						'losses' => 0,
100
						'second' => 0,
101
						'third'  => 0
102
					];
103
				}
104
			}
105
			else throw new \Exception('Trying to add team which is not an instance of Team class');
106
		}
107
		return $this;
108
	}
109
	public function team(string $name = '') {
110
		$t = new Team($name);
111
		$this->teams[] = $t;
112
		return $t;
113
	}
114
	public function getTeams(bool $ordered = false, $ordering = \POINTS) {
115
		if (count($this->teams) == 0) {
116
			$teams = [];
117
			foreach ($this->groups as $group) {
118
				$teams = array_merge($teams, $group->getTeams());
119
			}
120
			$this->teams = $teams;
121
		}
122
		if ($ordered) {
123
			$this->sortTeams($ordering);
124
		}
125
		return $this->teams;
126
	}
127
	public function sortTeams($ordering = \POINTS) {
128
		Utilis\Sorter\Teams::sortRound($this->teams, $this, $ordering);
129
		return $this->teams;
130
	}
131
132
	public function splitTeams(...$groups) {
133
134
		if (count($groups) === 0) $groups = $this->getGroups();
135
136
		foreach ($groups as $key => $value) {
137
			if (gettype($value) === 'array') {
138
				unset($groups[$key]);
139
				$groups = array_merge($groups, $value);
140
			}
141
		}
142
143
		$teams = $this->getTeams();
144
		shuffle($teams);
145
146
		while (count($teams) > 0) {
147
			foreach ($groups as $group) {
148
				if ($group instanceof Group) {
149
					if (count($teams) > 0) $group->addTeam(array_shift($teams));
150
				}
151
			}
152
		}
153
		return $this;
154
	}
155
156
	public function progress(bool $blank = false){
157
		foreach ($this->groups as $group) {
158
			$group->progress($blank);
159
		}
160
		return $this;
161
	}
162
163
	public function simulate() {
164
		foreach ($this->groups as $group) {
165
			if ($group->isPlayed()) continue;
166
			$group->simulate([], false);
167
		}
168
		return $this;
169
	}
170
	public function resetGames() {
171
		foreach ($this->groups as $group) {
172
			$group->resetGames();
173
		}
174
		return $this;
175
	}
176
}
177