Passed
Push — master ( d0ce80...210dba )
by Jakub
06:27
created

TextCombatLogRender::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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
use HeroesofAbenez\Combat\CombatActions;
9
10
/**
11
 * TextCombatLogRender
12
 *
13
 * @property-write string $template
14
 */
15 1
final class TextCombatLogRender implements ICombatLogRender {
16
  use \Nette\SmartObject;
17
18
  protected \Latte\Engine $latte;
19
  protected ITranslator $translator;
20
  protected string $template = __DIR__ . "/CombatLog.latte";
21
22
  public function __construct(ILatteFactory $latteFactory, ITranslator $translator) {
23 1
    $this->latte = $latteFactory->create();
24 1
    $this->translator = $translator;
25 1
  }
26
27
  /**
28
   * @throws \RuntimeException
29
   */
30
  protected function setTemplate(string $template): void {
31
    if(!is_file($template)) {
32
      throw new \RuntimeException("File $template does not exist.");
33
    }
34
    $this->template = $template;
35
  }
36
37
  public function render(array $params): string {
38 1
    $params["render"] = $this;
39 1
    return $this->latte->renderToString($this->template, $params);
40
  }
41
42
  /**
43
   * @param CombatLogEntry|string $item
44
   */
45
  public function renderItem($item): string {
46 1
    if(!$item instanceof CombatLogEntry) {
47 1
      return $item;
48
    }
49 1
    $character1 = $item->character1->name;
50 1
    $character2 = $item->character2->name;
51 1
    switch($item->action) {
52 1
      case CombatActions\Attack::ACTION_NAME:
53 1
        $message = ($item->result) ? "combat.log.attackHits" : "combat.log.attackFails";
54 1
        $text = $this->translator->translate($message, $item->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 130 characters
Loading history...
55 1
        if($item->result && $item->character2->hitpoints < 1) {
56
          $text .= $this->translator->translate("combat.log.characterFalls");
57
        }
58 1
        return $text;
59 1
      case CombatActions\SkillAttack::ACTION_NAME:
60 1
        $message = ($item->result) ? "combat.log.specialAttackHits" : "combat.log.specialAttackFails";
61 1
        $text = $this->translator->translate($message, $item->amount, ["character1" => $character1, "character2" => $character2, "name" => $item->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 153 characters
Loading history...
62 1
        if($item->result && $item->character2->hitpoints < 1) {
63
          $text .= $this->translator->translate("combat.log.characterFalls");
64
        }
65 1
        return $text;
66 1
      case CombatActions\SkillSpecial::ACTION_NAME:
67 1
        $message = ($item->result) ? "combat.log.specialSkillSuccess" : "combat.log.specialSKillFailure";
68 1
        return $this->translator->translate($message, 0, ["character1" => $character1, "character2" => $character2, "name" => $item->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 140 characters
Loading history...
69 1
      case CombatActions\Heal::ACTION_NAME:
70 1
        $message = ($item->result) ? "combat.log.healingSuccess" : "combat.log.healingFailure";
71 1
        return $this->translator->translate($message, $item->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 129 characters
Loading history...
72 1
      case CombatLogEntry::ACTION_POISON:
73 1
        return $this->translator->translate("combat.log.poison", $item->amount, ["character1" => $character1]);
74
    }
75
    return "";
76
  }
77
}
78
?>