Cards against humanity

This commit is contained in:
Daniel Odendahl Jr
2017-11-06 22:23:26 +00:00
parent 012d14b861
commit c8afe4f3fb
5 changed files with 3378 additions and 25 deletions
File diff suppressed because it is too large Load Diff
+132
View File
@@ -0,0 +1,132 @@
const { Command } = require('discord.js-commando');
const { Collection, escapeMarkdown } = require('discord.js');
const { stripIndents } = require('common-tags');
const { awaitPlayers } = require('../../util/Util');
const { blackCards, whiteCards } = require('../../assets/json/cards-against-humanity');
module.exports = class CardsAgainstHumanityCommand extends Command {
constructor(client) {
super(client, {
name: 'cards-against-humanity',
aliases: ['crude-cards', 'pretend-youre-xyzzy'],
group: 'games',
memberName: 'cards-against-humanity',
description: 'Play a game of Cards Against Humanity.',
guildOnly: true,
args: [
{
key: 'maxPts',
label: 'maximum amount of points',
prompt: 'What amount of points should determine the winner?',
type: 'integer',
min: 1,
max: 20
}
]
});
this.playing = new Set();
}
async run(msg, { maxPts }) {
if (this.playing.has(msg.channel.id)) return msg.reply('Only one game may be occurring per channel.');
this.playing.add(msg.channel.id);
try {
await msg.say('You will need at least 2 more players, at maximum 20. To join, type `join game`.');
const awaitedPlayers = await awaitPlayers(msg, 20, 3);
const players = new Map();
let i = 1;
for (const player of awaitedPlayers) {
const cards = new Set();
for (let j = 0; j < 5; j++) cards.add(whiteCards[Math.floor(Math.random() * whiteCards.length)]);
players.set(i, {
id: i,
user: player,
points: 0,
hand: cards
});
await player.send('Testing...');
i++;
}
let czars = Array.from(players.values());
let winner = null;
while (!winner) {
const czar = czars[0];
czars.push(czar);
czars.shift();
const black = blackCards[Math.floor(Math.random() * blackCards.length)];
await msg.say(stripIndents`
The card czar will be ${czar.user.username}!
The black card is: ${escapeMarkdown(black.text)}
Sending DMs...
`);
const chosenCards = new Collection();
for (const player of players) {
player.hand.add(whiteCards[Math.floor(Math.random() * whiteCards.length)]);
if (player.hand.size < black.pick) {
await player.user.send('You don\'t have enough cards!');
continue;
}
await player.user.send(stripIndents`
Your hand is:
${Array.from(player.hand).join('\n')}
The black card is; ${escapeMarkdown(black.text)}
The card czar is: ${czar.user.username}
Pick ${black.pick} cards!
`);
const chosen = [];
const filter = res => {
if (chosen.includes(res.content)) return false;
if (!player.hand.has(res.content)) return false;
player.hand.delete(res.content);
chosen.push(res.content);
return true;
};
const choices = await player.user.dmChannel.awaitMessages(filter, {
max: black.pick,
time: 30000
});
if (!choices.size || choices.size < black.pick) {
await player.user.send('Skipping your turn...');
continue;
}
chosenCards.set(player.id, {
id: player.id,
cards: chosen
});
}
if (!chosenCards.length) {
await msg.say('Hmm... No one even tried.');
break;
}
await msg.say(stripIndents`
${czar}, which cards do you pick?
${chosenCards.map(card => `**${card.id}.** ${card.cards.join(', ')}`)}
`);
const filter = res => {
if (res.author.id !== czar.user.id) return false;
if (!chosenCards.map(card => card.id).includes(parseInt(res.content, 10))) return false;
return true;
};
const chosen = await msg.channel.awaitMessages(filter, {
max: 1,
time: 30000
});
if (!chosen.size) {
await msg.say('Hmm... No one wins.');
continue;
}
const player = parseInt(chosen.first().content, 10);
++players.get(player).points;
if (players.get(player).points > maxPts) winner = players.get(player).user;
}
this.playing.delete(msg.channel.id);
if (!winner) return msg.say('See you next time!');
return msg.say(`And the winner is... ${winner}! Great job!`);
} catch (err) {
this.playing.delete(msg.channel.id);
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};
+9 -24
View File
@@ -1,7 +1,7 @@
const { Command } = require('discord.js-commando');
const { Collection } = require('discord.js');
const { stripIndents } = require('common-tags');
const { shuffle, wait } = require('../../util/Util');
const { shuffle, wait, awaitPlayers } = require('../../util/Util');
const { questions, stories } = require('../../assets/json/wizard-convention');
module.exports = class WizardConventionCommand extends Command {
@@ -22,35 +22,20 @@ module.exports = class WizardConventionCommand extends Command {
this.playing.add(msg.channel.id);
try {
await msg.say('You will need at least 2 more players, at maximum 15. To join, type `join game`.');
const joined = [];
joined.push(msg.author.id);
const filter = res => {
if (joined.includes(res.author.id)) return false;
if (res.content.toLowerCase() !== 'join game') return false;
joined.push(res.author.id);
return true;
};
const verify = await msg.channel.awaitMessages(filter, {
max: 15,
time: 30000
});
if (verify.size < 2) {
this.playing.delete(msg.channel.id);
return msg.say('Failed to start the game...');
}
const awaitedPlayers = await awaitPlayers(msg, 15, 3);
if (!awaitedPlayers) return msg.say('Game could not be started...');
let roles = ['dragon', 'healer', 'mind reader'];
for (let i = 0; i < (verify.size - 2); i++) roles.push(`pleb ${i + 1}`);
for (let i = 0; i < (awaitedPlayers.length - 2); i++) roles.push(`pleb ${i + 1}`);
roles = shuffle(roles);
verify.set(msg.id, msg);
const players = new Collection();
let i = 1;
for (const message of verify.values()) {
for (const player of awaitedPlayers) {
players.set(i, {
id: i,
user: message.author,
user: player,
role: roles[i - 1]
});
await message.author.send(`Your role will be: ${roles[i - 1]}!`);
await player.send(`Your role will be: ${roles[i - 1]}!`);
i++;
}
let turn = 1;
@@ -65,8 +50,8 @@ module.exports = class WizardConventionCommand extends Command {
${questions[player.role]} Please type the number.
${valid.map(p => `**${p.id}.** ${p.user.tag}`).join('\n')}
`);
const filter2 = res => valid.map(p => p.id.toString()).includes(res.content);
const decision = await player.user.dmChannel.awaitMessages(filter2, {
const filter = res => valid.map(p => p.id.toString()).includes(res.content);
const decision = await player.user.dmChannel.awaitMessages(filter, {
max: 1,
time: 30000
});
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiaobot",
"version": "52.0.0",
"version": "52.1.0",
"description": "Your personal server companion.",
"main": "XiaoBot.js",
"scripts": {
+15
View File
@@ -82,6 +82,21 @@ class Util {
.replace(/\[i\]|\[\/i\]/g, '*');
}
static async awaitPlayers(msg, max, min, { text = 'join game', time = 30000 }) {
const joined = [];
joined.push(msg.author.id);
const filter = res => {
if (joined.includes(res.author.id)) return false;
if (res.content.toLowerCase() !== text) return false;
joined.push(res.author.id);
return true;
};
const verify = await msg.channel.awaitMessages(filter, { max, time });
verify.set(msg.id, msg);
if (verify.size < min) return false;
return verify.map(message => message.author);
}
static async verify(channel, user, time = 30000) {
const filter = res => {
const value = res.content.toLowerCase();