/
backend.js
77 lines (74 loc) · 2.93 KB
/
backend.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
module.exports = function (config) {
if (config.backend) {
config.cronjobs.recreateNpcOrders = [60, () => basicNPCOrders(config)]
}
}
async function basicNPCOrders (config) {
const { common: { storage: { db } }, market } = config
market.randomVariance = market.randomVariance || 1
let terminals = await db['rooms.objects'].find({ $and: [{ type: 'terminal' }, { user: { $eq: null } }] })
let ps = terminals.map(async terminal => {
let orders = await db['market.orders'].find({ roomName: terminal.room })
orders = orders.reduce((l, v) => { l[v.type][v.resourceType] = v; return l }, { buy: {}, sell: {} })
let updates = []
Object.keys(market.buyPrices).forEach(mineral => {
let buy = orders.buy[mineral]
if (!buy) {
buy = {
created: 1,
active: true,
type: 'buy',
amount: market.buyAmount,
remainingAmount: market.buyAmount,
totalAmount: market.buyAmount,
resourceType: mineral,
roomName: terminal.room
}
}
if (market.priceMode === 'fixed') {
buy.remainingAmount = market.buyAmount
buy.totalAmount = market.buyAmount
buy.amount = market.buyAmount
buy.price = market.buyPrices[mineral]
} else if (market.priceMode === 'random') {
let amount = Math.floor(market.buyAmount * (Math.random() * market.randomVariance + (market.randomVariance / 2)))
buy.totalAmount = amount
buy.remainingAmount = amount
buy.amount = amount
buy.price = market.buyPrices[mineral] * (Math.random() * market.randomVariance + (market.randomVariance / 2))
}
updates.push(buy)
})
Object.keys(market.sellPrices).forEach(mineral => {
let sell = orders.sell[mineral]
if (!sell) {
sell = {
created: 1,
active: true,
type: 'sell',
amount: market.sellAmount,
remainingAmount: market.sellAmount,
totalAmount: market.sellAmount,
resourceType: mineral,
roomName: terminal.room
}
}
if (market.priceMode === 'fixed') {
sell.remainingAmount = market.sellAmount
sell.totalAmount = market.sellAmount
sell.amount = market.sellAmount
sell.price = market.sellPrices[mineral]
} else if (market.priceMode === 'random') {
let amount = Math.floor(market.buyAmount * (Math.random() * market.randomVariance + (market.randomVariance / 2)))
sell.totalAmount = amount
sell.remainingAmount = amount
sell.amount = amount
sell.price = market.sellPrices[mineral] * (Math.random() * market.randomVariance + (market.randomVariance / 2))
}
updates.push(sell)
})
updates.forEach(o => delete o._id)
return updates.map(order => db['market.orders'].update({ type: order.type, roomName: order.roomName, resourceType: order.resourceType }, order, { upsert: true }))
})
return Promise.all(ps)
}