Cards Against Humanity and Apples to Apples

This commit is contained in:
Dragon Fire
2021-03-13 13:10:47 -05:00
parent d796ef01b4
commit cebe468390
8 changed files with 31903 additions and 1 deletions
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+137
View File
@@ -0,0 +1,137 @@
const Command = require('../../structures/Command');
const { escapeMarkdown } = require('discord.js');
const { stripIndents } = require('common-tags');
const { shuffle } = require('../../util/Util');
const Game = require('../../structures/cards-against-humanity/Game');
const { greenCards, redCards } = require('../../assets/json/apples-to-apples');
module.exports = class ApplesToApplesCommand extends Command {
constructor(client) {
super(client, {
name: 'apples-to-apples',
aliases: ['a2a'],
group: 'games-mp',
memberName: 'apples-to-apples',
description: 'Compete to see who can come up with the best card to match an adjective.',
guildOnly: true,
clientPermissions: ['ADD_REACTIONS', 'READ_MESSAGE_HISTORY'],
credit: [
{
name: 'Mattel',
url: 'https://www.mattel.com/en-us',
reason: 'Original "Apples to Apples" Game, Card Data',
reasonURL: 'https://www.mattelgames.com/games/en-us/family/apples-apples'
},
{
name: 'JSON Against Humanity',
url: 'https://www.crhallberg.com/cah/',
reason: 'Card Data'
}
],
args: [
{
key: 'maxPts',
label: 'awesome points',
prompt: 'What amount of awesome points should determine the winner?',
type: 'integer',
max: 20,
min: 1
},
{
key: 'bot',
prompt: 'Do you want me to play as well?',
type: 'boolean'
}
]
});
}
async run(msg, { maxPts, bot }) {
const current = this.client.games.get(msg.channel.id);
if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`);
this.client.games.set(msg.channel.id,
new Game(this.client, this.name, msg.channel, redCards, greenCards, 'Green'));
const game = this.client.games.get(msg.channel.id);
try {
const awaitedPlayers = await game.awaitPlayers(msg, bot);
if (!awaitedPlayers) {
this.client.games.delete(msg.channel.id);
return msg.say('Game could not be started...');
}
game.createJoinLeaveCollector(msg.channel, game);
while (!game.winner) {
const czar = game.changeCzar();
for (const player of game.players.values()) {
if (player.id === czar.id) continue;
if (player.kickable) game.kick(player);
}
if (game.players.size < 3) {
await msg.say('Oh... It looks like everyone left...');
break;
}
const black = game.blackDeck.draw();
await msg.say(stripIndents`
The card czar will be ${czar.user}!
The Green Card is: **${escapeMarkdown(black.text)}**
Sending DMs...
`);
const chosenCards = [];
const turns = await Promise.all(game.players.map(player => player.turn(black, chosenCards)));
const extra = turns.reduce((a, b) => a + b);
if (!chosenCards.length) {
await msg.say('Hmm... No one even tried.');
continue;
}
const cards = shuffle(chosenCards);
await msg.say(stripIndents`
${czar.user}, which card${black.pick > 1 ? 's' : ''} do you pick?
**Green Card:** ${escapeMarkdown(black.text)}
${cards.map((card, i) => `**${i + 1}.** ${card.cards.join(' | ')}`).join('\n')}
`);
const filter = res => {
if (res.author.id !== czar.user.id) return false;
if (!/^[0-9]+$/g.test(res.content)) return false;
if (!cards[Number.parseInt(res.content, 10) - 1]) return false;
return true;
};
const chosen = await msg.channel.awaitMessages(filter, {
max: 1,
time: 120000
});
if (!chosen.size) {
await msg.say('Hmm... No one wins. Dealing back cards...');
for (const pick of cards) {
for (const card of pick.cards) {
if (!game.players.has(pick.id)) continue;
game.players.get(pick.id).hand.add(card);
}
}
game.czar.strikes++;
continue;
}
const player = game.players.get(cards[Number.parseInt(chosen.first().content, 10) - 1].id);
if (!player) {
await msg.say('Oh no, I think that player left! No awesome points will be awarded...');
continue;
}
player.points += 1 + extra;
if (player.points >= maxPts) {
game.winner = player.user;
} else {
const addS = player.points > 1 ? 's' : '';
await msg.say(`Nice, ${player.user}! You now have **${player.points}** awesome point${addS}!`);
}
}
game.stopJoinLeaveCollector();
this.client.games.delete(msg.channel.id);
if (!game.winner) return msg.say('See you next time!');
return msg.say(`And the winner is... ${game.winner}! Great job!`);
} catch (err) {
game.stopJoinLeaveCollector();
this.client.games.delete(msg.channel.id);
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};
+137
View File
@@ -0,0 +1,137 @@
const Command = require('../../structures/Command');
const { escapeMarkdown } = require('discord.js');
const { stripIndents } = require('common-tags');
const { shuffle } = require('../../util/Util');
const Game = require('../../structures/cards-against-humanity/Game');
const { whiteCards, blackCards } = require('../../assets/json/cards-against-humanity');
module.exports = class CardsAgainstHumanityCommand extends Command {
constructor(client) {
super(client, {
name: 'cards-against-humanity',
aliases: ['crude-cards', 'cah'],
group: 'games-mp',
memberName: 'cards-against-humanity',
description: 'Compete to see who can come up with the best card to fill in the blank.',
guildOnly: true,
nsfw: true,
clientPermissions: ['ADD_REACTIONS', 'READ_MESSAGE_HISTORY'],
credit: [
{
name: 'Cards Against Humanity',
url: 'https://cardsagainsthumanity.com/',
reason: 'Original Game, Card Data'
},
{
name: 'JSON Against Humanity',
url: 'https://www.crhallberg.com/cah/',
reason: 'Card Data'
}
],
args: [
{
key: 'maxPts',
label: 'awesome points',
prompt: 'What amount of awesome points should determine the winner?',
type: 'integer',
max: 20,
min: 1
},
{
key: 'bot',
prompt: 'Do you want me to play as well?',
type: 'boolean'
}
]
});
}
async run(msg, { maxPts, bot }) {
const current = this.client.games.get(msg.channel.id);
if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`);
this.client.games.set(msg.channel.id,
new Game(this.client, this.name, msg.channel, whiteCards, blackCards, 'Black'));
const game = this.client.games.get(msg.channel.id);
try {
const awaitedPlayers = await game.awaitPlayers(msg, bot);
if (!awaitedPlayers) {
this.client.games.delete(msg.channel.id);
return msg.say('Game could not be started...');
}
game.createJoinLeaveCollector(msg.channel, game);
while (!game.winner) {
const czar = game.changeCzar();
for (const player of game.players.values()) {
if (player.id === czar.id) continue;
if (player.kickable) game.kick(player);
}
if (game.players.size < 3) {
await msg.say('Oh... It looks like everyone left...');
break;
}
const black = game.blackDeck.draw();
await msg.say(stripIndents`
The card czar will be ${czar.user}!
The Black Card is: **${escapeMarkdown(black.text)}**
Sending DMs...
`);
const chosenCards = [];
const turns = await Promise.all(game.players.map(player => player.turn(black, chosenCards)));
const extra = turns.reduce((a, b) => a + b);
if (!chosenCards.length) {
await msg.say('Hmm... No one even tried.');
continue;
}
const cards = shuffle(chosenCards);
await msg.say(stripIndents`
${czar.user}, which card${black.pick > 1 ? 's' : ''} do you pick?
**Black Card:** ${escapeMarkdown(black.text)}
${cards.map((card, i) => `**${i + 1}.** ${card.cards.join(' | ')}`).join('\n')}
`);
const filter = res => {
if (res.author.id !== czar.user.id) return false;
if (!/^[0-9]+$/g.test(res.content)) return false;
if (!cards[Number.parseInt(res.content, 10) - 1]) return false;
return true;
};
const chosen = await msg.channel.awaitMessages(filter, {
max: 1,
time: 120000
});
if (!chosen.size) {
await msg.say('Hmm... No one wins. Dealing back cards...');
for (const pick of cards) {
for (const card of pick.cards) {
if (!game.players.has(pick.id)) continue;
game.players.get(pick.id).hand.add(card);
}
}
game.czar.strikes++;
continue;
}
const player = game.players.get(cards[Number.parseInt(chosen.first().content, 10) - 1].id);
if (!player) {
await msg.say('Oh no, I think that player left! No awesome points will be awarded...');
continue;
}
player.points += 1 + extra;
if (player.points >= maxPts) {
game.winner = player.user;
} else {
const addS = player.points > 1 ? 's' : '';
await msg.say(`Nice, ${player.user}! You now have **${player.points}** awesome point${addS}!`);
}
}
game.stopJoinLeaveCollector();
this.client.games.delete(msg.channel.id);
if (!game.winner) return msg.say('See you next time!');
return msg.say(`And the winner is... ${game.winner}! Great job!`);
} catch (err) {
game.stopJoinLeaveCollector();
this.client.games.delete(msg.channel.id);
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "xiao", "name": "xiao",
"version": "132.2.0", "version": "132.3.0",
"description": "Your personal server companion.", "description": "Your personal server companion.",
"main": "Xiao.js", "main": "Xiao.js",
"scripts": { "scripts": {
+20
View File
@@ -0,0 +1,20 @@
const { shuffle } = require('../util/Util');
module.exports = class Deck {
constructor(cards) {
this.cards = cards;
this.deck = shuffle(cards);
}
draw() {
if (!this.deck.length) this.reset();
const card = this.deck[0];
this.deck.shift();
return card;
}
reset() {
this.deck = shuffle(this.cards);
return this.deck;
}
};
+83
View File
@@ -0,0 +1,83 @@
const Collection = require('@discordjs/collection');
const Player = require('./Player');
const Deck = require('./Deck');
const { removeFromArray, awaitPlayers } = require('../util/Util');
const { SUCCESS_EMOJI_ID, FAILURE_EMOJI_ID } = process.env;
module.exports = class Game {
constructor(client, name, channel, whiteCards, blackCards, blackType) {
Object.defineProperty(this, 'client', { value: client });
this.name = name;
this.channel = channel;
this.players = new Collection();
this.czars = [];
this.whiteDeck = new Deck(whiteCards);
this.blackDeck = new Deck(blackCards);
this.joinLeaveCollector = null;
this.winner = null;
this.blackType = blackType;
}
addUser(user) {
const player = new Player(this, user);
player.dealHand();
this.players.set(player.id, player);
if (!user.bot) this.czars.push(player.id);
return this.players;
}
get czar() {
return this.players.get(this.czars[0]);
}
changeCzar() {
this.czars.push(this.czars[0]);
this.czars.shift();
return this.czar;
}
kick(player) {
this.players.delete(player.id);
removeFromArray(this.czars, player.id);
}
async awaitPlayers(msg, bot) {
const max = bot ? 9 : 10;
const min = bot ? 2 : 3;
const players = await awaitPlayers(msg, max, min, this.client.blacklist.user);
if (!players) return false;
for (const player of players.values()) {
const user = await this.client.users.fetch(player);
this.addUser(user);
}
if (bot) this.addUser(this.client.user);
return true;
}
createJoinLeaveCollector() {
const collector = this.channel.createMessageCollector(res => {
if (res.author.bot) return false;
if (this.players.has(res.author.id) && res.content.toLowerCase() !== 'leave game') return false;
if (!this.players.has(res.author.id) && res.content.toLowerCase() !== 'join game') return false;
if (this.czar.id === res.author.id || this.players.size >= 10) {
res.react(FAILURE_EMOJI_ID || '❌').catch(() => null);
return false;
}
if (!['join game', 'leave game'].includes(res.content.toLowerCase())) return false;
res.react(SUCCESS_EMOJI_ID || '✅').catch(() => null);
return true;
});
collector.on('collect', msg => {
if (msg.content.toLowerCase() === 'join game') this.addUser(msg.author);
else if (msg.content.toLowerCase() === 'leave game') this.kick(msg.author);
});
this.joinLeaveCollector = collector;
return this.joinLeaveCollector;
}
stopJoinLeaveCollector() {
if (!this.joinLeaveCollector) return null;
return this.joinLeaveCollector.stop();
}
};
+123
View File
@@ -0,0 +1,123 @@
const { stripIndents } = require('common-tags');
const { escapeMarkdown } = require('discord.js');
const { SUCCESS_EMOJI_ID, FAILURE_EMOJI_ID } = process.env;
module.exports = class Player {
constructor(game, user) {
this.game = game;
this.id = user.id;
this.user = user;
this.points = 0;
this.hand = new Set();
this.strikes = 0;
}
dealHand() {
if (this.hand.size > 9) return this.hand;
const drawCount = 10 - this.hand.size;
for (let i = 0; i < drawCount; i++) this.hand.add(this.game.whiteDeck.draw());
return this.hand;
}
get kickable() {
return this.strikes >= 3;
}
async turn(black, chosenCards) {
if (this.user.id === this.game.czar.user.id) return 0;
this.dealHand();
try {
const extra = await this.chooseCards(black, chosenCards);
if (!this.user.bot) await this.user.send(`Nice! Return to ${this.game.channel} to await the results!`);
return extra;
} catch (err) {
this.strikes++;
return 0;
}
}
async chooseCards(black, chosenCards) {
let hand = Array.from(this.hand);
if (this.user.bot) {
const chosen = [];
for (let i = 0; i < black.pick; i++) {
const valid = hand.filter(card => !chosen.includes(card));
chosen.push(valid[Math.floor(Math.random() * valid.length)]);
}
for (const card of chosen) this.hand.delete(card);
chosenCards.push({ id: this.id, cards: chosen });
return null;
}
const chosen = [];
await this.sendHand(hand, black);
let gambled = false;
let swapped = false;
const collector = this.user.dmChannel.createMessageCollector(res => {
if (res.content.toLowerCase() === 'swap' && this.points > 0 && !swapped) return true;
if (res.content.toLowerCase() === 'gamble' && this.points > 0 && !gambled) return true;
const existing = hand[Number.parseInt(res.content, 10) - 1];
if (!existing || chosen.includes(existing)) {
res.react(FAILURE_EMOJI_ID || '❌').catch(() => null);
return false;
}
return true;
}, { time: 60000 });
collector.on('collect', async msg => {
const existing = hand[Number.parseInt(msg.content, 10) - 1];
if (msg.content.toLowerCase() === 'swap') {
await msg.react(SUCCESS_EMOJI_ID || '✅');
for (const card of this.hand) this.hand.delete(card);
this.dealHand();
hand = Array.from(this.hand);
this.points--;
swapped = true;
await this.sendHand(hand, black);
return;
} else if (msg.content.toLowerCase() === 'gamble') {
await msg.react(SUCCESS_EMOJI_ID || '✅');
this.points--;
gambled = true;
return;
} else if (existing) {
await msg.react(SUCCESS_EMOJI_ID || '✅');
chosen.push(existing);
}
if (chosen.length >= black.pick * (gambled ? 2 : 1)) collector.stop();
});
return new Promise(resolve => collector.once('end', () => {
if (chosen.length < black.pick * (gambled ? 2 : 1)) {
const count = black.pick - chosen.length;
for (let i = 0; i < count; i++) {
const valid = hand.filter(card => !chosen.includes(card));
chosen.push(valid[Math.floor(Math.random() * valid.length)]);
}
this.strikes++;
}
for (const card of chosen) this.hand.delete(card);
if (gambled) {
const first = chosen.splice(0, chosen.length / 2);
chosenCards.push({ id: this.id, cards: first });
chosenCards.push({ id: this.id, cards: chosen });
} else {
chosenCards.push({ id: this.id, cards: chosen });
}
return resolve(gambled ? 1 : 0);
}));
}
sendHand(hand, black) {
return this.user.send(stripIndents`
__**Your hand is:**__
${hand.map((card, i) => `**${i + 1}.** ${card}`).join('\n')}
**${this.game.blackType} Card:** ${escapeMarkdown(black.text)}
**Card Czar:** ${this.game.czar.user.username}
**Awesome Points:** ${this.points}
**Strikes:** ${this.strikes}/3
Pick **${black.pick}** card${black.pick > 1 ? 's' : ''}!
_Type \`gamble\` to exchange a point for an extra play._
_Type \`swap\` to exchange a point for a new hand._
`);
}
};