|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Framework\Core\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Framework\Core\Model\Helpers\ChatNotificationInterface; |
|
6
|
|
|
use eXpansion\Framework\Core\Model\UserGroups\Group; |
|
7
|
|
|
use eXpansion\Framework\Core\Services\Console; |
|
8
|
|
|
use eXpansion\Framework\Core\Storage\PlayerStorage; |
|
9
|
|
|
use Maniaplanet\DedicatedServer\Connection; |
|
10
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ChatNotification |
|
14
|
|
|
* |
|
15
|
|
|
* @package eXpansion\Framework\Core\Helpers; |
|
16
|
|
|
* @author oliver de Cramer <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class ChatNotification implements ChatNotificationInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var Connection */ |
|
21
|
|
|
protected $connection; |
|
22
|
|
|
|
|
23
|
|
|
/** @var Translations */ |
|
24
|
|
|
protected $translations; |
|
25
|
|
|
|
|
26
|
|
|
/** @var PlayerStorage */ |
|
27
|
|
|
protected $playerStorage; |
|
28
|
|
|
|
|
29
|
|
|
/** @var Console */ |
|
30
|
|
|
protected $console; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* ChatNotification constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param Connection $connection |
|
36
|
|
|
* @param Translations $translations |
|
37
|
|
|
* @param PlayerStorage $playerStorage |
|
38
|
|
|
*/ |
|
39
|
8 |
|
public function __construct( |
|
40
|
|
|
Connection $connection, |
|
41
|
|
|
Translations $translations, |
|
42
|
|
|
PlayerStorage $playerStorage, |
|
43
|
|
|
Console $console |
|
44
|
|
|
) { |
|
45
|
8 |
|
$this->connection = $connection; |
|
46
|
8 |
|
$this->translations = $translations; |
|
47
|
8 |
|
$this->playerStorage = $playerStorage; |
|
48
|
8 |
|
$this->console = $console; |
|
49
|
8 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Send message. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $messageId |
|
55
|
|
|
* @param string|string[]|Group|null $to |
|
56
|
|
|
* @param string[] $parameters |
|
57
|
|
|
*/ |
|
58
|
3 |
|
public function sendMessage($messageId, $to = null, $parameters = []) |
|
59
|
|
|
{ |
|
60
|
3 |
|
$message = $messageId; |
|
61
|
|
|
|
|
62
|
3 |
|
if (is_string($to)) { |
|
63
|
1 |
|
$player = $this->playerStorage->getPlayerInfo($to); |
|
64
|
1 |
|
$message = $this->translations->getTranslation($messageId, $parameters, strtolower($player->getLanguage())); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
if (is_array($to)) { |
|
68
|
1 |
|
$to = implode(",", $to); |
|
69
|
1 |
|
$message = $this->translations->getTranslations($messageId, $parameters); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
if ($to instanceof Group) { |
|
73
|
|
|
$to = implode(",", $to->getLogins()); |
|
74
|
|
|
$message = $this->translations->getTranslations($messageId, $parameters); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
3 |
|
if ($to === null || $to instanceof Group) { |
|
78
|
1 |
|
$message = $this->translations->getTranslations($messageId, $parameters); |
|
79
|
1 |
|
$this->console->writeln(end($message)['Text']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
3 |
|
$this->connection->chatSendServerMessage($message, $to); |
|
|
|
|
|
|
84
|
|
|
} catch (UnknownPlayerException $e) { |
|
85
|
|
|
// Nothing to do, it happens. |
|
86
|
|
|
} |
|
87
|
3 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Return messageId with arguments as a string |
|
91
|
|
|
* Usage: used for retrieving partials for chat messages |
|
92
|
|
|
* * defaults to English locale, without parameters |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $messageId |
|
95
|
|
|
* @param array $parameters |
|
96
|
|
|
* @param string $locale |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function getMessage($messageId, $parameters = [], $locale = "en") |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $this->translations->getTranslation($messageId, $parameters, $locale); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.