Completed
Push — master ( 3d8890...b672a0 )
by Jakub
02:05
created

Team::getAliveMembers()   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 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nexendrie\Utils\Collection,
7
    Nette\Utils\Arrays;
8
9
/**
10
 * Structure for a team in combat
11
 * 
12
 * @author Jakub Konečný
13
 * @property-read string $name
14
 * @property-read Character[] $aliveMembers
15
 * @property-read Character[] $usableMembers
16
 */
17 1
final class Team extends Collection {
18
  protected $class = Character::class;
19
  /** @var string Name of the team */
20
  protected $name;
21
  
22 1
  use \Nette\SmartObject;
23
  
24
  public function __construct(string $name) {
25 1
    parent::__construct();
26 1
    $this->name = $name;
27 1
  }
28
  
29
  public function getName(): string {
30 1
    return $this->name;
31
  }
32
  
33
  /**
34
   * Check if the team has a character
35
   * 
36
   * @param string|int $id Character's id
37
   */
38
  public function hasMember($id): bool {
39 1
    return $this->hasMembers(["id" => $id]);
40
  }
41
  
42
  /**
43
   * Check if the team has at least 1 member matching the filter
44
   *
45
   * @todo make it possible to use different comparing rules
46
   */
47
  public function hasMembers(array $filter = []): bool {
48 1
    if(count($filter) === 0) {
49 1
      return (count($this->items) > 0);
50
    }
51 1
    return Arrays::some($this->items, function(Character $character) use($filter) {
52 1
      foreach($filter as $key => $value) {
53 1
        if($character->$key !== $value) {
54 1
          return false;
55
        }
56
      }
57 1
      return true;
58 1
    });
59
  }
60
  
61
  /**
62
   * Get all team members matching the filter
63
   *
64
   * @todo make it possible to use different comparing rules
65
   * @return Character[]
66
   */
67
  public function getMembers(array $filter = []): array {
68 1
    if(count($filter) === 0) {
69 1
      return $this->items;
70
    }
71 1
    return array_values(array_filter($this->items, function(Character $character) use($filter) {
72 1
      foreach($filter as $key => $value) {
73 1
        if($character->$key !== $value) {
74 1
          return false;
75
        }
76
      }
77 1
      return true;
78 1
    }));
79
  }
80
  
81
  /**
82
   * Get alive members from the team
83
   * 
84
   * @return Character[]
85
   */
86
  public function getAliveMembers(): array {
87 1
    return array_values(array_filter($this->items, function(Character $value) {
88 1
      return ($value->hitpoints > 0);
89 1
    }));
90
  }
91
  
92
  /**
93
   * Get members which can perform an action
94
   * 
95
   * @return Character[]
96
   */
97
  public function getUsableMembers(): array {
98 1
    return array_values(array_filter($this->items, function(Character $value) {
99 1
      return (!$value->stunned AND $value->hitpoints > 0);
100 1
    }));
101
  }
102
  
103
  /**
104
   * Check whether the team has at least 1 alive member
105
   */
106
  public function hasAliveMembers(): bool {
107 1
    return count($this->getAliveMembers()) > 0;
108
  }
109
}
110
?>