vendredi 5 avril 2019

how to fix my if statement that tries to do something?

Hoping you can help point me in the right direction please?

Iv built a snake game that is supposed to upon eating to lets say 10 fruits carry out a simple ILP-SPSP payment.

my ILP-SPSP code works fine and makes a payment if run.

my game works fine until i put the two together, either using require= failing miserable or dropping the payment code into the game slightly better success but still failing.

on adding require to my if statement or using the let require= the game doesn't load the canvas.

adding the payment code to the game code setting up a function called steamPayment is where i thought i was getting somewhere, the game now plays but does the following(it got tedious going to 10 so i dropped the score to 2):

start game snake eats fruit, fruit appears in a new location, score goes to 1. snake eats fruit, fruit stays in position, score stays the same, should streamPayment. snake eats fruit, fruit appears in new location, score goes to 3. snake eats fruit, fruit stays in position, score stays the same, should streamPayment.snake eats fruit, fruit appears in new location, score goes to 5.

and so on.

also tried using pay; and removing function streamPayment; but get the same issue.

//change difficulty level
function Check_Level()


{     
if (score % 10 == 0)
{
  difficulty ++;
  clearInterval(interval);
  interval = setInterval(Game_Loop,Speed());
  streamPayment();
 }
}

this is the ILP-SPSP

    //make the payment here to the destination ILP Pointer of the user who is playing and set a amount you want to pay
  function streamPayment()
  {
  const plugin = require('ilp-plugin')()
  const { createConnection } = require('ilp-protocol-stream')
  const { URL } = require('url')
  const fetch = require('node-fetch')

  // To request info from the ILP payment pointer (who are you, what's our shared secret?)
  async function query (receiver) {
    const endpoint = new URL(receiver.startsWith('$') ? 'https://' + receiver.substring(1) : receiver)
    endpoint.pathname = endpoint.pathname === '/' ? '/.well-known/pay' : endpoint.pathname
    const response = await fetch(endpoint.href, { headers: { accept: 'application/spsp4+json, application/spsp+json' } })

    if (response.status !== 200) {
      throw new Error(`Got error response from SPSP receiver. endpoint="${endpoint.href}" status=${response.status} message="${await response.text()}"`)
    }

    const json = await response.json()

    return {
      destinationAccount: json.destination_account,
      sharedSecret: Buffer.from(json.shared_secret, 'base64'),
      contentType: response.headers.get('content-type'),
      // Unused in simple ILP XRP-XRP example:
      balance: json.balance,
      ledgerInfo: json.ledger_info,
      receiverInfo: json.receiver_info
    }
  }

  // To start sending the payment
  const pay = (destination, amount) => {
    return new Promise((resolve, reject) => {
      return (async () => {
        console.log(`Connecting plugin (to local moneyd)`)
        await plugin.connect()

        console.log(`Sending payment. Paying ${amount} to "${destination}"\n`)
        console.log(`  - Fetching ILP payment pointer details`)
        const response = await query(destination)
        console.log(`    -> ILP address ${response.destinationAccount}`)

        if (response.contentType.indexOf('application/spsp4+json') !== -1) {
          let packetCount = 0
          let lastDeliveredAmount = 0
          const ilpConn = await createConnection({ plugin, destinationAccount: response.destinationAccount, sharedSecret: response.sharedSecret })

          const payStream = ilpConn.createStream()
          payStream.setSendMax(amount)
          payStream.on('error', reject)

          payStream.on('outgoing_money', () => {
            console.log(`  > $ Sending...`)
            packetCount++
          })

          let deliveredInterval = setInterval(() => {
            if (ilpConn.totalDelivered > 0 && ilpConn.totalDelivered !== lastDeliveredAmount) {
              if (lastDeliveredAmount === parseFloat(amount)) clearInterval(deliveredInterval)
              lastDeliveredAmount = ilpConn.totalDelivered
              console.log(`  < $ Delivered ${lastDeliveredAmount}`)

              // You can always decide it's enough at some point by re-setting the sendMax.
              // payStream.setSendMax(2000)
            }
          }, 250)

          payStream.on('end', async () => {
            clearInterval(deliveredInterval)
            const resolveData = { amount: ilpConn.totalDelivered, destination: destination, packets: packetCount }
            await payStream.destroy()
            await ilpConn.end()
            resolve(resolveData)
          })
        } else {
          reject(new Error('No application/spsp4+json content-type received from payment pointer', destination))
        }
      })()
    })
  }
  pay('$bfc6c94f7d862370.localtunnel.me', 20).then(payment => {
      console.log(`\n:D Sent ${payment.amount} to ${payment.destination} in ${payment.packets} packets\n`)
  }) 

  }

When running ILP-SPSP separately, output is successful:

Connecting plugin (to local moneyd)

Sending payment. Paying 10 to "$bfc6c94f7d862370.localtunnel.me"


- Fetching ILP payment pointer details
-> ILP address g.n3tc4t.testnet.ilsp.C-El7JSbs8QI0Jt2xtGmI9nm4G7yUb3DbQVHezURCLo.local.j-e-qzg7JlmpST1oXXXx69epOPFScllx-QHZoXPXXIY.mB-RiFgTLn1d2oQBj3v6KkJC


> $ Sending...
< $ Delivered 10

and localtunnel.me:

got packet for 10 units

this is what should happen when snake gets to a score of 10 fruits.

Aucun commentaire:

Enregistrer un commentaire