Total Complexity | 82 |
Total Lines | 527 |
Duplicated Lines | 0 % |
Coverage | 91.81% |
Changes | 0 |
Complex classes like Character often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Character, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
56 | 1 | class Character { |
|
57 | 1 | use \Nette\SmartObject; |
|
58 | |||
59 | public const HITPOINTS_PER_CONSTITUTION = 5; |
||
60 | public const STAT_STRENGTH = "strength"; |
||
61 | public const STAT_DEXTERITY = "dexterity"; |
||
62 | public const STAT_CONSTITUTION = "constitution"; |
||
63 | public const STAT_INTELLIGENCE = "intelligence"; |
||
64 | public const STAT_CHARISMA = "charisma"; |
||
65 | public const STAT_MAX_HITPOINTS = "maxHitpoints"; |
||
66 | public const STAT_DAMAGE = "damage"; |
||
67 | public const STAT_DEFENSE = "defense"; |
||
68 | public const STAT_HIT = "hit"; |
||
69 | public const STAT_DODGE = "dodge"; |
||
70 | public const STAT_INITIATIVE = "initiative"; |
||
71 | public const BASE_STATS = [ |
||
72 | self::STAT_STRENGTH, self::STAT_DEXTERITY, self::STAT_CONSTITUTION, self::STAT_INTELLIGENCE, self::STAT_CHARISMA, |
||
73 | ]; |
||
74 | public const SECONDARY_STATS = [ |
||
75 | self::STAT_MAX_HITPOINTS, self::STAT_DAMAGE, self::STAT_DEFENSE, self::STAT_HIT, self::STAT_DODGE, self::STAT_INITIATIVE, |
||
|
|||
76 | ]; |
||
77 | |||
78 | /** @var int|string */ |
||
79 | protected $id; |
||
80 | /** @var string */ |
||
81 | protected $name; |
||
82 | /** @var string */ |
||
83 | protected $gender = "male"; |
||
84 | /** @var string */ |
||
85 | protected $race; |
||
86 | /** @var string */ |
||
87 | protected $occupation; |
||
88 | /** @var string */ |
||
89 | protected $specialization; |
||
90 | /** @var int */ |
||
91 | protected $level; |
||
92 | /** @var int */ |
||
93 | protected $strength; |
||
94 | /** @var int */ |
||
95 | protected $strengthBase; |
||
96 | /** @var int */ |
||
97 | protected $dexterity; |
||
98 | /** @var int */ |
||
99 | protected $dexterityBase; |
||
100 | /** @var int */ |
||
101 | protected $constitution; |
||
102 | /** @var int */ |
||
103 | protected $constitutionBase; |
||
104 | /** @var int */ |
||
105 | protected $intelligence; |
||
106 | /** @var int */ |
||
107 | protected $intelligenceBase; |
||
108 | /** @var int */ |
||
109 | protected $charisma; |
||
110 | /** @var int */ |
||
111 | protected $charismaBase; |
||
112 | /** @var int */ |
||
113 | protected $maxHitpoints; |
||
114 | /** @var int */ |
||
115 | protected $maxHitpointsBase; |
||
116 | /** @var int */ |
||
117 | protected $hitpoints; |
||
118 | /** @var int */ |
||
119 | protected $damage = 0; |
||
120 | /** @var int */ |
||
121 | protected $damageBase = 0; |
||
122 | /** @var int */ |
||
123 | protected $hit = 0; |
||
124 | /** @var int */ |
||
125 | protected $hitBase = 0; |
||
126 | /** @var int */ |
||
127 | protected $dodge = 0; |
||
128 | /** @var int */ |
||
129 | protected $dodgeBase = 0; |
||
130 | /** @var int */ |
||
131 | protected $initiative = 0; |
||
132 | /** @var int */ |
||
133 | protected $initiativeBase = 0; |
||
134 | /** @var string */ |
||
135 | protected $initiativeFormula; |
||
136 | /** @var IInitiativeFormulaParser */ |
||
137 | protected $initiativeFormulaParser; |
||
138 | /** @var float */ |
||
139 | protected $defense = 0; |
||
140 | /** @var float */ |
||
141 | protected $defenseBase = 0; |
||
142 | /** @var Equipment[]|Collection Character's equipment */ |
||
143 | protected $equipment; |
||
144 | /** @var Pet[]|Collection Character's pets */ |
||
145 | protected $pets; |
||
146 | /** @var BaseCharacterSkill[]|Collection Character's skills */ |
||
147 | protected $skills; |
||
148 | /** @var int|null */ |
||
149 | protected $activePet = null; |
||
150 | /** @var CharacterEffect[]|CharacterEffectsCollection Active effects */ |
||
151 | protected $effects; |
||
152 | /** @var ICharacterEffectsProvider[]|Collection */ |
||
153 | protected $effectProviders; |
||
154 | /** @var bool */ |
||
155 | protected $stunned = false; |
||
156 | /** @var int */ |
||
157 | protected $positionRow = 0; |
||
158 | /** @var int */ |
||
159 | protected $positionColumn = 0; |
||
160 | |||
161 | /** |
||
162 | * |
||
163 | * @param array $stats Stats of the character |
||
164 | * @param Equipment[] $equipment Equipment of the character |
||
165 | * @param Pet[] $pets Pets owned by the character |
||
166 | * @param BaseCharacterSkill[] $skills Skills acquired by the character |
||
167 | */ |
||
168 | public function __construct(array $stats, array $equipment = [], array $pets = [], array $skills = [], IInitiativeFormulaParser $initiativeFormulaParser = null) { |
||
169 | 1 | $this->initiativeFormulaParser = $initiativeFormulaParser ?? new InitiativeFormulaParser(); |
|
170 | 1 | $this->effectProviders = new class extends Collection { |
|
171 | /** @var string */ |
||
172 | protected $class = ICharacterEffectsProvider::class; |
||
173 | }; |
||
174 | 1 | $this->equipment = new class extends Collection { |
|
175 | /** @var string */ |
||
176 | protected $class = Equipment::class; |
||
177 | }; |
||
178 | 1 | foreach($equipment as $eq) { |
|
179 | 1 | $this->equipment[] = $this->effectProviders[] = $eq; |
|
180 | } |
||
181 | 1 | $this->pets = new class extends Collection { |
|
182 | /** @var string */ |
||
183 | protected $class = Pet::class; |
||
184 | }; |
||
185 | 1 | foreach($pets as $pet) { |
|
186 | 1 | $this->pets[] = $this->effectProviders[] = $pet; |
|
187 | } |
||
188 | 1 | $this->skills = new class extends Collection { |
|
189 | /** @var string */ |
||
190 | protected $class = BaseCharacterSkill::class; |
||
191 | }; |
||
192 | 1 | foreach($skills as $skill) { |
|
193 | 1 | $this->skills[] = $skill; |
|
194 | } |
||
195 | 1 | $this->equipment->lock(); |
|
196 | 1 | $this->pets->lock(); |
|
197 | 1 | $this->skills->lock(); |
|
198 | 1 | $this->setStats($stats); |
|
199 | 1 | $this->effects = new CharacterEffectsCollection($this); |
|
200 | 1 | } |
|
201 | |||
202 | protected function setStats(array $stats): void { |
||
203 | 1 | $requiredStats = array_merge(["id", "name", "level", "initiativeFormula",], static::BASE_STATS); |
|
204 | 1 | $allStats = array_merge($requiredStats, ["occupation", "race", "specialization", "gender",]); |
|
205 | 1 | $numberStats = static::BASE_STATS; |
|
206 | 1 | $textStats = ["name", "race", "occupation", "initiativeFormula",]; |
|
207 | 1 | $resolver = new OptionsResolver(); |
|
208 | 1 | $resolver->setDefined($allStats); |
|
209 | 1 | $resolver->setAllowedTypes("id", ["integer", "string"]); |
|
210 | 1 | foreach($numberStats as $stat) { |
|
211 | 1 | $resolver->setAllowedTypes($stat, ["integer", "float"]); |
|
212 | 1 | $resolver->setNormalizer($stat, function(OptionsResolver $resolver, $value) { |
|
1 ignored issue
–
show
|
|||
213 | 1 | return (int) $value; |
|
214 | 1 | }); |
|
215 | } |
||
216 | 1 | foreach($textStats as $stat) { |
|
217 | 1 | $resolver->setNormalizer($stat, function(OptionsResolver $resolver, $value) { |
|
1 ignored issue
–
show
|
|||
218 | 1 | return (string) $value; |
|
219 | 1 | }); |
|
220 | } |
||
221 | 1 | $resolver->setRequired($requiredStats); |
|
222 | 1 | $stats = array_filter($stats, function($key) use($allStats) { |
|
223 | 1 | return in_array($key, $allStats, true); |
|
224 | 1 | }, ARRAY_FILTER_USE_KEY); |
|
225 | 1 | $stats = $resolver->resolve($stats); |
|
226 | 1 | foreach($stats as $key => $value) { |
|
227 | 1 | if(in_array($key, $numberStats, true)) { |
|
228 | 1 | $this->$key = $value; |
|
229 | 1 | $this->{$key . "Base"} = $value; |
|
230 | } else { |
||
231 | 1 | $this->$key = $value; |
|
232 | } |
||
233 | } |
||
234 | 1 | $this->hitpoints = $this->maxHitpoints = $this->maxHitpointsBase = $this->constitution * static::HITPOINTS_PER_CONSTITUTION; |
|
235 | 1 | $this->recalculateSecondaryStats(); |
|
236 | 1 | $this->hitBase = $this->hit; |
|
237 | 1 | $this->dodgeBase = $this->dodge; |
|
238 | 1 | } |
|
239 | |||
240 | /** |
||
241 | * @return int|string |
||
242 | */ |
||
243 | public function getId() { |
||
244 | 1 | return $this->id; |
|
245 | } |
||
246 | |||
247 | public function getName(): string { |
||
248 | 1 | return $this->name; |
|
249 | } |
||
250 | |||
251 | public function getGender(): string { |
||
252 | return $this->gender; |
||
253 | } |
||
254 | |||
255 | public function getRace(): string { |
||
256 | return $this->race; |
||
257 | } |
||
258 | |||
259 | public function getOccupation(): string { |
||
260 | return $this->occupation; |
||
261 | } |
||
262 | |||
263 | public function getLevel(): int { |
||
264 | 1 | return $this->level; |
|
265 | } |
||
266 | |||
267 | public function getStrength(): int { |
||
268 | 1 | return $this->strength; |
|
269 | } |
||
270 | |||
271 | public function getStrengthBase(): int { |
||
272 | return $this->strengthBase; |
||
273 | } |
||
274 | |||
275 | public function getDexterity(): int { |
||
276 | 1 | return $this->dexterity; |
|
277 | } |
||
278 | |||
279 | public function getDexterityBase(): int { |
||
280 | return $this->dexterityBase; |
||
281 | } |
||
282 | |||
283 | public function getConstitution(): int { |
||
284 | 1 | return $this->constitution; |
|
285 | } |
||
286 | |||
287 | public function getConstitutionBase(): int { |
||
288 | return $this->constitutionBase; |
||
289 | } |
||
290 | |||
291 | public function getCharisma(): int { |
||
292 | 1 | return $this->charisma; |
|
293 | } |
||
294 | |||
295 | public function getCharismaBase(): int { |
||
296 | return $this->charismaBase; |
||
297 | } |
||
298 | |||
299 | public function getMaxHitpoints(): int { |
||
300 | 1 | return $this->maxHitpoints; |
|
301 | } |
||
302 | |||
303 | public function getMaxHitpointsBase(): int { |
||
304 | 1 | return $this->maxHitpointsBase; |
|
305 | } |
||
306 | |||
307 | public function getHitpoints(): int { |
||
308 | 1 | return $this->hitpoints; |
|
309 | } |
||
310 | |||
311 | public function getDamage(): int { |
||
312 | 1 | return $this->damage; |
|
313 | } |
||
314 | |||
315 | public function getDamageBase(): int { |
||
316 | return $this->damageBase; |
||
317 | } |
||
318 | |||
319 | public function getHit(): int { |
||
320 | 1 | return $this->hit; |
|
321 | } |
||
322 | |||
323 | public function getHitBase(): int { |
||
324 | return $this->hitBase; |
||
325 | } |
||
326 | |||
327 | public function getDodge(): int { |
||
328 | 1 | return $this->dodge; |
|
329 | } |
||
330 | |||
331 | public function getDodgeBase(): int { |
||
332 | return $this->dodgeBase; |
||
333 | } |
||
334 | |||
335 | public function getInitiative(): int { |
||
336 | 1 | return $this->initiative; |
|
337 | } |
||
338 | |||
339 | public function getInitiativeBase(): int { |
||
340 | 1 | return $this->initiativeBase; |
|
341 | } |
||
342 | |||
343 | public function getInitiativeFormula(): string { |
||
344 | 1 | return $this->initiativeFormula; |
|
345 | } |
||
346 | |||
347 | public function getDefense(): int { |
||
348 | 1 | return (int) $this->defense; |
|
349 | } |
||
350 | |||
351 | public function getDefenseBase(): int { |
||
352 | return (int) $this->defenseBase; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @return Equipment[]|Collection |
||
357 | */ |
||
358 | public function getEquipment(): Collection { |
||
359 | 1 | return $this->equipment; |
|
360 | } |
||
361 | |||
362 | /** |
||
363 | * @return Pet[]|Collection |
||
364 | */ |
||
365 | public function getPets(): Collection { |
||
366 | return $this->pets; |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * @return BaseCharacterSkill[]|Collection |
||
371 | */ |
||
372 | public function getSkills(): Collection { |
||
374 | } |
||
375 | |||
376 | public function getActivePet(): ?int { |
||
377 | /** @var Pet|null $pet */ |
||
378 | 1 | $pet = $this->pets->getItem(["deployed" => true]); |
|
379 | 1 | if(is_null($pet)) { |
|
380 | 1 | return null; |
|
381 | } |
||
382 | 1 | return $pet->id; |
|
383 | } |
||
384 | |||
385 | public function getEffects(): CharacterEffectsCollection { |
||
386 | 1 | return $this->effects; |
|
387 | } |
||
388 | |||
389 | /** |
||
390 | * @return ICharacterEffectsProvider[]|Collection |
||
391 | */ |
||
392 | public function getEffectProviders(): Collection { |
||
393 | 1 | return $this->effectProviders; |
|
394 | } |
||
395 | |||
396 | public function isStunned(): bool { |
||
397 | 1 | return $this->stunned; |
|
398 | } |
||
399 | |||
400 | public function getSpecialization(): string { |
||
401 | return $this->specialization; |
||
402 | } |
||
403 | |||
404 | public function getIntelligence(): int { |
||
405 | 1 | return $this->intelligence; |
|
406 | } |
||
407 | |||
408 | public function getIntelligenceBase(): int { |
||
409 | return $this->intelligenceBase; |
||
410 | } |
||
411 | |||
412 | public function getInitiativeFormulaParser(): IInitiativeFormulaParser { |
||
413 | 1 | return $this->initiativeFormulaParser; |
|
414 | } |
||
415 | |||
416 | public function setInitiativeFormulaParser(IInitiativeFormulaParser $initiativeFormulaParser): void { |
||
417 | 1 | $oldParser = $this->initiativeFormulaParser; |
|
418 | 1 | $this->initiativeFormulaParser = $initiativeFormulaParser; |
|
419 | 1 | if($oldParser !== $initiativeFormulaParser) { |
|
420 | 1 | $this->recalculateStats(); |
|
421 | } |
||
422 | 1 | } |
|
423 | |||
424 | public function getPositionRow(): int { |
||
425 | 1 | return $this->positionRow; |
|
426 | } |
||
427 | |||
428 | public function setPositionRow(int $positionRow): void { |
||
429 | 1 | $this->positionRow = Numbers::range($positionRow, 1, PHP_INT_MAX); |
|
430 | 1 | } |
|
431 | |||
432 | public function getPositionColumn(): int { |
||
433 | 1 | return $this->positionColumn; |
|
434 | } |
||
435 | |||
436 | public function setPositionColumn(int $positionColumn): void { |
||
438 | 1 | } |
|
439 | |||
440 | /** |
||
441 | * @internal |
||
442 | */ |
||
443 | public function applyEffectProviders(): void { |
||
444 | 1 | foreach($this->effectProviders as $item) { |
|
445 | 1 | $effects = $item->getCombatEffects(); |
|
446 | 1 | array_walk($effects, function(CharacterEffect $effect) { |
|
447 | 1 | $this->effects->removeByFilter(["id" => $effect->id]); |
|
448 | 1 | $this->effects[] = $effect; |
|
449 | 1 | }); |
|
450 | } |
||
451 | 1 | } |
|
452 | |||
453 | /** |
||
454 | * Get specified equipment of the character |
||
455 | * |
||
456 | * @throws \OutOfBoundsException |
||
457 | */ |
||
458 | public function getItem(int $itemId): Equipment { |
||
459 | /** @var Equipment|null $equipment */ |
||
460 | 1 | $equipment = $this->equipment->getItem(["id" => $itemId]); |
|
461 | 1 | if(is_null($equipment)) { |
|
462 | 1 | throw new \OutOfBoundsException("Item was not found."); |
|
463 | } |
||
464 | 1 | return $equipment; |
|
465 | } |
||
466 | |||
467 | /** |
||
468 | * Get specified pet |
||
469 | * |
||
470 | * @throws \OutOfBoundsException |
||
471 | */ |
||
472 | public function getPet(int $petId): Pet { |
||
473 | /** @var Pet|null $pet */ |
||
474 | 1 | $pet = $this->pets->getItem(["id" => $petId]); |
|
475 | 1 | if(is_null($pet)) { |
|
476 | 1 | throw new \OutOfBoundsException("Pet was not found."); |
|
477 | } |
||
478 | 1 | return $pet; |
|
479 | } |
||
480 | |||
481 | /** |
||
482 | * @return BaseCharacterSkill[] |
||
483 | */ |
||
484 | public function getUsableSkills(): array { |
||
485 | 1 | return $this->skills->getItems(["usable" => true]); |
|
486 | } |
||
487 | |||
488 | /** |
||
489 | * Harm the character |
||
490 | */ |
||
491 | public function harm(int $amount): void { |
||
493 | 1 | } |
|
494 | |||
495 | /** |
||
496 | * Heal the character |
||
497 | */ |
||
498 | public function heal(int $amount): void { |
||
499 | 1 | $this->hitpoints += Numbers::range($amount, 0, $this->maxHitpoints - $this->hitpoints); |
|
500 | 1 | } |
|
501 | |||
502 | /** |
||
503 | * Determine which (primary) stat should be used to calculate damage |
||
504 | */ |
||
505 | public function damageStat(): string { |
||
506 | /** @var Weapon|null $item */ |
||
507 | 1 | $item = $this->equipment->getItem(["%class%" => Weapon::class, "worn" => true, ]); |
|
508 | 1 | if(is_null($item)) { |
|
509 | 1 | return static::STAT_STRENGTH; |
|
510 | } |
||
511 | 1 | return $item->damageStat; |
|
512 | } |
||
513 | |||
514 | /** |
||
515 | * Recalculate secondary stats from the the primary ones |
||
516 | */ |
||
517 | public function recalculateSecondaryStats(): void { |
||
518 | $stats = [ |
||
519 | 1 | static::STAT_DAMAGE => $this->damageStat(), static::STAT_HIT => static::STAT_DEXTERITY, |
|
520 | 1 | static::STAT_DODGE => static::STAT_DEXTERITY, static::STAT_MAX_HITPOINTS => static::STAT_CONSTITUTION, |
|
521 | 1 | static::STAT_INITIATIVE => "", |
|
522 | ]; |
||
523 | 1 | foreach($stats as $secondary => $primary) { |
|
524 | 1 | $gain = $this->$secondary - $this->{$secondary . "Base"}; |
|
525 | 1 | if($secondary === static::STAT_DAMAGE) { |
|
526 | 1 | $base = (int) round($this->$primary / 2); |
|
527 | 1 | } elseif($secondary === static::STAT_MAX_HITPOINTS) { |
|
528 | 1 | $base = $this->$primary * static::HITPOINTS_PER_CONSTITUTION; |
|
529 | 1 | } elseif($secondary === static::STAT_INITIATIVE) { |
|
530 | 1 | $base = $this->initiativeFormulaParser->calculateInitiative($this); |
|
531 | } else { |
||
532 | 1 | $base = $this->$primary * 3; |
|
533 | } |
||
534 | 1 | $this->{$secondary . "Base"} = $base; |
|
535 | 1 | $this->$secondary = $base + $gain; |
|
536 | } |
||
537 | 1 | } |
|
538 | |||
539 | /** |
||
540 | * Recalculates stats of the character (mostly used during combat) |
||
541 | */ |
||
542 | public function recalculateStats(): void { |
||
543 | 1 | $stats = array_merge(static::BASE_STATS, static::SECONDARY_STATS); |
|
544 | 1 | $stunned = false; |
|
545 | 1 | $debuffs = []; |
|
546 | 1 | foreach($stats as $stat) { |
|
547 | 1 | $$stat = $this->{$stat . "Base"}; |
|
548 | 1 | $debuffs[$stat] = 0; |
|
549 | } |
||
550 | 1 | $this->effects->removeByFilter(["duration<" => 1]); |
|
551 | 1 | foreach($this->effects as $i => $effect) { |
|
552 | 1 | $stat = $effect->stat; |
|
553 | 1 | $type = $effect->type; |
|
554 | 1 | if(!in_array($type, SkillSpecial::NO_STAT_TYPES, true)) { |
|
555 | 1 | $bonus_value = ($effect->valueAbsolute) ? $effect->value : $$stat / 100 * $effect->value; |
|
556 | } |
||
557 | 1 | if($type == SkillSpecial::TYPE_BUFF) { |
|
558 | 1 | $$stat += $bonus_value; |
|
559 | 1 | } elseif($type == SkillSpecial::TYPE_DEBUFF) { |
|
560 | 1 | $debuffs[$stat] += $bonus_value; |
|
561 | 1 | } elseif($type == SkillSpecial::TYPE_STUN) { |
|
562 | 1 | $stunned = true; |
|
563 | } |
||
564 | 1 | unset($stat, $type, $bonus_value); |
|
565 | } |
||
566 | 1 | foreach($debuffs as $stat => $value) { |
|
567 | 1 | $value = min($value, 80); |
|
568 | 1 | $bonus_value = $$stat / 100 * $value; |
|
569 | 1 | $$stat -= $bonus_value; |
|
570 | } |
||
571 | 1 | foreach($stats as $stat) { |
|
572 | 1 | $this->$stat = (int) round($$stat); |
|
573 | } |
||
574 | 1 | $this->recalculateSecondaryStats(); |
|
575 | 1 | $this->stunned = $stunned; |
|
576 | 1 | } |
|
577 | |||
578 | /** |
||
579 | * Reset character's initiative |
||
580 | */ |
||
581 | public function resetInitiative(): void { |
||
583 | 1 | } |
|
584 | } |
||
585 | ?> |