/
check-items.php
179 lines (139 loc) · 5.01 KB
/
check-items.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
<?php
# Copyright (c) 2015 Jordan Turley, CSGO Win Big. All Rights Reserved.
include 'default.php';
$db = getDB();
$maxPotCount = 60;
# Get password, owner steam ID, and all deposit items
$password = isset($_POST['password']) ? $_POST['password'] : null;
$tradeOwnerSteamId32 = isset($_POST['owner']) ? $_POST['owner'] : null;
$allItemsJson = isset($_POST['items']) ? $_POST['items'] : null;
if (is_null($password) || is_null($tradeOwnerSteamId32) || is_null($allItemsJson) || strlen($password) === 0 || strlen($tradeOwnerSteamId32) === 0 || strlen($allItemsJson) === 0) {
echo jsonErr('One of the required fields was not sent correctly or was left blank, in check-items.php ' . $password . ' \ ' . $tradeOwnerSteamId32 . ' \ ' . $allItemsJson);
return;
}
# Convert Steam ID to Steam64 ID
$tradeOwnerSteamId64 = steamid32ToSteamid64($tradeOwnerSteamId32);
# Get the password from config file and make sure it matches
$fileLoc = $_SERVER['DOCUMENT_ROOT'] . '/../passwords.txt';
if (file_exists($fileLoc)) {
$fh = fopen($fileLoc, 'r');
$jsonStr = fgets($fh);
$arr = json_decode($jsonStr, true);
$realPassword = $arr['default-password'];
fclose($fh);
} else {
die('no file found');
}
if ($password !== $realPassword) {
echo jsonErr('The password was incorrect.');
return;
}
# Create all items array from json
$allItems = json_decode($allItemsJson, true);
# Get the depositor's inventory
$depositorInventory = json_decode(file_get_contents("https://steamcommunity.com/profiles/$tradeOwnerSteamId64/inventory/json/730/2"), true);
if ($depositorInventory['success'] !== true) {
echo jsonErr('An error occured fetching the depositor\'s inventory.');
return;
}
$rgInventory = $depositorInventory['rgInventory'];
$rgDescriptions = $depositorInventory['rgDescriptions'];
$totalPrice = 0;
$itemsArr = array();
# Loop through each item and get their name and price
foreach ($allItems as $item) {
$appId = $item['appid'];
$contextId = $item['contextid'];
$amount = $item['amount'];
$assetId = $item['assetid'];
# Check if any items are not for CSGO, just in case.
if ($appId !== 730 || $contextId !== 2) {
echo jsonErr('One of the items was not for CSGO.');
return;
}
$inventoryItem = $rgInventory[$assetId];
$classId = $inventoryItem['classid'];
$instanceId = $inventoryItem['instanceid'];
$descriptionItem = $rgDescriptions[$classId . '_' . $instanceId];
$marketName = $descriptionItem['market_name'];
$iconUrl = $descriptionItem['icon_url'];
# Get all item tags
$tags = $descriptionItem['tags'];
# Loop through all tags and find the rarity tag
$tagFound = false;
foreach ($tags as $tag) {
$tagCategory = $tag['category'];
if ($tagCategory === 'Rarity') {
$tagFound = true;
$rarityName = $tag['name'];
$rarityColor = $tag['color'];
}
}
# Just in case for some reason the rarity couldn't be found
if (!$tagFound) {
$rarityName = '';
$rarityColor = '';
}
# Get price of item from database
$stmt = $db->prepare('SELECT * FROM items WHERE marketName = :name');
$stmt->bindValue(':name', $marketName);
$stmt->execute();
$item = $stmt->fetch();
$price = intval($item['avgPrice30Days']);
# If the 30 day average is 0, set it to the 7 day average
if ($price === 0) {
$price = intval($item['avgPrice7Days']);
}
# If the 7 day average is 0 again, set it to the current price
if ($price === 0) {
$price = intval($item['currentPrice']);
}
if ($price === 0) {
$price = intval($item['suggestedPriceMin']);
}
# If all of those are 0, set it to the Steam market price
if ($price === 0) {
$hash = urlencode($marketName);
$marketObj = json_decode(file_get_contents('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=' . $hash), true);
/* if ($marketObjStr === false) {
echo jsonErr('An error occured while doing the url request');
return;
}
$marketObj = json_decode($marketObjStr, true); */
if ($marketObj['success'] !== true) {
echo jsonErr('An error occured while fetching market price for an item.');#' Server response: |' . $marketObjStr . '|');
return;
}
$medianPrice = $marketObj['median_price'];
$lowestPrice = $marketObj['lowest_price'];
if (!isset($medianPrice) && !isset($lowestPrice)) {
echo jsonErr('One or more items was not found on the steam market place.');
return;
}
if (isset($medianPrice)) {
$price = doubleval(substr($medianPrice, 1)) * 100;
} else {
$price = doubleval(substr($lowestPrice, 1)) * 100;
}
}
$arr = array(
'classId' => $classId,
'instanceId' => $instanceId,
'marketName' => $marketName,
'rarityName' => $rarityName,
'rarityColor' => $rarityColor,
'price' => $price,
'iconUrl' => $iconUrl
);
# Just in case the SteamBot decides to have the amount more than 1
for ($i1=0; $i1 < $amount; $i1++) {
array_push($itemsArr, $arr);
$totalPrice += $price;
}
}
# 20 means $0.20, or 20 cents
$minDeposit = $totalPrice >= 20 ? 1 : 0;
$allItemsObj = json_encode($itemsArr);
$data = array('totalPrice' => $totalPrice, 'minDeposit' => $minDeposit, 'allItems' => $allItemsObj);
echo jsonSuccess($data);
?>