Added the battle fighting commands, restructured the way hp and mp are stored, and added weapons. NOTE: UPDATING THE HP AND MP DOES NOT WORK

This commit is contained in:
ION606
2022-05-29 20:59:39 +03:00
parent 7fa6b85f10
commit 69a5a2e18d
6 changed files with 288 additions and 185 deletions
+23 -2
View File
@@ -1,3 +1,7 @@
let d = new Date();
const START = d.getTime();
const { MongoClient, ServerApiVersion } = require('mongodb');
const mongouri = process.env.MONGODB_URI; //DO NOT RUN LOCALLY (no process.env)
@@ -7,7 +11,8 @@ client.connect(err => {
collectiontemp = client.db("main").collection("shop");
// perform actions on the collection object
collectiontemp.insertMany(
[{ name: 'Grapes', cost: 2, icon: '🍇', sect: 'Food' },
[
{ name: 'Grapes', cost: 2, icon: '🍇', sect: 'Food' },
{ name: 'Melon', cost: 5, icon: '🍈', sect: 'Food' },
{ name: 'Watermelon', cost: 5, icon: '🍉', sect: 'Food' },
{ name: 'Tangerine', cost: 3, icon: '🍊', sect: 'Food' },
@@ -118,7 +123,23 @@ client.connect(err => {
{ name: 'Soda', cost: 3, icon: '🥤', sect: 'Food' },
{ name: 'Bubble Tea', cost: 3, icon: '🧋', sect: 'Food' },
{ name: 'Beverage Box', cost: 30, icon: '🧃', sect: 'Food' },
{ name: 'Mate', cost: 3, icon: '🧉', sect: 'Food' }]);
{ name: 'Mate', cost: 3, icon: '🧉', sect: 'Food' },
//Weapons
{ name: 'Swords_special', cost: 3, icon: '⚔️', sect: 'Weapons' },
{ name: 'Boomerang', cost: 300, icon: '🪃', sect: 'Weapons' },
{ name: 'Boomerang', cost: 200, icon: '🏹', sect: 'Weapons' },
{ name: 'Knife', cost: 20, icon: '🔪', sect: 'Weapons' },
{ name: 'Dagger', cost: 60, icon: '🗡', sect: 'Weapons' },
{ name: 'Shield', cost: 100, icon: '🛡', sect: 'Weapons' },
{ name: 'Axe', cost: 40, icon: '🪓', sect: 'Weapons' },
{ name: 'Trident', cost: 140, icon: '🔱', sect: 'Weapons' },
{ name: 'Scissors', cost: 10, icon: '✂️', sect: 'Weapons' }
]);
});
client.close();
const END = d.getTime();
console.log(`Total time in SECONDS: ${(((END - START) % 60000) / 1000).toFixed(0)} ms!`);
+66 -8
View File
@@ -1,5 +1,7 @@
//@ts-check
const { STATE } = require('./econ');
const { winGame } = require('./external_game_functions.js');
/**
* Called by "attack"
@@ -9,9 +11,50 @@ function attack_special() {
}
function attack(user_dbo, other_dbo, bot, thread, command, mongouri, items) {
//Bow special phrase: Σ>―(´・ω・`)→
function attack(client, user_dbo, other_dbo, bot, thread, command, mongouri, items, xp_collection, interaction) {
//Get the weapon
user_dbo.find({'equipped': {$exists: true}}).toArray(function(err, docs) {
const doc = docs[0];
const all_weapons = doc.equipped.weapons;
console.log(all_weapons);
const weapon = all_weapons.main;
var dmg = 0;
//No weapons (punch)
if (weapon == null) {
dmg = doc.rank;
} else {
dmg = (doc.rank - 1) + Math.round(weapon.cost/5);
}
other_dbo.find({'equipped': {$exists: true}}).toArray(function (err, docs) {
const odoc = docs[0];
//Handle defending
if (odoc.state == STATE.DEFENDING) {
var def = odoc.rank - doc.rank;
//Make sure we don't go negative
if (def < 0) { def = 0; }
dmg /= 2 + def;
}
var new_hp = odoc.hpmp.hp -= dmg;
if (new_hp <= 0) {
winGame(client, bot, client.db(user_dbo.s.namespace.db), user_dbo, xp_collection, interaction.message);
} else {
other_dbo.updateOne({'equipped': {$exists: true}}, { $set: { hp :new_hp }}); //THIS DOES NOT WORK
}
});
})
//Check for a "special" animation
postActionBar(thread, other_dbo);
}
@@ -19,7 +62,8 @@ function attack(user_dbo, other_dbo, bot, thread, command, mongouri, items) {
* Called by "item"
*/
function heal(user_dbo, bot, thread, command, mongouri, items) {
postActionBar(thread, user_dbo);
}
@@ -29,6 +73,15 @@ function item() {
}
function defend(user_dbo, bot, thread, command, mongouri, items) {
user_dbo.find({'equipped': {$exists: true}}).toArray(function(err, docs) {
const doc = docs[0];
const all_weapons = doc.get('weapons');
const shield = all_weapons.get('secondary');
})
}
function cast() {
}
@@ -46,6 +99,10 @@ function postActionBar(thread, user_dbo) {
.setCustomId('HEAL')
.setLabel('HEAL')
.setStyle('SUCCESS'),
new MessageButton()
.setCustomId('DEFEND')
.setLabel('DEFEND')
.setStyle('PRIMARY'),
new MessageButton()
.setCustomId('ITEMS')
.setLabel('ITEMS')
@@ -57,19 +114,20 @@ function postActionBar(thread, user_dbo) {
function handle(user_dbo, other_dbo, bot, thread, command, mongouri, items) {
function handle(client, user_dbo, other_dbo, bot, thread, command, mongouri, items, interaction, xp_collection) {
if (command == 'initalize') {
postActionBar(thread, user_dbo);
return postActionBar(thread, user_dbo);
} else if (command == 'attack') {
attack(user_dbo, other_dbo, bot, thread, command, mongouri, items);
attack(client, user_dbo, other_dbo, bot, thread, command, mongouri, items, xp_collection, interaction);
} else if (command == 'items') {
item();
} else if (command == 'heal') {
heal();
}
//Post the action bar for the next person's turn
postActionBar(thread, other_dbo);
// initiate(user_dbo, other_dbo, command, message);
}
module.exports = { handle }
+18 -4
View File
@@ -14,8 +14,9 @@ const BASE = {
const STATE = {
IDLE: 0,
FIGHTING: 1,
PRONE: 2,
WAITING: 3
DEFENDING: 2,
PRONE: 3,
WAITING: 4 //For items ONLY
}
//Note that leveling up to the next level takes 10% more xp than the previous one
@@ -37,7 +38,8 @@ function CreateNewCollection(message, client, server, id, opponent = null, game
if (err) { return console.log(err); }
if (!collinfo) {
message.reply("You didn't have a place in my databases, so I created one for you!\nPlease try your command again!")
dbo.insertOne({balance: 10, rank: 1, lastdayworked: 0, xp: 0, hp: BASE.HP, mp: BASE.MP, game: game, opponent: opponent, state: STATE.IDLE});
let hp_mp = {maxhp: BASE.HP, hp: BASE.HP, maxmp: BASE.MP, mp: BASE.MP}
dbo.insertOne({balance: 10, rank: 1, lastdayworked: 0, xp: 0, hpmp: hp_mp, game: game, opponent: opponent, state: STATE.IDLE, equipped: { weapons: {main: null, secondary: null}, items: {}}});
}
});
});
@@ -65,9 +67,21 @@ function addxp(message, dbo, amt, xp_list) {
needed = xp_list.get(rank);
}
rank --; //Maybe?
dbo.updateOne({balance: temp.balance, rank: temp.rank, lastdayworked: temp.lastdayworked}, { $set: { rank: rank }});
let newhp;
if (newhp < 200) {
newhp = BASE.HP * rank;
} else {
newhp = temp.hpmp.hp + 50;
}
let newmp = temp.mp + 5;
dbo.updateOne({balance: temp.balance, rank: temp.rank, lastdayworked: temp.lastdayworked}, { $set: { rank: rank, hpmp: {maxhp: newhp, maxmp: newmp} }});
message.channel.send('Congradulations <@' + message.author.id + '> for reaching rank ' + String(rank) + '!');
}
} else {
message.reply("You've already reached max level!");
}
dbo.updateOne({balance: temp.balance}, { $set: { xp: txp}});
+61
View File
@@ -0,0 +1,61 @@
//@ts-check
const { addxp, STATE, BASE } = require("./econ.js");
//#region game lose/win
function loseGame(user_dbo, xp_collection, message, bot = null) {
return new Promise(function(resolve, reject) {
user_dbo.find({"game": {$exists: true}}).toArray(function(err, docs){
const doc = docs[0];
if (doc == undefined) { return message.reply("Oops! There's been an error! Please contact support if this problem persists!"); }
if (doc.game == null) { return message.reply("You're not even in a game and you're trying to quit! Sad..."); }
//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]
var addbal = doc.rank * 2;
if (doc.balance - addbal < 5) { addbal = addbal - doc.balance; }
if (doc.balance > 5) {
user_dbo.updateOne(doc, { $set: { balance: doc.balance - addbal}});
}
} else { message.channel.delete(); }
//Update the player's xp
addxp(message, user_dbo, Math.ceil((BASE.XP * doc.rank)/2),xp_collection)
user_dbo.updateOne({"game": {$exists: true}}, { $set: { game: null, opponent: null, state: STATE.IDLE, hp: doc.hpmp.maxhp, mp: doc.hpmp.maxmp }});
resolve(addbal);
});
});
}
function winGame(client, bot, db, user_dbo, xp_collection, message) {
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);
let promise_temp = loseGame(other, xp_collection, message);
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
client.db('B|S' + bot.user.id).collection(user_dbo.s.namespace.db.substr(0, user_dbo.s.namespace.db.length - 6)).drop();
//Update the player with xp
user_dbo.updateOne({"game": {$exists: true}}, { $set: { game: null, opponent: null, state: STATE.IDLE, xp: doc.xp + (BASE.XP * doc.rank), hp: doc.hpmp.maxhp, mp: doc.hpmp.maxmp }});
message.channel.delete();
});
}
module.exports = { winGame, loseGame }
+7 -59
View File
@@ -2,11 +2,13 @@
const { MongoClient, ServerApiVersion } = require('mongodb');
let ecoimport = require("./econ.js");
let battle = require("./battle.js");
let { handle } = require("./battle.js"); //PROBLEM (CIRCULAR DEPENDANCY)
let snowflake = require("./addons/snowflake.js");
const STATE = ecoimport.STATE;
const BASE = ecoimport.BASE;
const { winGame, loseGame } = require('./external_game_functions.js');
//Has a list of all games (used to change player state)
const allGames = ['battle'];
// const { NULL } = require('mysql/lib/protocol/constants/types');
@@ -56,59 +58,6 @@ async function Initialize(bot, user_dbo, command, message, first, second, other_
});
}
//#region game lose/win
function loseGame(user_dbo, xp_collection, message, bot = null) {
return new Promise(function(resolve, reject) {
user_dbo.find({"game": {$exists: true}}).toArray(function(err, docs){
const doc = docs[0];
if (doc == undefined) { return message.reply("Oops! There's been an error! Please contact support if this problem persists!"); }
if (doc.game == null) { return message.reply("You're not even in a game and you're trying to quit! Sad..."); }
//If this function was not called from "winGame", return
if (doc.opponent) {
//If remove some money (looting) [maybe implement a "friendly" game setting later with no looting]
var addbal = doc.rank * 2;
if (doc.balance - addbal < 5) { addbal = addbal - doc.balance; }
if (doc.balance > 5) {
user_dbo.updateOne(doc, { $set: { balance: doc.balance - addbal}});
}
}
//Update the player's xp
ecoimport.addxp(message, user_dbo, Math.ceil((BASE.XP * doc.rank)/2),xp_collection)
user_dbo.updateOne({"game": {$exists: true}}, { $set: { game: null, opponent: null, state: STATE.IDLE }});
resolve(addbal);
});
});
}
function winGame(client, bot, db, user_dbo, xp_collection, message) {
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);
let promise_temp = loseGame(other, xp_collection, message);
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
client.db('B|S' + bot.user.id).collection(user_dbo.s.namespace.db.substr(0, user_dbo.s.namespace.db.length - 6)).drop();
//Update the player with xp
user_dbo.updateOne({"game": {$exists: true}}, { $set: { game: null, opponent: null, state: STATE.IDLE, xp: doc.xp + (BASE.XP * doc.rank) }});
});
}
//#endregion
//replies to the message with current game specifics
@@ -168,6 +117,7 @@ function acceptIsValid(bot, other_discord, message, msg, tag_len) {
function hpmp(message, command, dbo) {
throw 'THIS HAS NOT BEEN UPDATED WITH THE MOST RECENT VERSION OF THE MONGODB STRUCTURE!';
if (command == 'hp') {
dbo.find({"hp": {$exists: true}}).toArray(function(err, doc) {
return message.reply(`You have ${String(doc[0].hp)} hp left!`);
@@ -183,7 +133,7 @@ function hpmp(message, command, dbo) {
//#region GAME SPECIFIC
function in_game_redirector(bot, interaction, threadname, doc, client, mongouri, items) {
function in_game_redirector(bot, interaction, threadname, doc, client, mongouri, items, xp_collection) {
//Maybe fix this later......
let turn = doc.turn;
@@ -197,7 +147,7 @@ function in_game_redirector(bot, interaction, threadname, doc, client, mongouri,
dbo.find({'game': {$exists: true}}).toArray(function (err, docs) {
const game = docs[0].game
switch (game) {
case 'battle': battle.handle(dbo, other, bot, thread, interaction.customId.toLowerCase(), mongouri, items);
case 'battle': handle(client, dbo, other, bot, thread, interaction.customId.toLowerCase(), mongouri, items, interaction, xp_collection);
}
});
}
@@ -296,7 +246,7 @@ module.exports ={
if (newCommand == 'battle') {
const result = Initialize(bot, dbo, newCommand, msg, id, other_discord.id, other);
result.then(function (thread) {
battle.handle(dbo, other, bot, thread, 'initalize', mongouri, items);
handle(client, dbo, other, bot, thread, 'initalize', mongouri, items, null, xp_collection);
});
}
} else if (command == 'quit') {
@@ -313,8 +263,6 @@ module.exports ={
loseGame(dbo, xp_collection, message, bot);
channel.send(`<@${message.author.id}> has quit a game of "${game}"!`);
}
message.channel.delete();
}
else if (command == 'status') {
getGame(message, args, db);
+2 -1
View File
@@ -147,6 +147,7 @@ bot.on('interactionCreate', async interaction => {
const doc = result[1];
const threadname = doc.thread;
const dbo = client.db(interaction.guildId + '[ECON]').collection(id);
dbo.find({ 'state': {$exists: true} }).toArray(async function (err, docs) {
if (interaction.user.id == id) {
await interaction.deferReply();
@@ -154,7 +155,7 @@ bot.on('interactionCreate', async interaction => {
//Check State
if (docs[0].state == STATE.FIGHTING) {
//Do turn stuff
bot.commands.get('game').in_game_redirector(bot, interaction, threadname, doc, client, mongouri, items);
bot.commands.get('game').in_game_redirector(bot, interaction, threadname, doc, client, mongouri, items, xp_collection);
}
turnManager.changeTurn(client, bot, interaction);