Passed
Branch master (c018ee)
by
unknown
02:28
created

siphons::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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 siphons
30
 */
31
class siphons
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
32
{
33
    /**
34
     * @var
35
     */
36
    var $config;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $config.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
37
    /**
38
     * @var
39
     */
40
    var $discord;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $discord.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
41
    /**
42
     * @var
43
     */
44
    var $logger;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $logger.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
45
    /**
46
     * @var
47
     */
48
    var $groupConfig;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $groupConfig.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
49
    public $guild;
50
51
    /**
52
     * @param $config
53
     * @param $discord
54
     * @param $logger
55
     */
56
    function init($config, $discord, $logger)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
57
    {
58
        $this->config = $config;
59
        $this->discord = $discord;
60
        $this->logger = $logger;
61
        $this->guild = $config["bot"]["guild"];
62
        $this->groupConfig = $config["plugins"]["siphons"]["groupConfig"];
63
    }
64
65
    /**
66
     *
67
     */
68
    function tick()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
69
    {
70
        // What was the servers last reported state
71
        $lastStatus = getPermCache("serverState");
72
        if ($lastStatus == "online") {
73
            foreach($this->groupConfig as $siphonCorp) {
74
                //Check if user is running old config
75
                if(!isset($siphonCorp["channelID"])){
76
                    $this->logger->addInfo("Siphons: Update your config file to the latest version.");
77
                    return;
78
                }
79
                //If group channel is set to 0 skip
80
                if($siphonCorp["channelID"] == 0){
81
                    continue;
82
                }
83
                $lastChecked = getPermCache("siphonLastChecked{$siphonCorp["keyID"]}");
84
                if ($lastChecked == NULL) {
85
                    // Schedule it for right now if first run
86
                    $lastChecked = 1;
87
                }
88
89
                if ($lastChecked <= time()) {
90
                    $this->logger->addInfo("Siphons: Checking keyID - {$siphonCorp["keyID"]} for siphons");
91
                    $this->checkTowers();
92
                }
93
            }
94
        }
95
    }
96
97
    function checkTowers()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
98
    {
99
        foreach($this->groupConfig as $siphonCorp) {
100
            //If group channel is set to 0 skip
101
            if($siphonCorp["channelID"] == 0){
102
                continue;
103
            }
104
            $url = "https://api.eveonline.com/corp/AssetList.xml.aspx?keyID={$siphonCorp["keyID"]}&vCode={$siphonCorp["vCode"]}";
105
            $xml = makeApiRequest($url);
106
            $rawGoo = array(16634, 16643, 16647, 16641, 16640, 16635, 16648, 16633, 16646, 16651, 16650, 16644, 16652, 16639, 16636, 16649, 16653, 16638, 16637, 16642);
107
            foreach ($xml->result->rowset->row as $structures) {
108
                //Check silos
109 View Code Duplication
                if ($structures->attributes()->typeID == 14343) {
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...
110
                    if (isset($structures->rowset->row)) {
111
                        foreach ($structures->rowset->row as $silo) {
112
                            //Avoid reporting empty silos
113
                            if ($silo->attributes()->quantity != 0 && in_array($silo->attributes()->typeID, $rawGoo)) {
114
                                $siloID = $structures->attributes()->itemID;
115
                                $lastAmount = getPermCache("silo{$siloID}Amount");
116
                                $gooAmount = $silo->attributes()->quantity;
117
                                $gooDifference = $gooAmount - $lastAmount;
118
                                //Check if silo has been checked before
119
                                if(!isset($lastAmount) || $gooDifference < 0){
120
                                    setPermCache("silo{$siloID}Amount", $gooAmount);
121
                                    continue;
122
                                }
123
                                //Check for a multiple of 50 in the difference
124
                                if ($gooDifference % 50 != 0) {
125
                                    $gooType = apiTypeName($silo->attributes()->typeID);
126
                                    $systemName = apiCharacterName($structures->attributes()->locationID);
127
                                    $msg = "{$siphonCorp["prefix"]}";
128
                                    $msg .= "**POSSIBLE SIPHON**\n";
129
                                    $msg .= "**System: **{$systemName} has a possible siphon stealing {$gooType} from a silo.\n";
130
                                    // Queue the message
131
                                    priorityQueueMessage($msg, $siphonCorp["channelID"], $this->guild);
132
                                    $this->logger->addInfo("Siphons: {$msg}");
133
                                    setPermCache("silo{$siloID}Amount", $gooAmount);
134
                                } else {
135
                                    setPermCache("silo{$siloID}Amount", $gooAmount);
136
                                }
137
                            }
138
                        }
139
                    }
140
                }
141 View Code Duplication
                if ($structures->attributes()->typeID == 17982) {
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...
142
                    if (isset($structures->rowset->row)) {
143
                        foreach ($structures->rowset->row as $coupling) {
144
                            //Avoid reporting empty coupling arrays
145
                            if ($coupling->attributes()->quantity != 0 && in_array($coupling->attributes()->typeID, $rawGoo)) {
146
                                $couplingID = $structures->attributes()->itemID;
147
                                $lastAmount = getPermCache("couplingArray{$couplingID}Amount");
148
                                $gooAmount = $coupling->attributes()->quantity;
149
                                $gooDifference = $gooAmount - $lastAmount;
150
                                //Check if silo has been checked before
151
                                if(!isset($lastAmount) || $gooDifference < 0){
152
                                    setPermCache("couplingArray{$couplingID}Amount", $gooAmount);
153
                                    continue;
154
                                }
155
                                //Check for a multiple of 50 in the difference
156
                                if ($gooDifference % 50 != 0) {
157
                                    $gooType = apiTypeName($coupling->attributes()->typeID);
158
                                    $systemName = apiCharacterName($structures->attributes()->locationID);
159
                                    $msg = "{$siphonCorp["prefix"]}";
160
                                    $msg .= "**POSSIBLE SIPHON**\n";
161
                                    $msg .= "**System: **{$systemName} has a possible siphon stealing {$gooType} from a coupling array.\n";
162
                                    // Queue the message
163
                                    priorityQueueMessage($msg, $siphonCorp["channelID"], $this->guild);
164
                                    $this->logger->addInfo("Siphons: {$msg}");
165
                                    setPermCache("couplingArray{$couplingID}Amount", $gooAmount);
166
                                } else {
167
                                    setPermCache("couplingArray{$couplingID}Amount", $gooAmount);
168
                                }
169
                            }
170
                        }
171
                    }
172
                }
173
            }
174
            $cached = $xml->cachedUntil[0];
175
            $baseUnix = strtotime($cached);
176
            $cacheClr = $baseUnix - 13500;
177 View Code Duplication
            if ($cacheClr <= time()) {
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...
178
                $weirdTime = time() + 21700;
179
                $cacheTimer = gmdate("Y-m-d H:i:s", $weirdTime);
180
                setPermCache("siphonLastChecked{$siphonCorp["keyID"]}", $weirdTime);
181
            } else {
182
                $cacheTimer = gmdate("Y-m-d H:i:s", $cacheClr);
183
                setPermCache("siphonLastChecked{$siphonCorp["keyID"]}", $cacheClr);
184
            }
185
            $this->logger->addInfo("Siphons: Siphon Check Complete Next Check At {$cacheTimer}");
186
            return null;
187
        }
188
    }
189
}
190