Completed
Pull Request — master (#59)
by
unknown
06:25 queued 03:32
created

CustomChat   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 121
rs 10
c 0
b 0
f 0
ccs 0
cts 61
cp 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C onPlayerChat() 0 39 7
A setStatus() 0 10 3
A onApplicationInit() 0 4 1
A onApplicationReady() 0 8 2
A onApplicationStop() 0 4 1
1
<?php
2
3
namespace eXpansion\Bundle\CustomChat\Plugins;
4
5
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
6
use eXpansion\Framework\Core\DataProviders\Listener\ApplicationDataListenerInterface;
7
use eXpansion\Framework\Core\DataProviders\Listener\ChatDataListenerInterface;
8
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
9
use eXpansion\Framework\Core\Services\Console;
10
use eXpansion\Framework\Core\Storage\Data\Player;
11
use Maniaplanet\DedicatedServer\Connection;
12
13
14
class CustomChat implements ApplicationDataListenerInterface, ChatDataListenerInterface, StatusAwarePluginInterface
15
{
16
    /** @var Connection */
17
    protected $connection;
18
19
    /** @var Console */
20
    protected $console;
21
22
    /** @var AdminGroups */
23
    protected $adminGroups;
24
25
    /** @var bool */
26
    protected $enabled = true;
27
28
    function __construct(Connection $connection, Console $console, AdminGroups $adminGroups)
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...
29
    {
30
        $this->connection = $connection;
31
        $this->console = $console;
32
        $this->adminGroups = $adminGroups;
33
    }
34
35
    /**
36
     * Called when a player chats.
37
     *
38
     * @param Player $player
39
     * @param $text
40
     *
41
     * @return void
42
     */
43
    public function onPlayerChat(Player $player, $text)
44
    {
45
        $text = trim($text);
46
        $from = trim($player->getNickName());
47
48
        if ($player->getPlayerId() === 0) {
49
            $from = '';
50
        }
51
52
        if ($player->getPlayerId() != 0 && substr($text, 0, 1) != "/" && $this->enabled) {
53
            $force = "";
54
            $nick = $player->getNickName();
55
56
            $nick = str_ireplace('$w', '', $nick);
57
            $nick = str_ireplace('$z', '$z$s', $nick);
58
            // fix for chat...
59
            $nick = str_replace('$<', '', $nick);
60
            $text = str_replace('$<', '', $text);
61
62
            try {
63
                $color = '$ff0';
64
                if ($this->adminGroups->isAdmin($player->getLogin())) {
65
                    $color = '$f90';
66
                }
67
68
                $this->connection->chatSendServerMessage(
69
                    '$fff$<'.$nick.'$z$s$>  '.$color.$force.$text,
70
                    null
71
                );
72
                $this->console->writeln('$ff0['.$from.'$ff0] '.$text);
73
            } catch (\Exception $e) {
74
                $this->console->writeln('$ff0 error while sending chat: $fff'.$e->getMessage());
75
            }
76
        } else {
77
            $this->connection->chatSendServerMessage('chat is disabled at the moment.', $player->getLogin());
78
79
        }
80
81
    }
82
83
    /**
84
     * Set the status of the plugin
85
     *
86
     * @param boolean $status
87
     *
88
     * @return null
89
     */
90
    public function setStatus($status)
91
    {
92
        if (!$status) {
93
            try {
94
                $this->connection->chatEnableManualRouting(false);
95
            } catch (\Exception $e) {
96
                $this->console->writeln('Error while disabling custom chat: $f00'.$e->getMessage());
97
            }
98
        }
99
    }
100
101
    /**
102
     * called at eXpansion init
103
     *
104
     * @return void
105
     */
106
    public function onApplicationInit()
107
    {
108
        // TODO: Implement onApplicationInit() method.
109
    }
110
111
    /**
112
     * called when init is done and callbacks are enabled
113
     *
114
     * @return void
115
     */
116
    public function onApplicationReady()
117
    {
118
        try {
119
            $this->connection->chatEnableManualRouting();
120
        } catch (\Exception $e) {
121
            $this->console->writeln('Error while enabling custom chat: $f00'.$e->getMessage());
122
        }
123
    }
124
125
    /**
126
     * called when requesting application stop
127
     *
128
     * @return void
129
     */
130
    public function onApplicationStop()
131
    {
132
        // TODO: Implement onApplicationStop() method.
133
    }
134
}
135