Completed
Pull Request — master (#130)
by
unknown
02:49
created

WindowsGuiHandler::addToDisplay()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
ccs 0
cts 9
cp 0
cc 5
eloc 8
nc 3
nop 1
crap 30
1
<?php
2
3
namespace eXpansion\Bundle\ImmersiveWindows\Plugins;
4
5
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceMpLegacyPlayer;
6
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
7
use eXpansion\Framework\Core\Model\Gui\Window;
8
use eXpansion\Framework\Core\Plugins\GuiHandler;
9
use eXpansion\Framework\Core\Plugins\GuiHandlerInterface;
10
use eXpansion\Framework\Core\Storage\Data\Player;
11
12
/**
13
 * Class WindowsGuiHandler, replaces the native GuiHandler only for windows type manialinks in order to :
14
 *  - Prevent more then 1 window to open.
15
 *  - Change the display
16
 *
17
 *
18
 * @author    de Cramer Oliver<[email protected]>
19
 * @copyright 2017 Smile
20
 * @package eXpansion\Bundle\Menu\Plugins
21
 */
22
class WindowsGuiHandler implements GuiHandlerInterface, ListenerInterfaceMpLegacyPlayer
23
{
24
    /** @var  GuiHandlerInterface */
25
    protected $guiHandler;
26
27
    /** @var Window[] */
28
    protected $userWindows = [];
29
30
    public function __construct(GuiHandler $guiHandler)
31
    {
32
        $this->guiHandler = $guiHandler;
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function addToDisplay(ManialinkInterface $manialink)
39
    {
40
        $logins = $manialink->getUserGroup()->getLogins();
41
        if (count($logins) == 1 && !$manialink->getUserGroup()->isPersistent()) {
42
            $login = $logins[0];
43
44
            // If a window is already displayed hide it. We wish to have 1 window max.
45
            if (isset($this->userWindows[$login]) && $this->userWindows[$login]->getId() != $manialink->getId()) {
46
                $this->guiHandler->addToHide($this->userWindows[$login]);
47
            }
48
49
            $this->userWindows[$login] = $manialink;
50
        }
51
52
        $this->guiHandler->addToDisplay($manialink);
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function addToHide(ManialinkInterface $manialink)
59
    {
60
        $logins = $manialink->getUserGroup()->getLogins();
61
        if (count($logins) == 1 && !$manialink->getUserGroup()->isPersistent()) {
62
            $login = $logins[0];
63
64
            if (isset($this->userWindows[$login])) {
65
                unset($this->userWindows[$login]);
66
            }
67
        }
68
69
        $this->guiHandler->addToHide($manialink);
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function onPlayerConnect(Player $player)
76
    {
77
        // Nothing
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function onPlayerDisconnect(Player $player, $disconnectionReason)
84
    {
85
        if (isset($this->userWindows[$player->getLogin()])) {
86
            unset($this->userWindows[$player->getLogin()]);
87
        }
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93
    public function onPlayerInfoChanged(Player $oldPlayer, Player $player)
94
    {
95
        // Nothing
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
102
    {
103
        // Nothing
104
    }
105
}
106