Added Rock, Paper, Scissors.

This commit is contained in:
ThePendulum 2022-10-24 01:55:30 +02:00
parent 29f69fa40a
commit 6428c01e6c
8 changed files with 83 additions and 11 deletions

View File

@ -4,7 +4,6 @@ module.exports = {
platform: 'schat',
user: {
id: 'aisha',
nick: 'aisha',
username: 'Aisha',
realName: 'Aisha',
},
@ -22,7 +21,7 @@ module.exports = {
greeting: 'Hi, I am aisha, your game host!',
usernamePrefix: '@',
channels: ['GamesNight'],
games: ['mash', 'trivia', 'letters', 'duck', 'dice', 'ping', 'say', 'kill', 'uptime', 'help'],
games: ['mash', 'trivia', 'letters', 'duck', 'dice', 'rock-paper-scissors', 'ping', 'say', 'kill', 'uptime', 'help'],
schatColors: {
red: 'red',
orange: 'orange',

View File

@ -16,8 +16,8 @@ function onCommand(args, context) {
return;
}
if (rolls > config.dice.maxFaces) {
context.sendMessage(`Your dice can have at most ${config.dice.maxFace} faces`, context.room.id);
if (faces > config.dice.maxFaces) {
context.sendMessage(`Your dice can have at most ${config.dice.maxFaces} faces`, context.room.id);
return;
}
@ -28,10 +28,11 @@ function onCommand(args, context) {
return `${style.grey(dieFaces[result - 1] || '☐')} ${style.bold(result)}`; // eslint-disable-line no-irregular-whitespace
});
context.sendMessage(results.join(' | '), context.room.id);
context.sendMessage(results.join(style.grey(' | ')), context.room.id);
}
module.exports = {
onCommand,
commands: ['dice', 'die', 'roll'],
help: 'What\'s your next move? Try ~dice [rolls] [faces], ~die or ~roll',
};

View File

@ -7,7 +7,7 @@ function onCommand(args, context) {
context.sendMessage('Shutting down... :sleeping:', context.room?.id, { type: 'message', label: false }, context.message.user?.username);
}
if (config.platform === 'irc' && context.room.id === config.user.nick) {
if (config.platform === 'irc' && context.room.id === config.user.id) {
// if the room ID is the bot's own nickname, it's a PM and we should reply to the sender
context.sendMessage('Shutting down... 😴', context.user.id, { label: false });
} else if (config.platform === 'irc') {

View File

@ -0,0 +1,72 @@
'use strict';
const config = require('config');
const crypto = require('crypto');
const timers = require('timers/promises');
const style = require('../utils/style');
const wins = {
rock: 'scissors',
paper: 'rock',
scissors: 'paper',
};
const emojis = {
rock: '🪨',
paper: '📄',
scissors: '✂️',
};
async function play(context, hand) {
const counter = Object.keys(wins)[crypto.randomInt(0, 3)];
const userWins = wins[hand] === counter;
const botWins = wins[counter] === hand;
await timers.setTimeout(500);
context.sendMessage(`${emojis.rock} Rock!`, context.room.id);
await timers.setTimeout(500);
context.sendMessage(`${emojis.paper} Paper!`, context.room.id);
await timers.setTimeout(500);
context.sendMessage(`${emojis.scissors} Scissors!`, context.room.id);
await timers.setTimeout(750);
if (userWins) {
context.sendMessage(`${style.bold('You win!')} ${config.user.username} played ${style.italic(counter)} ${emojis[counter]} against your ${style.italic(hand)} ${emojis[hand]}`, context.room.id);
context.setPoints(context.user, 1);
context.setPoints(config.user, 1);
return;
}
if (botWins) {
context.sendMessage(`${style.bold('You lose...')} ${config.user.username} played ${style.italic(counter)} ${emojis[counter]} against your ${style.italic(hand)} ${emojis[hand]}`, context.room.id);
context.setPoints(context.user, -1);
context.setPoints(config.user, 1);
return;
}
context.sendMessage(`${style.bold('It\'s a draw.')} ${config.user.username} also played ${style.italic(counter)} ${emojis[counter]}`, context.room.id);
}
async function onCommand(args, context) {
return play(context, context.command);
}
async function onMessage(args, context) {
if (context.user?.username !== config.user.username && Object.values(emojis).some((emoji) => new RegExp(`^${emoji}$`, 'u').test(context.message.body))) {
play(context, context.command);
}
}
module.exports = {
onCommand,
onMessage,
name: 'Rock, Paper, Scissors',
commands: ['rock', 'paper', 'scissors'],
help: 'What\'s your next move? Try ~dice [rolls] [faces], ~die or ~roll',
};

View File

@ -12,7 +12,7 @@ function onCommand(args, context) {
return;
}
if (config.platform === 'irc' && context.room.id === config.user.nick) {
if (config.platform === 'irc' && context.room.id === config.user.id) {
// if the room ID is the bot's own nickname, it's a PM and we should reply to the sender
context.sendMessage(args.join(' '), context.user.id, { label: false });
}

View File

@ -13,7 +13,7 @@ const {
logger.setLevel(argv.level || 'info');
const client = new irc.Client(config.server, config.user.nick, {
const client = new irc.Client(config.server, config.user.id, {
userName: config.user.username,
realName: config.user.realName,
password: config.user.password,
@ -30,7 +30,7 @@ async function init() {
})),
};
const games = await getGames(bot, config.user.nick);
const games = await getGames(bot, config.user.id);
client.addListener('registered', () => {
logger.info('Connected!');

View File

@ -235,7 +235,7 @@ function onMessage(message, bot, games) {
}
}
Object.values(games).forEach((game) => game.onMessage?.(message, {
Object.values(Object.fromEntries(Object.values(games).map((game) => [game.key, game]))).forEach((game) => game.onMessage?.(message, {
...game,
bot,
message,

View File

@ -11,7 +11,7 @@ function getLeaders(points, user, ping = true, limit = Infinity) {
const username = userKey.split(':')[1] || userKey; // process the points file
if (index === 0) {
return `${style.bold(style.yellow(`${ping ? config.usernamePrefix : ''}${username}`))} with ${style.bold(`${score}`)} points`;
return `${style.bold(style.yellow(`${ping || username === user.username ? config.usernamePrefix : ''}${username}`))} with ${style.bold(`${score}`)} points`;
}
return `${style.bold(style.cyan(`${ping ? config.usernamePrefix : ''}${username}`))} with ${style.bold(`${score}`)} points`;