|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Bundle\LocalRecords\Plugins; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Framework\Core\Helpers\ChatNotification as ChatNotificationHelper; |
|
6
|
|
|
use eXpansion\Bundle\LocalRecords\DataProviders\Listener\RecordsDataListener; |
|
7
|
|
|
use eXpansion\Bundle\LocalRecords\Entity\Record; |
|
8
|
|
|
use eXpansion\Framework\Core\Helpers\Time; |
|
9
|
|
|
use eXpansion\Framework\Core\Storage\PlayerStorage; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ChatNotificaiton |
|
14
|
|
|
* |
|
15
|
|
|
* @package eXpansion\Bundle\LocalRecords\Plugins; |
|
16
|
|
|
* @author oliver de Cramer <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class ChatNotification implements RecordsDataListener |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var ChatNotificationHelper */ |
|
21
|
|
|
protected $chatNotification; |
|
22
|
|
|
|
|
23
|
|
|
/** @var PlayerStorage */ |
|
24
|
|
|
protected $playerStorage; |
|
25
|
|
|
|
|
26
|
|
|
/** @var Time */ |
|
27
|
|
|
protected $timeFormater; |
|
28
|
|
|
|
|
29
|
|
|
/** @var string */ |
|
30
|
|
|
protected $translationPrefix; |
|
31
|
|
|
|
|
32
|
|
|
/** @var int */ |
|
33
|
|
|
protected $positionForPublicMessage; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* ChatNotification constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param ChatNotificationHelper $chatNotification |
|
39
|
|
|
* @param PlayerStorage $playerStorage |
|
40
|
|
|
* @param Time $timeFormater |
|
41
|
|
|
* @param string $translationPrefix |
|
42
|
|
|
* @param int $positionForPublicMessage |
|
43
|
|
|
*/ |
|
44
|
15 |
|
public function __construct( |
|
45
|
|
|
ChatNotificationHelper $chatNotification, |
|
46
|
|
|
PlayerStorage $playerStorage, |
|
47
|
|
|
Time $timeFormater, |
|
48
|
|
|
$translationPrefix, |
|
49
|
|
|
$positionForPublicMessage |
|
50
|
|
|
) { |
|
51
|
15 |
|
$this->chatNotification = $chatNotification; |
|
52
|
15 |
|
$this->playerStorage = $playerStorage; |
|
53
|
15 |
|
$this->timeFormater = $timeFormater; |
|
54
|
15 |
|
$this->translationPrefix = $translationPrefix; |
|
55
|
15 |
|
$this->positionForPublicMessage = $positionForPublicMessage; |
|
56
|
15 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Called when local records are loaded. |
|
60
|
|
|
* |
|
61
|
|
|
* @param Record[] $records |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function onLocalRecordsLoaded($records) |
|
64
|
|
|
{ |
|
65
|
2 |
|
if (!empty($records)) { |
|
66
|
1 |
|
$firstRecord = $records[0]; |
|
67
|
|
|
|
|
68
|
1 |
|
$this->sendMessage('loaded.top1', null, [ |
|
69
|
1 |
|
'%nickname%' => $firstRecord->getPlayerLogin(), // TODO get player nickname from database. |
|
70
|
1 |
|
'%score%' => $this->timeFormater->timeToText($firstRecord->getScore(), true), |
|
71
|
|
|
]); |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
1 |
|
$onlinePlayers = $this->playerStorage->getOnline(); |
|
75
|
1 |
|
for ($i = 1; $i < count($records); $i++) { |
|
|
|
|
|
|
76
|
1 |
|
if (isset($onlinePlayers[$records[$i]->getPlayerLogin()])) { |
|
77
|
1 |
|
$this->sendMessage('loaded.any', $records[$i]->getPlayerLogin(), [ |
|
78
|
1 |
|
'%nickname%' => $onlinePlayers[$records[$i]->getPlayerLogin()]->getNickName(), |
|
79
|
1 |
|
'%score%' => $this->timeFormater->timeToText($records[$i]->getScore(), true), |
|
80
|
1 |
|
'%position%' => $i + 1, |
|
81
|
|
|
]); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
2 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Called when a player finishes map for the very first time (basically first record). |
|
89
|
|
|
* |
|
90
|
|
|
* @param Record $record |
|
91
|
|
|
* @param Record[] $records |
|
92
|
|
|
* @param int $position |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function onLocalRecordsFirstRecord(Record $record, $records, $position) |
|
95
|
|
|
{ |
|
96
|
1 |
|
$this->messageFirstPlaceNew($record); |
|
97
|
1 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Called when a player finishes map and does same time as before. |
|
101
|
|
|
* |
|
102
|
|
|
* @param Record $record |
|
103
|
|
|
* @param Record $oldRecord |
|
104
|
|
|
* @param Record[] $records |
|
105
|
|
|
* |
|
106
|
|
|
* @return void |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function onLocalRecordsSameScore(Record $record, Record $oldRecord, $records) |
|
109
|
|
|
{ |
|
110
|
|
|
// Nothing. |
|
111
|
1 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Called when a player finishes map with better time and has better position. |
|
115
|
|
|
* |
|
116
|
|
|
* @param Record $record |
|
117
|
|
|
* @param Record $oldRecord |
|
118
|
|
|
* @param Record[] $records |
|
119
|
|
|
* @param int $position |
|
120
|
|
|
* @param int $oldPosition |
|
121
|
|
|
* |
|
122
|
|
|
* @return void |
|
123
|
|
|
*/ |
|
124
|
6 |
|
public function onLocalRecordsBetterPosition(Record $record, Record $oldRecord, $records, $position, $oldPosition) |
|
125
|
|
|
{ |
|
126
|
6 |
|
if ($position == 1 && $oldPosition == null) { |
|
127
|
1 |
|
$this->messageFirstPlaceNew($record); |
|
128
|
1 |
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// Check to who to send. |
|
132
|
5 |
|
$to = null; |
|
133
|
5 |
|
if ($position > $this->positionForPublicMessage) { |
|
134
|
1 |
|
$to = $record->getPlayerLogin(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// Check which message to send. |
|
138
|
5 |
|
$msg = 'better'; |
|
139
|
5 |
|
if ($oldPosition == null) { |
|
140
|
1 |
|
$msg = 'new'; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// Check for top status |
|
144
|
5 |
View Code Duplication |
if ($position == 1) { |
|
|
|
|
|
|
145
|
1 |
|
$msg .= '.top1'; |
|
146
|
|
|
} else { |
|
147
|
4 |
|
if ($position <= 5) { |
|
148
|
1 |
|
$msg .= '.top5'; |
|
149
|
|
|
} else { |
|
150
|
3 |
|
$msg .= '.any'; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
5 |
|
$securedBy = $this->getSecuredBy($record, $oldRecord); |
|
155
|
5 |
|
$this->sendMessage( |
|
156
|
5 |
|
$msg, |
|
157
|
5 |
|
$to, |
|
158
|
|
|
[ |
|
159
|
5 |
|
'%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(), |
|
160
|
5 |
|
'%score%' => $this->timeFormater->timeToText($record->getScore(), true), |
|
161
|
5 |
|
'%position%' => $position, |
|
162
|
5 |
|
'%old_position%' => $oldPosition, |
|
163
|
5 |
|
'%by%' => $securedBy, |
|
164
|
|
|
] |
|
165
|
|
|
); |
|
166
|
5 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Called when a player finishes map with better time but keeps same position. |
|
170
|
|
|
* |
|
171
|
|
|
* @param Record $record |
|
172
|
|
|
* @param Record $oldRecord |
|
173
|
|
|
* @param Record[] $records |
|
174
|
|
|
* @param $position |
|
175
|
|
|
* |
|
176
|
|
|
* @return void |
|
177
|
|
|
*/ |
|
178
|
6 |
|
public function onLocalRecordsSamePosition(Record $record, Record $oldRecord, $records, $position) |
|
179
|
|
|
{ |
|
180
|
|
|
// Check to who to send. |
|
181
|
6 |
|
$to = null; |
|
182
|
6 |
|
if ($position > $this->positionForPublicMessage) { |
|
183
|
3 |
|
$to = $record->getPlayerLogin(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
// Check which message to send. |
|
187
|
6 |
|
$msg = 'secures'; |
|
188
|
6 |
View Code Duplication |
if ($position == 1) { |
|
|
|
|
|
|
189
|
1 |
|
$msg .= '.top1'; |
|
190
|
|
|
} else { |
|
191
|
5 |
|
if ($position <= 5) { |
|
192
|
1 |
|
$msg .= '.top5'; |
|
193
|
|
|
} else { |
|
194
|
4 |
|
$msg .= '.any'; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
6 |
|
$securedBy = $this->getSecuredBy($record, $oldRecord); |
|
199
|
6 |
|
$this->sendMessage( |
|
200
|
6 |
|
$msg, |
|
201
|
6 |
|
$to, |
|
202
|
|
|
[ |
|
203
|
6 |
|
'%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(), |
|
204
|
6 |
|
'%score%' => $this->timeFormater->timeToText($record->getScore(), true), |
|
205
|
6 |
|
'%position%' => $position, |
|
206
|
6 |
|
'%by%' => $securedBy, |
|
207
|
|
|
] |
|
208
|
|
|
); |
|
209
|
6 |
|
} |
|
210
|
|
|
|
|
211
|
11 |
|
protected function getSecuredBy(Record $record, Record $oldRecord) |
|
212
|
|
|
{ |
|
213
|
11 |
|
$securedBy = $this->timeFormater->timeToText($oldRecord->getScore() - $record->getScore(), true); |
|
214
|
|
|
|
|
215
|
11 |
|
if (substr($securedBy, 0, 4) === "00:0") { |
|
216
|
1 |
|
$securedBy = substr($securedBy, 4); |
|
217
|
|
|
} else { |
|
218
|
10 |
|
if (substr($securedBy, 0, 3) === "00:") { |
|
219
|
1 |
|
$securedBy = substr($securedBy, 3); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
11 |
|
return '-'.$securedBy; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
2 |
|
protected function messageFirstPlaceNew(Record $record) |
|
227
|
|
|
{ |
|
228
|
2 |
|
$this->sendMessage( |
|
229
|
2 |
|
'new.top1', |
|
230
|
2 |
|
null, |
|
231
|
|
|
[ |
|
232
|
2 |
|
'%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(), |
|
233
|
2 |
|
'%score%' => $this->timeFormater->timeToText($record->getScore(), true), |
|
234
|
2 |
|
'%position%' => 1, |
|
235
|
|
|
] |
|
236
|
|
|
); |
|
237
|
2 |
|
} |
|
238
|
|
|
|
|
239
|
14 |
|
protected function sendMessage($message, $recipe, $params) |
|
240
|
|
|
{ |
|
241
|
14 |
|
$this->chatNotification->sendMessage( |
|
242
|
14 |
|
$this->translationPrefix.'.'.$message, |
|
243
|
14 |
|
$recipe, |
|
244
|
14 |
|
$params |
|
245
|
|
|
); |
|
246
|
14 |
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: