Completed
Pull Request — master (#68)
by De Cramer
05:45
created

ChatNotification::onLocalRecordsBetterPosition()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 40
Code Lines 24

Duplication

Lines 7
Ratio 17.5 %

Code Coverage

Tests 22
CRAP Score 7.0283

Importance

Changes 0
Metric Value
dl 7
loc 40
ccs 22
cts 24
cp 0.9167
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 24
nc 13
nop 5
crap 7.0283
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 14
    public function __construct(
45
        ChatNotificationHelper $chatNotification,
46
        PlayerStorage $playerStorage,
47
        Time $timeFormater,
48
        $translationPrefix,
49
        $positionForPublicMessage
50
    ) {
51 14
        $this->chatNotification = $chatNotification;
52 14
        $this->playerStorage = $playerStorage;
53 14
        $this->timeFormater = $timeFormater;
54 14
        $this->translationPrefix = $translationPrefix;
55 14
        $this->positionForPublicMessage = $positionForPublicMessage;
56 14
    }
57
58
59
    /**
60
     * Called when local records are loaded.
61
     *
62
     * @param Record[] $records
63
     */
64 1
    public function onLocalRecordsLoaded($records)
65
    {
66
        // TODO send chat information.
67 1
    }
68
69
    /**
70
     * Called when a player finishes map for the very first time (basically first record).
71
     *
72
     * @param Record   $record
73
     * @param Record[] $records
74
     * @param          $position
75
     */
76 1
    public function onLocalRecordsFirstRecord(Record $record, $records, $position)
77
    {
78 1
        $this->messageFirstPlaceNew($record);
79 1
    }
80
81
    /**
82
     * Called when a player finishes map and does same time as before.
83
     *
84
     * @param Record   $record
85
     * @param Record   $oldRecord
86
     * @param Record[] $records
87
     *
88
     * @return mixed
89
     */
90 1
    public function onLocalRecordsSameScore(Record $record, Record $oldRecord, $records)
91
    {
92
        // Nothing.
93 1
    }
94
95
    /**
96
     * Called when a player finishes map with better time and has better position.
97
     *
98
     * @param Record   $record
99
     * @param Record   $oldRecord
100
     * @param Record[] $records
101
     * @param int      $position
102
     * @param int      $oldPosition
103
     */
104 6
    public function onLocalRecordsBetterPosition(Record $record, Record $oldRecord, $records, $position, $oldPosition)
105
    {
106 6
        if ($position == 1 && $oldPosition == null) {
107 1
            return $this->messageFirstPlaceNew($record);
108
        }
109
110
        // Check to who to send.
111 5
        $to = null;
112 5
        if ($position > $this->positionForPublicMessage) {
113 1
            $to = $record->getPlayerLogin();
114
        }
115
116
        // Check which message to send.
117 5
        $msg = 'better';
118 5
        if ($oldPosition == null) {
119 1
            $msg = 'new';
120
        }
121
122
        // Check for top status
123 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...
124 1
            $msg .= '.top1';
125 4
        } else if ($position <= 5) {
126 1
            $msg .= '.top5';
127
        } else {
128 3
            $msg .= '.any';
129
        }
130
131 5
        $securedBy = $this->getSecuredBy($record, $oldRecord);
132 5
        $this->sendMessage(
133
            $msg,
134
            $to,
135
            [
136 5
                '%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(),
137 5
                '%score%' => $this->timeFormater->milisecondsToTrackmania($record->getScore(), true),
138 5
                '%position%' => $position,
139 5
                '%old_position%' => $oldPosition,
140 5
                '%by%' => $securedBy
141
            ]
142
        );
143 5
    }
144
145
    /**
146
     * Called when a player finishes map with better time but keeps same position.
147
     *
148
     * @param Record   $record
149
     * @param Record   $oldRecord
150
     * @param Record[] $records
151
     * @param          $position
152
     *
153
     * @return mixed
154
     */
155 6
    public function onLocalRecordsSamePosition(Record $record, Record $oldRecord, $records, $position)
156
    {
157
        // Check to who to send.
158 6
        $to = null;
159 6
        if ($position > $this->positionForPublicMessage) {
160 3
            $to = $record->getPlayerLogin();
161
        }
162
163
        // Check which message to send.
164 6
        $msg = 'secures';
165 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...
166 1
            $msg .= '.top1';
167 5
        } else if ($position <= 5) {
168 1
            $msg .= '.top5';
169
        } else {
170 4
            $msg .= '.any';
171
        }
172
173 6
        $securedBy = $this->getSecuredBy($record, $oldRecord);
174 6
        $this->sendMessage(
175
            $msg,
176
            $to,
177
            [
178 6
                '%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(),
179 6
                '%score%' => $this->timeFormater->milisecondsToTrackmania($record->getScore(), true),
180 6
                '%position%' => $position,
181 6
                '%by%' => $securedBy
182
            ]
183
        );
184 6
    }
185
186 11
    protected function getSecuredBy(Record $record, Record $oldRecord)
187
    {
188 11
        $securedBy =$this->timeFormater->milisecondsToTrackmania($oldRecord->getScore() - $record->getScore(), true);
189
190 11
        if (substr($securedBy, 0, 4) === "00:0") {
191 1
            $securedBy = substr($securedBy, 4);
192
        } else {
193 10
            if (substr($securedBy, 0, 3) === "00:") {
194 1
                $securedBy = substr($securedBy, 3);
195
            }
196
        }
197
198 11
        return '-' . $securedBy;
199
    }
200
201 2
    protected function messageFirstPlaceNew(Record $record)
202
    {
203 2
        $this->sendMessage(
204 2
            'new.top1',
205 2
            null,
206
            [
207 2
                '%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(),
208 2
                '%score%' => $this->timeFormater->milisecondsToTrackmania($record->getScore(), true),
209 2
                '%position%' => 1
210
            ]
211
        );
212 2
    }
213
214 13
    protected function sendMessage($message, $recipe, $params)
215
    {
216 13
        $this->chatNotification->sendMessage(
217 13
            $this->translationPrefix . '.' . $message,
218
            $recipe,
219 13
            $params
220
        );
221 13
    }
222
}
223