AI Car Race

This commit is contained in:
Dragon Fire
2021-01-27 18:42:57 -05:00
parent 8e3851465e
commit bb2a1c2b88
+59 -27
View File
@@ -7,6 +7,12 @@ const fs = require('fs');
const cars = fs.readdirSync(path.join(__dirname, '..', '..', 'assets', 'images', 'car-race', 'cars')) const cars = fs.readdirSync(path.join(__dirname, '..', '..', 'assets', 'images', 'car-race', 'cars'))
.map(car => car.replace('.png', '')); .map(car => car.replace('.png', ''));
const words = ['go', 'zoom', 'drive', 'advance', 'pedal', 'vroom']; const words = ['go', 'zoom', 'drive', 'advance', 'pedal', 'vroom'];
const difficulties = {
baby: 30000,
easy: 7500,
medium: 5000,
hard: 2500
};
module.exports = class CarRaceCommand extends Command { module.exports = class CarRaceCommand extends Command {
constructor(client) { constructor(client) {
@@ -15,7 +21,7 @@ module.exports = class CarRaceCommand extends Command {
aliases: ['cars', 'race'], aliases: ['cars', 'race'],
group: 'games-mp', group: 'games-mp',
memberName: 'car-race', memberName: 'car-race',
description: 'Race a car against another user.', description: 'Race a car against another user or the AI.',
credit: [ credit: [
{ {
name: 'iStock', name: 'iStock',
@@ -185,7 +191,7 @@ module.exports = class CarRaceCommand extends Command {
args: [ args: [
{ {
key: 'opponent', key: 'opponent',
prompt: 'What user would you like to challenge?', prompt: 'What user would you like to challenge? To play against AI, choose me.',
type: 'user' type: 'user'
}, },
{ {
@@ -200,7 +206,6 @@ module.exports = class CarRaceCommand extends Command {
} }
async run(msg, { opponent, car }) { async run(msg, { opponent, car }) {
if (opponent.bot) return msg.reply('Bots may not be played against.');
if (opponent.id === msg.author.id) return msg.reply('You may not play against yourself.'); if (opponent.id === msg.author.id) return msg.reply('You may not play against yourself.');
const current = this.client.games.get(msg.channel.id); const current = this.client.games.get(msg.channel.id);
if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`);
@@ -210,33 +215,55 @@ module.exports = class CarRaceCommand extends Command {
path.join(__dirname, '..', '..', 'assets', 'images', 'car-race', 'cars', `${car}.png`) path.join(__dirname, '..', '..', 'assets', 'images', 'car-race', 'cars', `${car}.png`)
); );
let oppoCar; let oppoCar;
let difficulty;
try { try {
const available = cars.filter(car2 => car !== car2); const available = cars.filter(car2 => car !== car2);
await msg.say(`${opponent}, do you accept this challenge?`); if (opponent.bot) {
const verification = await verify(msg.channel, opponent); await msg.reply(`What difficulty do you want to use? Either ${list(Object.keys(difficulties), 'or')}.`);
if (!verification) { const difficultyFilter = res => {
this.client.games.delete(msg.channel.id); if (res.author.id !== msg.author.id) return false;
return msg.say('Looks like they declined...'); return Object.keys(difficulties).includes(res.content.toLowerCase());
} };
await msg.say(`${opponent}, what car do you want to be? Either ${list(available, 'or')}.`); const difficultyPick = await msg.channel.awaitMessages(difficultyFilter, {
const filter = res => { max: 1,
if (res.author.id !== opponent.id) return false; time: 30000
return available.includes(res.content.toLowerCase()); });
}; if (!difficultyPick.size) {
const p2Car = await msg.channel.awaitMessages(filter, { this.client.games.delete(msg.channel.id);
max: 1, return msg.say('Failed to pick difficulty. Aborted command.');
time: 30000 }
}); difficulty = difficultyPick.first().content.toLowerCase();
if (p2Car.size) { const oppoCarPick = available[Math.floor(Math.random() * available.length)];
const choice = p2Car.first().content.toLowerCase();
oppoCar = await loadImage( oppoCar = await loadImage(
path.join(__dirname, '..', '..', 'assets', 'images', 'car-race', 'cars', `${choice}.png`) path.join(__dirname, '..', '..', 'assets', 'images', 'car-race', 'cars', `${oppoCarPick}.png`)
); );
} else { } else {
const chosen = cars[Math.floor(Math.random() * cars.length)]; await msg.say(`${opponent}, do you accept this challenge?`);
oppoCar = await loadImage( const verification = await verify(msg.channel, opponent);
path.join(__dirname, '..', '..', 'assets', 'images', 'car-race', 'cars', `${chosen}.png`) if (!verification) {
); this.client.games.delete(msg.channel.id);
return msg.say('Looks like they declined...');
}
await msg.say(`${opponent}, what car do you want to be? Either ${list(available, 'or')}.`);
const filter = res => {
if (res.author.id !== opponent.id) return false;
return available.includes(res.content.toLowerCase());
};
const p2Car = await msg.channel.awaitMessages(filter, {
max: 1,
time: 30000
});
if (p2Car.size) {
const choice = p2Car.first().content.toLowerCase();
oppoCar = await loadImage(
path.join(__dirname, '..', '..', 'assets', 'images', 'car-race', 'cars', `${choice}.png`)
);
} else {
const chosen = cars[Math.floor(Math.random() * cars.length)];
oppoCar = await loadImage(
path.join(__dirname, '..', '..', 'assets', 'images', 'car-race', 'cars', `${chosen}.png`)
);
}
} }
let userCarSpaces = 0; let userCarSpaces = 0;
let oppoCarSpaces = 0; let oppoCarSpaces = 0;
@@ -283,10 +310,15 @@ module.exports = class CarRaceCommand extends Command {
}; };
const winner = await msg.channel.awaitMessages(turnFilter, { const winner = await msg.channel.awaitMessages(turnFilter, {
max: 1, max: 1,
time: 30000 time: opponent.bot ? difficulties[difficulty] : 30000
}); });
if (!winner.size) { if (!winner.size) {
if (lastTurnTimeout) { if (opponent.bot) {
oppoCarSpaces += 1;
lastRoundWinner = opponent;
if (lastTurnTimeout) lastTurnTimeout = false;
continue;
} else if (lastTurnTimeout) {
this.client.games.delete(msg.channel.id); this.client.games.delete(msg.channel.id);
return msg.say('Game ended due to inactivity.'); return msg.say('Game ended due to inactivity.');
} else { } else {