Test Setup Failed
Branch dev (952e73)
by
unknown
06:04
created

src/plugins/onTick/getKillmails.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Robert Sardinia
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
use discord\discord;
27
28
/**
29
 * Class getKillmails
30
 */
31
class getKillmails
32
{
33
    public $config;
34
    public $db;
35
    public $discord;
36
    public $channelConfig;
37
    public $lastCheck = 0;
38
    public $logger;
39
    public $groupConfig;
40
41
    /**
42
     * @param $config
43
     * @param $discord
44
     * @param $logger
45
     */
46 View Code Duplication
    public function init($config, $discord, $logger)
47
    {
48
        $this->config = $config;
49
        $this->discord = $discord;
50
        $this->logger = $logger;
51
        $this->groupConfig = $config['plugins']['getKillmails']['groupConfig'];
52
        $this->guild = $config['bot']['guild'];
53
        //Refresh check at bot start
54
        setPermCache('killmailCheck', time() - 5);
55
    }
56
57
    /**
58
     *
59
     */
60
    public function tick()
61
    {
62
        // What was the servers last reported state
63
        $lastStatus = getPermCache('serverState');
64
        if ($lastStatus === 'online') {
65
            $lastChecked = getPermCache('killmailCheck');
66
            if ($lastChecked <= time()) {
67
                //check if user is still using the old config
68 View Code Duplication
                if (null === $this->groupConfig) {
69
                    $this->logger->addError('Killmails: UPDATE YOUR CONFIG TO RECEIVE KILLMAILS');
70
                    setPermCache('killmailCheck', time() + 600);
71
                    return null;
72
                }
73
                $this->logger->addInfo('Killmails: Checking for new killmails.');
74
                $this->getKM();
75
                setPermCache('killmailCheck', time() + 600);
76
                if (@$this->config['plugins']['getKillmails']['bigKills']['shareBigKills'] === 'true') {
77
                    $this->logger->addInfo('Killmails: Checking for 10b+ kills.');
78
                    $this->getBigKM();
79
                }
80
            }
81
        }
82
    }
83
84
    private function getKM()
85
    {
86
        foreach ($this->groupConfig as $kmGroup) {
87
            $SavedKillID = getPermCache("{$kmGroup['name']}newestKillmailID");
88
            //check if start id is greater than current id and if it is set
89
            if (null !== $SavedKillID && preg_match('/[a-z]/i', $SavedKillID)) {
90
                getStartMail($kmGroup);
91
                $SavedKillID = getPermCache("{$kmGroup['name']}newestKillmailID");
92
            }
93
            if ($kmGroup['startMail'] > $SavedKillID || null === $SavedKillID) {
94
                $SavedKillID = $kmGroup['startMail'];
95
            }
96 View Code Duplication
            if ((string)$kmGroup['allianceID'] === '0' & $kmGroup['lossMails'] === 'true') {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: ((string) $kmGroup['alli...'lossMails'] === 'true', Probably Intended Meaning: (string) $kmGroup['allia...lossMails'] === 'true')

When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.

Let’s take a look at these examples:

// Returns always int(0).
return 0 === $foo & 4;
return (0 === $foo) & 4;

// More likely intended return: true/false
return 0 === ($foo & 4);
Loading history...
97
                $url = "https://zkillboard.com/api/corporationID/{$kmGroup['corpID']}/no-attackers/no-items/orderDirection/asc/pastSeconds/10800/";
98
            }
99 View Code Duplication
            if ((string)$kmGroup['allianceID'] === '0' & $kmGroup['lossMails'] === 'false') {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: ((string) $kmGroup['alli...lossMails'] === 'false', Probably Intended Meaning: (string) $kmGroup['allia...ossMails'] === 'false')

When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.

Let’s take a look at these examples:

// Returns always int(0).
return 0 === $foo & 4;
return (0 === $foo) & 4;

// More likely intended return: true/false
return 0 === ($foo & 4);
Loading history...
100
                $url = "https://zkillboard.com/api/corporationID/{$kmGroup['corpID']}/no-attackers/no-items/kills/orderDirection/asc/pastSeconds/10800/";
101
            }
102 View Code Duplication
            if ((string)$kmGroup['allianceID'] !== '0' & $kmGroup['lossMails'] === 'true') {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: ((string) $kmGroup['alli...'lossMails'] === 'true', Probably Intended Meaning: (string) $kmGroup['allia...lossMails'] === 'true')

When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.

Let’s take a look at these examples:

// Returns always int(0).
return 0 === $foo & 4;
return (0 === $foo) & 4;

// More likely intended return: true/false
return 0 === ($foo & 4);
Loading history...
103
                $url = "https://zkillboard.com/api/allianceID/{$kmGroup['allianceID']}/no-attackers/no-items/orderDirection/asc/pastSeconds/10800/";
104
            }
105 View Code Duplication
            if ((string)$kmGroup['allianceID'] !== '0' & $kmGroup['lossMails'] === 'false') {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: ((string) $kmGroup['alli...lossMails'] === 'false', Probably Intended Meaning: (string) $kmGroup['allia...ossMails'] === 'false')

When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.

Let’s take a look at these examples:

// Returns always int(0).
return 0 === $foo & 4;
return (0 === $foo) & 4;

// More likely intended return: true/false
return 0 === ($foo & 4);
Loading history...
106
                $url = "https://zkillboard.com/api/allianceID/{$kmGroup['allianceID']}/no-attackers/no-items/kills/orderDirection/asc/pastSeconds/10800/";
107
            }
108
109
            if (!isset($url)) { // Make sure it's always set.
110
                $this->logger->addInfo('Killmails: ERROR - Ensure your config file is setup correctly for killmails.');
111
                return null;
112
            }
113
114
            $kills = json_decode(downloadData($url), true);
115
//            $i = 0;
116
            if (isset($kills)) {
117
                foreach ($kills as $kill) {
118
 //                   if ($i < 10) {
119
                        //if big kill isn't set, disable it
120
                        if (!array_key_exists('bigKill', $kmGroup)) {
121
                            $kmGroup['bigKill'] = 99999999999999999999999999;
122
                        }
123
                        $killID = $kill['killID'];
124
                        if ($killID <= $SavedKillID) {
125
//                            $i++;
126
                            continue;
127
                        }
128
                        $channelID = $kmGroup['channel'];
129
                        $solarSystemID = $kill['solarSystemID'];
130
                        $systemName = getSystemName($solarSystemID);
131
                        $killTime = $kill['killTime'];
132
                        $victimAllianceName = '';
133 View Code Duplication
                    if ($kill['victim']['allianceName'] !== null && $kill['victim']['allianceName'] !== '') {
134
                            $victimAllianceName = "|{$kill['victim']['allianceName']}";
135
                        }
136
                        $victimName = $kill['victim']['characterName'];
137
                        $victimCorpName = $kill['victim']['corporationName'];
138
                        $victimShipID = $kill['victim']['shipTypeID'];
139
                        $shipName = getTypeName($victimShipID);
140
                        $rawValue = $kill['zkb']['totalValue'];
141
                        //Check if killmail meets minimum value and if it meets lost minimum value
142
                        if (isset($kmGroup['minimumValue']) && isset($kmGroup['minimumlossValue'])) {
143
                            if ($rawValue < $kmGroup['minimumValue']) {
144
                                if ($kill['victim']['corporationID'] == $kmGroup['corpID'] && $rawValue > $kmGroup['minimumlossValue'] ||
145
                                $kill['victim']['allianceID'] == $kmGroup['allianceID'] && $rawValue > $kmGroup['minimumlossValue'])
146
                                {
147
                                    $this->logger->addInfo("Killmails: Mail {$killID} posted because it meet minimum loss value required.");
148
                                } else {
149
                                    $this->logger->addInfo("Killmails: Mail {$killID} ignored for not meeting the minimum value required.");
150
                                    setPermCache("{$kmGroup['name']}newestKillmailID", $killID);
151
                                    continue;
152
                                }
153
                            }
154
                        }
155
                        $totalValue = number_format($kill['zkb']['totalValue']);
156
                        // Check if it's a structure
157 View Code Duplication
                        if ($victimName !== '' || ' ' || null) {
158
                            if ($kmGroup['bigKill'] != null && $rawValue >= $kmGroup['bigKill']) {
159
                                $channelID = $kmGroup['bigKillChannel'];
160
                                $msg = "@here \n :warning:***Expensive Killmail***:warning: \n **{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** flown by **{$victimName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
161
                            } elseif ($kmGroup['bigKill'] == null || $rawValue <= $kmGroup['bigKill']) {
162
                                $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** flown by **{$victimName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
163
                            }
164
                        } elseif ($victimName === '') {
165
                            $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** owned by (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
166
                        }
167
168
                        if (!isset($msg)) { // Make sure it's always set.
169
                            return null;
170
                        }
171
                        queueMessage($msg, $channelID, $this->guild);
172
                        $this->logger->addInfo("Killmails: Mail {$killID} queued.");
173
174
//                        $i++;
175
/*                    } else {
176
                        $updatedID = getPermCache("{$kmGroup['name']}newestKillmailID");
177
                        $this->logger->addInfo("Killmails: Kill posting cap reached, newest kill id for {$kmGroup['name']} is {$updatedID}");
178
                        break;
179
                    }*/
180
                }
181
                if (!isset($killID)) {
182
                    $killID = $SavedKillID;
183
                }
184
                setPermCache("{$kmGroup['name']}newestKillmailID", $killID);
185
            }
186
            $updatedID = getPermCache("{$kmGroup['name']}newestKillmailID");
187
            $this->logger->addInfo("Killmails: All kills posted, newest kill id for {$kmGroup['name']} is {$updatedID}");
188
            continue;
189
        }
190
    }
191
192
    private function getBigKM()
193
    {
194
        $oldID = getPermCache('bigKillNewestKillmailID');
195
        if (null === $oldID || preg_match('/[a-z]/i', $oldID)) {
196
            getStartBigMail();
197
            $oldID = getPermCache('bigKillNewestKillmailID');
198
        }
199
200
        $url = 'https://zkillboard.com/api/kills/orderDirection/desc/iskValue/10000000000/limit/10/';
201
202
        $kills = json_decode(downloadData($url), true);
203
        $i = 0;
0 ignored issues
show
$i 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...
204
        if (isset($kills)) {
205
            foreach ($kills as $kill) {
206
                $cacheID = getPermCache('bigKillNewestKillmailID');
207
 //               if ($i < 10) {
208
                    $killID = $kill['killID'];
209
                    //check if mail is old
210
                    if ((int)$killID <= (int)$oldID) {
211
                        continue;
212
                    }
213
                    //save highest killID for cache
214
                    if ($killID > $cacheID) {
215
                        setPermCache('bigKillNewestKillmailID', $killID);
216
                    }
217
                    $channelID = $this->config['plugins']['getKillmails']['bigKills']['bigKillChannel'];
218
                    $solarSystemID = $kill['solarSystemID'];
219
                    $systemName = getSystemName($solarSystemID);
220
                    $killTime = $kill['killTime'];
221
                $victimAllianceName = '';
222 View Code Duplication
                if ($kill['victim']['allianceName'] !== null && $kill['victim']['allianceName'] !== '') {
223
                    $victimAllianceName = "|{$kill['victim']['allianceName']}";
224
                }
225
                    $victimName = $kill['victim']['characterName'];
226
                    $victimCorpName = $kill['victim']['corporationName'];
227
                    $victimShipID = $kill['victim']['shipTypeID'];
228
                    $shipName = getTypeName($victimShipID);
229
                    $totalValue = number_format($kill['zkb']['totalValue']);
230
                    // Check if it's a structure
231 View Code Duplication
                    if ($victimName !== '' || ' ' || null) {
232
                        $msg = "**10b+ Kill Reported**\n\n**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** flown by **{$victimName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
233
                    } else {
234
                        $msg = "**10b+ Kill Reported**\n\n**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** owned by (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
235
                    }
236
                    if (!isset($msg)) { // Make sure it's always set.
237
                        return null;
238
                    }
239
                    queueMessage($msg, $channelID, $this->guild);
240
                    $this->logger->addInfo("Killmails: Mail {$killID} queued.");
241
242
/*                    $i++;
243
                } else {
244
                    $cacheID = getPermCache('bigKillNewestKillmailID');
245
                    $this->logger->addInfo("Killmails: bigKill posting cap reached, newest kill id is {$cacheID}");
246
                    break;
247
                }*/
248
            }
249
        }
250
        $updatedID = getPermCache('bigKillNewestKillmailID');
251
        $this->logger->addInfo("Killmails: All bigKills posted, newest kill id is {$updatedID}");
252
    }
253
}
254