Completed
Push — dev ( 40ad60...735e11 )
by Julien
02:34
created

AwaleTopic::onPartyStart()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Eole\Games\Awale;
4
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Eole\Core\Event\SlotEvent;
7
use Eole\Core\Event\PartyEvent;
8
use Eole\Sandstone\Websocket\Topic as BaseTopic;
9
use Eole\Games\Awale\Event\AwaleEvent;
10
11
class AwaleTopic extends BaseTopic implements EventSubscriberInterface
12
{
13
    /**
14
     * @var int
15
     */
16
    private $partyId;
17
18
    /**
19
     * @param string $topicPath
20
     * @param int $partyId
21
     */
22
    public function __construct($topicPath, $partyId)
23
    {
24
        parent::__construct($topicPath);
25
26
        $this->partyId = $partyId;
27
    }
28
29
    /**
30
     * @param AwaleEvent $event
31
     */
32
    public function onPlay(AwaleEvent $event)
33
    {
34
        $this->broadcast(array(
35
            'type' => 'played',
36
            'move' => $event->getAwaleParty()->getLastMove(),
37
            'current_player' => $event->getAwaleParty()->getCurrentPlayer(),
38
        ));
39
    }
40
41
    /**
42
     * @param SlotEvent $event
43
     */
44
    public function onPlayerJoin(SlotEvent $event)
45
    {
46
        if ($event->getParty()->getId() !== $this->partyId) {
47
            return;
48
        }
49
50
        $this->broadcast(array(
51
            'type' => 'join',
52
            'party' => $event->getParty(),
53
        ));
54
    }
55
56
    /**
57
     * @param PartyEvent $event
58
     */
59
    public function onPartyStart(PartyEvent $event)
60
    {
61
        if ($event->getParty()->getId() !== $this->partyId) {
62
            return;
63
        }
64
65
        $this->broadcast(array(
66
            'type' => 'party_start',
67
        ));
68
    }
69
70
    /**
71
     * @param AwaleEvent $event
72
     */
73
    public function onPartyEnd(AwaleEvent $event)
74
    {
75
        $this->broadcast(array(
76
            'type' => 'party_end',
77
            'winner' => $event->getWinner(),
78
        ));
79
    }
80
81
    /**
82
     * {@InheritDoc}
83
     */
84
    public static function getSubscribedEvents()
85
    {
86
        return array(
87
            SlotEvent::JOIN_AFTER => array(
88
                array('onPlayerJoin'),
89
            ),
90
            PartyEvent::STARTED => array(
91
                array('onPartyStart'),
92
            ),
93
            AwaleEvent::PLAY => array(
94
                array('onPlay'),
95
            ),
96
            AwaleEvent::PARTY_END => array(
97
                array('onPartyEnd'),
98
            ),
99
        );
100
    }
101
}
102