|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rottenwood\KingdomBundle\Command\Game; |
|
4
|
|
|
|
|
5
|
|
|
use Rottenwood\KingdomBundle\Command\Infrastructure\AbstractGameCommand; |
|
6
|
|
|
use Rottenwood\KingdomBundle\Command\Infrastructure\CommandResponse; |
|
7
|
|
|
use Rottenwood\KingdomBundle\Entity\Room; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Отрисовка карты |
|
11
|
|
|
* Применение в js: Kingdom.Websocket.command('composeMap') |
|
12
|
|
|
*/ |
|
13
|
|
|
class ComposeMap extends AbstractGameCommand |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Количество комнат от центра в каждую сторону |
|
18
|
|
|
* @var int |
|
19
|
|
|
*/ |
|
20
|
|
|
private $mapRadius = 2; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return CommandResponse |
|
24
|
|
|
*/ |
|
25
|
|
|
public function execute(): CommandResponse |
|
26
|
|
|
{ |
|
27
|
|
|
$currentRoom = $this->user->getRoom(); |
|
28
|
|
|
$currentX = $currentRoom->getX(); |
|
29
|
|
|
$currentY = $currentRoom->getY(); |
|
30
|
|
|
|
|
31
|
|
|
$roomRepository = $this->container->get('kingdom.room_repository'); |
|
32
|
|
|
$resourceRepository = $this->container->get('kingdom.room_resource_repository'); |
|
33
|
|
|
|
|
34
|
|
|
$map = []; |
|
35
|
|
|
|
|
36
|
|
|
for ($y = $currentY - $this->mapRadius, $relativeY = 1, $relativeX = 1; |
|
|
|
|
|
|
37
|
|
|
$y <= $currentY + $this->mapRadius; |
|
38
|
|
|
$y++, $relativeY++, $relativeX = 1) { |
|
39
|
|
|
|
|
40
|
|
|
for ($x = $currentX - $this->mapRadius; |
|
41
|
|
|
$x <= $currentX + $this->mapRadius; |
|
42
|
|
|
$x++, $relativeX++) { |
|
43
|
|
|
|
|
44
|
|
|
// пропуск центра карты, комнаты где находится персонаж |
|
45
|
|
|
if ($relativeY == 3 && $relativeX == 3) { |
|
46
|
|
|
continue; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$room = $roomRepository->findOneByXandY($x, $y); |
|
50
|
|
|
|
|
51
|
|
|
if ($room) { |
|
52
|
|
|
$map[] = $this->addRoom($room, $relativeX, $relativeY); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$result = [ |
|
58
|
|
|
'name' => $currentRoom->getName(), |
|
59
|
|
|
'description' => $currentRoom->getDescription(), |
|
60
|
|
|
'x' => $currentX, |
|
61
|
|
|
'y' => $currentY, |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
$roomResources = $resourceRepository->findByRoom($currentRoom); |
|
65
|
|
|
|
|
66
|
|
|
foreach ($roomResources as $roomResource) { |
|
67
|
|
|
$resourceQuantity = $roomResource->getQuantity(); |
|
68
|
|
|
|
|
69
|
|
|
if ($resourceQuantity > 0) { |
|
70
|
|
|
$result['resources'][] = [ |
|
71
|
|
|
'id' => $roomResource->getItem()->getId(), |
|
72
|
|
|
'name' => $roomResource->getItem()->getName(), |
|
73
|
|
|
'name4' => $roomResource->getItem()->getName4(), |
|
74
|
|
|
'quantity' => $resourceQuantity, |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->result->setData($result); |
|
80
|
|
|
$this->result->setMapData($map); |
|
81
|
|
|
|
|
82
|
|
|
return $this->result; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param Room $room |
|
87
|
|
|
* @param int $x |
|
88
|
|
|
* @param int $y |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
private function addRoom(Room $room, int $x, int $y): array |
|
92
|
|
|
{ |
|
93
|
|
|
return [ |
|
94
|
|
|
'x' => $x, |
|
95
|
|
|
'y' => $y, |
|
96
|
|
|
'pic' => $room->getType()->getPicture(), |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|