|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Eole\Websocket\Topic; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
6
|
|
|
use Ratchet\Wamp\WampConnection; |
|
7
|
|
|
use Eole\Sandstone\Websocket\Topic; |
|
8
|
|
|
use Eole\Core\Model\Party; |
|
9
|
|
|
use Eole\Core\Event\PartyEvent; |
|
10
|
|
|
use Eole\Core\Event\SlotEvent; |
|
11
|
|
|
|
|
12
|
|
|
class PartiesTopic extends Topic implements EventSubscriberInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var null|string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $gameName; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string $topicPath |
|
21
|
|
|
* @param array $arguments |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct($topicPath, array $arguments = array()) |
|
24
|
|
|
{ |
|
25
|
|
|
parent::__construct($topicPath, $arguments); |
|
26
|
|
|
|
|
27
|
|
|
$this->gameName = $arguments['game_name']; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@InheritDoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function onSubscribe(WampConnection $conn, $topic) |
|
34
|
|
|
{ |
|
35
|
|
|
parent::onSubscribe($conn, $topic); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@InheritDoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function onPublish(WampConnection $conn, $topic, $event) |
|
42
|
|
|
{ |
|
43
|
|
|
// noop |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param PartyEvent $event |
|
48
|
|
|
*/ |
|
49
|
|
|
public function onPartyAvailable(PartyEvent $event) |
|
50
|
|
|
{ |
|
51
|
|
|
$party = $event->getParty(); |
|
52
|
|
|
|
|
53
|
|
|
if (!$this->souldBroadcastForGame($party)) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->broadcast([ |
|
58
|
|
|
'type' => 'created', |
|
59
|
|
|
'party' => $party, |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param PartyEvent $event |
|
65
|
|
|
*/ |
|
66
|
|
View Code Duplication |
public function onPartyGone(PartyEvent $event) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
$party = $event->getParty(); |
|
69
|
|
|
|
|
70
|
|
|
if (!$this->souldBroadcastForGame($party)) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->broadcast([ |
|
75
|
|
|
'type' => 'gone', |
|
76
|
|
|
'party' => $party, |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param SlotEvent $event |
|
82
|
|
|
*/ |
|
83
|
|
|
public function onSlotJoin(SlotEvent $event) |
|
84
|
|
|
{ |
|
85
|
|
|
$party = $event->getParty(); |
|
86
|
|
|
|
|
87
|
|
|
if (!$this->souldBroadcastForGame($party)) { |
|
88
|
|
|
return; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->broadcast([ |
|
92
|
|
|
'type' => 'slot_join', |
|
93
|
|
|
'event' => $event, |
|
94
|
|
|
]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param PartyEvent $event |
|
99
|
|
|
*/ |
|
100
|
|
View Code Duplication |
public function onPartyStarted(PartyEvent $event) |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
$party = $event->getParty(); |
|
103
|
|
|
|
|
104
|
|
|
if (!$this->souldBroadcastForGame($party)) { |
|
105
|
|
|
return; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$this->broadcast([ |
|
109
|
|
|
'type' => 'party_started', |
|
110
|
|
|
'event' => $event, |
|
111
|
|
|
]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param PartyEvent $event |
|
116
|
|
|
*/ |
|
117
|
|
View Code Duplication |
public function onPartyEnded(PartyEvent $event) |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
$party = $event->getParty(); |
|
120
|
|
|
|
|
121
|
|
|
if (!$this->souldBroadcastForGame($party)) { |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->broadcast([ |
|
126
|
|
|
'type' => 'party_ended', |
|
127
|
|
|
'event' => $event, |
|
128
|
|
|
]); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* {@InheritDoc} |
|
133
|
|
|
*/ |
|
134
|
|
|
public function onUnSubscribe(WampConnection $conn, $topic) |
|
135
|
|
|
{ |
|
136
|
|
|
parent::onUnSubscribe($conn, $topic); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Returns whether it should broadcast event |
|
141
|
|
|
* depending on the current game name. |
|
142
|
|
|
* |
|
143
|
|
|
* @param Party $party |
|
144
|
|
|
* |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
|
|
private function souldBroadcastForGame(Party $party) |
|
148
|
|
|
{ |
|
149
|
|
|
return |
|
150
|
|
|
(null === $this->gameName) || |
|
151
|
|
|
($party->getGame()->getName() === $this->gameName) |
|
152
|
|
|
; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@InheritDoc} |
|
157
|
|
|
*/ |
|
158
|
|
|
public static function getSubscribedEvents() |
|
159
|
|
|
{ |
|
160
|
|
|
return array( |
|
161
|
|
|
PartyEvent::CREATE_AFTER => array( |
|
162
|
|
|
array('onPartyAvailable'), |
|
163
|
|
|
), |
|
164
|
|
|
SlotEvent::JOIN_AFTER => array( |
|
165
|
|
|
array('onSlotJoin'), |
|
166
|
|
|
), |
|
167
|
|
|
PartyEvent::STARTED => array( |
|
168
|
|
|
array('onPartyStarted'), |
|
169
|
|
|
), |
|
170
|
|
|
PartyEvent::ENDED => array( |
|
171
|
|
|
array('onPartyEnded'), |
|
172
|
|
|
), |
|
173
|
|
|
); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.