mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
Public :)
This commit is contained in:
@@ -1,118 +0,0 @@
|
||||
const Collection = require('@discordjs/collection');
|
||||
const path = require('path');
|
||||
const { stripIndents } = require('common-tags');
|
||||
const Player = require('./Player');
|
||||
const { shuffle } = require('../../util/Util');
|
||||
const { SUCCESS_EMOJI_ID } = process.env;
|
||||
|
||||
module.exports = class Game {
|
||||
constructor(client, channel, voiceChannel) {
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
|
||||
this.name = 'mafia';
|
||||
this.players = new Collection();
|
||||
this.channel = channel;
|
||||
this.voiceChannelRaw = voiceChannel;
|
||||
this.connection = null;
|
||||
this.dispatcher = null;
|
||||
this.turn = 1;
|
||||
}
|
||||
|
||||
determineRoles(playerCount) {
|
||||
const roles = ['detective', 'mafia', 'mafia'];
|
||||
for (let i = 0; i < (playerCount - 3); i++) roles.push('innocent');
|
||||
return shuffle(roles);
|
||||
}
|
||||
|
||||
async generate(list) {
|
||||
const roles = this.determineRoles(list.length);
|
||||
let i = 0;
|
||||
for (const user of list) {
|
||||
try {
|
||||
await user.send(`You are ${roles[i] === 'detective' ? 'the' : 'a part of the'} **${roles[i]}**.`);
|
||||
} catch (err) {
|
||||
await this.channel.send(
|
||||
`${user}, I couldn't send a DM to you. Please allow DMs and use the \`mafia-role\` command to see your role.`
|
||||
);
|
||||
}
|
||||
const player = new Player(this, user, roles[i]);
|
||||
this.players.set(user.id, player);
|
||||
i++;
|
||||
}
|
||||
return this.players;
|
||||
}
|
||||
|
||||
async init() {
|
||||
this.connection = await this.voiceChannel.join();
|
||||
return this;
|
||||
}
|
||||
|
||||
end() {
|
||||
if (this.voiceChannel) this.voiceChannel.leave();
|
||||
this.client.games.delete(this.channel.id);
|
||||
return this;
|
||||
}
|
||||
|
||||
playAudio(id) {
|
||||
this.dispatcher = this.connection.play(path.join(__dirname, '..', '..', 'assets', 'sounds', 'mafia', `${id}.mp3`), {
|
||||
volume: 2
|
||||
});
|
||||
return new Promise((res, rej) => {
|
||||
this.dispatcher.once('finish', () => {
|
||||
this.dispatcher = null;
|
||||
return res(true);
|
||||
});
|
||||
this.dispatcher.once('error', err => {
|
||||
this.dispatcher = null;
|
||||
return rej(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async getVotes(playersArr) {
|
||||
await this.channel.send(stripIndents`
|
||||
Who do you think is a Mafia member? Please type the number.
|
||||
${playersArr.map((p, i) => `**${i + 1}.** ${p.user.tag}`).join('\n')}
|
||||
`);
|
||||
const voted = [];
|
||||
const filter = res => {
|
||||
if (!this.players.some(p => p.user.id === res.author.id)) return false;
|
||||
if (voted.includes(res.author.id)) return false;
|
||||
if (!playersArr[Number.parseInt(res.content, 10) - 1]) return false;
|
||||
voted.push(res.author.id);
|
||||
res.react(SUCCESS_EMOJI_ID || '✅').catch(() => null);
|
||||
return true;
|
||||
};
|
||||
const votes = await this.channel.awaitMessages(filter, {
|
||||
max: this.players.size,
|
||||
time: 90000
|
||||
});
|
||||
if (!votes.size) return null;
|
||||
return votes;
|
||||
}
|
||||
|
||||
getHanged(votes, playersArr) {
|
||||
const counts = new Collection();
|
||||
for (const vote of votes.values()) {
|
||||
const player = this.players.get(playersArr[Number.parseInt(vote.content, 10) - 1].id);
|
||||
if (counts.has(player.id)) {
|
||||
++counts.get(player.id).votes;
|
||||
} else {
|
||||
counts.set(player.id, {
|
||||
id: player.id,
|
||||
votes: 1,
|
||||
user: player.user
|
||||
});
|
||||
}
|
||||
}
|
||||
return counts.sort((a, b) => b.votes - a.votes).first();
|
||||
}
|
||||
|
||||
get shouldEnd() {
|
||||
return this.players.size < 4 && !this.players.some(p => p.role === 'mafia');
|
||||
}
|
||||
|
||||
get voiceChannel() {
|
||||
return this.connection ? this.connection.channel : this.voiceChannelRaw;
|
||||
}
|
||||
};
|
||||
@@ -1,43 +0,0 @@
|
||||
const { stripIndents } = require('common-tags');
|
||||
const questions = {
|
||||
mafia: 'Who would you like to kill?',
|
||||
detective: 'Who do you think is a Mafia member?'
|
||||
};
|
||||
|
||||
module.exports = class Player {
|
||||
constructor(game, user, role) {
|
||||
this.game = game;
|
||||
this.user = user;
|
||||
this.id = user.id;
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
toString() {
|
||||
return this.user.toString();
|
||||
}
|
||||
|
||||
async dmRound() {
|
||||
const valid = Array.from(this.game.players.filter(p => p.role !== this.role).values());
|
||||
await this.user.send(stripIndents`
|
||||
${questions[this.role]} Please type the number.
|
||||
${valid.map((p, i) => `**${i + 1}.** ${p.user.tag}`).join('\n')}
|
||||
`);
|
||||
const filter = res => valid[Number.parseInt(res.content, 10) - 1];
|
||||
const decision = await this.user.dmChannel.awaitMessages(filter, {
|
||||
max: 1,
|
||||
time: 120000
|
||||
});
|
||||
if (!decision.size) {
|
||||
await this.user.send('Sorry, time is up!');
|
||||
return null;
|
||||
}
|
||||
const choice = valid[Number.parseInt(decision.first().content, 10) - 1].id;
|
||||
if (this.role === 'detective') {
|
||||
const isMafia = this.game.players.get(choice).role === 'mafia';
|
||||
await this.user.send(isMafia ? 'Yes, they are a Mafioso.' : 'No, they are not a Mafioso.');
|
||||
} else {
|
||||
await this.user.send(`**${this.game.players.get(choice).user.tag}** is your choice...`);
|
||||
}
|
||||
return choice;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user