Completed
Push — master ( 7c5112...0705e2 )
by
unknown
15s
created

ChatNotification::onLocalRecordsSamePosition()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 20

Duplication

Lines 7
Ratio 21.88 %

Code Coverage

Tests 19
CRAP Score 4

Importance

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

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
231
    }
232
233 2
    protected function messageFirstPlaceNew(Record $record)
234
    {
235 2
        $this->sendMessage(
236 2
            'new.top1',
237 2
            null,
238
            [
239 2
                '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickname()),
240 2
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
241 2
                '%position%' => 1,
242
            ]
243
        );
244 2
    }
245
246
    /**
247
     * @param string      $message
248
     * @param null|string $recipe
249
     */
250 14
    protected function sendMessage($message, $recipe, $params)
251
    {
252 14
        $this->chatNotification->sendMessage(
253 14
            $this->translationPrefix.'.'.$message,
254 14
            $recipe,
255 14
            $params
256
        );
257 14
    }
258
}
259