This commit is contained in:
Lukian LEIZOUR 2022-11-27 21:09:37 +01:00
parent 4087e18757
commit b9e306320c
2 changed files with 57 additions and 46 deletions

5
app.js
View file

@ -5,6 +5,7 @@ const { Telegraf } = require('telegraf');
const { getJoke } = require('./libs/dadJokes'); const { getJoke } = require('./libs/dadJokes');
const { rtag, r34 } = require('./libs/rule34'); const { rtag, r34 } = require('./libs/rule34');
const { addToLogs, isTrue, image_search } = require('./libs/botTools'); const { addToLogs, isTrue, image_search } = require('./libs/botTools');
const { rockPaperScissorsAgainstBot } = require('./libs/games');
//bot initialization //bot initialization
const bot = new Telegraf(process.env.TELEGRAM); const bot = new Telegraf(process.env.TELEGRAM);
@ -66,5 +67,9 @@ bot.command('dadjoke', ctx => {
console.log('--> sent a dad joke') console.log('--> sent a dad joke')
}) })
bot.command('rps', ctx => {
rockPaperScissorsAgainstBot(ctx.message.text.slice(+5), ctx, bot)
})
//bot launch //bot launch
bot.launch(); bot.launch();

View file

@ -1,50 +1,56 @@
function rockPaperScissorsAgainstBot(ctx, bot) { function rockPaperScissorsAgainstBot(userChoice, ctx, bot) {
// Variables const choices = ["rock", "paper", "scissors"];
var userChoice;
var computerChoice;
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)]; //wait 50ms
setTimeout(function() {
// User choice if (userChoice == botChoice)
{
bot.telegram.sendMessage(ctx.chat.id, "Choose between rock, paper or scissors", {}); bot.telegram.sendMessage(ctx.chat.id, "It's a tie!", {});
}
else if (userChoice == "rock")
{
//Display choices if (botChoice == "paper")
bot.telegram.sendMessage(ctx.chat.id, "You chose " + userChoice + ". The bot chose " + computerChoice + "."); {
bot.telegram.sendMessage(ctx.chat.id, "You lose! Paper beats rock.", {});
// Winner }
switch (userChoice) { else
case "rock": {
if (computerChoice == "rock") { bot.telegram.sendMessage(ctx.chat.id, "You win! Rock beats scissors.", {});
ctx.reply("It's a tie"); }
} else if (computerChoice == "paper") { }
ctx.reply("You lose"); else if (userChoice == "paper")
} else { {
ctx.reply("You win"); 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; }, 50);
case "paper": } else
if (computerChoice == "rock") { {
ctx.reply("You win"); bot.telegram.sendMessage(ctx.chat.id, "Please choose between rock, paper and scisors", {});
} 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;
} }
}
}
module.exports = { rockPaperScissorsAgainstBot };