-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.market.js
235 lines (211 loc) · 10.2 KB
/
util.market.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
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
/*
* Module code goes here. Use 'module.exports' to export things:
* module.exports.thing = 'a thing';
*
* You can import it from another modules like this:
* var mod = require('util.market');
* mod.thing == 'a thing'; // true
*/
var logger = require("screeps.logger");
logger = new logger("util.market");
//logger.enabled = false;
module.exports = {
//call once at end of tick
watchMarket: function() {
var orders = Game.market.getAllOrders();
var buySellPrices = {};
for(var o in orders) {
var order = orders[o];
if (!buySellPrices[order.resourceType])
buySellPrices[order.resourceType] = {buy:[],sell:[]};
buySellPrices[order.resourceType][order.type].push({price:order.price, amount:order.remainingAmount});
}
for(var type in buySellPrices) {
var prices = buySellPrices[type];
var maxBuyPrice = _.max(_.map(prices.buy, function(p) {return p.price}));
var minSellPrice = _.min(_.map(prices.sell, function(p) {return p.price}));
if (Math.abs(maxBuyPrice) == Infinity)
maxBuyPrice = 0.3;
if (Math.abs(minSellPrice) == Infinity) {
minSellPrice = maxBuyPrice * 4;
}
var snipe = false;
if (maxBuyPrice > minSellPrice)
snipe = true;
prices.buysAt = maxBuyPrice;
prices.sellsAt = minSellPrice;
}
Memory.marketPrices = buySellPrices;
//delete my inactive orders for now(should use this to up the price..)
var myOrders = Game.market.orders;
for(var o in myOrders) {
var order = myOrders[o];
logger.log('order:', order.resourceType, order.active)
logger.log("dates: ", order.created, Game.time, Game.time - order.created, (Game.time - order.created) > 10000);
if (!order.active || ((Game.time - order.created) > 100000)) {
Game.market.cancelOrder(order.id);
}
}
},
sellMyMinerals:function(roomName, terminalContents) {
if (!Memory.marketPrices) {
return;
}
terminalContents = _.clone(terminalContents);
var energyInTerminal = terminalContents[RESOURCE_ENERGY];
delete terminalContents[RESOURCE_ENERGY];
var targetsToSell = [];
for(var type in terminalContents) {
var amt = terminalContents[type];
if (amt >= 1000) {
targetsToSell.push(type)
}
}
if (targetsToSell.length > 0 && energyInTerminal > 10000) {
var selling = targetsToSell[0];
var amt = terminalContents[selling];
amt = Math.min(amt, 20000);
if (!Memory.marketPrices[selling]) {
return false;
}
var targetPrice = Memory.marketPrices[selling].sellsAt;
var buyPrice = Memory.marketPrices[selling].buysAt;
var makeSellOrder = false;
if (false || targetPrice * 0.9 <= buyPrice || buyPrice >= 1) {
//just fill buy orders
//if (buyPrice < 0.8) {
// makeSellOrder = true;
//} else {
logger.log(roomName, "fill buy order", amt, selling, "@", buyPrice, "sell price", targetPrice);
makeSellOrder = !this.fillBuyOrder(roomName, selling, amt, buyPrice);
//}
} else {
makeSellOrder = true;
}
if (makeSellOrder) {
//make sell order
var pr = Math.max(Math.min(buyPrice * 4, targetPrice * 1.2), 0.1)
logger.log(roomName, "sell order", amt, selling, "@", pr, "buyers at", buyPrice);
this.makeSellOrder(roomName, selling, amt, pr);
}
}
//logger.log('roomName', terminalContents.energy)
//if terminal has less than 50k energy, make a buy order for energy
if (energyInTerminal < 20000 && Memory.marketPrices[RESOURCE_ENERGY]) {
var energyBuyPrice = Memory.marketPrices[RESOURCE_ENERGY].buysAt;
var energySellPrice = Memory.marketPrices[RESOURCE_ENERGY].sellsAt;
var buyPrice = energySellPrice;
var res = false;
if (energyInTerminal < 100 || !(energySellPrice < energyBuyPrice * 1.2)) {
buyPrice = energyBuyPrice;
res = this.makeBuyOrder(roomName, RESOURCE_ENERGY, 30000 - energyInTerminal, buyPrice);
logger.log(roomName, "energy buy order at", buyPrice, res)
} else {
//buy from a sell order within 20% of buy order price.
res = this.fillSellOrder(roomName, RESOURCE_ENERGY, 30000 - energyInTerminal, buyPrice);
logger.log(roomName, "buy energy at", buyPrice, res)
}
//this.makeBuyOrder(roomName, RESOURCE_ENERGY, 10000, buyPrice)
}
// _.each(Game.market.orders, function(o) {
// //if (o.resourceType == RESOURCE_ENERGY) {
// Game.market.cancelOrder(o.id);
// //}
// })
},
fillBuyOrder: function(roomName, resource, amt, targetPrice) {
var maxTransferEnergyCost = amt;
var orders = Game.market.getAllOrders({type: ORDER_BUY, resourceType: resource});
orders = _.sortBy(orders, [function(o) { return o.price; }]).reverse();
for(let i=0; i<orders.length; i++) {
const transferEnergyCost = Game.market.calcTransactionCost(
Math.min(amt, orders[i].amount), roomName, orders[i].roomName);
logger.log("fillBuyOrder--------------------------------------------- amt:", amt, "price", orders[i].price, "my price", targetPrice)
logger.log(orders[i].amount * orders[i].price, 'creds', transferEnergyCost, "cost", maxTransferEnergyCost, 'max cost', orders[i].price >= targetPrice, "price ok")
if(transferEnergyCost < maxTransferEnergyCost && orders[i].price >= targetPrice) {
logger.log(roomName, "selling off", amt, "of", resource, "for", orders[i].price)
var ret = Game.market.deal(orders[i].id, amt, roomName);
logger.log("-----", ret)
var numTries = 20;
while (ret == -6 && numTries > 0) {
//not enough energy, reduce amt and try again till it works
amt *= 0.8;
if (numTries < 4)
amt *= 0.1
amt = Math.max(Math.floor(amt), 1);
logger.log("costs too much energy, trying again with:", amt)
ret = Game.market.deal(orders[i].id, amt, roomName);
numTries--;
}
return true;
break;
}
}
return false;
},
fillSellOrder: function(roomName, resource, amt, targetPrice) {
var maxTransferEnergyCost = amt;
var orders = Game.market.getAllOrders({type: ORDER_SELL, resourceType: resource});
orders = _.sortBy(orders, [function(o) { return o.price; }]).reverse();
console.log("all orders", JSON.stringify(orders))
for(let i=0; i<orders.length; i++) {
const transferEnergyCost = Game.market.calcTransactionCost(
Math.min(amt, orders[i].amount), roomName, orders[i].roomName);
logger.log("fillSellOrder--------------------------------------------- amt:", amt, "price", orders[i].price, "my price", targetPrice)
logger.log(orders[i].amount * orders[i].price, 'creds', transferEnergyCost, "cost", maxTransferEnergyCost, 'max cost', orders[i].price >= targetPrice, "price ok")
if(transferEnergyCost < maxTransferEnergyCost && orders[i].price >= targetPrice) {
logger.log(roomName, "buying from sell order", amt, "of", resource, "for", orders[i].price)
var ret = Game.market.deal(orders[i].id, amt, roomName);
logger.log("-----", ret)
var numTries = 20;
while (ret == -6 && numTries > 0) {
//not enough energy, reduce amt and try again till it works
amt *= 0.8;
if (numTries < 4)
amt *= 0.1
amt = Math.max(Math.floor(amt), 1);
logger.log("costs too much energy, trying again with:", amt)
ret = Game.market.deal(orders[i].id, amt, roomName);
numTries--;
}
return true;
break;
}
}
return false;
},
makeBuyOrder: function(roomName, resource, amt, targetPrice) {
amt = Math.min(amt, 100000);
if (!this.haveBuyOrder(resource, roomName)) {
logger.log(roomName, "making buy order for", amt, "of", resource, "for", targetPrice)
return Game.market.createOrder(ORDER_BUY, resource, targetPrice, amt, roomName);
}
return false;
},
makeSellOrder: function(roomName, resource, amt, targetPrice) {
amt = Math.min(amt, 100000);
if (!this.haveSellOrder(resource, roomName)) {
logger.log(roomName, "making sell order for", amt, "of", resource, "for", targetPrice)
return Game.market.createOrder(ORDER_SELL, resource, targetPrice, amt, roomName);
}
return false;
},
haveSellOrder: function(resource, roomName) {
var myOrders = Game.market.orders;
for(var i in myOrders) {
if (myOrders[i].resourceType == resource && myOrders[i].type == ORDER_SELL && myOrders[i].roomName == roomName) {
return true;
}
}
return false;
},
haveBuyOrder: function(resource, roomName) {
var myOrders = Game.market.orders;
for(var i in myOrders) {
if (myOrders[i].resourceType == resource && myOrders[i].type == ORDER_BUY && myOrders[i].roomName == roomName) {
return true;
}
}
return false;
}
};