Completed
Pull Request — master (#120)
by De Cramer
09:28
created

WindowsGuiHandler::addToDisplay()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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