Completed
Pull Request — master (#294)
by De Cramer
17:11
created

ChatNotification::messageFirstPlaceNew()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php
2
3
namespace eXpansion\Bundle\LocalRecords\Plugins;
4
5
use eXpansion\Bundle\LocalRecords\DataProviders\Listener\RecordsDataListener;
6
use eXpansion\Bundle\LocalRecords\Model\Record;
7
use eXpansion\Framework\Config\Model\ConfigInterface;
8
use eXpansion\Framework\Core\Helpers\ChatNotification as ChatNotificationHelper;
9
use eXpansion\Framework\Core\Helpers\Time;
10
use eXpansion\Framework\Core\Helpers\TMString;
11
use eXpansion\Framework\Core\Storage\PlayerStorage;
12
13
14
/**
15
 * Class ChatNotificaiton
16
 *
17
 * @package eXpansion\Bundle\LocalRecords\Plugins;
18
 * @author  oliver de Cramer <[email protected]>
19
 */
20
class ChatNotification implements RecordsDataListener
21
{
22
    /** @var ChatNotificationHelper */
23
    protected $chatNotification;
24
25
    /** @var Time */
26
    protected $timeFormater;
27
28
    /** @var PlayerStorage */
29
    protected $playerStorage;
30
31
    /** @var string */
32
    protected $translationPrefix;
33
34
    /** @var ConfigInterface */
35
    protected $positionForPublicMessage;
36
37
    /**
38
     * ChatNotification constructor.
39
     *
40
     * @param ChatNotificationHelper $chatNotification
41
     * @param Time                   $timeFormater
42
     * @param PlayerStorage          $playerStorage
43
     * @param string                 $translationPrefix
44
     * @param ConfigInterface        $positionForPublicMessage
45
     */
46 15
    public function __construct(
47
        ChatNotificationHelper $chatNotification,
48
        Time $timeFormater,
49
        PlayerStorage $playerStorage,
50
        $translationPrefix,
51
        ConfigInterface $positionForPublicMessage
52
    ) {
53 15
        $this->chatNotification = $chatNotification;
54 15
        $this->timeFormater = $timeFormater;
55 15
        $this->playerStorage = $playerStorage;
56 15
        $this->translationPrefix = $translationPrefix;
57 15
        $this->positionForPublicMessage = $positionForPublicMessage;
58 15
    }
59
60
    /**
61
     * Called when local records are loaded.
62
     *
63
     * @param Record[] $records
64
     * @throws \Propel\Runtime\Exception\PropelException
65
     */
66 2
    public function onLocalRecordsLoaded($records)
67
    {
68 2
        if (!empty($records)) {
69 1
            $firstRecord = $records[0];
70
71 1
            $this->sendMessage('loaded.top1', null, [
72 1
                '%nickname%' => TMString::trimStyles($firstRecord->getPlayer()->getNickname()),
73 1
                '%score%' => $this->timeFormater->timeToText($firstRecord->getScore(), true),
74
            ]);
75
76 1
            $online = $this->playerStorage->getOnline();
77 1
            $count = count($records);
78 1
            for ($i = 1; $i < $count; $i++) {
79 1
                if (isset($online[$records[$i]->getPlayer()->getLogin()])) {
80 1
                    $this->sendMessage('loaded.any', $records[$i]->getPlayer()->getLogin(), [
81 1
                        '%nickname%' => TMString::trimStyles($records[$i]->getPlayer()->getNickname()),
82 1
                        '%score%' => $this->timeFormater->timeToText($records[$i]->getScore(), true),
83 1
                        '%position%' => $i + 1,
84
                    ]);
85
                }
86
            }
87
        }
88 2
    }
89
90
    /**
91
     * Called when a player finishes map for the very first time (basically first record).
92
     *
93
     * @param Record   $record
94
     * @param Record[] $records
95
     * @param int      $position
96
     * @throws \Propel\Runtime\Exception\PropelException
97 1
     */
98
    public function onLocalRecordsFirstRecord(Record $record, $records, $position)
99 1
    {
100 1
        $this->messageFirstPlaceNew($record);
101
    }
102
103
    /**
104
     * Called when a player finishes map and does same time as before.
105
     *
106
     * @param Record   $record
107
     * @param Record   $oldRecord
108
     * @param Record[] $records
109
     *
110
     * @return void
111 1
     */
112
    public function onLocalRecordsSameScore(Record $record, Record $oldRecord, $records)
113
    {
114 1
        // Nothing.
115
    }
116
117
    /**
118
     * Called when a player finishes map with better time and has better position.
119
     *
120
     * @param Record   $record
121
     * @param Record   $oldRecord
122
     * @param Record[] $records
123
     * @param int      $position
124
     * @param int      $oldPosition
125
     *
126
     * @return void
127 6
     * @throws \Propel\Runtime\Exception\PropelException
128
     */
129 6
    public function onLocalRecordsBetterPosition(Record $record, Record $oldRecord, $records, $position, $oldPosition)
130 1
    {
131
        if ($position == 1 && $oldPosition == null) {
132 1
            $this->messageFirstPlaceNew($record);
133
134
            return;
135
        }
136 5
137 5
        // Check to who to send.
138 1
        $to = null;
139
        if ($position > $this->positionForPublicMessage->get()) {
140
            $to = $record->getPlayer()->getLogin();
141
        }
142 5
143 5
        // Check which message to send.
144 1
        $msg = 'better';
145
        if ($oldPosition == null) {
146
            $msg = 'new';
147
        }
148 5
149 1
        // Check for top status
150 View Code Duplication
        if ($position == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
151 4
            $msg .= '.top1';
152 1
        } else {
153
            if ($position <= 5) {
154 3
                $msg .= '.top5';
155
            } else {
156
                $msg .= '.any';
157
            }
158 5
        }
159 5
160 5
        $securedBy = $this->getSecuredBy($record, $oldRecord);
161 5
        $this->sendMessage(
162
            $msg,
163 5
            $to,
164 5
            [
165 5
                '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickName()),
166 5
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
167 5
                '%position%' => $position,
168
                '%old_position%' => $oldPosition,
169
                '%by%' => $securedBy,
170 5
            ]
171
        );
172
    }
173
174
    /**
175
     * Called when a player finishes map with better time but keeps same position.
176
     *
177
     * @param Record   $record
178
     * @param Record   $oldRecord
179
     * @param Record[] $records
180
     * @param          $position
181
     *
182 6
     * @return void
183
     * @throws \Propel\Runtime\Exception\PropelException
184
     */
185 6
    public function onLocalRecordsSamePosition(Record $record, Record $oldRecord, $records, $position)
186 6
    {
187 3
        // Check to who to send.
188
        $to = null;
189
        if ($position > $this->positionForPublicMessage->get()) {
190
            $to = $record->getPlayer()->getLogin();
191 6
        }
192 6
193 1
        // Check which message to send.
194
        $msg = 'secures';
195 5 View Code Duplication
        if ($position == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
196 1
            $msg .= '.top1';
197
        } else {
198 4
            if ($position <= 5) {
199
                $msg .= '.top5';
200
            } else {
201
                $msg .= '.any';
202 6
            }
203 6
        }
204 6
205 6
        $securedBy = $this->getSecuredBy($record, $oldRecord);
206
        $this->sendMessage(
207 6
            $msg,
208 6
            $to,
209 6
            [
210 6
                '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickName()),
211
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
212
                '%position%' => $position,
213 6
                '%by%' => $securedBy,
214
            ]
215 11
        );
216
    }
217 11
218 11
    /**
219
     * @param Record $record
220 11
     * @param Record $oldRecord
221 1
     * @return string
222
     */
223 10
    protected function getSecuredBy(Record $record, Record $oldRecord)
224 1
    {
225
        if ($oldRecord->getScore()) {
226
            $securedBy = $this->timeFormater->timeToText($oldRecord->getScore() - $record->getScore(), true);
227
228 11
            if (substr($securedBy, 0, 4) === "00:0") {
229
                $securedBy = substr($securedBy, 4);
230
            } else {
231
                if (substr($securedBy, 0, 3) === "00:") {
232
                    $securedBy = substr($securedBy, 3);
233
                }
234 2
            }
235
236 2
            return '-'.$securedBy;
237 2
        }
238 2
239
        return $this->timeFormater->timeToText(0);
240 2
    }
241 2
242 2
    /**
243
     * @param Record $record
244
     * @throws \Propel\Runtime\Exception\PropelException
245 2
     */
246
    protected function messageFirstPlaceNew(Record $record)
247
    {
248
        $this->sendMessage(
249
            'new.top1',
250
            null,
251 14
            [
252
                '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickname()),
253 14
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
254 14
                '%position%' => 1,
255 14
            ]
256 14
        );
257
    }
258 14
259
    /**
260
     * @param string      $message
261
     * @param null|string $recipient
262
     * @param             $params
263
     */
264
    protected function sendMessage($message, $recipient, $params)
265
    {
266
        $this->chatNotification->sendMessage(
267
            $this->translationPrefix.'.'.$message,
268
            $recipient,
269
            $params
270
        );
271
    }
272
}
273