Completed
Pull Request — master (#161)
by De Cramer
02:59
created

CustomChat::onApplicationInit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 2
cp 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace eXpansion\Bundle\CustomChat\Plugins;
4
5
6
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
7
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpApplication;
8
use eXpansion\Framework\Core\Helpers\ChatNotification;
9
use eXpansion\Framework\Core\Services\Console;
10
use eXpansion\Framework\Core\Storage\Data\Player;
11
use eXpansion\Framework\Core\Storage\PlayerStorage;
12
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpLegacyChat;
13
use Maniaplanet\DedicatedServer\Connection;
14
15
16
class CustomChat implements ListenerInterfaceExpApplication, ListenerInterfaceMpLegacyChat
17
{
18
    /** @var Connection */
19
    protected $connection;
20
21
    /** @var Console */
22
    protected $console;
23
24
    /** @var AdminGroups */
25
    protected $adminGroups;
26
27
    /** @var bool */
28
    protected $enabled = true;
29
    /**
30
     * @var ChatNotification
31
     */
32
    private $chatNotification;
33
    /**
34
     * @var PlayerStorage
35
     */
36
    private $playerStorage;
37
38
    /**
39
     * CustomChat constructor.
40
     * @param Connection $connection
41
     * @param Console $console
42
     * @param AdminGroups $adminGroups
43
     * @param ChatNotification $chatNotification
44
     * @param PlayerStorage $playerStorage
45
     */
46 View Code Duplication
    function __construct(
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
        Connection $connection,
48
        Console $console,
49
        AdminGroups $adminGroups,
50
        ChatNotification $chatNotification,
51
        PlayerStorage $playerStorage
52
    ) {
53
        $this->connection = $connection;
54
        $this->console = $console;
55
        $this->adminGroups = $adminGroups;
56
        $this->chatNotification = $chatNotification;
57
        $this->playerStorage = $playerStorage;
58
    }
59
60
    /**
61
     * Called when a player chats.
62
     *
63
     * @param Player $player
64
     * @param $text
65
     *
66
     * @return void
67
     */
68
    public function onPlayerChat(Player $player, $text)
69
    {
70
        $text = trim($text);
71
        $nick = trim($player->getNickName());
72
73
        if ($player->getPlayerId() == 0) {
74
            return;
75
        }
76
77
        if ($player->getPlayerId() != 0 && substr($text, 0, 1) != "/") {
78
            $matches = [];
79
80
            if ($this->enabled || $this->adminGroups->isAdmin($player->getLogin())) {
81
                $matchFound = false;
82
                $matchLogin = [];
83
84
                if (preg_match_all("/(\s|\G)(\@(?P<login>[\w-\._]+)[\s]{0,1})/", $text, $matches)) {
85
                    $group = [];
86
87
                    foreach ($matches['login'] as $login) {
88
                        foreach ($this->playerStorage->getOnline() as $player2) {
89
                            if ($player2->getLogin() == $login) {
90
                                $matchFound = true;
91
                                $matchLogin[$player2->getLogin()] = $player2->getLogin();
92
                            } else {
93
                                if (!in_array($player->getLogin(), $matchLogin)) {
94
                                    $group[$player2->getLogin()] = $player2->getLogin();
95
                                }
96
                            }
97
                        }
98
                    }
99
100
                    $diff = array_diff($group, $matchLogin);
101
102
                    if ($matchFound) {
103
                        $this->sendChat($player, $text, '$ff0$o', $matchLogin);
104
105
                        if (count($diff) > 0) {
106
                            $this->sendChat($player, $text, '$ff0', $group);
107
                        }
108
                        $this->console->writeln('$ff0['.$nick.'$ff0] '.$text);
109
110
                        return;
111 View Code Duplication
                    } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
                        $this->sendChat($player, $text, '$ff0', null);
113
                        $this->console->writeln('$ff0['.$nick.'$ff0] '.$text);
114
115
                        return;
116
                    }
117 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
                    $this->sendChat($player, $text, '$ff0', null);
119
                    $this->console->writeln('$ff0['.$nick.'$ff0] '.$text);
120
                }
121
            } else {
122
                $this->console->writeln('$333['.$nick.'$333] '.$text);
123
                $this->chatNotification->sendMessage('expansion_customchat.chat.disabledstate',
124
                    $player->getLogin());
125
            }
126
        }
127
128
    }
129
130
    /**
131
     * @param Player $player
132
     * @param $text
133
     * @param $color
134
     * @param null $group
135
     */
136
    private function sendChat(Player $player, $text, $color, $group = null)
137
    {
138
        $nick = trim($player->getNickName());
139
        $nick = str_ireplace('$w', '', $nick);
140
        $nick = str_ireplace('$z', '$z$s', $nick);
141
142
        // fix for chat...
143
        $nick = str_replace(['$<', '$>'], '', $nick);
144
        $text = str_replace(['$<', '$>'], '', $text);
145
146
        $separator = '';
147
        if ($this->adminGroups->isAdmin($player->getLogin())) {
148
            $separator = '';
149
        }
150
151
        try {
152
            $this->connection->chatSendServerMessage(
153
                '$fff$<'.$nick.'$>$z$s$fff '.$separator.' '.$color.$text, $group
154
            );
155
        } catch (\Exception $e) {
156
            $this->console->writeln('$ff0 error while sending chat: $fff'.$e->getMessage());
157
        }
158
    }
159
160
    /**
161
     * Set the status of the plugin
162
     *
163
     * @param boolean $status
164
     *
165
     * @return void
166
     */
167
    public function setStatus($status)
168
    {
169
        $this->enabled = $status;
170
    }
171
172
    /**
173
     * called at eXpansion init
174
     *
175
     * @return void
176
     */
177
    public function onApplicationInit()
178
    {
179
        // TODO: Implement onApplicationInit() method.
180
    }
181
182
    /**
183
     * called when init is done and callbacks are enabled
184
     *
185
     * @return void
186
     */
187
    public function onApplicationReady()
188
    {
189
        try {
190
            $this->connection->chatEnableManualRouting();
191
        } catch (\Exception $e) {
192
            $this->console->writeln('Error while enabling custom chat: $f00'.$e->getMessage());
193
        }
194
    }
195
196
    /**
197
     * called when requesting application stop
198
     *
199
     * @return void
200
     */
201
    public function onApplicationStop()
202
    {
203
        // TODO: Implement onApplicationStop() method.
204
    }
205
}
206