Fixed rock paper scissors emoji input.

This commit is contained in:
ThePendulum 2022-10-24 22:57:08 +02:00
parent 2656c97b7d
commit 3039471764
2 changed files with 48 additions and 20 deletions

View File

@ -16,9 +16,24 @@ const emojis = {
rock: '🪨', rock: '🪨',
paper: '📄', paper: '📄',
scissors: '✂️', scissors: '✂️',
scissoring: ':scissoring:',
}; };
async function play(context, hand) { const emojiAliases = {
rock: 'rock',
paper: 'paper',
page_facing_up: 'paper',
page_with_curl: 'paper',
newspaper: 'paper',
newspaper_roll: 'paper',
scroll: 'paper',
scissors: 'scissors',
scissoring: 'scissors',
};
const emojiLookup = Object.fromEntries(Object.entries(emojis).map(([key, emoji]) => [emoji, key]));
async function play(context, hand, originalHand) {
const counter = Object.keys(wins)[crypto.randomInt(0, 3)]; const counter = Object.keys(wins)[crypto.randomInt(0, 3)];
const userWins = wins[hand] === counter; const userWins = wins[hand] === counter;
@ -29,11 +44,17 @@ async function play(context, hand) {
await timers.setTimeout(500); await timers.setTimeout(500);
context.sendMessage(`${emojis.paper} Paper!`, context.room.id); context.sendMessage(`${emojis.paper} Paper!`, context.room.id);
await timers.setTimeout(500); await timers.setTimeout(500);
if (originalHand === 'scissoring') {
context.sendMessage(`${emojis.scissoring} Scissoring!`, context.room.id);
} else {
context.sendMessage(`${emojis.scissors} Scissors!`, context.room.id); context.sendMessage(`${emojis.scissors} Scissors!`, context.room.id);
}
await timers.setTimeout(750); await timers.setTimeout(750);
if (userWins) { 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.sendMessage(`${style.bold('You win!')} ${config.user.username} played ${style.italic(counter)} ${emojis[counter]} against your ${style.italic(originalHand)} ${emojis[originalHand]}`, context.room.id);
context.setPoints(context.user, 1); context.setPoints(context.user, 1);
context.setPoints(config.user, 1); context.setPoints(config.user, 1);
@ -42,7 +63,7 @@ async function play(context, hand) {
} }
if (botWins) { 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.sendMessage(`${style.bold('You lose...')} ${config.user.username} played ${style.italic(counter)} ${emojis[counter]} against your ${style.italic(originalHand)} ${emojis[originalHand]}`, context.room.id);
context.setPoints(context.user, -1); context.setPoints(context.user, -1);
context.setPoints(config.user, 1); context.setPoints(config.user, 1);
@ -50,16 +71,21 @@ async function play(context, hand) {
return; return;
} }
context.sendMessage(`${style.bold('It\'s a draw.')} ${config.user.username} also played ${style.italic(counter)} ${emojis[counter]}`, context.room.id); context.sendMessage(`${style.bold('It\'s a draw.')} ${config.user.username} also played ${style.italic(originalHand)} ${emojis[originalHand]}`, context.room.id);
} }
async function onCommand(args, context) { async function onCommand(args, context) {
return play(context, context.command); return play(context, context.command, context.command);
} }
async function onMessage(args, context) { 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))) { const emojiHand = emojiLookup[context.message.body];
play(context, context.command); const emojiAliasHand = config.platform === 'schat' && context.message.originalBody?.match(/^:((?:rock)|(?:paper)|(?:scissors)|(?:scissoring)|(?:page_facing_up)|(?:page_with_curl)):$/)?.[1];
const hand = emojiHand || emojiAliases[emojiAliasHand];
if (hand && context.user?.username !== config.user.username) {
play(context, hand, emojiHand || emojiAliasHand);
} }
} }
@ -68,5 +94,5 @@ module.exports = {
onMessage, onMessage,
name: 'Rock, Paper, Scissors', name: 'Rock, Paper, Scissors',
commands: ['rock', 'paper', 'scissors'], commands: ['rock', 'paper', 'scissors'],
help: 'What\'s your next move? Try ~dice [rolls] [faces], ~die or ~roll', help: 'Can you read my mind? Play ~rock, ~paper, or ~scissors.',
}; };

View File

@ -235,6 +235,7 @@ function onMessage(message, bot, games) {
} }
} }
if (message.type === 'message') {
Object.values(Object.fromEntries(Object.values(games).map((game) => [game.key, game]))).forEach((game) => game.onMessage?.(message, { Object.values(Object.fromEntries(Object.values(games).map((game) => [game.key, game]))).forEach((game) => game.onMessage?.(message, {
...game, ...game,
bot, bot,
@ -247,6 +248,7 @@ function onMessage(message, bot, games) {
logger, logger,
})); }));
} }
}
module.exports = { module.exports = {
onMessage, onMessage,