Improve battle command

This commit is contained in:
Daniel Odendahl Jr
2018-10-09 20:59:49 +00:00
parent 51e8bac784
commit bae943bff7
7 changed files with 110 additions and 67 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
const { CommandoClient } = require('discord.js-commando');
const { WebhookClient } = require('discord.js');
const winston = require('winston');
const PokemonStore = require('./PokemonStore');
const PokemonStore = require('./pokemon/PokemonStore');
const { XIAO_WEBHOOK_ID, XIAO_WEBHOOK_TOKEN } = process.env;
module.exports = class XiaoClient extends CommandoClient {
+30
View File
@@ -0,0 +1,30 @@
const Battler = require('./Battler');
module.exports = class Battle {
constructor(user, opponent) {
this.user = new Battler(this, user);
this.opponent = new Battler(this, opponent);
this.userTurn = false;
}
get attacker() {
return this.userTurn ? this.user : this.opponent;
}
get defender() {
return this.userTurn ? this.opponent : this.user;
}
reset(changeGuard = true) {
if (changeGuard && this.user.guarding) this.user.changeGuard();
if (changeGuard && this.opponent.guarding) this.opponent.changeGuard();
this.userTurn = !this.userTurn;
return null;
}
get winner() {
if (this.user.hp <= 0) return this.opponent;
if (this.opponent.hp <= 0) return this.user;
return null;
}
};
+49
View File
@@ -0,0 +1,49 @@
const { stripIndents } = require('common-tags');
const { list } = require('../../util/Util');
const choices = ['fight', 'guard', 'special', 'run'];
const botChoices = ['fight', 'guard', 'special'];
module.exports = class Battler {
constructor(battle, user) {
this.battle = battle;
this.user = user;
this.bot = user.bot;
this.hp = 500;
this.guarding = false;
}
async chooseAction(msg) {
if (this.bot) return botChoices[Math.floor(Math.random() * botChoices.length)];
await msg.say(stripIndents`
${this}, do you ${list(choices.map(choice => `**${choice}**`), 'or')}?
**${this.battle.user.user.tag}:** ${this.battle.user.hp} HP
**${this.battle.opponent.user.tag}:** ${this.battle.opponent.hp} HP
`);
const filter = res => res.author.id === this.user.id && choices.includes(res.content.toLowerCase());
const turn = await msg.channel.awaitMessages(filter, {
max: 1,
time: 30000
});
if (!turn.size) return 'failed:time';
return turn.first().content.toLowerCase();
}
dealDamage(amount) {
this.hp -= amount;
return this.hp;
}
changeGuard() {
this.guarding = !this.guarding;
return this.guarding;
}
forfeit() {
this.hp = 0;
return null;
}
toString() {
return this.user.toString();
}
};