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[] $items Characters in the team |
15
|
|
|
* @property-read Character[] $aliveMembers |
16
|
|
|
* @property-read Character[] $usableMembers |
17
|
|
|
*/ |
18
|
1 |
|
final class Team extends Collection { |
19
|
|
|
protected $class = Character::class; |
20
|
|
|
/** @var string Name of the team */ |
21
|
|
|
protected $name; |
22
|
|
|
|
23
|
1 |
|
use \Nette\SmartObject; |
24
|
|
|
|
25
|
|
|
public function __construct(string $name) { |
26
|
1 |
|
$this->name = $name; |
27
|
1 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return Character[] |
31
|
|
|
*/ |
32
|
|
|
public function getItems(): array { |
33
|
1 |
|
return $this->items; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getName(): string { |
37
|
1 |
|
return $this->name; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Check if the team has a character |
42
|
|
|
* |
43
|
|
|
* @param string|int $id Character's id |
44
|
|
|
*/ |
45
|
|
|
public function hasMember($id): bool { |
46
|
1 |
|
return $this->hasMembers(["id" => $id]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Check if the team has at least 1 member matching the filter |
51
|
|
|
* |
52
|
|
|
* @todo make it possible to use different comparing rules |
53
|
|
|
*/ |
54
|
|
|
public function hasMembers(array $filter = []): bool { |
55
|
1 |
|
if(count($filter) === 0) { |
56
|
1 |
|
return (count($this->items) > 0); |
57
|
|
|
} |
58
|
1 |
|
return Arrays::some($this->items, function(Character $character) use($filter) { |
59
|
1 |
|
foreach($filter as $key => $value) { |
60
|
1 |
|
if($character->$key !== $value) { |
61
|
1 |
|
return false; |
62
|
|
|
} |
63
|
|
|
} |
64
|
1 |
|
return true; |
65
|
1 |
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get all team members matching the filter |
70
|
|
|
* |
71
|
|
|
* @todo make it possible to use different comparing rules |
72
|
|
|
* @return Character[] |
73
|
|
|
*/ |
74
|
|
|
public function getMembers(array $filter = []): array { |
75
|
1 |
|
if(count($filter) === 0) { |
76
|
1 |
|
return $this->items; |
77
|
|
|
} |
78
|
1 |
|
return array_values(array_filter($this->items, function(Character $character) use($filter) { |
79
|
1 |
|
foreach($filter as $key => $value) { |
80
|
1 |
|
if($character->$key !== $value) { |
81
|
1 |
|
return false; |
82
|
|
|
} |
83
|
|
|
} |
84
|
1 |
|
return true; |
85
|
1 |
|
})); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get alive members from the team |
90
|
|
|
* |
91
|
|
|
* @return Character[] |
92
|
|
|
*/ |
93
|
|
|
public function getAliveMembers(): array { |
94
|
1 |
|
return array_values(array_filter($this->items, function(Character $value) { |
95
|
1 |
|
return ($value->hitpoints > 0); |
96
|
1 |
|
})); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get members which can perform an action |
101
|
|
|
* |
102
|
|
|
* @return Character[] |
103
|
|
|
*/ |
104
|
|
|
public function getUsableMembers(): array { |
105
|
1 |
|
return array_values(array_filter($this->items, function(Character $value) { |
106
|
1 |
|
return (!$value->stunned AND $value->hitpoints > 0); |
107
|
1 |
|
})); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Check whether the team has at least 1 alive member |
112
|
|
|
*/ |
113
|
|
|
public function hasAliveMembers(): bool { |
114
|
1 |
|
return count($this->getAliveMembers()) > 0; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
?> |