Completed
Pull Request — master (#130)
by De Cramer
02:47
created

WindowsGuiHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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