|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Framework\Core\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Framework\Core\Model\Helpers\ChatNotificationInterface; |
|
6
|
|
|
use eXpansion\Framework\Core\Services\Console; |
|
7
|
|
|
use eXpansion\Framework\Core\Storage\PlayerStorage; |
|
8
|
|
|
use Maniaplanet\DedicatedServer\Connection; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class ChatNotification |
|
12
|
|
|
* |
|
13
|
|
|
* @package eXpansion\Framework\Core\Helpers; |
|
14
|
|
|
* @author oliver de Cramer <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class ChatNotification implements ChatNotificationInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var Connection */ |
|
19
|
|
|
protected $connection; |
|
20
|
|
|
|
|
21
|
|
|
/** @var Translations */ |
|
22
|
|
|
protected $translations; |
|
23
|
|
|
|
|
24
|
|
|
/** @var PlayerStorage */ |
|
25
|
|
|
protected $playerStorage; |
|
26
|
|
|
|
|
27
|
|
|
/** @var Console */ |
|
28
|
|
|
protected $console; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* ChatNotification constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param Connection $connection |
|
34
|
|
|
* @param Translations $translations |
|
35
|
|
|
* @param PlayerStorage $playerStorage |
|
36
|
|
|
*/ |
|
37
|
6 |
|
public function __construct( |
|
38
|
|
|
Connection $connection, |
|
39
|
|
|
Translations $translations, |
|
40
|
|
|
PlayerStorage $playerStorage, |
|
41
|
|
|
Console $console |
|
42
|
|
|
) { |
|
43
|
6 |
|
$this->connection = $connection; |
|
44
|
6 |
|
$this->translations = $translations; |
|
45
|
6 |
|
$this->playerStorage = $playerStorage; |
|
46
|
6 |
|
$this->console = $console; |
|
47
|
6 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Send message. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $messageId |
|
53
|
|
|
* @param string|string[]|null $to |
|
54
|
|
|
* @param string[] $parameters |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function sendMessage($messageId, $to = null, $parameters = []) |
|
57
|
|
|
{ |
|
58
|
2 |
|
$message = $messageId; |
|
59
|
|
|
|
|
60
|
2 |
|
if (is_string($to)) { |
|
61
|
1 |
|
$player = $this->playerStorage->getPlayerInfo($to); |
|
62
|
1 |
|
$message = $this->translations->getTranslation($messageId, $parameters, strtolower($player->getLanguage())); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
View Code Duplication |
if (is_array($to)) { |
|
|
|
|
|
|
66
|
|
|
$to = implode(",", $to); |
|
67
|
|
|
$message = $this->translations->getTranslations($messageId, $parameters); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
2 |
View Code Duplication |
if ($to === null) { |
|
|
|
|
|
|
71
|
1 |
|
$message = $this->translations->getTranslations($messageId, $parameters); |
|
72
|
1 |
|
$this->console->writeln(end($message)['Text']); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
$this->connection->chatSendServerMessage($message, $to); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
2 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Return messageId with arguments as a string |
|
81
|
|
|
* Usage: used for retrieving partials for chat messages |
|
82
|
|
|
* * defaults to English locale, without parameters |
|
83
|
|
|
* |
|
84
|
|
|
* @param $messageId |
|
85
|
|
|
* @param array $parameters |
|
86
|
|
|
* @param string $locale |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getMessage($messageId, $parameters = [], $locale = "en") |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->translations->getTranslation($messageId, $parameters, $locale); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
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.