Completed
Push — master ( 2fcfdc...9f7c01 )
by De Cramer
11s
created

ChatNotification::onLocalRecordsLoaded()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 15
cts 15
cp 1
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 1
crap 4
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 int                    $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
     */
97 1
    public function onLocalRecordsFirstRecord(Record $record, $records, $position)
98
    {
99 1
        $this->messageFirstPlaceNew($record);
100 1
    }
101
102
    /**
103
     * Called when a player finishes map and does same time as before.
104
     *
105
     * @param Record   $record
106
     * @param Record   $oldRecord
107
     * @param Record[] $records
108
     *
109
     * @return void
110
     */
111 1
    public function onLocalRecordsSameScore(Record $record, Record $oldRecord, $records)
112
    {
113
        // Nothing.
114 1
    }
115
116
    /**
117
     * Called when a player finishes map with better time and has better position.
118
     *
119
     * @param Record   $record
120
     * @param Record   $oldRecord
121
     * @param Record[] $records
122
     * @param int      $position
123
     * @param int      $oldPosition
124
     *
125
     * @return void
126
     */
127 6
    public function onLocalRecordsBetterPosition(Record $record, Record $oldRecord, $records, $position, $oldPosition)
128
    {
129 6
        if ($position == 1 && $oldPosition == null) {
130 1
            $this->messageFirstPlaceNew($record);
131
132 1
            return;
133
        }
134
135
        // Check to who to send.
136 5
        $to = null;
137 5
        if ($position > $this->positionForPublicMessage->get()) {
138 1
            $to = $record->getPlayer()->getLogin();
139
        }
140
141
        // Check which message to send.
142 5
        $msg = 'better';
143 5
        if ($oldPosition == null) {
144 1
            $msg = 'new';
145
        }
146
147
        // Check for top status
148 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...
149 1
            $msg .= '.top1';
150
        } else {
151 4
            if ($position <= 5) {
152 1
                $msg .= '.top5';
153
            } else {
154 3
                $msg .= '.any';
155
            }
156
        }
157
158 5
        $securedBy = $this->getSecuredBy($record, $oldRecord);
159 5
        $this->sendMessage(
160 5
            $msg,
161 5
            $to,
162
            [
163 5
                '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickName()),
164 5
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
165 5
                '%position%' => $position,
166 5
                '%old_position%' => $oldPosition,
167 5
                '%by%' => $securedBy,
168
            ]
169
        );
170 5
    }
171
172
    /**
173
     * Called when a player finishes map with better time but keeps same position.
174
     *
175
     * @param Record   $record
176
     * @param Record   $oldRecord
177
     * @param Record[] $records
178
     * @param          $position
179
     *
180
     * @return void
181
     */
182 6
    public function onLocalRecordsSamePosition(Record $record, Record $oldRecord, $records, $position)
183
    {
184
        // Check to who to send.
185 6
        $to = null;
186 6
        if ($position > $this->positionForPublicMessage->get()) {
187 3
            $to = $record->getPlayer()->getLogin();
188
        }
189
190
        // Check which message to send.
191 6
        $msg = 'secures';
192 6 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...
193 1
            $msg .= '.top1';
194
        } else {
195 5
            if ($position <= 5) {
196 1
                $msg .= '.top5';
197
            } else {
198 4
                $msg .= '.any';
199
            }
200
        }
201
202 6
        $securedBy = $this->getSecuredBy($record, $oldRecord);
203 6
        $this->sendMessage(
204 6
            $msg,
205 6
            $to,
206
            [
207 6
                '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickName()),
208 6
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
209 6
                '%position%' => $position,
210 6
                '%by%' => $securedBy,
211
            ]
212
        );
213 6
    }
214
215 11
    protected function getSecuredBy(Record $record, Record $oldRecord)
216
    {
217 11
        if ($oldRecord->getScore()) {
218 11
            $securedBy = $this->timeFormater->timeToText($oldRecord->getScore() - $record->getScore(), true);
219
220 11
            if (substr($securedBy, 0, 4) === "00:0") {
221 1
                $securedBy = substr($securedBy, 4);
222
            } else {
223 10
                if (substr($securedBy, 0, 3) === "00:") {
224 1
                    $securedBy = substr($securedBy, 3);
225
                }
226
            }
227
228 11
            return '-'.$securedBy;
229
        }
230
231
        return $this->timeFormater->timeToText(0);
232
    }
233
234 2
    protected function messageFirstPlaceNew(Record $record)
235
    {
236 2
        $this->sendMessage(
237 2
            'new.top1',
238 2
            null,
239
            [
240 2
                '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickname()),
241 2
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
242 2
                '%position%' => 1,
243
            ]
244
        );
245 2
    }
246
247
    /**
248
     * @param string      $message
249
     * @param null|string $recipe
250
     */
251 14
    protected function sendMessage($message, $recipe, $params)
252
    {
253 14
        $this->chatNotification->sendMessage(
254 14
            $this->translationPrefix.'.'.$message,
255 14
            $recipe,
256 14
            $params
257
        );
258 14
    }
259
}
260