Guest User

Untitled

a guest
Oct 11th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. const tmi = require('tmi.js')
  2.  
  3. const ADMIN_USER = 'username'
  4. const CHANNEL_NAME = '#channelname'
  5. const TWITCH_USER = 'username'
  6. const PASSWORD = 'oauth:password'
  7.  
  8. const game = {
  9. activePlayers: {},
  10. bettingActive: true,
  11. wallets: {},
  12. bettingPool: 0,
  13. }
  14.  
  15. const client = new tmi.client({
  16. options:{
  17. debug: false,
  18. debugIgnore: ['chat']
  19. },
  20. connection: {
  21. reconnect: true
  22. },
  23. identity: {
  24. username: TWITCH_USER,
  25. password: PASSWORD
  26. },
  27. channels: [CHANNEL_NAME]
  28. })
  29.  
  30. client.connect()
  31.  
  32. function getWallet(user) {
  33. if (!game.wallets[user.username]) {
  34. game.wallets[user.username] = {
  35. ammount: 1000,
  36. }
  37. }
  38.  
  39. return game.wallets[user.username]
  40. }
  41.  
  42. function reply(user, message, isWhisper) {
  43. if (isWhisper) {
  44. client.whisper(user.username, message)
  45. } else {
  46. client.say(CHANNEL_NAME, message)
  47. }
  48. }
  49.  
  50. function handleCommands(user, message, isWhisper) {
  51. if (message === '!play') {
  52. game.activePlayers[user.username] = Object.assign(user, {
  53. betOnBy: {}
  54. })
  55. }
  56.  
  57. if (message === '!currency') {
  58. const wallet = getWallet(user)
  59.  
  60. reply(user, `${user.username} has ${wallet.ammount} coins to bet with`, isWhisper)
  61. }
  62.  
  63. if (typeof message === 'string' && message.indexOf('!bet') === 0) {
  64. if (game.bettingActive === false) {
  65. reply(user, `Bets are closed`, true)
  66. return
  67. }
  68.  
  69. const [command, betOnUser, ammount] = message.toLowerCase().split(' ')
  70.  
  71. const betAmmount = parseInt(100)
  72.  
  73. if (typeof betAmmount !== 'number') {
  74. reply(user, `Please enter a valid amount`, true)
  75. }
  76.  
  77. if (!game.activePlayers[betOnUser]) {
  78. reply(user, `${betOnUser} is not in this game`, true)
  79. return
  80. }
  81.  
  82. const wallet = getWallet(user)
  83.  
  84. if (wallet.ammount < betAmmount) {
  85. reply(user, `You only have ${wallet.ammount} coins to bet with`)
  86. return
  87. }
  88.  
  89. game.wallets[user.username].ammount = wallet.ammount - betAmmount
  90. game.activePlayers[betOnUser].betOnBy[user.username] = user
  91. game.bettingPool = game.bettingPool + betAmmount
  92.  
  93. reply(user, `You have placed a ${betAmmount} bet on ${betOnUser}`, true)
  94. }
  95.  
  96. if (user.username === ADMIN_USER) {
  97. if (message === '!closebets') {
  98. game.bettingActive = false
  99.  
  100. reply(user, `Bets are closed`, isWhisper)
  101. }
  102.  
  103. if (typeof message === 'string' && message.indexOf('!winner') === 0) {
  104. const [command, winningUser] = message.toLowerCase().split(' ')
  105.  
  106. if (!game.activePlayers[winningUser]) {
  107. reply(user, `${winningUser} was not playing the game`)
  108. return
  109. }
  110.  
  111. const winningBetters = game.activePlayers[winningUser].betOnBy
  112. const winningBettersCollection = Object.keys(winningBetters)
  113. const totalWinners = winningBettersCollection.length
  114. const prizePerBetter = game.bettingPool / totalWinners
  115.  
  116. winningBettersCollection.map(better => {
  117. game.wallets[better].ammount = game.wallets[better].ammount + prizePerBetter
  118. reply(winningBetters[better], `You won ${prizePerBetter}, your total currency is ${game.wallets[better].ammount}`, true)
  119. })
  120. }
  121.  
  122. if (message === '!reset') {
  123. game.activePlayers = {}
  124. game.bettingActive = true
  125. game.bettingPool = 0
  126.  
  127. reply(user, `Reseting game`, isWhisper)
  128. }
  129.  
  130. if (message === '!totalplayers') {
  131. reply(user, `${Object.keys(game.activePlayers).length} players in the game`, isWhisper)
  132. }
  133. }
  134. }
  135.  
  136. client.addListener('chat', function (channel, user, message) {
  137. // console.log('HELLO', user.username, message)
  138. handleCommands(user, message, false)
  139. })
  140.  
  141. client.on('whisper', function (from, user, message, self) {
  142. handleCommands(user, message, true)
  143. })
Add Comment
Please, Sign In to add comment