Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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);
- 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;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement