Javascript Discord RPG机器人为阵列中的每个玩家显示ID
我正在尝试学习javascript以及如何使用discord提供的开发者api 我相信到目前为止我想要的一切都在运行,除了我想创建一个像数据库那样的系统。表中的每个玩家都有一个唯一的id键。我不确定在不使用db的情况下这是否可行 [Index.js]Javascript Discord RPG机器人为阵列中的每个玩家显示ID,javascript,arrays,discord,Javascript,Arrays,Discord,我正在尝试学习javascript以及如何使用discord提供的开发者api 我相信到目前为止我想要的一切都在运行,除了我想创建一个像数据库那样的系统。表中的每个玩家都有一个唯一的id键。我不确定在不使用db的情况下这是否可行 [Index.js] /* Discord API Information */ const Discord = require('discord.js'); const token = ''; const Game = require('./game.js'); co
/* Discord API Information */
const Discord = require('discord.js');
const token = '';
const Game = require('./game.js');
const client = new Discord.Client();
let playersName = []; // tracks each player that joins
let currPlayers = 0; //tracker for total players online
let INIT_GAME;
let userJoined = false;
let inBattle = false;
client.on('message', (msg) => {
if(msg.content === '!join'){
// Prevents multiple instances of the same person from joining
for(var x = 0; x < playersName.length; x++){
if(playersName[x]===msg.author.username){
return playersName[x];
}
}
currPlayers++;
userJoined = true;
playersName.push(msg.author.username);
//My attempt at having the question im asking
function convertToID(arr, width) {
return arr.reduce(function (rows, key, index) {
return (index % width == 0 ? rows.push([key])
: rows[rows.length-1].push(key)) && rows;
}, []);
}
console.log(convertToID(playersName,1)); /* Tracks players by ID in developer tools */
INIT_GAME = new Game(playersName, client, 'bot-testing', currPlayers);
let myRet = INIT_GAME.startGame();
const embed = new Discord.RichEmbed()
.setTitle("Welcome To Era Online")
.setColor(0xFF0000)
.addField(`${msg.author.username} has Joined`, myRet);
msg.channel.send(embed);
msg.channel.send(`${msg.author} has joined the game.`);
return;
}
if(userJoined == true){
if(msg.content === '!fight' && (!inBattle)){
let grabCurrPlayer = msg.author.username;
msg.channel.send(`${INIT_GAME.initBattle(grabCurrPlayer)}`);
}
else if(msg.content === '!leave'){
let tempLeave = msg.author.username;
for(var y = 0; y < playersName.length; y++){
if(playersName[y] == msg.author.username){
playersName[y] = [`${playersName[y]} was previously ID: ` + [y]];
currPlayers--;
}
}
msg.channel.send([`${tempLeave} has left the server.`]);
userJoined = false;
}
else if(msg.content === '!newgame'){
msg.channel.send(INIT_GAME.newGame());
}
/* Simply checks the bonus damage. command for developer*/
else if(msg.content === '!bonus'){
msg.channel.send(INIT_GAME.bonusAttack());
}
}
/* checks whose currently online. command for developer*/
if(msg.content === '!online'){
msg.channel.send(INIT_GAME.getOnline());
}
});
client.on('ready', () => {
console.log('Bot is now connected');
});
client.login(token);
[game.js]
class Game {
constructor(player, client, channelName='bot-testing', playersOnline){
this.client = client;
this.channelName = channelName;
this.currentPlayer = player;
this.playersOnline = [];
this.hitpoints = 120;
this.damage = '';
this.chance = 3;
this.inBattle = false;
this.online = playersOnline;
this.monster = [{
hp: Math.floor(Math.random() * 200),
temphp: 0,
damage: 10
}];
};
/* main menu information, players online */
startGame(){
for(var x = 0; x < this.currentPlayer.length; x++){
this.playersOnline.push(this.currentPlayer[x]);
if(this.playersOnline[x] === this.currentPlayer[x]){
return [`Players Online: ${this.online}\n`];
}
}
}
/* Battle system */
initBattle(currPlayer){
this.inBattle = true;
let npcHP = this.monster[0].hp;
let numberOfAttacks = 0;
let totalDamage=0, totalBonusDamage=0;
while( this.monster[0].hp > 0 ){
let playerDamage = Math.floor(Math.random() * (npcHP / 4));
if(this.bonusAttack() === 2){
console.log(`Bonus Attack: ${this.bonusAttack()}`);
console.log(`Regular damage without bonus attack: ${playerDamage}`);
playerDamage = playerDamage + 2;
}
this.monster[0].hp -= playerDamage;
this.hitpoints -= this.monster[0].damage;
console.log('Monster: ' + this.monster[0].hp);
console.log('Player: ' + this.hitpoints);
console.log(`${currPlayer} has attacked for ${playerDamage}`);
console.log(`NPC health: ${this.monster[0].hp}`);
if(this.hitpoints <= 0){
return [`You lost the battle.`];
}
this.inBattle = false;
numberOfAttacks++;
totalDamage += playerDamage;
totalBonusDamage = playerDamage + this.bonusAttack();
}
if(this.monster[0].hp <= 0 && this.inBattle !== true){
let maxDamage = totalDamage + totalBonusDamage;
return [`${currPlayer} has attacked ${numberOfAttacks} times dealing ${totalDamage} + (${totalBonusDamage}) bonus damage for a total of ${maxDamage} damage. The monster is dead.\n
Your Health: ${this.hitpoints}`];
}
else{
this.newGame();
return [`You rejuvenated your hitpoints and are ready for battle. \nType !fight again to start a new battle!`];
}
}
/* bonus attack damage [ 1 in 3 chance ] */
bonusAttack(bonusDamage){
let chance = Math.floor(Math.random() * 3);
return chance === 2 ? bonusDamage = 2 : false;
}
/* displays players currently online */
getOnline(){
console.log(this.currentPlayer);
return this.currentPlayer;
}
/* refresh stats */
newGame(){
this.monster[0].hp = Math.floor(Math.random() * 50);
this.hitpoints = 150;
}
}
module.exports = Game;
[我的问题]
这两个文件中唯一真正重要的部分是index.js中关于玩家何时离开的那一行。所以离开
我有一个问题,其中一个球员键入!离开,两个人都会离开。这就是我用来修复它的解决方案
我无法让它只为键入命令的人清空数组
例如:
A型人!加入
玩家在线=[PlayerA]
B类人!加入
玩家在线=[PlayerA,PlayerB]
玩家A型!离开
在线玩家=[],玩家B]]
它总是在spot中插入一个空数组。所以我所做的只是用用户以前的名字和他们的数组id来填充这个位置
我想要的是,它可以从数组中完全删除这个人,并删除那个空白点
我也想知道是否有可能每次有人打字!加入,我将能够插入到一个新的数组,是多维的,并有每个球员的id,所以当我键入!在线,它将显示
[[0,PlayerA],[1,PlayerB]]。就像一个数据库,如果需要,我可以随时查看它们的索引
到目前为止,我所拥有的:
它只跟踪他们离开后的最后一个索引。如何使其在线显示玩家的当前索引?使用findIndex查找名称在数组中的位置。然后使用拼接方法从数组中删除值。您不需要将其用于循环,因为findIndex将运行类似的循环
var playerIndex = playersName.findIndex(function(index) {
return index === tempLeave
})
playersName.splice(playerIndex, 1)
在阅读了问题的第二部分之后,我认为您应该在数组中创建对象。例如:
[
{
playerName: "Foo",
id: indexNumber,
isOnline: true
},
{
playerName: "Bar",
id: indexNumber,
isOnline: true
}
]
当有人加入时,您可以检查他们的名称是否已分配给对象,您可以在此处再次使用findIndex。如果没有,您将创建一个新的玩家,否则您将把玩家的isOnline属性更改为true。我确信这是存储用户信息的最佳方式,但它可能对您有用。我只是在尝试使用您提供的代码时出错。拼接未定义。很抱歉,您必须在拼接前添加阵列,playerName。拼接playerIndex,1