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

Simulator   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 62
rs 10
wmc 19

4 Methods

Rating   Name   Duplication   Size   Complexity  
A simulateTournamentReal() 0 14 5
A simulateRound() 0 6 3
A simulateGroup() 0 15 5
A simulateTournament() 0 18 6
1
<?php
2
3
namespace TournamentGenerator\Utilis;
4
5
/**
6
 *
7
 */
8
class Simulator
9
{
10
11
	public static function simulateGroup(\TournamentGenerator\Group $group, array $filters = [], bool $reset = true) {
12
		foreach ($group->getGames() as $game) {
13
			$teams = $game->getTeams();
14
			$results = [];
15
			foreach ($teams as $team) {
16
				$results[$team->id] = floor(rand(0, 500));
17
			}
18
			$game->setResults($results);
19
		}
20
		$return = $group->sortTeams($filters);
21
		if (!$reset) return $return;
22
		foreach ($group->getGames() as $game) {
23
			$game->resetResults();
24
		}
25
		return $return;
26
	}
27
28
	public static function simulateRound(\TournamentGenerator\Round $round) {
29
		foreach ($round->getGroups() as $group) {
30
			if ($group->isPlayed()) continue;
31
			$group->simulate([], false);
32
		}
33
		return true;
34
	}
35
36
	public static function simulateTournament(\TournamentGenerator\Tournament $tournament) {
37
		if (count($tournament->getCategories()) === 0 && count($tournament->getRounds()) === 0) throw new \Exception('There are no rounds or categories to simulate games from.');
38
39
		$games = [];
40
41
		foreach ($tournament->getCategories() as $category) {
42
			$games = array_merge($games, $category->genGamesSimulate());
43
		}
44
45
		foreach ($tournament->getRounds() as $round) {
46
			$games = array_merge($games, $round->genGames());
47
			$round->simulate()->progress(true);
48
		}
49
		foreach ($tournament->getRounds() as $round) {
50
			$round->resetGames();
51
		}
52
53
		return $games;
54
	}
55
56
	public static function simulateTournamentReal(\TournamentGenerator\Tournament $tournament) {
57
		$games = [];
58
		if (count($tournament->getCategories()) === 0 && count($tournament->getRounds()) === 0) throw new \Exception('There are no rounds or categories to simulate games from.');
59
60
		foreach ($tournament->getCategories() as $category) {
61
			$games = array_merge($games, $category->genGamesSimulate());
62
		}
63
64
		foreach ($tournament->getRounds() as $round) {
65
			$games = array_merge($games, $round->genGames());
66
			$round->simulate();
67
			$round->progress();
68
		}
69
		return $games;
70
	}
71
72
}
73