don't let people join games if they're in a game + polish
This commit is contained in:
parent
bb8d1a2dc4
commit
3b76e9a7ff
|
@ -1,4 +1,4 @@
|
|||
import { GuildMember, SlashCommandBuilder, Interaction } from 'discord.js';
|
||||
import { GuildMember, SlashCommandBuilder, Interaction, messageLink } from 'discord.js';
|
||||
import { getTextResponsePrettyPlease, randomWord, sendSegments, startGame } from '../lib/game';
|
||||
|
||||
const END_TEMPLATES = [
|
||||
|
@ -34,7 +34,7 @@ module.exports = {
|
|||
const context = interaction.options.getInteger('context') || 3;
|
||||
const maxIterations = interaction.options.getInteger('iterations') || 10;
|
||||
|
||||
startGame(interaction, member.user, 'Markov game', async (participants, channel) => {
|
||||
startGame(interaction, member.user, 'Markov Chain', async (participants, channel) => {
|
||||
let sentences: string[][] = Array(participants.length).fill(0);
|
||||
sentences = sentences.map(() => []);
|
||||
let iterations = 0;
|
||||
|
@ -61,12 +61,15 @@ module.exports = {
|
|||
const endTemplate = END_TEMPLATES[Math.floor(Math.random() * END_TEMPLATES.length)];
|
||||
|
||||
const segments = [
|
||||
endTemplate + '\n\n',
|
||||
endTemplate + '\n',
|
||||
...sentences.map(sentence => '- ' + sentence.join(' ')),
|
||||
'\n\nThank you for participating :)'
|
||||
'\nThank you for participating :)'
|
||||
];
|
||||
|
||||
await sendSegments(segments, channel);
|
||||
const messages = await sendSegments(segments, channel);
|
||||
participants.forEach(player => {
|
||||
player.send(`**The game has concluded!** See your results here: ${messageLink(messages[0].channelId, messages[0].id)}`);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
|
@ -1,4 +1,4 @@
|
|||
import { GuildMember, SlashCommandBuilder, Interaction } from 'discord.js';
|
||||
import { GuildMember, SlashCommandBuilder, Interaction, messageLink } from 'discord.js';
|
||||
import { getTextResponsePrettyPlease, randomWord, sendSegments, startGame } from '../lib/game';
|
||||
import { shuffle } from 'd3-array';
|
||||
import { knownServers } from '../lib/knownServers';
|
||||
|
@ -13,7 +13,7 @@ module.exports = {
|
|||
execute: async (interaction: Interaction, member: GuildMember) => {
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
|
||||
startGame(interaction, member.user, 'Markov game', async (players, channel) => {
|
||||
startGame(interaction, member.user, 'Two Sentence Horror', async (players, channel) => {
|
||||
const firstPlayers = shuffle(players);
|
||||
const secondPlayers = [...firstPlayers.slice(1), firstPlayers[0]]; // shift by 1
|
||||
|
||||
|
@ -44,12 +44,15 @@ module.exports = {
|
|||
}));
|
||||
|
||||
const segments = [
|
||||
'Here\'s the bone-chilling stories you\'ve all created:\n\n',
|
||||
...firstHalves.map((firstHalf, i) => firstHalf + ' ' + secondHalves[i]),
|
||||
'\n\nThank you for participating :)'
|
||||
'Here\'s the bone-chilling stories you\'ve all created:\n',
|
||||
...firstHalves.map((firstHalf, i) => '- ' + firstHalf + ' ' + secondHalves[i]),
|
||||
'\nThank you for participating :)'
|
||||
];
|
||||
|
||||
await sendSegments(segments, channel);
|
||||
const messages = await sendSegments(segments, channel);
|
||||
players.forEach(player => {
|
||||
player.send(`**The game has concluded!** See your results here: ${messageLink(messages[0].channelId, messages[0].id)}`);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
|
@ -29,9 +29,20 @@ function formatMessage(users: User[], time: number, name: string, ended = false)
|
|||
);
|
||||
}
|
||||
|
||||
let membersInGame: string[] = [];
|
||||
|
||||
export async function startGame(interaction: Interaction, startingUser: User, name: string, callback: (players: User[], channel: TextBasedChannel) => Promise<void>) {
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
|
||||
if (membersInGame.includes(startingUser.id)) {
|
||||
await interaction.reply({
|
||||
ephemeral: true,
|
||||
content: 'You are already in a game!'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
membersInGame.push(startingUser.id);
|
||||
let participants: User[] = [startingUser];
|
||||
const duration = 25_000;
|
||||
|
||||
|
@ -52,7 +63,8 @@ export async function startGame(interaction: Interaction, startingUser: User, na
|
|||
!user.bot && (
|
||||
(
|
||||
(emoji ? reaction.emoji.id === emoji.id : reaction.emoji.name === DEFAULT_EMOJI) &&
|
||||
!participants.find(u => user.id === u.id)
|
||||
!participants.find(u => user.id === u.id) &&
|
||||
!membersInGame.includes(user.id)
|
||||
) || (reaction.emoji.name === STOP_EMOJI && user.id === startingUser.id)
|
||||
),
|
||||
time: duration,
|
||||
|
@ -68,12 +80,14 @@ export async function startGame(interaction: Interaction, startingUser: User, na
|
|||
collector.stop();
|
||||
} else {
|
||||
participants.push(user);
|
||||
membersInGame.push(user.id);
|
||||
m.edit(formatMessage(participants, duration - (Date.now() - started), name));
|
||||
}
|
||||
});
|
||||
|
||||
collector.on('remove', (_, user) => {
|
||||
participants = participants.filter(u => u.id !== user.id);
|
||||
membersInGame = membersInGame.filter(u => u !== user.id);
|
||||
m.edit(formatMessage(participants, duration - (Date.now() - started), name));
|
||||
});
|
||||
|
||||
|
@ -84,6 +98,7 @@ export async function startGame(interaction: Interaction, startingUser: User, na
|
|||
await callback(participants, m.channel);
|
||||
|
||||
m.edit(formatMessage(participants, 0, name, true));
|
||||
membersInGame = membersInGame.filter(id => !participants.find(u => u.id === id));
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -142,12 +157,12 @@ export async function sendSegments(segments: string[], channel: TextBasedChannel
|
|||
}
|
||||
if (contentBuffer !== '') content.push(contentBuffer);
|
||||
|
||||
content.forEach(async content => {
|
||||
return Promise.all(content.map(async content =>
|
||||
await channel.send({
|
||||
content: content,
|
||||
allowedMentions: {
|
||||
parse: ['users']
|
||||
}
|
||||
});
|
||||
});
|
||||
})
|
||||
));
|
||||
}
|
Loading…
Reference in New Issue