|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Bundle\InfoMessages\Plugins; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Bundle\InfoMessages\DependencyInjection\InfoMessagesExtension; |
|
6
|
|
|
use eXpansion\Framework\Config\Model\IntegerConfig; |
|
7
|
|
|
use eXpansion\Framework\Config\Model\TextListConfig; |
|
8
|
|
|
use eXpansion\Framework\Config\Services\ConfigManagerInterface; |
|
9
|
|
|
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer; |
|
10
|
|
|
use eXpansion\Framework\Core\Helpers\Translations; |
|
11
|
|
|
use eXpansion\Framework\Core\Services\DedicatedConnection\Factory; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class InfoMessagesPlugin |
|
15
|
|
|
* |
|
16
|
|
|
* @package eXpansion\Bundle\InfoMessages\Plugins; |
|
17
|
|
|
* @author oliver de Cramer <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class InfoMessagesPlugin implements ListenerInterfaceExpTimer |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var IntegerConfig */ |
|
22
|
|
|
protected $frequencyConfig; |
|
23
|
|
|
|
|
24
|
|
|
/** @var ConfigManagerInterface */ |
|
25
|
|
|
protected $configManager; |
|
26
|
|
|
|
|
27
|
|
|
/** @var Factory */ |
|
28
|
|
|
protected $factory; |
|
29
|
|
|
|
|
30
|
|
|
/** @var Translations */ |
|
31
|
|
|
protected $translations; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string[] */ |
|
34
|
|
|
protected $supportedLocales; |
|
35
|
|
|
|
|
36
|
|
|
/** @var int */ |
|
37
|
|
|
protected $lastSentTime; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* InfoMessagesPlugin constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param IntegerConfig $frequencyConfig |
|
43
|
|
|
* @param ConfigManagerInterface $configManager |
|
44
|
|
|
* @param string[] $supportedLocales |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct( |
|
47
|
|
|
IntegerConfig $frequencyConfig, |
|
48
|
|
|
ConfigManagerInterface $configManager, |
|
49
|
|
|
Factory $factory, |
|
50
|
|
|
Translations $translations, |
|
51
|
|
|
array $supportedLocales |
|
52
|
|
|
) { |
|
53
|
|
|
$this->frequencyConfig = $frequencyConfig; |
|
54
|
|
|
$this->configManager = $configManager; |
|
55
|
|
|
$this->factory = $factory; |
|
56
|
|
|
$this->translations = $translations; |
|
57
|
|
|
$this->supportedLocales = $supportedLocales; |
|
58
|
|
|
|
|
59
|
|
|
$this->lastSentTime = time(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritdoc |
|
64
|
|
|
*/ |
|
65
|
|
|
public function onEverySecond() |
|
66
|
|
|
{ |
|
67
|
|
|
if ($this->lastSentTime + $this->frequencyConfig->get() < time()) { |
|
68
|
|
|
$this->lastSentTime = time(); |
|
69
|
|
|
$this->sendRandomMessage(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Send a random information message to all players. |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function sendRandomMessage() |
|
77
|
|
|
{ |
|
78
|
|
|
$messagesPerLocale = []; |
|
79
|
|
|
|
|
80
|
|
|
foreach ($this->supportedLocales as $locale) { |
|
81
|
|
|
// Ideally the list of configs should have been passed in the construct, to achieve this we would need |
|
82
|
|
|
// to inject the config services throught the Extension. We have chose not to do this to simply simplify |
|
83
|
|
|
// the code to make it a bit easier for people that are new to symfony to understand. |
|
84
|
|
|
$values = $this->configManager->get(InfoMessagesExtension::CONFIG_PATH_PREFIX . $locale); |
|
85
|
|
|
|
|
86
|
|
|
if (count($values) > 0) { |
|
87
|
|
|
$messagesPerLocale[] = [ |
|
88
|
|
|
"Lang" => lcfirst($locale), |
|
89
|
|
|
"Text" => $this->translations->getTranslation($values[rand(0, count($values) - 1)]) |
|
90
|
|
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($messagesPerLocale) { |
|
|
|
|
|
|
95
|
|
|
// Normally we should always use the ChatNotification helper, but in this cas we wish to bypass the |
|
96
|
|
|
// translations. |
|
97
|
|
|
$this->factory->getConnection()->chatSendServerMessage($messagesPerLocale); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @inheritdoc |
|
103
|
|
|
*/ |
|
104
|
|
|
public function onPreLoop() |
|
105
|
|
|
{ |
|
106
|
|
|
// Nothing |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @inheritdoc |
|
111
|
|
|
*/ |
|
112
|
|
|
public function onPostLoop() |
|
113
|
|
|
{ |
|
114
|
|
|
// Nothing |
|
115
|
|
|
} |
|
116
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.