plays.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Import modules. */
  2. import moment from 'moment'
  3. import PouchDB from 'pouchdb'
  4. import gameplay from './_gameplay.js'
  5. import getSender from './_getSender.js'
  6. /* Initialize databases. */
  7. const gamesDb = new PouchDB(`http://${process.env.COUCHDB_USER}:${process.env.COUCHDB_PASSWORD}@127.0.0.1:5984/games`)
  8. const playsDb = new PouchDB(`http://${process.env.COUCHDB_USER}:${process.env.COUCHDB_PASSWORD}@127.0.0.1:5984/plays`)
  9. const MIN_PLAY_SATOSHIS = 1000000 // 10,000.00 NEX (~$0.10 as of 2023.5.25)
  10. // NOTE: We add 5% to account for variance in spot price calculation.
  11. const MAX_PLAY_SATOSHIS = 105000000 // 1,050,000.00 NEX (~$10.50 as of 2023.5.25)
  12. /**
  13. * Handle (Plays) Queue
  14. *
  15. * Process the pending queue of open transactions.
  16. *
  17. * NOTE: We handle payment processing in a SINGLE thread,
  18. * to mitigate the possibility of a "double send".
  19. */
  20. export default async (_queue, _pending) => {
  21. let response
  22. for (let i = 0; i < _pending.length; i++) {
  23. const play = _queue[_pending[i]]
  24. // console.log('PLAY (pending):', (i + 1), 'of', (_pending.length + 1), play)
  25. /* Remove (payment) from queue. */
  26. // FIXME DON'T DELETE! MARK AS USED, THEN CHECK!
  27. delete _queue[_pending[i]]
  28. const game = await gamesDb
  29. .get(play.gameid)
  30. .catch(err => console.error(err))
  31. // console.log('GAME', game)
  32. /* Set satoshis. */
  33. const satoshis = play.satoshis
  34. // console.log('SATOSHIS', satoshis)
  35. // FIXME ERROR IN QUEUE LOGIC CAUSING BAD DB
  36. if (satoshis === 0) continue
  37. /* Validate satoshis. */
  38. if (satoshis < MIN_PLAY_SATOSHIS) {
  39. play.txidem = 'DUST'
  40. play.updatedAt = moment().valueOf()
  41. response = await playsDb
  42. .put(play)
  43. .catch(err => console.error(err))
  44. // console.log('RESPONSE', response)
  45. continue
  46. }
  47. /* Validate satoshis. */
  48. if (satoshis > MAX_PLAY_SATOSHIS) {
  49. play.txidem = 'OVER_LIMIT'
  50. play.updatedAt = moment().valueOf()
  51. response = await playsDb
  52. .put(play)
  53. .catch(err => console.error(err))
  54. // console.log('RESPONSE', response)
  55. continue
  56. }
  57. /* Request player. */
  58. const sender = await getSender(play.unspent)
  59. // console.log('SENDER', sender)
  60. /* Handle gameplay. */
  61. await gameplay(game, play, sender)
  62. }
  63. }