diff --git a/app.js b/app.js index 1765bbb..f99feda 100644 --- a/app.js +++ b/app.js @@ -5,6 +5,7 @@ const { Telegraf } = require('telegraf'); const { getJoke } = require('./libs/dadJokes'); const { rtag, r34 } = require('./libs/rule34'); const { addToLogs, isTrue, image_search } = require('./libs/botTools'); +const { rockPaperScissorsAgainstBot } = require('./libs/games'); //bot initialization const bot = new Telegraf(process.env.TELEGRAM); @@ -66,5 +67,9 @@ bot.command('dadjoke', ctx => { console.log('--> sent a dad joke') }) +bot.command('rps', ctx => { + rockPaperScissorsAgainstBot(ctx.message.text.slice(+5), ctx, bot) +}) + //bot launch bot.launch(); \ No newline at end of file diff --git a/libs/games.js b/libs/games.js index 48b5eb4..8aa7ad6 100644 --- a/libs/games.js +++ b/libs/games.js @@ -1,50 +1,56 @@ -function rockPaperScissorsAgainstBot(ctx, bot) { - // Variables - var userChoice; - var computerChoice; - const CHOICES = ["rock", "paper", "scissors"]; +function rockPaperScissorsAgainstBot(userChoice, ctx, bot) { + const choices = ["rock", "paper", "scissors"]; - // Computer choice + if (choices.includes(userChoice) == true) + { + const botChoice = choices[Math.floor(Math.random() * choices.length)]; + bot.telegram.sendMessage(ctx.chat.id, "You chose " + userChoice + " and I chose " + botChoice, {}) - computerChoice = CHOICES[Math.floor(Math.random() * CHOICES.length)]; - - // User choice - - bot.telegram.sendMessage(ctx.chat.id, "Choose between rock, paper or scissors", {}); - - - - //Display choices - bot.telegram.sendMessage(ctx.chat.id, "You chose " + userChoice + ". The bot chose " + computerChoice + "."); - - // Winner - switch (userChoice) { - case "rock": - if (computerChoice == "rock") { - ctx.reply("It's a tie"); - } else if (computerChoice == "paper") { - ctx.reply("You lose"); - } else { - ctx.reply("You win"); + //wait 50ms + setTimeout(function() { + if (userChoice == botChoice) + { + bot.telegram.sendMessage(ctx.chat.id, "It's a tie!", {}); + } + else if (userChoice == "rock") + { + if (botChoice == "paper") + { + bot.telegram.sendMessage(ctx.chat.id, "You lose! Paper beats rock.", {}); + } + else + { + bot.telegram.sendMessage(ctx.chat.id, "You win! Rock beats scissors.", {}); + } + } + else if (userChoice == "paper") + { + if (botChoice == "scissors") + { + bot.telegram.sendMessage(ctx.chat.id, "You lose! Scissors beats paper.", {}); + } + else + { + bot.telegram.sendMessage(ctx.chat.id, "You win! Paper beats rock.", {}); + } + } + else if (userChoice == "scissors") + { + if (botChoice == "rock") + { + bot.telegram.sendMessage(ctx.chat.id, "You lose! Rock beats scissors.", {}); + } + else + { + bot.telegram.sendMessage(ctx.chat.id, "You win! Scissors beats paper.", {}); + } } - break; - case "paper": - if (computerChoice == "rock") { - ctx.reply("You win"); - } else if (computerChoice == "paper") { - ctx.reply("It's a tie"); - } else { - ctx.reply("You lose"); - } - break; - case "scissors": - if (computerChoice == "rock") { - ctx.reply("You lose"); - } else if (computerChoice == "paper") { - ctx.reply("You win"); - } else { - ctx.reply("It's a tie"); - } - break; + }, 50); + } else + { + bot.telegram.sendMessage(ctx.chat.id, "Please choose between rock, paper and scisors", {}); } -} \ No newline at end of file + +} + +module.exports = { rockPaperScissorsAgainstBot }; \ No newline at end of file