new.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* Import modules. */
  2. import {
  3. ripemd160,
  4. sha256,
  5. } from '@nexajs/crypto'
  6. import { binToHex } from '@nexajs/utils'
  7. /* Initialize stores. */
  8. import { useProfileStore } from '@/stores/profile'
  9. /**
  10. * Request a New Game
  11. *
  12. * Calls game engine, with player parameters, to receive a new game id.
  13. */
  14. export default async function () {
  15. /* Set Nexa Games endpoint. */
  16. const ENDPOINT = 'https://nexa.games'
  17. const Profile = useProfileStore()
  18. /* Set RPC method. */
  19. const method = 'POST'
  20. /* Validate requested payout. */
  21. if (this.payout === null) {
  22. throw new Error('Request a payout value to continue.')
  23. }
  24. /* Validate (player) position. */
  25. if (this.position === null) {
  26. throw new Error('Select a player position to continue.')
  27. }
  28. /* Validate player seed. */
  29. if (this.seed === null) {
  30. throw new Error('Set a player-defined seed to continue.')
  31. }
  32. /* Set payout for house participants (as a percentage %). */
  33. // NOTE: All participant shares must total 100.
  34. // NOTE: At least 1 participant is required.
  35. const house = [{
  36. // NEXA.games: 0.5% is automatically subtracted from the house edge
  37. // value shown above to cover game engine & blockchain fees.
  38. // 'PUT_A_VALID_NEXA_ADDRESS_HERE': 40, // Gamemaker -- TEMP FOR DEV
  39. // 'PUT_A_VALID_NEXA_ADDRESS_HERE': 40, // Bankers -- TEMP FOR DEV
  40. // 'PUT_A_VALID_NEXA_ADDRESS_HERE': 20, // Promoter -- TEMP FOR DEV
  41. }]
  42. /* Create RPC body. */
  43. const body = {
  44. gameid: this.gameid,
  45. playerid: Profile.playerid,
  46. payout: this.payout,
  47. seed: this.seed,
  48. position: this.position,
  49. rtp: this.rtp,
  50. house,
  51. }
  52. /* Request game play. */
  53. const response = await $fetch(ENDPOINT + '/v1/play', {
  54. method,
  55. body,
  56. }).catch(err => console.error(err))
  57. console.info('Gameplay details:', response)
  58. /* Set play id. */
  59. this.setPlayid(response?.playid)
  60. /* Set (play) address. */
  61. this.setAddress(response?.address)
  62. /* Build authorization value. */
  63. const authValue = `${this.playid}:${this.rtp}:${this.payout}:${this.position}:${this.seed}`
  64. // console.log('AUTH VALUE', authValue)
  65. /* Double-hash authorization value. */
  66. this.setAuthHash(binToHex(ripemd160(sha256(authValue), 'binary')))
  67. // console.log('AUTH HASH', this.authHash)
  68. }