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

WindowsGuiHandler   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 0
loc 87
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B addToDisplay() 0 16 5
A addToHide() 0 13 4
A onPlayerConnect() 0 4 1
A onPlayerDisconnect() 0 6 2
A onPlayerInfoChanged() 0 4 1
A onPlayerAlliesChanged() 0 4 1
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
        if (count($logins) == 1 && !$manialink->getUserGroup()->isPersistent()) {
48
            $login = $logins[0];
49
50
            // If a window is already displayed hide it. We wish to have 1 window max.
51
            if (isset($this->userWindows[$login]) && $this->userWindows[$login]->getId() != $manialink->getId()) {
52
                $this->guiHandler->addToHide($this->userWindows[$login]);
53
            }
54
55
            $this->userWindows[$login] = $manialink;
56
        }
57
58
        $this->guiHandler->addToDisplay($manialink);
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function addToHide(ManialinkInterface $manialink)
65
    {
66
        $logins = $manialink->getUserGroup()->getLogins();
67
        if (count($logins) == 1 && !$manialink->getUserGroup()->isPersistent()) {
68
            $login = $logins[0];
69
70
            if (isset($this->userWindows[$login])) {
71
                unset($this->userWindows[$login]);
72
            }
73
        }
74
75
        $this->guiHandler->addToHide($manialink);
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function onPlayerConnect(Player $player)
82
    {
83
        // Nothing
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function onPlayerDisconnect(Player $player, $disconnectionReason)
90
    {
91
        if (isset($this->userWindows[$player->getLogin()])) {
92
            unset($this->userWindows[$player->getLogin()]);
93
        }
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public function onPlayerInfoChanged(Player $oldPlayer, Player $player)
100
    {
101
        // Nothing
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107
    public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
108
    {
109
        // Nothing
110
    }
111
}
112