Completed
Branch master (83a2f0)
by De Cramer
02:40
created

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