2022-09-02 15:27:54 -04:00
|
|
|
const { MessageActionRow, MessageButton, Interaction } = require('discord.js');
|
|
|
|
|
const { winGame, loseGame, equipItem } = require('./external_game_functions.js');
|
|
|
|
|
const wait = require('node:timers/promises').setTimeout;
|
|
|
|
|
const { STATE } = require('../db/econ.js')
|
|
|
|
|
|
2022-11-16 16:53:24 -05:00
|
|
|
function startGame(bot, channel, interaction) {
|
|
|
|
|
const args = interaction.options.data;
|
2022-09-02 15:27:54 -04:00
|
|
|
let componentlist = [];
|
|
|
|
|
var diff;
|
|
|
|
|
|
2022-11-16 16:53:24 -05:00
|
|
|
if (args.length < 1 || args[0].value == 'easy') {
|
2022-09-02 15:27:54 -04:00
|
|
|
diff = 0;
|
2022-11-16 16:53:24 -05:00
|
|
|
} else if (args[0].value == 'medium') {
|
2022-09-02 15:27:54 -04:00
|
|
|
diff = 0.1;
|
2022-11-16 16:53:24 -05:00
|
|
|
} else if (args[0].value == 'hard') {
|
2022-09-02 15:27:54 -04:00
|
|
|
diff = 0.2;
|
|
|
|
|
} else {
|
|
|
|
|
diff = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 15:29:48 -04:00
|
|
|
let user = '';
|
2022-11-16 16:53:24 -05:00
|
|
|
if (args.length < 2) {
|
|
|
|
|
user = interaction.user.id;
|
2022-09-06 15:29:48 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-02 15:27:54 -04:00
|
|
|
for (let i = 0; i < 5; i ++) {
|
|
|
|
|
const row = new MessageActionRow();
|
|
|
|
|
|
|
|
|
|
for (let j = 0; j < 5; j ++) {
|
|
|
|
|
//customId = (spot in row)|(spot in column)
|
|
|
|
|
const btn = new MessageButton();
|
|
|
|
|
const isbmb = (Math.random() > (0.70 - diff));
|
|
|
|
|
|
|
|
|
|
if (isbmb) {
|
2022-09-06 15:29:48 -04:00
|
|
|
btn.setCustomId(`mswpr|${i}|${j}|t|${user}`);
|
2022-09-02 15:27:54 -04:00
|
|
|
} else {
|
2022-09-06 15:29:48 -04:00
|
|
|
btn.setCustomId(`mswpr|${i}|${j}|f|${user}`);
|
2022-09-02 15:27:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
btn.setLabel('?')
|
|
|
|
|
.setStyle('SECONDARY')
|
|
|
|
|
row.addComponents(btn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Add the row to the list of rows
|
|
|
|
|
componentlist.push(row);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-16 16:53:24 -05:00
|
|
|
interaction.reply(`${interaction.user} has started a solo game of Minesweeper!`);
|
2022-09-02 15:27:54 -04:00
|
|
|
channel.send({ content: `SCORE: \`0\`\nTILES LEFT: \`25\``, components: componentlist });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function gameOver(interaction, won = false) {
|
|
|
|
|
var components = interaction.message.components;
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
for (i in components) {
|
|
|
|
|
for (j in components[i].components) {
|
|
|
|
|
if (components[i].components[j].customId.split("|")[3] === 't') {
|
|
|
|
|
components[i].components[j].label = "💣";
|
|
|
|
|
components[i].components[j].style = "DANGER";
|
|
|
|
|
} else {
|
|
|
|
|
components[i].components[j].style = "SUCCESS";
|
|
|
|
|
components[i].components[j].label = "5";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
components[i].components[j].setDisabled(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (won) {
|
|
|
|
|
resolve(components);
|
|
|
|
|
} else {
|
|
|
|
|
interaction.message.edit({ components: components });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {Interaction} interaction
|
|
|
|
|
*/
|
|
|
|
|
async function changeBoard(bot, interaction, xp_collection) {
|
|
|
|
|
interaction.deferUpdate();
|
|
|
|
|
const id = interaction.customId.split('|');
|
|
|
|
|
|
2022-09-06 15:29:48 -04:00
|
|
|
//"mswpr|y|x|<t/f>|[user]"
|
2022-09-02 15:27:54 -04:00
|
|
|
const col = id[1];
|
|
|
|
|
const row = id[2];
|
|
|
|
|
const isbmb = (id[3] === 't');
|
2022-09-06 15:29:48 -04:00
|
|
|
const user = id[4];
|
|
|
|
|
if (user && user != '') {
|
|
|
|
|
if (interaction.user.id != user) {
|
2022-11-16 16:53:24 -05:00
|
|
|
interaction.user.send(`Message from a Minesweeper game in <#${interaction.channel.id}>: ***This is a solo game!***`);
|
2022-09-06 15:29:48 -04:00
|
|
|
return; // interaction.reply({ content: "It's not your turn!", ephemeral: true }); //Can only reply once
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-02 15:27:54 -04:00
|
|
|
|
|
|
|
|
var components = interaction.message.components;
|
|
|
|
|
var btn = components[col].components[row];
|
|
|
|
|
|
|
|
|
|
if (isbmb) {
|
|
|
|
|
gameOver(interaction);
|
|
|
|
|
bot.mongoconnection.then((client) => { client.db(interaction.guildId).collection(interaction.user.id).updateOne({ game: {$exists: true} }, { $set: { game: null } }); });
|
|
|
|
|
const channel = bot.channels.cache.get(interaction.message.channel.parentId);
|
|
|
|
|
channel.send(`${interaction.user} found a bomb in Minesweeper!`);
|
2022-11-16 16:53:24 -05:00
|
|
|
interaction.channel.send(`\`Thread closing\` <t:${Math.floor((new Date()).getTime()/1000) + 10}:R>`);
|
2022-09-02 15:27:54 -04:00
|
|
|
|
|
|
|
|
await wait(7000);
|
|
|
|
|
interaction.channel.delete();
|
|
|
|
|
} else {
|
|
|
|
|
btn.setDisabled(true);
|
|
|
|
|
btn.label = "1";
|
|
|
|
|
btn.style = "SUCCESS";
|
|
|
|
|
components[col].components[row] = btn;
|
|
|
|
|
|
|
|
|
|
let content = interaction.message.content;
|
|
|
|
|
let score = Number(content.split('`')[1]);
|
|
|
|
|
let tLeft = Number(content.split('`')[3]);
|
|
|
|
|
|
|
|
|
|
//Win the game (just clicked the last tile)
|
|
|
|
|
if (tLeft <= 1) {
|
|
|
|
|
gameOver(interaction, true).then(async (newComp) => {
|
|
|
|
|
interaction.message.edit({ content: `GAME WON!!!\nSCORE: \`${score + 1}\``, components: newComp });
|
|
|
|
|
const channel = bot.channels.cache.get(interaction.message.channel.parentId);
|
|
|
|
|
channel.send(`${interaction.user} won a game of Minesweeper with a score of ${score + 1}!`);
|
|
|
|
|
interaction.channel.send(`\`Thread closing\` <t:${Math.floor((new Date()).getTime()/1000) + 8}:R>`);
|
|
|
|
|
|
|
|
|
|
await wait(7000);
|
|
|
|
|
// interaction.channel.delete();
|
|
|
|
|
bot.mongoconnection.then(client => {
|
|
|
|
|
const db = client.db(interaction.guildId);
|
|
|
|
|
const dbo = db.collection(interaction.user.id);
|
|
|
|
|
winGame(client, bot, db, dbo, xp_collection, interaction.message, true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
interaction.message.edit({ content: `SCORE: \`${score + 1}\`\nTILES LEFT: \`${tLeft - 1}\``, components: components });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-11-16 16:53:24 -05:00
|
|
|
function checkAndStartGame(bot, interaction, channel) {
|
2022-09-06 15:29:48 -04:00
|
|
|
bot.mongoconnection.then(client => {
|
2022-11-16 16:53:24 -05:00
|
|
|
const db = client.db(interaction.guildId);
|
|
|
|
|
const dbo = db.collection(interaction.user.id);
|
2022-09-02 15:27:54 -04:00
|
|
|
dbo.findOne({game: {$exists: true}}).then((doc) => {
|
|
|
|
|
try {
|
2022-11-16 16:53:24 -05:00
|
|
|
if (doc.game != null) {
|
|
|
|
|
console.log(doc);
|
|
|
|
|
return interaction.reply("You're already in a game!").catch((err) => {
|
|
|
|
|
interaction.channel.send("You're already in a game!");
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-09-02 15:27:54 -04:00
|
|
|
|
|
|
|
|
dbo.updateOne({ "game": {$exists: true} }, { $set: { game: "minesweeper", state: STATE.FIGHTING }});
|
2022-11-16 16:53:24 -05:00
|
|
|
startGame(bot, channel, interaction);
|
2022-09-02 15:27:54 -04:00
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
const { addComplaintButton } = require('../dev only/submitcomplaint.js');
|
2022-11-16 16:53:24 -05:00
|
|
|
addComplaintButton(bot, interaction.message);
|
2022-09-02 15:27:54 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-11-16 16:53:24 -05:00
|
|
|
function handle(bot, interaction, channel = null, isStart = true, xp_collection = null) {
|
|
|
|
|
if (isStart) {
|
|
|
|
|
checkAndStartGame(bot, interaction, channel);
|
2022-09-02 15:27:54 -04:00
|
|
|
} else {
|
|
|
|
|
//Maybe add player checking later?
|
|
|
|
|
changeBoard(bot, interaction, xp_collection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = { handle }
|