|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace HeroesofAbenez\Combat; |
|
5
|
|
|
|
|
6
|
|
|
use Nexendrie\Utils\Collection, |
|
7
|
|
|
Nexendrie\Utils\Numbers; |
|
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
|
|
|
* @property int $maxRowSize |
|
17
|
|
|
*/ |
|
18
|
1 |
|
final class Team extends Collection { |
|
19
|
|
|
protected $class = Character::class; |
|
20
|
|
|
/** @var string Name of the team */ |
|
21
|
|
|
protected $name; |
|
22
|
|
|
protected $maxRowSize = 5; |
|
23
|
|
|
|
|
24
|
1 |
|
use \Nette\SmartObject; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(string $name) { |
|
27
|
1 |
|
parent::__construct(); |
|
28
|
1 |
|
$this->name = $name; |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getName(): string { |
|
32
|
1 |
|
return $this->name; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getMaxRowSize(): int { |
|
36
|
1 |
|
return $this->maxRowSize; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function setMaxRowSize(int $maxRowSize): void { |
|
40
|
1 |
|
$this->maxRowSize = Numbers::range($maxRowSize, 1, PHP_INT_MAX); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Check if the team has at least 1 member matching the filter |
|
45
|
|
|
*/ |
|
46
|
|
|
public function hasMembers(array $filter = []): bool { |
|
47
|
1 |
|
return (count($this->getMembers($filter)) > 0); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get all team members matching the filter |
|
52
|
|
|
* |
|
53
|
|
|
* @todo make it possible to use different comparing rules |
|
54
|
|
|
* @return Character[] |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getMembers(array $filter = []): array { |
|
57
|
1 |
|
if(count($filter) === 0) { |
|
58
|
1 |
|
return $this->items; |
|
59
|
|
|
} |
|
60
|
1 |
|
return array_values(array_filter($this->items, function(Character $character) use($filter) { |
|
61
|
1 |
|
foreach($filter as $key => $value) { |
|
62
|
1 |
|
if($character->$key !== $value) { |
|
63
|
1 |
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
1 |
|
return true; |
|
67
|
1 |
|
})); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get alive members from the team |
|
72
|
|
|
* |
|
73
|
|
|
* @return Character[] |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getAliveMembers(): array { |
|
76
|
1 |
|
return array_values(array_filter($this->items, function(Character $value) { |
|
77
|
1 |
|
return ($value->hitpoints > 0); |
|
78
|
1 |
|
})); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get members which can perform an action |
|
83
|
|
|
* |
|
84
|
|
|
* @return Character[] |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getUsableMembers(): array { |
|
87
|
1 |
|
return array_values(array_filter($this->items, function(Character $value) { |
|
88
|
1 |
|
return (!$value->stunned AND $value->hitpoints > 0); |
|
89
|
1 |
|
})); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Check whether the team has at least 1 alive member |
|
94
|
|
|
*/ |
|
95
|
|
|
public function hasAliveMembers(): bool { |
|
96
|
1 |
|
return count($this->getAliveMembers()) > 0; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Set character's position in team |
|
101
|
|
|
* |
|
102
|
|
|
* @param string|int $id |
|
103
|
|
|
* @throws \OutOfBoundsException |
|
104
|
|
|
* @throws InvalidCharacterPositionException |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setCharacterPosition($id, int $row, int $column): void { |
|
107
|
1 |
|
if(!$this->hasMembers(["id" => $id])) { |
|
108
|
1 |
|
throw new \OutOfBoundsException("Character $id is not in the team"); |
|
109
|
1 |
|
} elseif(count($this->getMembers(["positionRow" => $row])) >= $this->maxRowSize) { |
|
110
|
1 |
|
throw new InvalidCharacterPositionException("Row $row is full.", InvalidCharacterPositionException::ROW_FULL); |
|
111
|
1 |
|
} elseif($this->hasMembers(["positionRow" => $row, "positionColumn" => $column])) { |
|
112
|
1 |
|
throw new InvalidCharacterPositionException("Row $row column $column is occupied.", InvalidCharacterPositionException::POSITION_OCCUPIED); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
1 |
|
$character = $this->getMembers(["id" => $id])[0]; |
|
115
|
1 |
|
$character->positionRow = $row; |
|
116
|
1 |
|
$character->positionColumn = $column; |
|
117
|
1 |
|
} |
|
118
|
|
|
} |
|
119
|
|
|
?> |