If最近开始在rpg bot上工作。
我试图实现一个回合制的作战系统。
但在这个过程中,我发现我的while循环本应让战斗持续到有人获胜,但它不会发送任何让玩家能够玩的消息(console.log注释有效,但通过discordapi发送的消息无效(。
我认为,由于某种原因,它无法自行解决。
这是我正在使用的代码。
let winner = false;
let filter = m => m.author.id === message.author.id;
message.channel.send("A battle is about to begin !");
do {
message.channel.send(`What attack do you wanna use ?`).then(() => {
message.channel.awaitMessages(filter, {
max: 1,
time: 30000,
errors: ['time']
}).then(message => {
message = message.first()
if (message.content.toUpperCase() == 'Fireball' || message.content.toUpperCase() == 'fireball') {
message.channel.send(`Fireball has been used !`)
} else {
message.channel.send(`Terminated: Invalid Response`)
}
}).catch(collected => {
message.channel.send('Timeout');
});
})
} while (battleStats[0]["CurrentHp"] != 0);
注:battleStats[0]["CurrentHp"]
的值为100,包含en-Int值
String.prototype.toUpperCase()
使整个字符串大写,而不仅仅是第一个字母。除此之外,如果已经有一个具有该名称的变量,则无法为其赋值,就像对message
所做的那样。要解决此问题,请将.then()
设置为类似的内容。如果有任何您没有指定的错误,它也可能是其他地方的问题。
.then(messages => {
let response = messages.first()
if (response.content.toLowerCase() === "fireball") {
message.channel.send(`Fireball has been used!`);
/* change hp here */
} else {
message.channel.send(`Terminated: Invalid Response`);
break // to leave the while loop
}
}).catch(error => {
message.channel.send('Timeout');
break // also to leave the while loop
});
好吧,经过思考,我决定去掉while循环,这是问题的根源。我从这篇文章中获得了灵感。这让我意识到,制造一个回合制的战斗根本没有必要。
因此,为了解决这个问题,我使用module.exports
模块导出了一个包含我的所有玩家和对手数据的对象。
通过在其他命令中请求,我们将能够储存数据,并为回合制作战系统修改数据。
如果你在浏览我在这里使用的代码,它就是。
启动战斗命令,启动有关玩家和恩尼米的数据收集。(注意,我现在正在使用一个对象来存储数据,并不再注意数组
database.execute(
'SELECT * FROM `tstats`, `tMstats` WHERE playerId = ? and monsterStatsId = ?',
[`${message.author.id}`, utility.randomIntFromInterval(1, 2)]
//the randomIntFromInterval function is just here to select a random ennemy from id
, function (err, result, fields) {
if (err) throw err;
Stats.MaxHp = parseInt(result[0]['playerMaxHp']);
Stats.CurrentHp = parseInt(result[0]['playerCurrentHp']);
Stats.MaxMana = parseInt(result[0]['playerMaxMana']);
Stats.CurrentMana = parseInt(result[0]['playerCurrentMana']);
Stats.PhysicalDefence = parseInt(result[0]['playerPhysicalDefence']);
Stats.MagicalDefence = parseInt(result[0]['playerMagicalDefence']);
Stats.Speed = parseInt(result[0]['playerSpeed']);
Stats.Level = parseInt(result[0]['playerLevel']);
Stats.Xp = parseInt(result[0]['playerXp']);
Stats.Evasion = parseInt(result[0]['playerEvasion']);
MStats.name = result[0]['monsterName']
MStats.MaxHp = parseInt(result[0]['monsterMaxHp']);
MStats.CurrentHp = parseInt(result[0]['monsterCurrentHp']);
MStats.MaxMana = parseInt(result[0]['monsterMaxMana']);
MStats.CurrentMana = parseInt(result[0]['monsterCurrentMana']);
MStats.PhysicalDefence = parseInt(result[0]['monsterPhysicalDefence']);
MStats.MagicalDefence = parseInt(result[0]['monsterMagicalDefence']);
MStats.Speed = parseInt(result[0]['monsterSpeed']);
MStats.Level = parseInt(result[0]['monsterLevel']);
MStats.Evasion = parseInt(result[0]['monsterEvasion']);
let battle = combat.initiateBattle(message.author.id, MStats.name);
utility.addBattle(battle);
message.channel.send("A battle is about to begin !");
module.exports = { Stats, MStats };
});
这是我的实用程序文件,其中包含一组JSON作战日志系统所需的函数。
const fs = require("fs");
const logs = require('../database/battleLog.json')
let data = JSON.parse(fs.readFileSync('/home/username/Desktop/Dev perso/Nazarick/database/battleLog.json'));
function setIndex(nombre) {
data["index"] = nombre
fs.writeFile('/home/username/Desktop/Dev perso/Nazarick/database/battleLog.json', JSON.stringify(data), (err) => {
if (err) console.log(err);
});
}
function addBattle(battle) {
data["battle"].push({
id: battle["battle"]["id"],
current_turn: {
owner: battle["battle"]["current_turn"]["owner"],
number: battle["battle"]["current_turn"]["number"]
}
});
fs.writeFile('/home/username/Desktop/Dev perso/Nazarick/database/battleLog.json', JSON.stringify(data), (err) => {
if (err) console.log(err);
});
}
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
function generateId(prefix, start) {
start = data["index"];
var i = start+1 || 1;
return function () {
setIndex(i)
return prefix + i++;
}
}
var id = generateId("battle_", 1);
module.exports = {
randomIntFromInterval,
id,
addBattle
};
最后战斗功能文件启动战斗
const utility = require('./utility.js');
class Battle {
constructor(attacker, target) {
this.attacker = attacker;
this.target = target;
}
};
globalBattlesList = new Set();
// Starts a Battle -- could throw an exception if either combatant is already in combat.
function initiateBattle(attacker, target) {
var battle = new Battle(attacker, target);
return {
battle: {
id: utility.id(),
current_turn: {
owner: attacker,
number: 1,
}
}
};
}
// Send an attack in a battle
function attack(battle, attacker, target) {
if (battle.current_turn.owner != attack) {
return { result: "error", message: "It's not your turn!" };
}
target.HP -= attacker.ATK;
if (target.HP <= 0) {
game.end_battle(battle);
return {
status: "OK",
winner: attacker,
message: "${attacker.name} wins in ${battle.current_turn.number} turns!"
};
}
// change turn
battle.current_turn = { owner: target, number: battle.current_turn.number + 1 };
// does the AI have a delay?
setTimeout(function () {
// do_enemy_turn will choose an action for the AI
// and set battle.current_turn back to
do_enemy_turn(battle, target, attacker);
}, target.attack_delay);
return {
result: "continue",
message: `target is at ${target.HP} HP`
};
}
module.exports = { initiateBattle, attack };
注意:攻击功能不会针对此设置进行调整。