stacker.news/worker/auction.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-02-25 17:34:09 +00:00
const serialize = require('../api/resolvers/serial')
function auction ({ models }) {
return async function ({ name }) {
console.log('running', name)
2022-03-02 20:55:13 +00:00
// TODO: do this for each sub with auction ranking
// because we only have one auction sub, we don't need to do this
const SUB_BASE_COST = 10000
const BID_DELTA = 1000
// get all items we need to check in order of low to high bid
2022-02-25 17:34:09 +00:00
const items = await models.item.findMany(
{
where: {
maxBid: {
not: null
},
status: {
not: 'STOPPED'
}
2022-03-02 20:55:13 +00:00
},
orderBy: {
maxBid: 'asc'
2022-02-25 17:34:09 +00:00
}
}
)
2022-03-02 20:55:13 +00:00
// we subtract bid delta so that the lowest bidder, pays
// the sub base cost
let lastBid = SUB_BASE_COST - BID_DELTA
2022-02-25 17:34:09 +00:00
// for each item, run serialized auction function
2022-03-02 20:55:13 +00:00
for (const item of items) {
let bid = lastBid
// if this item's maxBid is great enough, have them pay more
// else have them match the last bid
if (item.maxBid >= lastBid + BID_DELTA) {
bid = lastBid + BID_DELTA
}
const [{ run_auction: succeeded }] = await serialize(models,
models.$queryRaw`SELECT run_auction(${item.id}, ${bid})`)
// if we succeeded update the lastBid
if (succeeded) {
lastBid = bid
}
}
2022-02-25 17:34:09 +00:00
console.log('done', name)
}
}
module.exports = { auction }