|
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
|
|
|
use Rottenwood\KingdomBundle\Exception\InvalidCommandParameter; |
|
9
|
|
|
use Rottenwood\KingdomBundle\Redis\RedisClientInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Перемещение по карте |
|
13
|
|
|
* Применение в js: Kingdom.Websocket.command('move', 'north|south|west|east') |
|
14
|
|
|
*/ |
|
15
|
|
|
class Move extends AbstractGameCommand |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
private $waitState = 5; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return CommandResponse |
|
22
|
|
|
* @throws InvalidCommandParameter |
|
23
|
|
|
*/ |
|
24
|
|
|
public function execute(): CommandResponse |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
if ($this->user->isBusy()) { |
|
28
|
|
|
$this->result->setWaitstate($this->user->getWaitstate()); |
|
29
|
|
|
|
|
30
|
|
|
return $this->result; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$roomRepository = $this->container->get('kingdom.room_repository'); |
|
34
|
|
|
$em = $roomRepository->getEntityManager(); |
|
35
|
|
|
|
|
36
|
|
|
$userId = $this->user->getId(); |
|
37
|
|
|
$userName = $this->user->getName(); |
|
38
|
|
|
|
|
39
|
|
|
/** @var Room $currentRoom */ |
|
40
|
|
|
$currentRoom = $this->user->getRoom(); |
|
41
|
|
|
|
|
42
|
|
|
$x = $currentRoom->getX(); |
|
43
|
|
|
$y = $currentRoom->getY(); |
|
44
|
|
|
|
|
45
|
|
|
if ($this->parameters == 'north') { |
|
46
|
|
|
$y++; |
|
47
|
|
|
$directionTo = 'на север'; |
|
48
|
|
|
$directionFrom = 'с юга'; |
|
49
|
|
|
} elseif ($this->parameters == 'south') { |
|
50
|
|
|
$y--; |
|
51
|
|
|
$directionTo = 'на юг'; |
|
52
|
|
|
$directionFrom = 'с севера'; |
|
53
|
|
|
} elseif ($this->parameters == 'east') { |
|
54
|
|
|
$x++; |
|
55
|
|
|
$directionTo = 'на восток'; |
|
56
|
|
|
$directionFrom = 'с запада'; |
|
57
|
|
|
} elseif ($this->parameters == 'west') { |
|
58
|
|
|
$x--; |
|
59
|
|
|
$directionTo = 'на запад'; |
|
60
|
|
|
$directionFrom = 'с востока'; |
|
61
|
|
|
} else { |
|
62
|
|
|
throw new InvalidCommandParameter; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$destinationRoom = $roomRepository->findOneByXandY($x, $y); |
|
66
|
|
|
|
|
67
|
|
|
if (!$destinationRoom || !$this->userCanWalkToRoom($destinationRoom)) { |
|
68
|
|
|
$this->result->addError('В эту сторону не пройти'); |
|
69
|
|
|
} else { |
|
70
|
|
|
$this->user->setRoom($destinationRoom); |
|
71
|
|
|
$em->flush($this->user); |
|
72
|
|
|
|
|
73
|
|
|
/** @var \Redis $redis */ |
|
74
|
|
|
$redis = $this->container->get('snc_redis.default'); |
|
75
|
|
|
$redis->hset(RedisClientInterface::ID_ROOM_HASH, $userId, $destinationRoom->getId()); |
|
76
|
|
|
|
|
77
|
|
|
$logger = $this->container->get('kingdom.logger'); |
|
78
|
|
|
$logString = sprintf( |
|
79
|
|
|
'[%d]%s переместился %s в комнату [%d]%s [%d/%d/%d]', |
|
80
|
|
|
$userId, |
|
81
|
|
|
$userName, |
|
82
|
|
|
$directionTo, |
|
83
|
|
|
$destinationRoom->getId(), |
|
84
|
|
|
$destinationRoom->getName(), |
|
85
|
|
|
$x, |
|
86
|
|
|
$y, |
|
87
|
|
|
$destinationRoom->getZ() |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
$userService = $this->container->get('kingdom.user_service'); |
|
91
|
|
|
|
|
92
|
|
|
$resultData = [ |
|
93
|
|
|
'name' => $userName, |
|
94
|
|
|
'directionTo' => $directionTo, |
|
95
|
|
|
'directionFrom' => $directionFrom, |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
if ($usersInCurrentRoom = $userService->getOnlineUsersIdsInRoom($currentRoom, $userId)) { |
|
99
|
|
|
$resultData['left'] = $userService->getSessionsByUserIds($usersInCurrentRoom); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ($usersInDestinationRoom = $userService->getOnlineUsersIdsInRoom($destinationRoom, $userId)) { |
|
103
|
|
|
$resultData['enter'] = $userService->getSessionsByUserIds($usersInDestinationRoom); |
|
104
|
|
|
|
|
105
|
|
|
$logString .= sprintf( |
|
106
|
|
|
', встретил игроков: [%s]', |
|
107
|
|
|
implode(',', $usersInDestinationRoom) |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$logger->info($logString); |
|
112
|
|
|
$this->result->setData($resultData); |
|
113
|
|
|
|
|
114
|
|
|
$userService->addWaitstate($this->user, $this->waitState); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $this->result; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Может ли персонаж войти в комнату |
|
122
|
|
|
* @param Room $room |
|
123
|
|
|
* @return bool |
|
124
|
|
|
*/ |
|
125
|
|
|
private function userCanWalkToRoom(Room $room): bool |
|
126
|
|
|
{ |
|
127
|
|
|
return $room->getType()->userCanWalk(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|