2022-05-29 20:59:39 +03:00
|
|
|
//@ts-check
|
2022-07-01 15:00:52 +03:00
|
|
|
const { addxp, STATE, BASE } = require("../db/econ");
|
2022-06-01 20:38:39 +03:00
|
|
|
const turnManger = require('../turnManager.js');
|
2022-07-19 15:41:49 +03:00
|
|
|
const { addComplaintButton } = require('../dev only/submitcomplaint');
|
2022-05-29 20:59:39 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//#region game lose/win
|
2022-11-16 16:53:24 -05:00
|
|
|
function loseGame(user_dbo, xp_collection, interaction, bot = null) {
|
2022-05-29 20:59:39 +03:00
|
|
|
return new Promise(function(resolve, reject) {
|
2022-11-16 16:53:24 -05:00
|
|
|
user_dbo.find({"game": {$exists: true}}).toArray(function(err, docs) {
|
2022-05-29 20:59:39 +03:00
|
|
|
const doc = docs[0];
|
2022-07-19 15:41:49 +03:00
|
|
|
if (doc == undefined) {
|
2022-11-16 16:53:24 -05:00
|
|
|
interaction.reply("Oops! There's been an error, click the ✅ to report this!");
|
|
|
|
|
addComplaintButton(bot, interaction);
|
2022-07-19 15:41:49 +03:00
|
|
|
return;
|
|
|
|
|
}
|
2022-11-16 16:53:24 -05:00
|
|
|
if (doc.game == null) { return interaction.reply("You're not even in a game and you're trying to quit! Sad..."); }
|
2022-05-29 20:59:39 +03:00
|
|
|
|
2022-06-17 16:41:02 +03:00
|
|
|
var addbal;
|
2022-05-29 20:59:39 +03:00
|
|
|
//If this function was called from "winGame", return
|
|
|
|
|
if (doc.opponent) {
|
|
|
|
|
//If remove some money (looting) [maybe implement a "friendly" game setting later with no looting]
|
2022-06-17 16:41:02 +03:00
|
|
|
addbal = doc.rank * 2;
|
2022-05-29 20:59:39 +03:00
|
|
|
if (doc.balance - addbal < 5) { addbal = addbal - doc.balance; }
|
|
|
|
|
if (doc.balance > 5) {
|
|
|
|
|
user_dbo.updateOne(doc, { $set: { balance: doc.balance - addbal}});
|
|
|
|
|
}
|
2022-11-16 16:53:24 -05:00
|
|
|
} else {
|
|
|
|
|
//Check if the channel is a thread
|
|
|
|
|
interaction.channel.delete();
|
|
|
|
|
}
|
2022-05-29 20:59:39 +03:00
|
|
|
|
|
|
|
|
//Update the player's xp
|
2022-11-18 11:01:15 -05:00
|
|
|
addxp(interaction, user_dbo, Math.ceil((BASE.XP * doc.rank)/2),xp_collection);
|
2022-05-30 20:37:15 +03:00
|
|
|
user_dbo.updateOne({"game": {$exists: true}}, { $set: { game: null, opponent: null, state: STATE.IDLE, 'hpmp.hp': doc.hpmp.maxhp, 'hpmp.mp': doc.hpmp.maxmp }});
|
2022-05-29 20:59:39 +03:00
|
|
|
|
|
|
|
|
resolve(addbal);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-11-16 16:53:24 -05:00
|
|
|
function winGame(client, bot, db, user_dbo, xp_collection, interaction, singlePlayer = false) {
|
2022-05-29 20:59:39 +03:00
|
|
|
user_dbo.find({"game": {$exists: true}}).toArray(function(err, docs){
|
|
|
|
|
const doc = docs[0];
|
|
|
|
|
|
|
|
|
|
//Check for an opponent
|
|
|
|
|
if (doc.opponent != null) {
|
|
|
|
|
let other = db.collection(doc.opponent);
|
2022-11-16 16:53:24 -05:00
|
|
|
let promise_temp = loseGame(other, xp_collection, interaction);
|
2022-05-29 20:59:39 +03:00
|
|
|
|
|
|
|
|
promise_temp.then(function(result) {
|
|
|
|
|
var amt_taken = result;
|
|
|
|
|
user_dbo.updateOne({'balance': {$exists: true}}, { $set: { balance: doc.balance + amt_taken}});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Delete the bot's record of the game
|
2022-09-02 15:27:54 -04:00
|
|
|
if (!singlePlayer) {
|
2022-11-16 16:53:24 -05:00
|
|
|
client.db('B|S' + bot.user.id).collection(interaction.guildId).drop();
|
2022-09-02 15:27:54 -04:00
|
|
|
}
|
2022-05-29 20:59:39 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//Update the player with xp
|
2022-05-30 20:37:15 +03:00
|
|
|
user_dbo.updateOne({"game": {$exists: true}}, { $set: { game: null, opponent: null, state: STATE.IDLE, xp: doc.xp + (BASE.XP * doc.rank), 'hpmp.hp': doc.hpmp.maxhp, 'hpmp.mp': doc.hpmp.maxmp }});
|
2022-05-29 20:59:39 +03:00
|
|
|
|
2022-09-02 15:27:54 -04:00
|
|
|
if (!singlePlayer) {
|
2022-11-16 16:53:24 -05:00
|
|
|
const channel = bot.channels.cache.get(interaction.channel.parentId);
|
2022-09-02 15:27:54 -04:00
|
|
|
channel.send(`<@${user_dbo.s.namespace.collection}> just won a game of "${docs[0].game}"!`);
|
|
|
|
|
}
|
2022-11-16 16:53:24 -05:00
|
|
|
interaction.channel.delete();
|
2022-05-29 20:59:39 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-11-16 16:53:24 -05:00
|
|
|
function equipItem(client, bot, db, dbo, interaction) {
|
|
|
|
|
return interaction.reply("This command is not implemented yet!");
|
2022-06-11 17:53:44 +03:00
|
|
|
if (!bot.inDebugMode) { return; }
|
|
|
|
|
let items = [
|
|
|
|
|
{ name: 'HP Potion', cost: 20, icon: 'CUSTOM|healing_potion', sect: 'HP', num: 2 },
|
|
|
|
|
{ name: 'Super HP Potion', cost: 50, icon: 'CUSTOM|super_healing_potion', sect: 'HP', num: 2 },
|
|
|
|
|
{ name: 'MP Potion', cost: 15, icon: 'CUSTOM|mana_potion', sect: 'MP', num: 2 }
|
|
|
|
|
]
|
2022-06-17 16:41:02 +03:00
|
|
|
for (let i = 1; i <= 10; i ++) {
|
|
|
|
|
|
|
|
|
|
items.push({ name: `${String.fromCharCode(i + 64)}`, cost: i * 10, icon: 'N/A', sect: 'N/A', num: i })
|
|
|
|
|
}
|
2022-06-11 17:53:44 +03:00
|
|
|
|
|
|
|
|
dbo.updateMany({}, {$set: {'equipped.items': items}});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getCustomEmoji(bot, name) {
|
|
|
|
|
let srv = bot.guilds.cache.get(bot.home_server).emojis.cache;
|
|
|
|
|
// console.log(srv);
|
|
|
|
|
let emj = srv.find((g) => { return g.name == name });
|
|
|
|
|
// message.channel.send(`${emj}`);
|
|
|
|
|
return `${emj}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = { winGame, loseGame, equipItem, getCustomEmoji }
|