|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace HeroesofAbenez\Combat; |
|
5
|
|
|
|
|
6
|
|
|
use Nette\Localization\ITranslator; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Combat log |
|
10
|
|
|
* |
|
11
|
|
|
* @author Jakub Konečný |
|
12
|
|
|
*/ |
|
13
|
1 |
|
final class CombatLogger implements \Countable, \IteratorAggregate { |
|
14
|
|
|
use \Nette\SmartObject; |
|
15
|
|
|
|
|
16
|
|
|
protected ICombatLogRender $render; |
|
17
|
|
|
protected ITranslator $translator; |
|
18
|
|
|
protected Team $team1; |
|
19
|
|
|
protected Team $team2; |
|
20
|
|
|
protected array $actions = []; |
|
21
|
|
|
public int $round = 0; |
|
22
|
|
|
public string $title = ""; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(ICombatLogRender $render, ITranslator $translator) { |
|
25
|
1 |
|
$this->render = $render; |
|
26
|
1 |
|
$this->translator = $translator; |
|
27
|
1 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Set teams |
|
31
|
|
|
*/ |
|
32
|
|
|
public function setTeams(Team $team1, Team $team2): void { |
|
33
|
1 |
|
if(isset($this->team1)) { |
|
34
|
1 |
|
throw new ImmutableException("Teams has already been set."); |
|
35
|
|
|
} |
|
36
|
1 |
|
$this->team1 = $team1; |
|
37
|
1 |
|
$this->team2 = $team2; |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Adds new entry |
|
42
|
|
|
*/ |
|
43
|
|
|
public function log(array $action): void { |
|
44
|
1 |
|
$this->actions[$this->round][] = new CombatLogEntry($action); |
|
45
|
1 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Adds text entry |
|
49
|
|
|
*/ |
|
50
|
|
|
public function logText(string $text, array $params = []): void { |
|
51
|
1 |
|
$this->actions[$this->round][] = $this->translator->translate($text, 0, $params); |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function __toString(): string { |
|
55
|
|
|
$params = [ |
|
56
|
1 |
|
"team1" => $this->team1, "team2" => $this->team2, "actions" => $this->actions, "title" => $this->title, |
|
57
|
|
|
]; |
|
58
|
1 |
|
return $this->render->render($params); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function count(): int { |
|
62
|
1 |
|
return count($this->actions); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getIterator(): \ArrayIterator { |
|
66
|
1 |
|
return new \ArrayIterator($this->actions); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
?> |