-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
308 lines (250 loc) · 11.8 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!DOCTYPE HTML>
<html>
<head>
<!-- SCRIPTS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="js/sorttable/sorttable.js"></script>
<script src="js/sum.js"></script>
<!-- Links/Stylesheets -->
<link rel="stylesheet" type="text/css" href="layout.css">
<link rel="stylesheet" type="text/css" href="index.css" />
<link rel="icon" type="image/png" href="/images/favicon.png" />
<!-- Meta Data -->
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<!-- Page Title -->
<title>Trade Assistant</title>
</head>
<body>
<div id="navbar">
<a href="help.html">How To / Info</a>
<a href="settings.php">Settings</a>
<a href="index.php">Home</a>
</div>
<a id="header" href="index.php"><h1>| TRADE ASSISTANT |</h1></a>
<img id="header_image" src="images/csgo_logo.png"/>
<!-- This Credit is a little obnoxious I know, feel free to remove it.
I would appreciate if you could credit me somewhere but it's not required as long as you do not claim my work is your own. -->
<p id="credit">Created by <a href="http://shane-brown.ca/" target="_blank">Shane "SajeOne" Brown</a><br /></p>
<form>
<p>Enter CSGOLounge Trade or Steam Profile URL:</p><br />
<input id="textview" type="text" name="url"/>
<br />
<input type="submit" value="Fetch Data"/>
</form>
</body>
</html>
<?php
function handleTrade($url, $host, $db, $user, $pass, $currency, $currencyConversion, $manual){
$multiArray = TradeTranslator::getTrade($_GET["url"]);
if($multiArray === FALSE){
echo 'Failed to fetch trade.';
return;
}
$total = array(0.0, 0.0, 0.0, 0.0);
echo "<h2 id=\"trader\">Trader's Items:</h2>";
echo '<table class="sortable" id="left" style="width: 100%">';
echo '<thead>';
echo '<tr><th>Item Name:</th><th>Current Price:</th><th>Median Price:</th><th>Market Worth:</th><th>Volume:</th></tr>';
echo '</thead>';
echo '<tbody>';
for($i = 0; $i < count($multiArray[0]); $i++){
$leftItem = AssistantUtility::fetchSqlData($multiArray[0][$i], $host, $db, $user, $pass, $currencyConversion);
if($manual || $leftItem === FALSE){
echo '<script>console.log("Manually Grabbing: ' . $multiArray[0][$i] . '");</script>';
$leftItemJSON = TradeTranslator::getItemJSON($multiArray[0][$i]);
$leftItem = new stdClass();
if($leftItemJSON != FALSE){
$leftItem->curPrice = TradeTranslator::getItemCurrentPrice($leftItemJSON);
$leftItem->medPrice = TradeTranslator::getItemMedianPrice($leftItemJSON);
$leftItem->taxPrice = $leftItem->curPrice - ($leftItem->curPrice * 0.15);
$leftItem->volume = floatval(str_replace(",", "", TradeTranslator::getItemVolume($leftItemJSON)));
$leftItem->curPrice *= $currencyConversion;
$leftItem->medPrice *= $currencyConversion;
$leftItem->taxPrice = $leftItem->curPrice - ($leftItem->curPrice * 0.15);
}else{
echo $leftItem->curPrice;
$leftItem->curPrice = 0.0;
$leftItem->medPrice = 0.0;
$leftItem->taxPrice = 0.0;
$leftItem->volume = 0;
}
}else{
$total[0] += $leftItem->curPrice;
$total[1] += $leftItem->medPrice;
$total[2] += $leftItem->taxPrice;
$total[3] += $leftItem->volume;
echo '<script>console.log("SQL Grabbing: ' . $multiArray[0][$i] . '");</script>';
}
echo '<tr><td>' . $multiArray[0][$i] . '</td><td>' . $currency . " " . number_format($leftItem->curPrice, 2) . '</td><td>' . $currency . " " . number_format($leftItem->medPrice, 2) . '</td><td>' . $currency . " " . number_format($leftItem->taxPrice, 2) . '</td><td>' . $leftItem->volume . '</td></tr>';
}
echo '<tr><td>Total:</td><td>' . $currency . " " . number_format($total[0], 2) . '</td><td>' . $currency . " " . number_format($total[1], 2) . '</td><td>' . $currency . " " . number_format($total[2], 2) . '</td><td>' . $total[3] . '</td></tr>';
echo '</tbody>';
echo '</table>';
$rightTotal = array(0.0, 0.0, 0.0, 0.0);
echo '<h2 id="requested">Requested Items:</h2>';
echo '<table class="sortable" id="right" style="width: 100%">';
echo '<thead>';
echo '<tr><th>Item Name:</th><th>Current Price:</th><th>Median Price:</th><th>Market Worth:</th><th>Volume:</th></tr>';
echo '</thead>';
echo '<tbody>';
for($i = 0; $i < count($multiArray[1]); $i++){
$rightItem = AssistantUtility::fetchSqlData($multiArray[1][$i], $host, $db, $user, $pass, $currencyConversion);
if($manual || $rightItem === FALSE){
echo '<script>console.log("Manually Grabbing: ' . $multiArray[1][$i] . '");</script>';
$rightItemJSON = TradeTranslator::getItemJSON($multiArray[1][$i]);
$rightItem = new stdClass();
if($rightItemJSON != FALSE){
$rightItem->curPrice = TradeTranslator::getItemCurrentPrice($rightItemJSON);
$rightItem->medPrice = TradeTranslator::getItemMedianPrice($rightItemJSON);
$rightItem->taxPrice = $rightItem->curPrice - ($rightItem->curPrice * 0.15);
$rightItem->volume = floatval(str_replace(",", "", TradeTranslator::getItemVolume($rightItemJSON)));
$rightItem->curPrice *= $currencyConversion;
$rightItem->medPrice *= $currencyConversion;
$rightItem->taxPrice = $rightItem->curPrice - ($rightItem->curPrice * 0.15);
}else{
$rightItem->curPrice = 0.0;
$rightItem->medPrice = 0.0;
$rightItem->taxPrice = 0.0;
$rightItem->volume = 0;
}
}else{
echo '<script>console.log("SQL Grabbing: ' . $multiArray[1][$i] . '");</script>';
}
echo '<tr><td>' . $multiArray[1][$i] . '</td><td>' . $currency . " " . number_format($rightItem->curPrice, 2) . '</td><td>' . $currency . " " . number_format($rightItem->medPrice, 2) . '</td><td>' . $currency . " " . number_format($rightItem->taxPrice, 2) . '</td><td>' . $rightItem->volume . '</td></tr>';
$rightTotal[0] += $rightItem->curPrice;
$rightTotal[1] += $rightItem->medPrice;
$rightTotal[2] += $rightItem->taxPrice;
$rightTotal[3] += $rightItem->volume;
}
echo '<tfoot>';
echo '<tr><td>Total:</td><td>' . $currency . " " . number_format($rightTotal[0], 2) . '</td><td>' . $currency . " " . number_format($rightTotal[1], 2) . '</td><td>' . $currency . " " . number_format($rightTotal[2], 2) . '</td><td>' . $rightTotal[3] . '</td></tr>';
echo '</tfoot>';
echo '</tbody>';
echo '</table>';
if($rightTotal[0] < $total[0]){
echo '<h3>Trade Status: <p style="color: green;">Good Trade</p></h3>';
}else if($rightTotal[0] > $total[0]){
echo '<h3>Trade Status: <p style="color: red;">Bad Trade</p></h3>';
}else{
echo '<h3>Trade Status: <p style="color: white;">Even Trade</p></h3>';
}
//echo '<script>sortable.makeSortable(document.getElementById("right"));</script>';
//echo '<script>sortable.makeSortable(document.getElementById("left"));</script>';
}
function handleInventory($url, $host, $db, $user, $pass, $currency, $currencyConversion, $manual){
$itemArray = InventoryTranslator::getInventory($url, $host, $db, $user, $pass);
$name = InventoryTranslator::getSteamName($url);
if($itemArray === FALSE){
echo 'Failed to fetch inventory, are you sure you used either a direct profile URL or CSGOLounge Trade URL?';
return;
}
if($name === FALSE){
$name = "User";
}
$total = array(0.0, 0.0, 0.0, 0.0);
echo "<h2 id=\"trader\">" . $name . "'s Inventory:</h2> <br />";
echo '<table class="sortable" id="left" style="width: 100%">';
echo '<thead>';
echo '<tr><th>Item Name:</th><th>Current Price:</th><th>Median Price:</th><th>Market Worth:</th><th>Volume:</th></tr>';
echo '</thead>';
echo '<tbody>';
for($i = 0; $i < count($itemArray); $i++){
$item = AssistantUtility::fetchSqlData($itemArray[$i]->name, $host, $db, $user, $pass, $currencyConversion);
if($manual || $item === FALSE){
echo '<script>console.log("Manually Grabbing: ' . $itemArray[$i]->name . '");</script>';
$item = new stdClass();
if(($itemJSON = TradeTranslator::getItemJSON($itemArray[$i]->name)) != FALSE){
$item->curPrice = TradeTranslator::getItemCurrentPrice($itemJSON);
$item->medPrice = TradeTranslator::getItemMedianPrice($itemJSON);
$item->taxPrice = $item->curPrice - ($item->curPrice * 0.15);
$item->curPrice *= $currencyConversion;
$item->medPrice *= $currencyConversion;
$item->taxPrice = $item->curPrice - ($item->curPrice * 0.15);
$item->volume = floatval(str_replace(",", "", TradeTranslator::getItemVolume($itemJSON)));
}else{
$item->curPrice = 0.0;
$item->medPrice = 0.0;
$item->taxPrice = 0.0;
$item->volume = 0.0;
}
}else{
echo '<script>console.log("SQL Grabbing: ' . $itemArray[$i]->name . '");</script>';
}
for($k = 0; $k < $itemArray[$i]->count; $k++){
echo '<tr><td>' . $itemArray[$i]->name . '</td><td>' . $currency . " " . number_format($item->curPrice, 2) . '</td><td>' . $currency . " " . number_format($item->medPrice, 2) . '</td><td>' . $currency . " " . number_format($item->taxPrice, 2) . '</td><td>' . $item->volume . '</td></tr>';
$total[0] += $item->curPrice;
$total[1] += $item->medPrice;
$total[2] += $item->taxPrice;
$total[3] += $item->volume;
}
}
echo '<tfoot>';
echo '<tr><td>Total:</td><td>' . $currency . " " . number_format($total[0], 2) . '</td><td>' . $currency . " " . number_format($total[1], 2) . '</td><td>' . $currency . " " . number_format($total[2], 2) . '</td><td>' . $total[3] . '</td></tr>';
echo '</tfoot>';
echo '</tbody>';
echo '</table>';
//echo '<script>sortable.makeSortable(document.getElementById("left"));</script>';
}
/* START OF PROGRAM FLOW */
require 'server/InventoryTranslator.php';
require 'server/TradeTranslator.php';
require 'server/AssistantUtility.php';
$sqlData = AssistantUtility::readSettingsFile();
if($sqlData === FALSE){
$sqlData = new stdClass();
// Default values, manual fetching will still work
$sqlData->host = "localhost";
$sqlData->db = "csgo";
$sqlData->user = "user";
$sqlData->pass = "pass";
}
if(isset($_GET['url'])){
$url = $_GET['url'];
if(($currency = AssistantUtility::getUserCurrency()) == FALSE){
$currency = "USD";
$currencyConversion = 1;
}else{
if(($currencyConversion = AssistantUtility::getCurrencyConversion("USD", $currency)) == FALSE){
$currency = "USD";
$currencyConversion = 1;
}
}
if(($manualPrice = AssistantUtility::getManualFetch()) == FALSE)
$manualPrice = FALSE;
if(strpos($url, "csgolounge.com/trade") !== FALSE){
handleTrade($url, $sqlData->host, $sqlData->db, $sqlData->user, $sqlData->pass, $currency, $currencyConversion, $manualPrice);
}else if(strpos($url, "steamcommunity.com/profiles/") !== FALSE){
$endIndex = AssistantUtility::findNthInstanceInString(str_replace("://", "aaa", $url), "/", 3);
if($endIndex !== FALSE)
$url = substr($url, 0, $endIndex + 1);
handleInventory($url, $sqlData->host, $sqlData->db, $sqlData->user, $sqlData->pass, $currency, $currencyConversion, $manualPrice);
}else if(strpos($url, "steamcommunity.com/id/") !== FALSE){
$endIndex = AssistantUtility::findNthInstanceInString(str_replace("://", "aaa", $url), "/", 3);
if($endIndex !== FALSE)
$url = substr($url, 0, $endIndex + 1);
handleInventory($url, $sqlData->host, $sqlData->db, $sqlData->user, $sqlData->pass, $currency, $currencyConversion, $manualPrice);
}else if(strpos($url, "csgolounge.com/profile")){
if(strlen($url) >= 50){
$index = strpos($url, "profile?id=") + 11;
handleInventory("http://steamcommunity.com/profiles/" . substr($url, $index), $sqlData->host, $sqlData->db, $sqlData->user, $sqlData->pass, $currency, $currencyConversion, $manualPrice);
}else{
echo 'Malformed URL';
return;
}
}else{
echo 'Malformed URL';
return;
}
@$timeFile = file_get_contents("server/datetime.txt");
if($timeFile != FALSE){
$unixTime = intval($timeFile);
$timestamp = new DateTime();
$timestamp->setTimestamp($unixTime);
$nowdate = new DateTime();
$interval = $timestamp->diff($nowdate);
echo '<center>Data Last Fetched ' . $interval->format("%a Days %h Hours %i Minutes %S Seconds") . ' Ago</center>';
}
}
?>