screeps/proc.market.js

63 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2020-11-09 19:31:39 -06:00
/*
* 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('proc.market');
* mod.thing == 'a thing'; // true
*/
let gomarket={
sellEnergy: function(myroom,mat) {
2020-11-09 19:31:39 -06:00
try{
let availtosell= Game.rooms[myroom].terminal.store.getUsedCapacity(mat)
if(availtosell>0){
console.log(myroom + ":" + mat +":" + availtosell)
2020-11-09 19:31:39 -06:00
//console.log(JSON.stringify(Game.market.orders))
// if(Game.market.orders.length > 9){
let myorders = Game.market.getAllOrders(order=>order.resourceType == mat && order.type == ORDER_BUY)
//console.log("Open orders: " + myorders.length)
2020-11-09 19:31:39 -06:00
//console.log(myorders.length)
for(let i=0; i<myorders.length; i++){
let recheckavailtosell= Game.rooms[myroom].terminal.store.getUsedCapacity(mat)
2020-11-09 19:31:39 -06:00
if(recheckavailtosell<1000){
console.log("too little to mess with")
} else {
//let maxTransferEnergyCost=500
//const transferEnergyCost = Game.market.calcTransactionCost(myorders[i].amount, myroom, myorders[i].roomName);
//if(transferEnergyCost < maxTransferEnergyCost) {
if(recheckavailtosell < myorders[i].amount){
amttosell = recheckavailtosell - 1000
} else {
amttosell=myorders[i].amount
}
console.log( myroom + " trying to sell " + amttosell + " " + mat + " on order size of " + myorders[i].amount + " with " + recheckavailtosell + " left and " + myorders.length + " orders open")
2020-11-09 19:31:39 -06:00
let mydeal = Game.market.deal(myorders[i].id, amttosell, myroom);
if(mydeal == 0){
console.log("Order Created")
} else if(mydeal==-6){
console.log("not enouhg resources")
} else if(mydeal == -8){
console.log("too many orders")
} else if (mydeal == -10){
console.log("Invalid arguments")
} else if (mydeal==-11){
console.log("terminal cooling down")
} else {
console.log(mydeal)
}
2020-11-09 19:31:39 -06:00
//} else {
// console.log("Too expensive")
//}
}
}
// } else {
// console.log("Too many orders pending")
// }
}
}catch(e){
console.log(e)
}
}
}
module.exports = gomarket;