Market Script’s Tutorial – Sell Energy

0
544
Order History for Screeps Energy Market. Here you can see the number of transactions per day, order volume and avg price.

 

In the game, there is a market for in-game resources. Using these resources you can earn credits which you can trade with or purchase subscription tokens (Play the Game for Free).

There are many ways to earn credits. You can sell minerals, power, boosts or energy. Energy is a great place to start since it is easy to acquire and for many players a surplus exists.

I have created a basic script below you can paste into your code to automatically sell energy to the best order. Let’s break down the code a bit below:

let buyOrders = Game.market.getAllOrders({resourceType: RESOURCE_ENERGY, type: ORDER_BUY});

The first part of the code queries the orders from the market on the shard. There is a changelog article which talks about the various ways to filter the results. An object literal is the fastest as it can be passed directly to the database. Market Order Filter – Changelog – 2016-09-30

The next thing we will do is sort the orders by price so we can always get the highest price for our resources.

_.sortBy(buyOrders, ['price']);

Next we are going to get a list of our rooms by filtering the Game.rooms object. (This contains all the room objects we have access to).

let myRooms = _.filter(Game.rooms, r => r.controller && r.controller.my);

The last part does the following:
1. loops through all our rooms.
2. gets the terminal
3. if the room has a terminal, it is not cooling down, and it has energy then initiate the best deal from the list.

_.forEach(myRooms, function(room) {
let terminal = room.terminal;
if(terminal && !terminal.cooldown && terminal.store.energy) {
Game.market.deal(buyOrders[0].id, terminal.store.energy / 2, room.name)
}
})

The deal amount is half of the store so that there is enough energy to execute the sale.

Full script below:

let buyOrders = Game.market.getAllOrders({resourceType: RESOURCE_ENERGY, type: ORDER_BUY});
_.sortBy(buyOrders, ['price']);
let myRooms = _.filter(Game.rooms, r => r.controller && r.controller.my);
_.forEach(myRooms, function(room) {
let terminal = room.terminal;
if(terminal && !terminal.cooldown && terminal.store.energy) {
Game.market.deal(buyOrders[0].id, terminal.store.energy / 2, room.name)
}
})