Tournament::genGamesSimulateReal()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace TournamentGenerator;
4
5
use Exception;
6
use TournamentGenerator\Containers\GameContainer;
7
use TournamentGenerator\Containers\HierarchyContainer;
8
use TournamentGenerator\Containers\TeamContainer;
9
use TournamentGenerator\Interfaces\WithCategories;
10
use TournamentGenerator\Interfaces\WithGames;
11
use TournamentGenerator\Interfaces\WithGroups;
12
use TournamentGenerator\Interfaces\WithRounds;
13
use TournamentGenerator\Interfaces\WithSkipSetters;
14
use TournamentGenerator\Interfaces\WithTeams;
15
use TournamentGenerator\Traits\WithCategories as WithCategoriesTrait;
16
use TournamentGenerator\Traits\WithGames as WithGamesTrait;
17
use TournamentGenerator\Traits\WithGroups as WithGroupsTrait;
18
use TournamentGenerator\Traits\WithRounds as WithRoundsTrait;
19
use TournamentGenerator\Traits\WithSkipSetters as WithSkipSettersTrait;
20
use TournamentGenerator\Traits\WithTeams as WithTeamsTrait;
21
22
/**
23
 * Tournament class
24
 *
25
 * Tournament is a main class. It is a container for every other object related to one tournament (categories -> rounds -> groups -> games -> teams).
26
 *
27
 * @package TournamentGenerator
28
 * @author  Tomáš Vojík <[email protected]>
29
 * @since   0.1
30
 */
31
class Tournament extends HierarchyBase implements WithSkipSetters, WithTeams, WithRounds, WithCategories, WithGroups, WithGames
32
{
33
	use WithTeamsTrait;
34
	use WithCategoriesTrait;
35
	use WithRoundsTrait;
36
	use WithGroupsTrait;
37
	use WithSkipSettersTrait;
38
	use WithGamesTrait;
39
40
	/** @var int Wait time between categories */
41
	protected int $expectedCategoryWait = 0;
42
	/** @var int Play time for one game */
43
	private int $expectedPlay = 0;
44
	/** @var int Wait time between games */
45
	private int $expectedGameWait = 0;
46
	/** @var int Wait time between rounds */
47
	private int $expectedRoundWait = 0;
48
49 94
	public function __construct(string $name = '') {
50 94
		$this->name = $name;
51 94
		$this->games = new GameContainer(0);
52 94
		$this->teams = new TeamContainer(0);
53 94
		$this->container = new HierarchyContainer(0);
54 94
	}
55
56
	/**
57
	 * Set play time for one game
58
	 *
59
	 * @param int $play
60
	 *
61
	 * @return $this
62
	 */
63 22
	public function setPlay(int $play) : Tournament {
64 22
		$this->expectedPlay = $play;
65 22
		return $this;
66
	}
67
68
	/**
69
	 * Get play time for one game
70
	 *
71
	 * @return int
72
	 */
73 14
	public function getPlay() : int {
74 14
		return $this->expectedPlay;
75
	}
76
77
	/**
78
	 * Set wait time between games
79
	 *
80
	 * @param int $wait
81
	 *
82
	 * @return $this
83
	 */
84 20
	public function setGameWait(int $wait) : Tournament {
85 20
		$this->expectedGameWait = $wait;
86 20
		return $this;
87
	}
88
89
	/**
90
	 * Get wait time between games
91
	 *
92
	 * @return int
93
	 */
94 13
	public function getGameWait() : int {
95 13
		return $this->expectedGameWait;
96
	}
97
98
	/**
99
	 * Set wait time between rounds
100
	 *
101
	 * @param int $wait
102
	 *
103
	 * @return $this
104
	 */
105 20
	public function setRoundWait(int $wait) : Tournament {
106 20
		$this->expectedRoundWait = $wait;
107 20
		return $this;
108
	}
109
110
	/**
111
	 * Get wait time between rounds
112
	 *
113
	 * @return int
114
	 */
115 13
	public function getRoundWait() : int {
116 13
		return $this->expectedRoundWait;
117
	}
118
119
	/**
120
	 * Set the wait time between categories
121
	 *
122
	 * @param int $wait
123
	 *
124
	 * @return $this
125
	 */
126 14
	public function setCategoryWait(int $wait) : Tournament {
127 14
		$this->expectedCategoryWait = $wait;
128 14
		return $this;
129
	}
130
131
	/**
132
	 * Get the wait time between categories
133
	 *
134
	 * @return int
135
	 */
136 13
	public function getCategoryWait() : int {
137 13
		return $this->expectedCategoryWait;
138
	}
139
140
	/**
141
	 * Simulate all games as they would be played in reality - with dummy teams
142
	 *
143
	 * @param bool $returnTime If true - return the expected play time
144
	 *
145
	 * @return Game[]|int Generated games, or expected play time
146
	 * @throws Exception
147
	 */
148 20
	public function genGamesSimulate(bool $returnTime = false) {
149 20
		$games = Helpers\Simulator::simulateTournament($this);
150
151 18
		if ($returnTime) {
152 2
			return $this->getTournamentTime();
153
		}
154 16
		return $games;
155
	}
156
157
	/**
158
	 * Get the whole tournament time
159
	 *
160
	 * @return int
161
	 */
162 10
	public function getTournamentTime() : int {
163 10
		$games = count($this->getGames());
164 10
		return $games * $this->expectedPlay + ($games - 1) * $this->expectedGameWait + (count($this->getRounds()) - 1) * $this->expectedRoundWait + (count($this->getCategories()) - 1) * $this->expectedCategoryWait;
165
	}
166
167
	/**
168
	 * Simulate all games as they would be played in reality - with real teams
169
	 *
170
	 * @param bool $returnTime If true - return the expected play time
171
	 *
172
	 * @return int|Game[] Generated games, or expected play time
173
	 * @throws Exception
174
	 */
175 19
	public function genGamesSimulateReal(bool $returnTime = false) {
176 19
		$games = Helpers\Simulator::simulateTournamentReal($this);
177
178 18
		if ($returnTime) {
179 2
			return $this->getTournamentTime();
180
		}
181 16
		return $games;
182
	}
183
184
	/**
185
	 * @inheritDoc
186
	 * @return array
187
	 * @throws Exception
188
	 */
189
	public function jsonSerialize() : array {
190
		return $this->export()->get();
191
	}
192
193
}
194