-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_APITradeBot.js.html
More file actions
209 lines (160 loc) · 9.81 KB
/
Copy pathexamples_APITradeBot.js.html
File metadata and controls
209 lines (160 loc) · 9.81 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: examples/APITradeBot.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: examples/APITradeBot.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* This is a simple example of a bot that will hook into the API and provide a way to deposit items into a bot inventory
* In which a bot will accept any trades in-which the offer is initiated via API, and decline any others.
* and
* the api can be called via below:
*
* METHOD: POST
* URL: localhost:1338/sendTrade (port can be changed in config)
* PARAMS:
* partner - A string representation of SteamID2, SteamID3, or SteamID64
* items - An array of strings in the form of "classid_instanceid" of items (repeating the same values is a valid case)
* RETURN:
* If failed to send trade:
* {status: 0, error: String}
*
* If successful to send trade:
* {status: 1, offerInformation: tradeOfferObject}
*
* Make sure you copy this file and not use it directly. Follow install instructions on github.
*/
/* We will require the node-steam-bot-manager module to use it */
var BotManager = require('node-steam-bot-manager');
var CONTEXT_ID = 2;
var MESSAGE_ON_TRADE = "This trade offer was initiated via API of node-steam-bot-manager.";
function APITradeBot() {
var botsManager = new BotManager();// Create new instance of the BotManager
// Once we receive an offer from someone (Using trade offers not live trades)
botsManager.on('newOffer', function (activeAccount, offer) {
// 'activeAccount' refers to us (the bot receiving the offer) doing the trade, check docs for more info.
// 'offer' contains details about the trade, check docs.
// Cancel all offers received (the event does not trigger on sent-offers.)
offer.cancel(function (err) {
if (err)
console.log(err);
});
});
botsManager.on('loadedAPI', function () {
// Register API endpoint
botsManager.addEndpoint('POST', '/sendTrade', function (req, res) {
if (req.body.hasOwnProperty("partner")) {
var partner = req.body.partner;// We are getting the 'partner' parameter from the body of the request.
if (req.body.hasOwnProperty("items")) {
var items = req.body.items;// We are getting the item list
var botAccount = botsManager.randomBot(['canTrade']);
try {
var itemsList = JSON.parse(items);
} catch (e) {
return res.json({status: 0, error: "Invalid item list (ensure it can be parsed)."});
}
var partnerSid = botAccount.getUser(partner);//Converting the id into a SteamID object (to verify it is valid)
if (partnerSid != null) {
// We will get user inventory so that we can find which items are requested.
botAccount.getUserInventory(partnerSid, botsManager.getAppID(), CONTEXT_ID, true, function (err, items) {
if (err)
return res.json({status: 0, error: "Failed to get Inventory - due to " + err});
// We will create an offer, and get the currentOffer object so that we fill it.
botAccount.Trade.createOffer(partnerSid, function (err, currentOffer) {
if (err)
return res.json({status: 0, error: "Failed to create offer - due to " + err});
// We will build a lookup system for classid_instanceid
var lookup = {};
var countTracker = {};
for (var i = 0, len = items.length; i < len; i++) {
if (!countTracker.hasOwnProperty(items[i].classid + "_" + items[i].instanceid))
countTracker[items[i].classid + "_" + items[i].instanceid] = 1;
else
countTracker[items[i].classid + "_" + items[i].instanceid] = countTracker[items[i].classid + "_" + items[i].instanceid] + 1;
lookup[items[i].classid + "_" + items[i].instanceid + "_" + countTracker[items[i].classid + "_" + items[i].instanceid]] = items[i];
}
countTracker = {};
// Go through all items in inventory, and check if they match any items needed for the trade
// Ensure to save the different properties of
for (var itemIndex in itemsList) {
if (itemsList.hasOwnProperty(itemIndex)) {
var neededItem = itemsList[itemIndex];
if (!countTracker.hasOwnProperty(neededItem))
countTracker[neededItem] = 1;
else
countTracker[neededItem] = countTracker[neededItem] + 1;
var itemFound = lookup[neededItem + "_" + countTracker[neededItem]];
if (itemFound != null) {
currentOffer.addTheirItem(itemFound);// Add their item to the offer
delete lookup[neededItem];
}
}
}
// Place some checks to ensure we don't send an empty trade or a missing-trade.
if (currentOffer.itemsToReceive.length != itemsList.length)
return res.json({status: 0, error: "Failed to get some items required from trade"});
if (currentOffer.itemsToReceive.length == 0)
return res.json({status: 0, error: "Failed to any items required from trade"});
currentOffer.setMessage(MESSAGE_ON_TRADE);
// We will send the offer to the user with 'partnerSid'
currentOffer.send(function (err, status) {
if (err) {
return res.json({status: 0, error: "Failed to send offer - due to " + err});
} else {
botAccount.Trade.confirmOutstandingTrades(function (confirmedTrades) {
return res.json({
status: 1,
offerInformation: currentOffer.itemsToReceive
});
});
}
});
});
});
}
else
return res.json({status: 0, error: "Invalid partner."});
}
else
return res.json({status: 0, error: "Missing 'items' parameter."});
} else
return res.json({status: 0, error: "Missing 'partner' parameter."});
});
});
botsManager.infoDebug("Starting Bot Manager");
// We start the manager, and then load the API Endpoints, since we need the webserver running to add the endpoints.
botsManager.startManager(function (err) {
if (err) {
return botsManager.errorDebug("Failed to start Bot Manager");
}
});// You must start the manager at the end so that all the hooks above it, are registered.
}
new APITradeBot();// Run the code above.
module.exports = APITradeBot;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Auth.html">Auth</a></li><li><a href="Bot.html">Bot</a></li><li><a href="global.html#BotManager">BotManager</a></li><li><a href="Community.html">Community</a></li><li><a href="Friends.html">Friends</a></li><li><a href="GUI_Handler.html">GUI_Handler</a></li><li><a href="Profile.html">Profile</a></li><li><a href="Request.html">Request</a></li><li><a href="Trade.html">Trade</a></li></ul><h3>Events</h3><ul><li><a href="Bot.html#event:chatMessage">chatMessage</a></li><li><a href="Bot.html#event:loggedIn">loggedIn</a></li><li><a href="Bot.html#event:newOffer">newOffer</a></li><li><a href="Bot.html#event:offerChanged">offerChanged</a></li><li><a href="Bot.html#event:offerList">offerList</a></li><li><a href="Bot.html#event:sentOfferChanged">sentOfferChanged</a></li><li><a href="Bot.html#event:tradeOffers">tradeOffers</a></li></ul><h3>Global</h3><ul><li><a href="global.html#webserver">webserver</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Sat Oct 28 2017 11:52:44 GMT-0400 (Eastern Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>