Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. /* Discord API Information */
  2. const Discord = require('discord.js');
  3. const token = '';
  4. const Game = require('./game.js');
  5. const client = new Discord.Client();
  6.  
  7. let playersName = []; // tracks each player that joins
  8. let currPlayers = 0; //tracker for total players online
  9. let INIT_GAME;
  10. let userJoined = false;
  11. let inBattle = false;
  12.  
  13. client.on('message', (msg) => {
  14. if(msg.content === '!join'){
  15.  
  16. // Prevents multiple instances of the same person from joining
  17. for(var x = 0; x < playersName.length; x++){
  18. if(playersName[x]===msg.author.username){
  19. return playersName[x];
  20. }
  21. }
  22.  
  23. currPlayers++;
  24. userJoined = true;
  25. playersName.push(msg.author.username);
  26.  
  27. //My attempt at having the question im asking
  28. function convertToID(arr, width) {
  29. return arr.reduce(function (rows, key, index) {
  30. return (index % width == 0 ? rows.push([key])
  31. : rows[rows.length-1].push(key)) && rows;
  32. }, []);
  33. }
  34.  
  35. console.log(convertToID(playersName,1)); /* Tracks players by ID in developer tools */
  36.  
  37. INIT_GAME = new Game(playersName, client, 'bot-testing', currPlayers);
  38. let myRet = INIT_GAME.startGame();
  39.  
  40. const embed = new Discord.RichEmbed()
  41. .setTitle("Welcome To Era Online")
  42. .setColor(0xFF0000)
  43. .addField(`${msg.author.username} has Joined`, myRet);
  44. msg.channel.send(embed);
  45. msg.channel.send(`${msg.author} has joined the game.`);
  46. return;
  47. }
  48.  
  49. if(userJoined == true){
  50. if(msg.content === '!fight' && (!inBattle)){
  51. let grabCurrPlayer = msg.author.username;
  52. msg.channel.send(`${INIT_GAME.initBattle(grabCurrPlayer)}`);
  53. }
  54.  
  55. else if(msg.content === '!leave'){
  56. let tempLeave = msg.author.username;
  57.  
  58. for(var y = 0; y < playersName.length; y++){
  59. if(playersName[y] == msg.author.username){
  60. playersName[y] = [`${playersName[y]} was previously ID: ` + [y]];
  61. currPlayers--;
  62. }
  63. }
  64. msg.channel.send([`${tempLeave} has left the server.`]);
  65. userJoined = false;
  66. }
  67.  
  68. else if(msg.content === '!newgame'){
  69. msg.channel.send(INIT_GAME.newGame());
  70. }
  71.  
  72. /* Simply checks the bonus damage. command for developer*/
  73. else if(msg.content === '!bonus'){
  74. msg.channel.send(INIT_GAME.bonusAttack());
  75. }
  76. }
  77.  
  78. /* checks whose currently online. command for developer*/
  79. if(msg.content === '!online'){
  80. msg.channel.send(INIT_GAME.getOnline());
  81. }
  82.  
  83. });
  84.  
  85. client.on('ready', () => {
  86. console.log('Bot is now connected');
  87. });
  88.  
  89. client.login(token);
  90.  
  91. class Game {
  92. constructor(player, client, channelName='bot-testing', playersOnline){
  93. this.client = client;
  94. this.channelName = channelName;
  95. this.currentPlayer = player;
  96. this.playersOnline = [];
  97. this.hitpoints = 120;
  98. this.damage = '';
  99. this.chance = 3;
  100. this.inBattle = false;
  101. this.online = playersOnline;
  102.  
  103. this.monster = [{
  104. hp: Math.floor(Math.random() * 200),
  105. temphp: 0,
  106. damage: 10
  107. }];
  108. };
  109.  
  110. /* main menu information, players online */
  111. startGame(){
  112. for(var x = 0; x < this.currentPlayer.length; x++){
  113. this.playersOnline.push(this.currentPlayer[x]);
  114. if(this.playersOnline[x] === this.currentPlayer[x]){
  115. return [`Players Online: ${this.online}n`];
  116. }
  117. }
  118. }
  119.  
  120. /* Battle system */
  121. initBattle(currPlayer){
  122. this.inBattle = true;
  123. let npcHP = this.monster[0].hp;
  124. let numberOfAttacks = 0;
  125. let totalDamage=0, totalBonusDamage=0;
  126.  
  127. while( this.monster[0].hp > 0 ){
  128. let playerDamage = Math.floor(Math.random() * (npcHP / 4));
  129. if(this.bonusAttack() === 2){
  130. console.log(`Bonus Attack: ${this.bonusAttack()}`);
  131. console.log(`Regular damage without bonus attack: ${playerDamage}`);
  132. playerDamage = playerDamage + 2;
  133. }
  134. this.monster[0].hp -= playerDamage;
  135. this.hitpoints -= this.monster[0].damage;
  136.  
  137. console.log('Monster: ' + this.monster[0].hp);
  138. console.log('Player: ' + this.hitpoints);
  139. console.log(`${currPlayer} has attacked for ${playerDamage}`);
  140. console.log(`NPC health: ${this.monster[0].hp}`);
  141.  
  142. if(this.hitpoints <= 0){
  143. return [`You lost the battle.`];
  144. }
  145.  
  146. this.inBattle = false;
  147. numberOfAttacks++;
  148. totalDamage += playerDamage;
  149. totalBonusDamage = playerDamage + this.bonusAttack();
  150. }
  151. if(this.monster[0].hp <= 0 && this.inBattle !== true){
  152. let maxDamage = totalDamage + totalBonusDamage;
  153. return [`${currPlayer} has attacked ${numberOfAttacks} times dealing ${totalDamage} + (${totalBonusDamage}) bonus damage for a total of ${maxDamage} damage. The monster is dead.n
  154. Your Health: ${this.hitpoints}`];
  155. }
  156. else{
  157. this.newGame();
  158. return [`You rejuvenated your hitpoints and are ready for battle. nType !fight again to start a new battle!`];
  159. }
  160. }
  161.  
  162. /* bonus attack damage [ 1 in 3 chance ] */
  163. bonusAttack(bonusDamage){
  164. let chance = Math.floor(Math.random() * 3);
  165. return chance === 2 ? bonusDamage = 2 : false;
  166. }
  167.  
  168. /* displays players currently online */
  169. getOnline(){
  170. console.log(this.currentPlayer);
  171. return this.currentPlayer;
  172.  
  173. }
  174.  
  175. /* refresh stats */
  176. newGame(){
  177. this.monster[0].hp = Math.floor(Math.random() * 50);
  178. this.hitpoints = 150;
  179. }
  180. }
  181.  
  182. module.exports = Game;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement