Completed
Push — master ( b2309d...0cebbd )
by De Cramer
15s
created

CustomChat::sendChat()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
ccs 0
cts 16
cp 0
cc 3
eloc 25
nc 4
nop 4
crap 12
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
        $replacements = [
142
            "(y)" => "",
143
            ":yes:" => "",
144
            ":no:" => "",
145
            "(n)" => "",
146
            ":happy:" => "",
147
            ":smile:" => "",
148
            ":sad:" => "",
149
            ":heart:" => '$d00$z$s',
150
            "<3" => '$d00$z$s',
151
152
        ];
153
        // fix for chat...
154
        $nick = str_replace(['$<', '$>'], '', $nick);
155
        $text = str_replace(['$<', '$>'], '', $text);
156
        $text = str_replace(array_keys($replacements), array_values($replacements), $text);
157
        $separator = '';
158
        if ($this->adminGroups->isAdmin($player->getLogin())) {
159
            $separator = '';
160
        }
161
162
        try {
163
            $this->connection->chatSendServerMessage(
164
                '$fff$<'.$nick.'$>$z$s$fff '.$separator.' '.$color.$text, $group
165
            );
166
        } catch (\Exception $e) {
167
            $this->console->writeln('$ff0 error while sending chat: $fff'.$e->getMessage());
168
        }
169
    }
170
171
    /**
172
     * Set the status of the plugin
173
     *
174
     * @param boolean $status
175
     *
176
     * @return void
177
     */
178
    public function setStatus($status)
179
    {
180
        $this->enabled = $status;
181
    }
182
183
    /**
184
     * called at eXpansion init
185
     *
186
     * @return void
187
     */
188
    public function onApplicationInit()
189
    {
190
        // TODO: Implement onApplicationInit() method.
191
    }
192
193
    /**
194
     * called when init is done and callbacks are enabled
195
     *
196
     * @return void
197
     */
198
    public function onApplicationReady()
199
    {
200
        try {
201
            $this->connection->chatEnableManualRouting();
202
        } catch (\Exception $e) {
203
            $this->console->writeln('Error while enabling custom chat: $f00'.$e->getMessage());
204
        }
205
    }
206
207
    /**
208
     * called when requesting application stop
209
     *
210
     * @return void
211
     */
212
    public function onApplicationStop()
213
    {
214
        // TODO: Implement onApplicationStop() method.
215
    }
216
}
217