1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace HeroesofAbenez\Combat; |
||
5 | |||
6 | use Nette\Bridges\ApplicationLatte\LatteFactory; |
||
7 | use Nette\Localization\Translator; |
||
8 | |||
9 | /** |
||
10 | * TextCombatLogRender |
||
11 | * |
||
12 | * @property-write string $template |
||
13 | */ |
||
14 | 1 | final class TextCombatLogRender implements ICombatLogRender { |
|
15 | use \Nette\SmartObject; |
||
16 | |||
17 | private \Latte\Engine $latte; |
||
18 | private string $template = __DIR__ . "/CombatLog.latte"; |
||
19 | |||
20 | 1 | public function __construct(LatteFactory $latteFactory, private Translator $translator) { |
|
21 | 1 | $this->latte = $latteFactory->create(); |
|
22 | 1 | } |
|
23 | |||
24 | /** |
||
25 | * @throws \RuntimeException |
||
26 | */ |
||
27 | protected function setTemplate(string $template): void { |
||
28 | if(!is_file($template)) { |
||
29 | throw new \RuntimeException("File $template does not exist."); |
||
30 | } |
||
31 | $this->template = $template; |
||
32 | } |
||
33 | |||
34 | public function render(array $params): string { |
||
35 | 1 | $params["render"] = $this; |
|
36 | 1 | return $this->latte->renderToString($this->template, $params); |
|
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param CombatLogEntry|string $item |
||
41 | */ |
||
42 | public function renderItem($item): string { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
43 | 1 | if(!$item instanceof CombatLogEntry) { |
|
44 | 1 | return $item; |
|
45 | } |
||
46 | 1 | $character1 = $item->character1->name; |
|
47 | 1 | $character2 = $item->character2->name; |
|
48 | 1 | switch($item->action) { |
|
49 | 1 | case CombatActions\Attack::ACTION_NAME: |
|
50 | 1 | $message = ($item->result) ? "combat.log.attackHits" : "combat.log.attackFails"; |
|
51 | 1 | $text = $this->translator->translate($message, $item->amount, ["character1" => $character1, "character2" => $character2]); |
|
0 ignored issues
–
show
|
|||
52 | 1 | if($item->result && $item->character2->hitpoints < 1) { |
|
53 | $text .= $this->translator->translate("combat.log.characterFalls"); |
||
54 | } |
||
55 | 1 | return $text; |
|
56 | 1 | case CombatActions\SkillAttack::ACTION_NAME: |
|
57 | 1 | $message = ($item->result) ? "combat.log.specialAttackHits" : "combat.log.specialAttackFails"; |
|
58 | 1 | $text = $this->translator->translate($message, $item->amount, ["character1" => $character1, "character2" => $character2, "name" => $item->name]); |
|
0 ignored issues
–
show
|
|||
59 | 1 | if($item->result && $item->character2->hitpoints < 1) { |
|
60 | $text .= $this->translator->translate("combat.log.characterFalls"); |
||
61 | } |
||
62 | 1 | return $text; |
|
63 | 1 | case CombatActions\SkillSpecial::ACTION_NAME: |
|
64 | 1 | $message = ($item->result) ? "combat.log.specialSkillSuccess" : "combat.log.specialSKillFailure"; |
|
65 | 1 | return $this->translator->translate($message, 0, ["character1" => $character1, "character2" => $character2, "name" => $item->name]); |
|
0 ignored issues
–
show
|
|||
66 | 1 | case CombatActions\Heal::ACTION_NAME: |
|
67 | 1 | $message = ($item->result) ? "combat.log.healingSuccess" : "combat.log.healingFailure"; |
|
68 | 1 | return $this->translator->translate($message, $item->amount, ["character1" => $character1, "character2" => $character2]); |
|
0 ignored issues
–
show
|
|||
69 | 1 | case CombatLogEntry::ACTION_POISON: |
|
70 | 1 | return $this->translator->translate("combat.log.poison", $item->amount, ["character1" => $character1]); |
|
71 | } |
||
72 | return ""; |
||
73 | } |
||
74 | } |
||
75 | ?> |