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