Passed
Push — master ( 7fb37f...0dc17b )
by Jakub
01:40
created

Character.php$3 ➔ getOccupation()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nexendrie\Utils\Numbers;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
use Nexendrie\Utils\Collection;
9
10
/**
11
 * Structure for single character
12
 *
13
 * @author Jakub Konečný
14
 * @property-read int|string $id
15
 * @property-read string $name
16
 * @property-read string $gender
17
 * @property-read string $race
18
 * @property-read string $occupation
19
 * @property-read int $level
20
 * @property-read int $experience
21
 * @property-read int $strength
22
 * @property-read int $strengthBase
23
 * @property-read int $dexterity
24
 * @property-read int $dexterityBase
25
 * @property-read int $constitution
26
 * @property-read int $constitutionBase
27
 * @property-read int $intelligence
28
 * @property-read int $intelligenceBase
29
 * @property-read int $charisma
30
 * @property-read int $charismaBase
31
 * @property-read int $maxHitpoints
32
 * @property-read int $maxHitpointsBase
33
 * @property-read int $hitpoints
34
 * @property-read int $damage
35
 * @property-read int $damageBase
36
 * @property-read int $hit
37
 * @property-read int $hitBase
38
 * @property-read int $dodge
39
 * @property-read int $dodgeBase
40
 * @property-read int $initiative
41
 * @property-read int $initiativeBase
42
 * @property-read string $initiativeFormula
43
 * @property-read int $defense
44
 * @property-read int $defenseBase
45
 * @property Equipment[]|Collection $equipment
46
 * @property Pet[]|Collection $pets
47
 * @property BaseCharacterSkill[]|Collection $skills
48
 * @property-read int|null $activePet
49
 * @property-read CharacterEffect[] $effects
50
 * @property ICharacterEffectsProvider[]|Collection $effectProviders
51
 * @property-read bool $stunned
52
 * @property-read BaseCharacterSkill[] $usableSkills
53
 * @property IInitiativeFormulaParser $initiativeFormulaParser
54
 * @property int $positionRow
55
 * @property int $positionColumn
56
 */
57 1
class Character {
58 1
  use \Nette\SmartObject;
59
  
60
  public const HITPOINTS_PER_CONSTITUTION = 5;
61
  public const STAT_STRENGTH = "strength";
62
  public const STAT_DEXTERITY = "dexterity";
63
  public const STAT_CONSTITUTION = "constitution";
64
  public const STAT_INTELLIGENCE = "intelligence";
65
  public const STAT_CHARISMA = "charisma";
66
  public const STAT_MAX_HITPOINTS = "maxHitpoints";
67
  public const STAT_DAMAGE = "damage";
68
  public const STAT_DEFENSE = "defense";
69
  public const STAT_HIT = "hit";
70
  public const STAT_DODGE = "dodge";
71
  public const STAT_INITIATIVE = "initiative";
72
  public const BASE_STATS = [
73
    self::STAT_STRENGTH, self::STAT_DEXTERITY, self::STAT_CONSTITUTION, self::STAT_INTELLIGENCE, self::STAT_CHARISMA,
74
  ];
75
  public const SECONDARY_STATS = [
76
    self::STAT_MAX_HITPOINTS, self::STAT_DAMAGE, self::STAT_DEFENSE, self::STAT_HIT, self::STAT_DODGE, self::STAT_INITIATIVE,
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 125 characters
Loading history...
77
  ];
78
  
79
  /** @var int|string */
80
  protected $id;
81
  /** @var string */
82
  protected $name;
83
  /** @var string */
84
  protected $gender = "male";
85
  /** @var string */
86
  protected $race;
87
  /** @var string */
88
  protected $occupation;
89
  /** @var string */
90
  protected $specialization;
91
  /** @var int */
92
  protected $level;
93
  /** @var int */
94
  protected $experience = 0;
95
  /** @var int */
96
  protected $strength;
97
  /** @var int */
98
  protected $strengthBase;
99
  /** @var int */
100
  protected $dexterity;
101
  /** @var int */
102
  protected $dexterityBase;
103
  /** @var int */
104
  protected $constitution;
105
  /** @var int */
106
  protected $constitutionBase;
107
  /** @var int */
108
  protected $intelligence;
109
  /** @var int */
110
  protected $intelligenceBase;
111
  /** @var int */
112
  protected $charisma;
113
  /** @var int */
114
  protected $charismaBase;
115
  /** @var int */
116
  protected $maxHitpoints;
117
  /** @var int */
118
  protected $maxHitpointsBase;
119
  /** @var int */
120
  protected $hitpoints;
121
  /** @var int */
122
  protected $damage = 0;
123
  /** @var int */
124
  protected $damageBase = 0;
125
  /** @var int */
126
  protected $hit = 0;
127
  /** @var int */
128
  protected $hitBase = 0;
129
  /** @var int */
130
  protected $dodge = 0;
131
  /** @var int */
132
  protected $dodgeBase = 0;
133
  /** @var int */
134
  protected $initiative = 0;
135
  /** @var int */
136
  protected $initiativeBase = 0;
137
  /** @var string */
138
  protected $initiativeFormula;
139
  /** @var IInitiativeFormulaParser */
140
  protected $initiativeFormulaParser;
141
  /** @var float */
142
  protected $defense = 0;
143
  /** @var float */
144
  protected $defenseBase = 0;
145
  /** @var Equipment[]|Collection Character's equipment */
146
  protected $equipment;
147
  /** @var Pet[]|Collection Character's pets */
148
  protected $pets;
149
  /** @var BaseCharacterSkill[]|Collection Character's skills */
150
  protected $skills;
151
  /** @var int|null */
152
  protected $activePet = null;
153
  /** @var CharacterEffect[] Active effects */
154
  protected $effects = [];
155
  /** @var ICharacterEffectsProvider[]|Collection */
156
  protected $effectProviders;
157
  /** @var bool */
158
  protected $stunned = false;
159
  /** @var int */
160
  protected $positionRow = 0;
161
  /** @var int */
162
  protected $positionColumn = 0;
163
  
164
  /**
165
   *
166
   * @param array $stats Stats of the character
167
   * @param Equipment[] $equipment Equipment of the character
168
   * @param Pet[] $pets Pets owned by the character
169
   * @param BaseCharacterSkill[] $skills Skills acquired by the character
170
   */
171
  public function __construct(array $stats, array $equipment = [], array $pets = [], array $skills = [], IInitiativeFormulaParser $initiativeFormulaParser = null) {
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 164 characters
Loading history...
172 1
    $this->initiativeFormulaParser = $initiativeFormulaParser ?? new InitiativeFormulaParser();
173 1
    $this->effectProviders = new class extends  Collection {
174
      /** @var string */
175
      protected $class = ICharacterEffectsProvider::class;
176
    };
177 1
    $this->equipment = new class extends Collection {
178
      /** @var string */
179
      protected $class = Equipment::class;
180
    };
181 1
    foreach($equipment as $eq) {
182 1
      $this->equipment[] = $this->effectProviders[] = $eq;
183
    }
184 1
    $this->pets = new class extends Collection {
185
      /** @var string */
186
      protected $class = Pet::class;
187
    };
188 1
    foreach($pets as $pet) {
189 1
      $this->pets[] = $this->effectProviders[] = $pet;
190
    }
191 1
    $this->skills = new class extends Collection {
192
      /** @var string */
193
      protected $class = BaseCharacterSkill::class;
194
    };
195 1
    foreach($skills as $skill) {
196 1
      $this->skills[] = $skill;
197
    }
198 1
    $this->equipment->lock();
199 1
    $this->pets->lock();
200 1
    $this->skills->lock();
201 1
    $this->setStats($stats);
202 1
  }
203
  
204
  protected function setStats(array $stats): void {
205 1
    $requiredStats = array_merge(["id", "name", "level", "initiativeFormula",], static::BASE_STATS);
206 1
    $allStats = array_merge($requiredStats, ["occupation", "race", "specialization", "gender", "experience",]);
207 1
    $numberStats = static::BASE_STATS;
208 1
    $textStats = ["name", "race", "occupation", "initiativeFormula",];
209 1
    $resolver = new OptionsResolver();
210 1
    $resolver->setDefined($allStats);
211 1
    $resolver->setAllowedTypes("id", ["integer", "string"]);
212 1
    $resolver->setAllowedTypes("experience", "integer");
213 1
    foreach($numberStats as $stat) {
214 1
      $resolver->setAllowedTypes($stat, ["integer", "float"]);
215 1
      $resolver->setNormalizer($stat, function(OptionsResolver $resolver, $value) {
1 ignored issue
show
introduced by
The method parameter $resolver is never used
Loading history...
216 1
        return (int) $value;
217 1
      });
218
    }
219 1
    foreach($textStats as $stat) {
220 1
      $resolver->setNormalizer($stat, function(OptionsResolver $resolver, $value) {
1 ignored issue
show
introduced by
The method parameter $resolver is never used
Loading history...
221 1
        return (string) $value;
222 1
      });
223
    }
224 1
    $resolver->setRequired($requiredStats);
225 1
    $stats = array_filter($stats, function($key) use($allStats) {
226 1
      return in_array($key, $allStats, true);
227 1
    }, ARRAY_FILTER_USE_KEY);
228 1
    $stats = $resolver->resolve($stats);
229 1
    foreach($stats as $key => $value) {
230 1
      if(in_array($key, $numberStats, true)) {
231 1
        $this->$key = $value;
232 1
        $this->{$key . "Base"} = $value;
233
      } else {
234 1
        $this->$key = $value;
235
      }
236
    }
237 1
    $this->hitpoints = $this->maxHitpoints = $this->maxHitpointsBase = $this->constitution * static::HITPOINTS_PER_CONSTITUTION;
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 128 characters
Loading history...
238 1
    $this->recalculateSecondaryStats();
239 1
    $this->hitBase = $this->hit;
240 1
    $this->dodgeBase = $this->dodge;
241 1
  }
242
  
243
  /**
244
   * @return int|string
245
   */
246
  public function getId() {
247 1
    return $this->id;
248
  }
249
  
250
  public function getName(): string {
251 1
    return $this->name;
252
  }
253
  
254
  public function getGender(): string {
255
    return $this->gender;
256
  }
257
  
258
  public function getRace(): string {
259
    return $this->race;
260
  }
261
  
262
  public function getOccupation(): string {
263
    return $this->occupation;
264
  }
265
  
266
  public function getLevel(): int {
267 1
    return $this->level;
268
  }
269
  
270
  public function getExperience(): int {
271
    return $this->experience;
272
  }
273
  
274
  public function getStrength(): int {
275 1
    return $this->strength;
276
  }
277
  
278
  public function getStrengthBase(): int {
279
    return $this->strengthBase;
280
  }
281
  
282
  public function getDexterity(): int {
283 1
    return $this->dexterity;
284
  }
285
  
286
  public function getDexterityBase(): int {
287
    return $this->dexterityBase;
288
  }
289
  
290
  public function getConstitution(): int {
291 1
    return $this->constitution;
292
  }
293
  
294
  public function getConstitutionBase(): int {
295
    return $this->constitutionBase;
296
  }
297
  
298
  public function getCharisma(): int {
299 1
    return $this->charisma;
300
  }
301
  
302
  public function getCharismaBase(): int {
303
    return $this->charismaBase;
304
  }
305
  
306
  public function getMaxHitpoints(): int {
307 1
    return $this->maxHitpoints;
308
  }
309
  
310
  public function getMaxHitpointsBase(): int {
311 1
    return $this->maxHitpointsBase;
312
  }
313
  
314
  public function getHitpoints(): int {
315 1
    return $this->hitpoints;
316
  }
317
  
318
  public function getDamage(): int {
319 1
    return $this->damage;
320
  }
321
  
322
  public function getDamageBase(): int {
323
    return $this->damageBase;
324
  }
325
  
326
  public function getHit(): int {
327 1
    return $this->hit;
328
  }
329
  
330
  public function getHitBase(): int {
331
    return $this->hitBase;
332
  }
333
  
334
  public function getDodge(): int {
335 1
    return $this->dodge;
336
  }
337
  
338
  public function getDodgeBase(): int {
339
    return $this->dodgeBase;
340
  }
341
  
342
  public function getInitiative(): int {
343 1
    return $this->initiative;
344
  }
345
  
346
  public function getInitiativeBase(): int {
347 1
    return $this->initiativeBase;
348
  }
349
  
350
  public function getInitiativeFormula(): string {
351 1
    return $this->initiativeFormula;
352
  }
353
  
354
  public function getDefense(): int {
355 1
    return (int) $this->defense;
356
  }
357
  
358
  public function getDefenseBase(): int {
359
    return (int) $this->defenseBase;
360
  }
361
  
362
  /**
363
   * @return Equipment[]|Collection
364
   */
365
  public function getEquipment(): Collection {
366 1
    return $this->equipment;
367
  }
368
  
369
  /**
370
   * @return Pet[]|Collection
371
   */
372
  public function getPets(): Collection {
373
    return $this->pets;
374
  }
375
  
376
  /**
377
   * @return BaseCharacterSkill[]|Collection
378
   */
379
  public function getSkills(): Collection {
380 1
    return $this->skills;
381
  }
382
  
383
  public function getActivePet(): ?int {
384
    /** @var Pet[] $pets */
385 1
    $pets = $this->pets->getItems(["deployed" => true]);
386 1
    if(count($pets) === 0) {
387 1
      return null;
388
    }
389 1
    return $pets[0]->id;
390
  }
391
  
392
  /**
393
   * @return CharacterEffect[]
394
   */
395
  public function getEffects(): array {
396 1
    return $this->effects;
397
  }
398
  
399
  /**
400
   * @return ICharacterEffectsProvider[]|Collection
401
   */
402
  public function getEffectProviders(): Collection {
403 1
    return $this->effectProviders;
404
  }
405
  
406
  public function isStunned(): bool {
407 1
    return $this->stunned;
408
  }
409
  
410
  public function getSpecialization(): string {
411
    return $this->specialization;
412
  }
413
  
414
  public function getIntelligence(): int {
415 1
    return $this->intelligence;
416
  }
417
  
418
  public function getIntelligenceBase(): int {
419
    return $this->intelligenceBase;
420
  }
421
  
422
  public function getInitiativeFormulaParser(): IInitiativeFormulaParser {
423 1
    return $this->initiativeFormulaParser;
424
  }
425
  
426
  public function setInitiativeFormulaParser(IInitiativeFormulaParser $initiativeFormulaParser): void {
427 1
    $oldParser = $this->initiativeFormulaParser;
428 1
    $this->initiativeFormulaParser = $initiativeFormulaParser;
429 1
    if($oldParser !== $initiativeFormulaParser) {
430 1
      $this->recalculateStats();
431
    }
432 1
  }
433
  
434
  public function getPositionRow(): int {
435 1
    return $this->positionRow;
436
  }
437
  
438
  public function setPositionRow(int $positionRow): void {
439 1
    $this->positionRow = Numbers::range($positionRow, 1, PHP_INT_MAX);
440 1
  }
441
  
442
  public function getPositionColumn(): int {
443 1
    return $this->positionColumn;
444
  }
445
  
446
  public function setPositionColumn(int $positionColumn): void {
447 1
    $this->positionColumn = Numbers::range($positionColumn, 1, PHP_INT_MAX);
448 1
  }
449
  
450
  /**
451
   * @internal
452
   */
453
  public function applyEffectProviders(): void {
454 1
    foreach($this->effectProviders as $item) {
455 1
      $effects = $item->getCombatEffects();
456 1
      array_walk($effects, function(CharacterEffect $effect) {
457 1
        $this->addEffect($effect);
458 1
      });
459
    }
460 1
  }
461
  
462
  /**
463
   * Applies new effect on the character
464
   */
465
  public function addEffect(CharacterEffect $effect): void {
466 1
    $this->effects[] = $effect;
467 1
    $effect->onApply($this, $effect);
468 1
  }
469
470
  /**
471
   * Removes specified effect from the character
472
   *
473
   * @throws \OutOfBoundsException
474
   */
475
  public function removeEffect(string $effectId): void {
476 1
    foreach($this->effects as $i => $effect) {
477 1
      if($effect->id == $effectId) {
478 1
        unset($this->effects[$i]);
479 1
        $effect->onRemove($this, $effect);
480 1
        return;
481
      }
482
    }
483 1
    throw new \OutOfBoundsException("Effect to remove was not found.");
484
  }
485
  
486
  /**
487
   * Get specified equipment of the character
488
   *
489
   * @throws \OutOfBoundsException
490
   */
491
  public function getItem(int $itemId): Equipment {
492 1
    $equipment = $this->equipment->getItems(["id" => $itemId]);
493 1
    if(count($equipment) === 0) {
494 1
      throw new \OutOfBoundsException("Item was not found.");
495
    }
496 1
    return $equipment[0];
497
  }
498
  
499
  /**
500
   * Get specified pet
501
   *
502
   * @throws \OutOfBoundsException
503
   */
504
  public function getPet(int $petId): Pet {
505 1
    $pets = $this->pets->getItems(["id" => $petId]);
506 1
    if(count($pets) === 0) {
507 1
      throw new \OutOfBoundsException("Pet was not found.");
508
    }
509 1
    return $pets[0];
510
  }
511
  
512
  /**
513
   * @return BaseCharacterSkill[]
514
   */
515
  public function getUsableSkills(): array {
516 1
    return $this->skills->getItems(["usable" => true]);
517
  }
518
  
519
  /**
520
   * Harm the character
521
   */
522
  public function harm(int $amount): void {
523 1
    $this->hitpoints -= Numbers::range($amount, 0, $this->hitpoints);
524 1
  }
525
  
526
  /**
527
   * Heal the character
528
   */
529
  public function heal(int $amount): void {
530 1
    $this->hitpoints += Numbers::range($amount, 0, $this->maxHitpoints - $this->hitpoints);
531 1
  }
532
  
533
  /**
534
   * Determine which (primary) stat should be used to calculate damage
535
   */
536
  public function damageStat(): string {
537 1
    foreach($this->equipment as $item) {
538 1
      if(!$item->worn OR !$item instanceof Weapon) {
539 1
        continue;
540
      }
541 1
      return $item->damageStat;
542
    }
543 1
    return static::STAT_STRENGTH;
544
  }
545
  
546
  /**
547
   * Recalculate secondary stats from the the primary ones
548
   */
549
  public function recalculateSecondaryStats(): void {
550
    $stats = [
551 1
      static::STAT_DAMAGE => $this->damageStat(), static::STAT_HIT => static::STAT_DEXTERITY,
552 1
      static::STAT_DODGE => static::STAT_DEXTERITY, static::STAT_MAX_HITPOINTS => static::STAT_CONSTITUTION,
553 1
      static::STAT_INITIATIVE => "",
554
    ];
555 1
    foreach($stats as $secondary => $primary) {
556 1
      $gain = $this->$secondary - $this->{$secondary . "Base"};
557 1
      if($secondary === static::STAT_DAMAGE) {
558 1
        $base = (int) round($this->$primary / 2);
559 1
      } elseif($secondary === static::STAT_MAX_HITPOINTS) {
560 1
        $base = $this->$primary * static::HITPOINTS_PER_CONSTITUTION;
561 1
      } elseif($secondary === static::STAT_INITIATIVE) {
562 1
        $base = $this->initiativeFormulaParser->calculateInitiative($this);
563
      } else {
564 1
        $base = $this->$primary * 3;
565
      }
566 1
      $this->{$secondary . "Base"} = $base;
567 1
      $this->$secondary = $base + $gain;
568
    }
569 1
  }
570
  
571
  /**
572
   * Recalculates stats of the character (mostly used during combat)
573
   */
574
  public function recalculateStats(): void {
575 1
    $stats = array_merge(static::BASE_STATS, static::SECONDARY_STATS);
576 1
    $stunned = false;
577 1
    $debuffs = [];
578 1
    foreach($stats as $stat) {
579 1
      $$stat = $this->{$stat . "Base"};
580 1
      $debuffs[$stat] = 0;
581
    }
582 1
    foreach($this->effects as $i => $effect) {
583 1
      $stat = $effect->stat;
584 1
      $type = $effect->type;
585 1
      $duration = $effect->duration;
586 1
      if(is_int($duration) AND $duration < 1) {
587 1
        $this->removeEffect($effect->id);
588 1
        continue;
589
      }
590 1
      if(!in_array($type, SkillSpecial::NO_STAT_TYPES, true)) {
591 1
        $bonus_value = ($effect->valueAbsolute) ? $effect->value : $$stat / 100 * $effect->value;
592
      }
593 1
      if($type == SkillSpecial::TYPE_BUFF) {
594 1
        $$stat += $bonus_value;
595 1
      } elseif($type == SkillSpecial::TYPE_DEBUFF) {
596 1
        $debuffs[$stat] += $bonus_value;
597 1
      } elseif($type == SkillSpecial::TYPE_STUN) {
598 1
        $stunned = true;
599
      }
600 1
      unset($stat, $type, $duration, $bonus_value);
601
    }
602 1
    foreach($debuffs as $stat => $value) {
603 1
      $value = min($value, 80);
604 1
      $bonus_value = $$stat / 100 * $value;
605 1
      $$stat -= $bonus_value;
606
    }
607 1
    foreach($stats as $stat) {
608 1
      $this->$stat = (int) round($$stat);
609
    }
610 1
    $this->recalculateSecondaryStats();
611 1
    $this->stunned = $stunned;
612 1
  }
613
  
614
  /**
615
   * Reset character's initiative
616
   */
617
  public function resetInitiative(): void {
618 1
    $this->initiative = $this->initiativeBase = 0;
619 1
  }
620
}
621
?>