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';
|
import { getTextResponsePrettyPlease, randomWord, sendSegments, startGame } from '../lib/game';
|
||||||
|
|
||||||
const END_TEMPLATES = [
|
const END_TEMPLATES = [
|
||||||
|
@ -34,7 +34,7 @@ module.exports = {
|
||||||
const context = interaction.options.getInteger('context') || 3;
|
const context = interaction.options.getInteger('context') || 3;
|
||||||
const maxIterations = interaction.options.getInteger('iterations') || 10;
|
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);
|
let sentences: string[][] = Array(participants.length).fill(0);
|
||||||
sentences = sentences.map(() => []);
|
sentences = sentences.map(() => []);
|
||||||
let iterations = 0;
|
let iterations = 0;
|
||||||
|
@ -61,12 +61,15 @@ module.exports = {
|
||||||
const endTemplate = END_TEMPLATES[Math.floor(Math.random() * END_TEMPLATES.length)];
|
const endTemplate = END_TEMPLATES[Math.floor(Math.random() * END_TEMPLATES.length)];
|
||||||
|
|
||||||
const segments = [
|
const segments = [
|
||||||
endTemplate + '\n\n',
|
endTemplate + '\n',
|
||||||
...sentences.map(sentence => '- ' + sentence.join(' ')),
|
...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 { getTextResponsePrettyPlease, randomWord, sendSegments, startGame } from '../lib/game';
|
||||||
import { shuffle } from 'd3-array';
|
import { shuffle } from 'd3-array';
|
||||||
import { knownServers } from '../lib/knownServers';
|
import { knownServers } from '../lib/knownServers';
|
||||||
|
@ -13,7 +13,7 @@ module.exports = {
|
||||||
execute: async (interaction: Interaction, member: GuildMember) => {
|
execute: async (interaction: Interaction, member: GuildMember) => {
|
||||||
if (!interaction.isChatInputCommand()) return;
|
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 firstPlayers = shuffle(players);
|
||||||
const secondPlayers = [...firstPlayers.slice(1), firstPlayers[0]]; // shift by 1
|
const secondPlayers = [...firstPlayers.slice(1), firstPlayers[0]]; // shift by 1
|
||||||
|
|
||||||
|
@ -44,12 +44,15 @@ module.exports = {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const segments = [
|
const segments = [
|
||||||
'Here\'s the bone-chilling stories you\'ve all created:\n\n',
|
'Here\'s the bone-chilling stories you\'ve all created:\n',
|
||||||
...firstHalves.map((firstHalf, i) => firstHalf + ' ' + secondHalves[i]),
|
...firstHalves.map((firstHalf, i) => '- ' + firstHalf + ' ' + secondHalves[i]),
|
||||||
'\n\nThank you for participating :)'
|
'\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>) {
|
export async function startGame(interaction: Interaction, startingUser: User, name: string, callback: (players: User[], channel: TextBasedChannel) => Promise<void>) {
|
||||||
if (!interaction.isChatInputCommand()) return;
|
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];
|
let participants: User[] = [startingUser];
|
||||||
const duration = 25_000;
|
const duration = 25_000;
|
||||||
|
|
||||||
|
@ -52,7 +63,8 @@ export async function startGame(interaction: Interaction, startingUser: User, na
|
||||||
!user.bot && (
|
!user.bot && (
|
||||||
(
|
(
|
||||||
(emoji ? reaction.emoji.id === emoji.id : reaction.emoji.name === DEFAULT_EMOJI) &&
|
(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)
|
) || (reaction.emoji.name === STOP_EMOJI && user.id === startingUser.id)
|
||||||
),
|
),
|
||||||
time: duration,
|
time: duration,
|
||||||
|
@ -68,12 +80,14 @@ export async function startGame(interaction: Interaction, startingUser: User, na
|
||||||
collector.stop();
|
collector.stop();
|
||||||
} else {
|
} else {
|
||||||
participants.push(user);
|
participants.push(user);
|
||||||
|
membersInGame.push(user.id);
|
||||||
m.edit(formatMessage(participants, duration - (Date.now() - started), name));
|
m.edit(formatMessage(participants, duration - (Date.now() - started), name));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
collector.on('remove', (_, user) => {
|
collector.on('remove', (_, user) => {
|
||||||
participants = participants.filter(u => u.id !== user.id);
|
participants = participants.filter(u => u.id !== user.id);
|
||||||
|
membersInGame = membersInGame.filter(u => u !== user.id);
|
||||||
m.edit(formatMessage(participants, duration - (Date.now() - started), name));
|
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);
|
await callback(participants, m.channel);
|
||||||
|
|
||||||
m.edit(formatMessage(participants, 0, name, true));
|
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);
|
if (contentBuffer !== '') content.push(contentBuffer);
|
||||||
|
|
||||||
content.forEach(async content => {
|
return Promise.all(content.map(async content =>
|
||||||
await channel.send({
|
await channel.send({
|
||||||
content: content,
|
content: content,
|
||||||
allowedMentions: {
|
allowedMentions: {
|
||||||
parse: ['users']
|
parse: ['users']
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
});
|
));
|
||||||
}
|
}
|
Loading…
Reference in New Issue