-
Notifications
You must be signed in to change notification settings - Fork 0
/
B_terminal.js
207 lines (179 loc) · 6.14 KB
/
B_terminal.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
function GetMedian(values) {
if (values.length === 0) return 0;
values.sort(function (a, b) {
return a - b;
});
var half = Math.floor(values.length / 2);
if (values.length % 2) return values[half];
return (values[half - 1] + values[half]) / 2.0;
}
function CalcCreditPerformance(amount) {
if (Memory.creditPerformance && Memory.creditPerformance.time && Memory.creditPerformance.avg) {
const timePast = Game.time - Memory.creditPerformance.time;
Memory.creditPerformance.avg = (Memory.creditPerformance.avg * Math.min(timePast - 1, 1) + amount / timePast) / timePast;
Memory.creditPerformance.time = Game.time;
} else {
Memory.creditPerformance = { avg: amount, time: Game.time };
}
}
StructureTerminal.prototype.getMaxAmount = function (order) {
const amount = this.store.getUsedCapacity(RESOURCE_ENERGY);
const distance = Game.map.getRoomLinearDistance(order.roomName, this.room.name, true);
if (order) {
const flag = [order.resourceType == RESOURCE_ENERGY, distance > 30].reduce((prev, curr, index) => (curr ? prev + 2 ** index : prev), 0);
switch (flag) {
case 0:
return Math.floor(amount / (1 - Math.exp(-distance / 30))) - 1;
case 1:
return Math.floor(amount / (2 - Math.exp(-distance / 30))) - 1;
case 2:
return Math.min(order.remainingAmount, this.store.getUsedCapacity(order.resourceType));
case 3:
return Math.min(Math.floor(amount / 2), order.remainingAmount * 2);
default:
console.log("flag not found");
return 0;
}
}
};
StructureTerminal.prototype.trySell = function (order, left = 0) {
if (order.type == ORDER_SELL) {
return false;
}
const amount = Math.min(order.remainingAmount, this.getMaxAmount(order), this.store.getUsedCapacity(order.resourceType) - left);
if (amount > 0) {
const cost = Game.market.calcTransactionCost(amount, this.room.name, order.roomName);
if (cost < this.store.getUsedCapacity(RESOURCE_ENERGY)) {
var deal = Game.market.deal(order.id, amount, this.room.name);
if (deal == OK) {
CalcCreditPerformance(amount * order.price);
return true;
} else if (deal == ERR_TIRED || deal == ERR_FULL) {
return true;
}
}
}
return false;
};
StructureTerminal.prototype.tryBuy = function (order, left = 0) {
if (order.type == ORDER_BUY) {
return false;
}
let amount = Math.min(order.remainingAmount, this.getMaxAmount(order), Math.floor((Game.market.credits - left) / order.price));
if (amount > 0) {
const cost = Game.market.calcTransactionCost(amount, this.room.name, order.roomName);
if (order.resourceType == RESOURCE_ENERGY && cost >= amount) {
return false;
}
if (cost < this.store.getUsedCapacity(RESOURCE_ENERGY)) {
var deal = Game.market.deal(order.id, amount, this.room.name);
if (deal == OK || deal == ERR_TIRED || deal == ERR_FULL) {
return true;
}
}
}
return false;
};
StructureTerminal.prototype.sellResource = function (resource, left = 0) {
if (this.store.getUsedCapacity(resource) <= left) {
return false;
}
const history = Game.market.getHistory(resource);
if (!history.length) {
return false;
}
const avgPrice = GetMedian(history.map((i) => i.avgPrice));
const stddev = history.map((i) => i.stddevPrice).reduce((a, b) => Math.min(a, b), history[0].stddevPrice);
const avg = avgPrice + stddev / 2;
const allOrders = Game.market.getAllOrders({ resourceType: resource });
const orders = allOrders.filter((order) => order.type == ORDER_BUY).sort((a, b) => b.price - a.price);
const highDemand = allOrders.filter((order) => order.type == ORDER_SELL).length < 10;
for (const order of orders) {
if (highDemand || order.price > avg) {
if (this.trySell(order, left)) {
return true;
}
} else {
break;
}
}
return false;
};
StructureTerminal.prototype.buyResource = function (resource, left = 0, force = false) {
if (Game.market.credits < left) {
return false;
}
if (force) {
const orders = Game.market.getAllOrders({ resourceType: resource, type: ORDER_SELL }).sort((a, b) => a.price - b.price);
for (const order of orders) {
if (order.price < Game.market.credits - left) {
if (this.tryBuy(order, left)) {
return true;
}
} else {
break;
}
}
} else {
const history = Game.market.getHistory(resource);
if (!history.length) {
return false;
}
const transactions = GetMedian(history.map((i) => i.transactions));
if (transactions > 100) {
const allOrders = Game.market.getAllOrders({ resourceType: resource });
if (allOrders.filter((order) => order.type == ORDER_BUY).length > 10) {
const avgPrice = GetMedian(history.map((i) => i.avgPrice));
const stddev = GetMedian(history.map((i) => i.stddevPrice));
const avg = avgPrice + stddev / 3;
const orders = allOrders.filter((order) => order.type == ORDER_SELL).sort((a, b) => a.price - b.price);
for (const order of orders) {
if (order.price < avg && order.price < Game.market.credits - left && avg < Game.market.credits - left) {
if (this.tryBuy(order, left)) {
return true;
}
} else {
break;
}
}
}
}
}
return false;
};
StructureTerminal.prototype.doRole = function () {
if (this.cooldown > 0 || this.store.getUsedCapacity(RESOURCE_ENERGY) < 2) {
return;
}
for (const element of Object.keys(this.store)) {
if (element != RESOURCE_ENERGY && this.sellResource(element)) {
return;
}
}
const reserve = Memory.bestPixelPrice * 1.1;
if (this.store.getUsedCapacity(RESOURCE_ENERGY) > 10000 && Game.market.credits > reserve && this.store.getFreeCapacity() > 10000) {
if (RESOURCES_ALL.includes(Memory.pump)) {
if (this.buyResource(Memory.pump, reserve, true)) {
console.log(`pumped ${Memory.pump} @${Game.time}`);
}
return;
} else {
for (const element of RESOURCES_ALL) {
if (element != RESOURCE_ENERGY && this.buyResource(element, reserve)) {
return;
}
}
}
}
if ((this.store.getUsedCapacity(RESOURCE_ENERGY) > 100000 || this.store.getFreeCapacity() < 10000) && this.sellResource(RESOURCE_ENERGY, 10000)) {
return;
}
if (Game.market.credits > reserve && this.store.getUsedCapacity(RESOURCE_ENERGY) < 1000) {
this.buyResource(RESOURCE_ENERGY, reserve);
}
};
module.exports = {
fx: function (terminal) {
terminal.doRole();
},
};