jaredlyon / JareBot

A revisited JS project
GNU General Public License v3.0
5 stars 1 forks source link

Blackjack Module Processes Bets Below $0.01 #4

Closed jaredlyon closed 5 years ago

jaredlyon commented 5 years ago

Users are able to submit extremely low values as a bet, allowing for an effectively nonexistent bet to be played. image

Additionally, users may submit certain small integer values (such as half a cent) which yield awkward returns. image

catamay commented 5 years ago

In line 83 of blackjack.js, a simple fix for this would be to make bet equal to Math.trunc(Number(msg.args[0])*100)/100; (This just makes an output of some number 0.123456... to 0.12) Then you can add whatever if statement required if the output above is zero (to prevent the minuscule bets issue.)

jaredlyon commented 5 years ago

Tested the following code:

//initial bet
        if (msg.args[0] && isNaN(msg.args[0])) { 
          bet = 10;
          if (bot.bank[msg.author.id].balance.toFixed(2) < bet) {
            msg.channel.send("You frickin' foolian juulian, you don't have enough money to cover your bet!")
            return;
          } else {
            bot.bank[msg.author.id].balance -= bet
          }
        }
        else if (msg.args[0] && !isNaN(msg.args[0]) && Number(msg.args[0]) > 0) {
          bet = Number(msg.args[0]);
          if (bot.bank[msg.author.id].balance.toFixed(2) < bet) {
            msg.channel.send("You frickin' foolian juulian, you don't have enough money to cover your bet!")
            return;
          } else if (bet < 0.01) {
            msg.channel.send("You frickin' foolian juulian, you can't bet that amount!")
            return;
          } else {
            bot.bank[msg.author.id].balance -= bet
          }
        }
        else {
          bet = 10;
          if (bot.bank[msg.author.id].balance.toFixed(2) < bet) {
            msg.channel.send("You frickin' foolian juulian, you don't have enough money to cover your bet!")
            return;
          } else {
            bot.bank[msg.author.id].balance -= bet
          }
        }

Results: image

Will continue testing to prevent $0 bets from causing the bot to attempt setting a $10 bet amount. Currently, the bot is live with the above code, but a patch will not be pushed to GitHub until the aforementioned issue is also resolved.

jaredlyon commented 5 years ago

In line 83 of blackjack.js, a simple fix for this would be to make bet equal to Math.trunc(Number(msg.args[0])*100)/100; (This just makes an output of some number 0.123456... to 0.12) Then you can add whatever if statement required if the output above is zero (to prevent the minuscule bets issue.)

Also note that a solution that involves the provided trunc() function would cause issue when rounding values close to zero: image

The given trunc() function ultimately returns certain values back to zero, prompting the current bot architecture to reset the bet amount to $10 instead of exiting the process entirely. In my opinion, the easiest solution would be to just outright block all bets with values below $0.01, as the current architecture rounds the bet using a .toFixed(2) function at the end of the initial bet amount assignment.

jaredlyon commented 5 years ago

Eventually, I just rewrote the entire bet init process:

//initial bet
        if (msg.args[0] && !isNaN(msg.args[0])) {
          bet = Number(msg.args[0]);
          if (bot.bank[msg.author.id].balance.toFixed(2) < bet) {
            msg.channel.send("You frickin' foolian juulian, you don't have enough money to cover your bet!")
            return;
          } else if (bet < 0.01) {
            msg.channel.send("You frickin' foolian juulian, you can't bet that amount!")
            return;
          } else {
            bot.bank[msg.author.id].balance -= bet
          }
        } else {
          bet = 10;
          if (bot.bank[msg.author.id].balance.toFixed(2) < bet) {
            msg.channel.send("You frickin' foolian juulian, you don't have enough money to cover your bet!")
            return;
          } else if (bet < 0.01) {
            msg.channel.send("You frickin' foolian juulian, you can't bet that amount!")
            return;
          } else {
            bot.bank[msg.author.id].balance -= bet
          }
        }

Results: image