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\WithGames; |
10
|
|
|
use TournamentGenerator\Interfaces\WithGroups; |
11
|
|
|
use TournamentGenerator\Interfaces\WithRounds; |
12
|
|
|
use TournamentGenerator\Interfaces\WithSkipSetters; |
13
|
|
|
use TournamentGenerator\Interfaces\WithTeams; |
14
|
|
|
use TournamentGenerator\Traits\WithGames as WithGamesTrait; |
15
|
|
|
use TournamentGenerator\Traits\WithGroups as WithGroupsTrait; |
16
|
|
|
use TournamentGenerator\Traits\WithRounds as WithRoundsTrait; |
17
|
|
|
use TournamentGenerator\Traits\WithSkipSetters as WithSkipSettersTrait; |
18
|
|
|
use TournamentGenerator\Traits\WithTeams as WithTeamsTrait; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Tournament category |
23
|
|
|
* |
24
|
|
|
* Category is an optional container for rounds in a tournament. It separates a tournament into bigger chunks that are played separately one after another. |
25
|
|
|
* This allows "sub-tournaments" in one tournament with the possibility to progress teams from one category to the next one. |
26
|
|
|
* |
27
|
|
|
* @package TournamentGenerator |
28
|
|
|
* |
29
|
|
|
* @author Tomáš Vojík <[email protected]> |
30
|
|
|
* @package TournamentGenerator |
31
|
|
|
* @since 0.1 |
32
|
|
|
*/ |
33
|
|
|
class Category extends HierarchyBase implements WithSkipSetters, WithRounds, WithTeams, WithGroups, WithGames |
34
|
|
|
{ |
35
|
|
|
use WithTeamsTrait; |
36
|
|
|
use WithRoundsTrait; |
37
|
|
|
use WithGroupsTrait; |
38
|
|
|
use WithSkipSettersTrait; |
39
|
|
|
use WithGamesTrait; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Category constructor. |
44
|
|
|
* |
45
|
|
|
* @param string $name Category name |
46
|
|
|
* @param string|int|null $id Category id - if omitted -> it is generated automatically as unique string |
47
|
|
|
*/ |
48
|
41 |
|
public function __construct(string $name = '', $id = null) { |
49
|
41 |
|
$this->setName($name); |
50
|
|
|
/** @infection-ignore-all */ |
51
|
41 |
|
$this->setId($id ?? uniqid('', false)); |
52
|
41 |
|
$this->games = new GameContainer($this->id); |
53
|
41 |
|
$this->teams = new TeamContainer($this->id); |
54
|
41 |
|
$this->container = new HierarchyContainer($this->id); |
55
|
41 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Allows skipping of games with less than the minimum amounts of games |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
2 |
|
public function allowSkip() : Category { |
63
|
2 |
|
$this->allowSkip = true; |
64
|
2 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Simulate all games as they would be played in reality - with dummy teams |
69
|
|
|
* |
70
|
|
|
* @return Game[] Generated games, or expected play time |
71
|
|
|
* @throws Exception |
72
|
|
|
*/ |
73
|
1 |
|
public function genGamesSimulate() : array { |
74
|
1 |
|
return Helpers\Simulator::simulateCategory($this); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Simulate all games as they would be played in reality - with real teams |
79
|
|
|
* |
80
|
|
|
* @return Game[] Generated games, or expected play time |
81
|
|
|
* @throws Exception |
82
|
|
|
*/ |
83
|
1 |
|
public function genGamesSimulateReal() : array { |
84
|
1 |
|
return Helpers\Simulator::simulateCategoryReal($this); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritDoc |
89
|
|
|
* @return array |
90
|
|
|
* @throws Exception |
91
|
|
|
*/ |
92
|
|
|
public function jsonSerialize() : array { |
93
|
|
|
return [ |
94
|
|
|
'id' => $this->getId(), |
95
|
|
|
'name' => $this->getName(), |
96
|
|
|
'games' => $this->getGames(), |
97
|
|
|
'teams' => $this->teams->ids(), |
98
|
|
|
'rounds' => $this->queryRounds()->ids(), |
99
|
|
|
'groups' => $this->queryGroups()->ids(), |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|