Test Setup Failed
Pull Request — master (#42)
by Bob
03:13
created

src/plugins/onMessage/price.php (1 issue)

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
/**
27
 * Class priceChecks
28
 * @property  excludeChannel
29
 * @property  message
30
 */
31
class price
32
{
33
    /**
34
     * @var
35
     */
36
    var $config;
37
    /**
38
     * @var
39
     */
40
    var $discord;
41
    /**
42
     * @var
43
     */
44
    var $logger;
45
    /**
46
     * @var
47
     */
48
    var $solarSystems;
49
    /**
50
     * @var array
51
     */
52
    var $triggers = array();
53
    public $excludeChannel;
54
    public $message;
55
56
    /**
57
     * @param $config
58
     * @param $discord
59
     * @param $logger
60
     */
61
    function init($config, $discord, $logger)
62
    {
63
        $this->config = $config;
64
        $this->discord = $discord;
65
        $this->logger = $logger;
66
        $this->triggers[] = $this->config["bot"]["trigger"] . "pc";
67
        $this->triggers[] = $this->config["bot"]["trigger"] . strtolower("Jita");
68
        $this->triggers[] = $this->config["bot"]["trigger"] . strtolower("Amarr");
69
        $this->triggers[] = $this->config["bot"]["trigger"] . strtolower("Rens");
70
        $this->triggers[] = $this->config["bot"]["trigger"] . strtolower("Dodixie");
71
        $this->excludeChannel = $config["plugins"]["priceChecker"]["channelID"];
72
    }
73
74
    /**
75
     *
76
     */
77
    function tick()
78
    {
79
80
    }
81
82
    /**
83
     * @param $msgData
84
     * @param $message
85
     * @return null
86
     */
87
    function onMessage($msgData, $message)
88
    {
89
        $this->message = $message;
90
        $user = $msgData["message"]["from"];
91
        $channelID = $msgData["message"]["channelID"];
92
93
94
        // Bind a few things to vars for the plugins
95
        $message = $msgData["message"]["message"];
96
97
        // Quick Lookups
98
        $quickLookUps = array(
99
            "plex" => array(
100
                "typeID" => 29668,
101
                "typeName" => "30 Day Pilot's License Extension (PLEX)"
102
            ),
103
            "30 day" => array(
104
                "typeID" => 29668,
105
                "typeName" => "30 Day Pilot's License Extension (PLEX)"
106
            )
107
        );
108
109
        $data = command(strtolower($message), $this->information()["trigger"], $this->config["bot"]["trigger"]);
110
111
        if (isset($data["trigger"])) {
112
113
            $systemName = $data["trigger"];
114
            $itemName = $data["messageString"];
115
            var_dump($itemName);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($itemName); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
116
            $single = apiTypeID(urlencode($itemName));
117
118
            // Quick lookups
119
            if (isset($quickLookUps[$itemName])) {
120
                $single = $quickLookUps[$itemName];
121
            }
122
123
            // Check if the channel is restricted
124
            if ($channelID == $this->excludeChannel) {
125
                return $this->message->reply("**Price Check not allowed in this channel**");
126
            }
127
128
            // If there is a single result, we'll get data now!
129
            if ($single) {
130
                $typeID = $single["typeID"];
131
132
                if (is_null($typeID)){
133
                    $typeID = $single;
134
                }
135
136
                if ($systemName == "pc"){
137
                    $solarSystemID = "global";
138
                } else {
139
                    $solarSystemID = apiCharacterID(urlencode($systemName));
140
                }
141
                var_dump($single[0]);
142
                var_dump($typeID);
143
                var_dump($solarSystemID);
144
145
                // Get pricing data
146
                if ($solarSystemID == "global") {
147
                    $data = new SimpleXMLElement(downloadData("https://api.eve-central.com/api/marketstat?typeid={$typeID}"));
148
                } else {
149
                    $data = new SimpleXMLElement(downloadData("https://api.eve-central.com/api/marketstat?usesystem={$solarSystemID}&typeid={$typeID}"));
150
                }
151
152
                $lowBuy = number_format((float) $data->marketstat->type->buy->min, 2);
153
                $avgBuy = number_format((float) $data->marketstat->type->buy->avg, 2);
154
                $highBuy = number_format((float) $data->marketstat->type->buy->max, 2);
155
                $lowSell = number_format((float) $data->marketstat->type->sell->min, 2);
156
                $avgSell = number_format((float) $data->marketstat->type->sell->avg, 2);
157
                $highSell = number_format((float) $data->marketstat->type->sell->max, 2);
158
159
                $this->logger->addInfo("Sending pricing info to {$user}");
160
                $solarSystemName = $systemName == "pc" ? "Global" : ucfirst($systemName);
161
                $messageData = "**System: {$solarSystemName}**
162
**Buy:**
163
   Low: {$lowBuy}
164
   Avg: {$avgBuy}
165
   High: {$highBuy}
166
**Sell:**
167
   Low: {$lowSell}
168
   Avg: {$avgSell}
169
   High: {$highSell}";
170
                $this->message->reply($messageData);
171
            } else {
172
                $this->message->reply("**Error:** ***{$itemName}*** not found");
173
            }
174
        }
175
        return null;
176
    }
177
178
    /**
179
     * @return array
180
     */
181
    function information()
182
    {
183
        return array(
184
            "name" => "pc",
185
            "trigger" => $this->triggers,
186
            "information" => "Shows price information for items in EVE. To use simply type **!pc item_name** for global stats or **!jita/amarr/rens_or_dodixie item_name** for hub specific info."
187
        );
188
    }
189
}
190