-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Game.php
330 lines (265 loc) · 8.89 KB
/
Game.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
declare(strict_types=1);
namespace sergittos\bedwars\game;
use pocketmine\entity\Location;
use pocketmine\Server;
use pocketmine\utils\Utils;
use pocketmine\world\format\Chunk;
use pocketmine\world\Position;
use pocketmine\world\sound\Sound;
use pocketmine\world\World;
use pocketmine\world\WorldException;
use sergittos\bedwars\BedWars;
use sergittos\bedwars\entity\PlayBedwarsEntity;
use sergittos\bedwars\game\entity\shop\ItemShopVillager;
use sergittos\bedwars\game\entity\shop\UpgradesShopVillager;
use sergittos\bedwars\game\entity\shop\Villager;
use sergittos\bedwars\game\generator\Generator;
use sergittos\bedwars\game\generator\presets\TextGenerator;
use sergittos\bedwars\game\map\Map;
use sergittos\bedwars\game\stage\Stage;
use sergittos\bedwars\game\stage\WaitingStage;
use sergittos\bedwars\game\task\RemoveGameTask;
use sergittos\bedwars\game\team\Team;
use sergittos\bedwars\session\Session;
use function array_merge;
use function array_search;
use function count;
use function in_array;
use function is_string;
class Game {
private int $id;
private Map $map;
private Stage $stage;
private ?World $world = null;
/** @var Generator[] */
private array $generators;
/** @var Team[] */
private array $teams;
/** @var Position[] */
private array $blocks = [];
/** @var Session[] */
private array $players = [];
/** @var Session[] */
private array $spectators = [];
public function __construct(Map $map, int $id) {
$this->id = $id;
$this->map = $map;
$this->teams = Utils::cloneObjectArray($map->getTeams());
$this->generators = Utils::cloneObjectArray($map->getGenerators());
$this->setStage(new WaitingStage());
}
public function getId(): int {
return $this->id;
}
public function getStage(): Stage {
return $this->stage;
}
public function getMap(): Map {
return $this->map;
}
public function getWorld(): ?World {
return $this->world;
}
/**
* @return Generator[]
*/
public function getGenerators(): array {
return $this->generators;
}
/**
* @return Team[]
*/
public function getTeams(): array {
return $this->teams;
}
/**
* @return Team[]
*/
public function getAliveTeams(): array {
$teams = [];
foreach($this->teams as $team) {
if($team->isAlive()) {
$teams[] = $team;
}
}
return $teams;
}
/**
* @return Session[]
*/
public function getPlayers(): array {
return $this->players;
}
/**
* @return Session[]
*/
public function getSpectators(): array {
return $this->spectators;
}
/**
* @return Session[]
*/
public function getPlayersAndSpectators(): array {
return array_merge($this->players, $this->spectators);
}
public function getPlayersCount(): int {
return count($this->players);
}
public function isFull(): bool {
return $this->getPlayersCount() >= $this->map->getMaxCapacity();
}
public function checkBlock(Position $position): bool {
foreach($this->blocks as $index => $block) {
if($block->equals($position)) {
unset($this->blocks[$index]);
return true;
}
}
return false;
}
public function isPlaying(Session $session): bool {
return in_array($session, $this->players, true);
}
public function isSpectator(Session $session): bool {
return in_array($session, $this->spectators, true);
}
public function setStage(Stage $stage): void {
$this->stage = $stage;
$this->stage->start($this);
}
public function addBlock(Position $position): void {
$this->blocks[] = $position;
}
public function addPlayer(Session $session): void {
$this->players[] = $session;
$this->stage->onJoin($session);
$this->updateScoreboards();
$this->updatePlayEntities();
}
public function removePlayer(Session $session, bool $teleport_to_hub = true, bool $set_spectator = false): void {
unset($this->players[array_search($session, $this->players, true)]);
$this->stage->onQuit($session);
if($teleport_to_hub) {
$session->teleportToHub();
}
if($set_spectator) {
$this->addSpectator($session);
} else {
$session->setGame(null);
}
$this->updateScoreboards();
$this->updatePlayEntities();
}
public function addSpectator(Session $session): void {
$this->spectators[] = $session;
$session->giveSpectatorItems();
$session->getSpectatorSettings()->apply();
}
public function removeSpectator(Session $session): void {
unset($this->spectators[array_search($session, $this->spectators, true)]);
$this->despawnGeneratorsFrom($session);
$session->setGame(null);
$session->teleportToHub();
}
public function despawnGeneratorsFrom(Session $session): void {
foreach($this->getGenerators() as $generator) {
if($generator instanceof TextGenerator) {
$generator->getText()->despawnFrom($session);
}
}
}
private function updatePlayEntities(): void {
foreach(Server::getInstance()->getWorldManager()->getDefaultWorld()->getEntities() as $entity) {
if($entity instanceof PlayBedwarsEntity and $entity->getPlayersPerTeam() === $this->map->getPlayersPerTeam()) {
$entity->updateNameTag();
}
}
}
private function spawnVillager(Villager $villager): void {
$position = $villager->getPosition()->floor();
$this->world->requestChunkPopulation($position->getX() >> Chunk::COORD_BIT_SIZE, $position->getZ() >> Chunk::COORD_BIT_SIZE, null)->onCompletion(
function() use ($villager) {
$villager->spawnToAll();
},
function() {}
);
}
public function tickGenerators(): void {
foreach($this->generators as $generator) {
$generator->tick($this);
}
foreach($this->teams as $team) {
$team->tickGenerators($this);
}
}
public function updateScoreboards(): void {
foreach($this->getPlayersAndSpectators() as $session) {
$session->updateScoreboard();
}
}
public function broadcastTitle(string $title): void {
foreach($this->players as $session) {
$session->title($title);
}
}
public function broadcastMessage(string $message): void {
foreach($this->getPlayersAndSpectators() as $session) {
$session->message($message);
}
}
public function broadcastSound(Sound|string $sound): void {
foreach($this->getPlayersAndSpectators() as $session) {
if(is_string($sound)) {
$session->playSound($sound);
} else {
$player = $session->getPlayer();
$player->broadcastSound($sound, [$player]);
}
}
}
public function setupWorld(): void {
$name = $this->map->getName() . "-" . $this->id;
$world_manager = Server::getInstance()->getWorldManager();
if(!$world_manager->loadWorld($name)) {
throw new WorldException("Failed to load world");
}
$this->world = $world_manager->getWorldByName($name);
$this->world->setAutoSave(false);
$this->world->setTime(World::TIME_DAY);
$this->world->stopTime();
foreach($this->map->getShopPositions() as $position) {
$this->spawnVillager(new ItemShopVillager(Location::fromObject($position, $this->world)));
}
foreach($this->map->getUpgradesPositions() as $position) {
$this->spawnVillager(new UpgradesShopVillager(Location::fromObject($position, $this->world)));
}
}
public function unloadWorld(): void {
if($this->world !== null) {
Server::getInstance()->getWorldManager()->unloadWorld($this->world);
$this->world = null;
$this->blocks = [];
}
}
public function reset(): void {
foreach($this->players as $session) {
$this->removePlayer($session);
}
foreach($this->spectators as $spectator) {
$this->removeSpectator($spectator);
}
foreach($this->generators as $generator) {
$generator->reset();
}
$this->unloadWorld();
if(BedWars::getInstance()->getGameManager()->getGamesCount($this->map) > 5) {
Server::getInstance()->getAsyncPool()->submitTask(new RemoveGameTask($this));
return;
}
foreach($this->teams as $team) {
$team->reset();
}
$this->setStage(new WaitingStage());
}
}