Advertisement
Guest User

bet.js

a guest
Mar 17th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* simple betting module for KoalaBot */
  2.  
  3. var modBet = (function(){
  4.  
  5.     // adding the command
  6.     apiAddCmd("bet", "modBet.betCmd", "all", "Bet on the outcome of the game!");
  7.  
  8.     // setup vars
  9.     var betsOpen = false;
  10.     var pool = {};
  11.  
  12.     // The command function
  13.     var _betCmd = function (params, from, mod) {
  14.  
  15.         //Check for mod
  16.         if(mod || from.toLowerCase() === apiGetChannelName()) {
  17.             if (params[0] === 'open' || params[0] === 'Open') {
  18.                 betsOpen = true
  19.                 return apiSay( `Bets on the outcome are now open!` );
  20.             }
  21.             if (params[0] === 'close' || params[0] === 'Close') {
  22.                 betsOpen = false
  23.                 return apiSay( `Bets on the outcome are now closed!` );;
  24.             }
  25.             if (params[0] === 'draw' || params[0] === 'Draw') {
  26.                 for (user in pool) {
  27.                     var winnings = parseInt(pool[user][1], 10);
  28.                     apiModPoints( user, winnings );
  29.                 }
  30.                 betsOpen = false
  31.                 pool = {};
  32.                 return apiSay(`Bet ended in a draw! All ${apiGetPointsUnit()} returned to users.`);
  33.             }
  34.             if (params[1] === 'Wins' || params[1] === 'wins' || params[1] === 'win' || params[1] === 'win') {
  35.                 var winnerList = 'Bets ended... The user(s): ';
  36.                 // Update winners here.
  37.                 for (user in pool) {
  38.                     if (pool[user][0] === params[0]) {
  39.                         var winnings = parseInt(pool[user][1], 10) * 2;
  40.                         if (winnerList === 'Bets ended... The user(s): ') {
  41.                             winnerList += from;
  42.                         }else{
  43.                             winnerList += (", "+from);
  44.                         }
  45.                         apiModPoints( user, winnings );
  46.                     }
  47.                 }
  48.                 betsOpen = false;
  49.                 pool = {};
  50.                 if (winnerList === 'Bets ended... The user(s): ') {
  51.                     return apiSay( `No one won the bet! </3` );
  52.                 }else{
  53.                     return apiSay( `${winnerList}. have won the bet!` );
  54.                 }
  55.             }
  56.         }
  57.  
  58.         if ( !params[0] ) {
  59.             return apiSay( `Type "!bet [number] [amount]" to bet on the outcome of this game!` );
  60.         }
  61.  
  62.         if (!betsOpen) {
  63.             return apiSay( `Sorry ${from}, but bets are currently closed!` );
  64.         }
  65.  
  66.         if ( !params[1] ){
  67.             return apiSay( `${from}, please enter an amount of ${apiGetPointsUnit()} to bet.` );
  68.         }
  69.  
  70.         var betAmount = params[1];
  71.  
  72.         if ( betAmount != parseInt(betAmount, 10) ) {
  73.             return apiSay( `${from}, ${bet} is not a number.` );
  74.         }else if ( betAmount < 0 ) {
  75.             return apiSay( `${from}, you can't bet a negative amount!` );
  76.         }
  77.         var totalMoney = apiGetPoints(from);
  78.         if ( betAmount > totalMoney ) {
  79.             return apiSay( `Sorry ${from}, you don't have enough ${apiGetPointsUnit()}.` );
  80.         }
  81.  
  82.         apiModPoints( from, -1*parseInt(betAmount, 10) )
  83.         pool[from] = [params[0], betAmount]
  84.         // return apiSay( `${from} successfully bet ${betAmount} on ${params[0]}` );
  85.     }
  86.  
  87.     // .gambleCmd() is the only thing in this module accessible from the outside
  88.     return {
  89.         betCmd : _betCmd,
  90.         pool: pool
  91.     };
  92.  
  93. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement