Total Complexity | 23 |
Total Lines | 140 |
Duplicated Lines | 0 % |
Coverage | 81.69% |
Changes | 0 |
1 | <?php |
||
22 | 1 | class CombatLogEntry { |
|
23 | 1 | use \Nette\SmartObject; |
|
24 | |||
25 | public const ACTION_ATTACK = "attack"; |
||
26 | public const ACTION_SKILL_ATTACK = "skill_attack"; |
||
27 | public const ACTION_SKILL_SPECIAL = "skill_special"; |
||
28 | public const ACTION_HEALING = "healing"; |
||
29 | public const ACTION_POISON = "poison"; |
||
30 | |||
31 | /** @var ITranslator */ |
||
32 | protected $translator; |
||
33 | /** @var Character */ |
||
34 | protected $character1; |
||
35 | /** @var Character */ |
||
36 | protected $character2; |
||
37 | /** @var string */ |
||
38 | protected $action; |
||
39 | /** @var string */ |
||
40 | protected $name; |
||
41 | /** @var bool */ |
||
42 | protected $result; |
||
43 | /** @var int */ |
||
44 | protected $amount; |
||
45 | /** @var string */ |
||
46 | protected $message; |
||
47 | |||
48 | public function __construct(ITranslator $translator, array $action) { |
||
49 | 1 | $resolver = new OptionsResolver(); |
|
50 | 1 | $this->configureOptions($resolver); |
|
51 | 1 | $action = $resolver->resolve($action); |
|
52 | 1 | $this->translator = $translator; |
|
53 | 1 | $this->action = $action["action"]; |
|
1 ignored issue
–
show
|
|||
54 | 1 | $this->result = $action["result"]; |
|
1 ignored issue
–
show
|
|||
55 | 1 | $this->amount = $action["amount"]; |
|
1 ignored issue
–
show
|
|||
56 | 1 | $this->character1 = $action["character1"]; |
|
1 ignored issue
–
show
|
|||
57 | 1 | $this->character2 = $action["character2"]; |
|
1 ignored issue
–
show
|
|||
58 | 1 | $this->name = $action["name"]; |
|
1 ignored issue
–
show
|
|||
59 | 1 | $this->parse(); |
|
60 | 1 | } |
|
61 | |||
62 | protected function configureOptions(OptionsResolver $resolver): void { |
||
63 | 1 | $requiredStats = ["action", "result", "character1", "character2",]; |
|
64 | 1 | $resolver->setDefined(["amount", "name",]); |
|
65 | 1 | $resolver->setRequired($requiredStats); |
|
66 | 1 | $resolver->setAllowedTypes("action", "string"); |
|
67 | 1 | $resolver->setAllowedValues("action", function(string $value) { |
|
68 | 1 | return in_array($value, $this->getAllowedActions(), true); |
|
69 | 1 | }); |
|
70 | 1 | $resolver->setAllowedTypes("result", "bool"); |
|
71 | 1 | $resolver->setAllowedTypes("amount", "integer"); |
|
72 | 1 | $resolver->setDefault("amount", 0); |
|
73 | 1 | $resolver->setAllowedTypes("name", "string"); |
|
74 | 1 | $resolver->setDefault("name", ""); |
|
75 | 1 | $resolver->setAllowedTypes("character1", Character::class); |
|
76 | 1 | $resolver->setAllowedTypes("character2", Character::class); |
|
77 | 1 | } |
|
78 | |||
79 | /** |
||
80 | * @return string[] |
||
81 | */ |
||
82 | protected function getAllowedActions(): array { |
||
83 | 1 | return Constants::getConstantsValues(static::class, "ACTION_"); |
|
84 | } |
||
85 | |||
86 | public function getCharacter1(): Character { |
||
88 | } |
||
89 | |||
90 | public function getCharacter2(): Character { |
||
91 | return $this->character2; |
||
92 | } |
||
93 | |||
94 | public function getAction(): string { |
||
95 | return $this->action; |
||
96 | } |
||
97 | |||
98 | public function getName(): string { |
||
99 | return $this->name; |
||
100 | } |
||
101 | |||
102 | public function isResult(): bool { |
||
103 | return $this->result; |
||
104 | } |
||
105 | |||
106 | public function getAmount(): int { |
||
107 | return $this->amount; |
||
108 | } |
||
109 | |||
110 | public function getMessage(): string { |
||
111 | return $this->message; |
||
112 | } |
||
113 | |||
114 | protected function parse(): void { |
||
115 | 1 | $character1 = $this->character1->name; |
|
116 | 1 | $character2 = $this->character2->name; |
|
117 | 1 | $text = ""; |
|
118 | 1 | switch($this->action) { |
|
119 | 1 | case static::ACTION_ATTACK: |
|
120 | 1 | if($this->result) { |
|
121 | 1 | $text = $this->translator->translate("combat.log.attackHits", $this->amount, ["character1" => $character1, "character2" => $character2]); |
|
122 | 1 | if($this->character2->hitpoints < 1) { |
|
123 | 1 | $text .= $this->translator->translate("combat.log.characterFalls"); |
|
124 | } |
||
125 | } else { |
||
126 | 1 | $text = $this->translator->translate("combat.log.attackFails", $this->amount, ["character1" => $character1, "character2" => $character2]); |
|
127 | } |
||
128 | 1 | break; |
|
129 | 1 | case static::ACTION_SKILL_ATTACK: |
|
130 | 1 | if($this->result) { |
|
131 | 1 | $text = $this->translator->translate("combat.log.specialAttackHits", $this->amount, ["character1" => $character1, "character2" => $character2, "name" => $this->name]); |
|
132 | 1 | if($this->character2->hitpoints < 1) { |
|
133 | 1 | $text .= $this->translator->translate("combat.log.characterFalls"); |
|
134 | } |
||
135 | } else { |
||
136 | 1 | $text = $this->translator->translate("combat.log.specialAttackFails", $this->amount, ["character1" => $character1, "character2" => $character2, "name" => $this->name]); |
|
137 | } |
||
138 | 1 | break; |
|
139 | 1 | case static::ACTION_SKILL_SPECIAL: |
|
140 | 1 | if($this->result) { |
|
141 | 1 | $text = $this->translator->translate("combat.log.specialSkillSuccess", 0, ["character1" => $character1, "character2" => $character2, "name" => $this->name]); |
|
142 | } else { |
||
143 | $text = $this->translator->translate("combat.log.specialSKillFailure", 0, ["character1" => $character1, "character2" => $character2, "name" => $this->name]); |
||
144 | } |
||
145 | 1 | break; |
|
146 | 1 | case static::ACTION_HEALING: |
|
147 | if($this->result) { |
||
148 | $text = $this->translator->translate("combat.log.healingSuccess", $this->amount, ["character1" => $character1, "character2" => $character2]); |
||
149 | } else { |
||
150 | $text = $this->translator->translate("combat.log.healingFailure", $this->amount, ["character1" => $character1, "character2" => $character2]); |
||
151 | } |
||
152 | break; |
||
153 | 1 | case static::ACTION_POISON: |
|
154 | 1 | $text = $this->translator->translate("combat.log.poison", $this->amount, ["character1" => $character1]); |
|
155 | 1 | break; |
|
156 | } |
||
157 | 1 | $this->message = $text; |
|
1 ignored issue
–
show
|
|||
158 | 1 | } |
|
159 | |||
160 | public function __toString(): string { |
||
162 | } |
||
163 | } |
||
164 | ?> |