-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.js
95 lines (88 loc) · 3.09 KB
/
Code.js
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
/*
* Copyright (C) 2018 William Granados<[email protected]>
*
* This file is part of TornSpread.
*
* TornSpread is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TornSpread is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TornSpread. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Retrieve the quantity of the specified item in the specified user's inentory.
*
* @desc requirement api_key should be from the specific user specified with
* userid otherwise the torn server will return an error code.
*
* @param {int} itemId id of the item you want to retrieve.
* @param {str} apiKey apikey for associated torn user.
* @param {str} userId userid you want to check.
* @return an int, specifying the amount of requested item is in the user's
* inventory.
*/
function ITEMINVENTORY(itemId, apiKey, userId) {
var response = UrlFetchApp.fetch('https://api.torn.com/user/'+userId+'?selections=inventory&key='+apiKey);
var items = JSON.parse(response.getContentText()).inventory;
var returnVal = 0;
for(var i = 0; i < items.length; i++) {
var item = items[i];
if(item.ID == itemId) {
returnVal = item.quantity;
break;
}
}
return returnVal;
}
/**
* Retrieve the minimum priced item available in bazaars.
*
* @desc requirement api_key should be from the specific user specified with
* userid otherwise the torn server will return an error code.
*
* @param {int} itemId id of the item you want to retrieve.
* @param {str} apiKey apikey for associated torn user.
* @return an int specifying the minimum value of an item in bazaars.
*/
function MINVALITEMBAZAAR(itemId, apiKey) {
var response = UrlFetchApp.fetch('https://api.torn.com/market/'+itemId+'?selections=bazaar&key='+apiKey);
var bazaars = JSON.parse(response.getContentText()).bazaar;
var returnVal = 0;
for(var i in bazaars) {
returnVal = bazaars[i].cost;
break;
}
return returnVal;
}
/**
* Parses cells for occurrences and retrieves total amount recorded in a cell
*
* @desc requirement items to be recorded should be preceeded by an asterik and number
* followed by a space. I.e. 'apples*1 oranges*2 pears*3'
*
* @param {str} content containing items
* @return an int specifying the total for the items recorded in a cell.
*/
function COUNTOCCUR(content) {
const items = content.split(" ");
const occurs = items.filter(
function (item) {
return item.match(/\*/i);
}
);
const vals = occurs.map(
function (item) {
const pos = item.indexOf("*");
return parseInt(item.slice(pos+1, item.length));
}
);
const tot = vals.reduce(function (sum, val) {return sum+val;}, 0);
return tot;
}