Add leaderboard

This commit is contained in:
Loïc Bersier 2019-07-28 23:55:59 +02:00
parent 1aba9f9a81
commit df3c7da255
3 changed files with 181 additions and 77 deletions

View file

@ -1,78 +1,137 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const guessLeaderboard = require('../../models').guessLeaderboard;
class guessCommand extends Command { const { MessageEmbed } = require('discord.js');
constructor() {
super('guess', { class guessCommand extends Command {
aliases: ['guess'], constructor() {
category: 'minigame', super('guess', {
description: { aliases: ['guess'],
content: 'Guess the number ( Say "stop" to stop playing )', category: 'minigame',
usage: '', args: [
examples: [''] {
} id: 'leaderboard',
}); type: 'flag',
} match: 'flag',
flag: ['--leaderboard', '--top']
async exec(message) { },
message.reply('1. Easy ( 0 - 100 )\n2. Medium ( 0 - 1000 )\n3. Hard ( 0 - 10000 )'); ],
const filter = m => m.content && m.author.id == message.author.id; description: {
message.channel.awaitMessages(filter, {time: 10000, max: 1, errors: ['time'] }) content: 'Guess the number ( Say "stop" to stop playing )',
.then(messages => { usage: '',
let max; examples: ['']
}
if (messages.map(messages => messages.content)[0] == 1) { });
max = 100; }
} else if (messages.map(messages => messages.content)[0] == 2) {
max = 1000; async exec(message, args) {
} else if (messages.map(messages => messages.content)[0] == 3) {
max = 10000; /*
} else { * TODO:
return message.reply('This isin\'t a valid difficulty number! Please try again.'); *
} * Make leaderboard look and work better
* Separate by categories
let secretnumber = Math.floor((Math.random() * max) + 1); *
let numberTry = 0; */
console.log(secretnumber); if (args.leaderboard) {
const leaderboard = await guessLeaderboard.findAll({order: ['try']});
message.reply('What is the number?'); let top = [];
message.channel.awaitMessages(filter, {max: 1}) let leaderboardEmbed = new MessageEmbed()
.then(input => { .setColor('#0099ff')
checkNumber(input.map(input => input.content)[0]); .setTitle('Guess leaderboard');
}); for (let i = 0; i < leaderboard.length; i++) {
this.client.users.fetch(leaderboard[i].get('memberID'))
function tryAgain (input) { .then(user => {
if (input != secretnumber) { let body = `**${user.username}**\nTry: ${leaderboard[i].get('try')}`;
if (input > secretnumber) { top.push(body);
message.reply('Its less!\nWhat is the number?');
} else if (input < secretnumber) { if (leaderboard[i].get('difficulty') == 'Easy') {
message.reply('Its more!\nWhat is the number?'); leaderboardEmbed.addField('Easy', body, true);
} } else if (leaderboard[i].get('difficulty') == 'Normal') {
} leaderboardEmbed.addField('Normal', body, true);
message.channel.awaitMessages(filter, {max: 1}) } else if (leaderboard[i].get('difficulty') == 'Hard') {
.then(input => { leaderboardEmbed.addField('Hard', body, true);
checkNumber(input.map(input => input.content)[0]); }
});
} if (i + 1 == leaderboard.length) {
return message.channel.send(leaderboardEmbed);
function checkNumber (input) { }
numberTry++; });
if (input.toLowerCase() == 'stop') { }
return message.reply('Ok, let\'s stop playing :('); return;
} else if (input != secretnumber) { }
tryAgain(input);
} else { message.reply('1. Easy ( 0 - 100 )\n2. Normal ( 0 - 1000 )\n3. Hard ( 0 - 10000 )');
if (numberTry > 1) { const filter = m => m.content && m.author.id == message.author.id;
return message.reply(`Congratulations! You won! It took you ${numberTry} turns!`); message.channel.awaitMessages(filter, {time: 10000, max: 1, errors: ['time'] })
} else { .then(messages => {
return message.reply('Congratulations! You won! It took you 1 Turn!'); let max;
} let difficulty;
}
} if (messages.map(messages => messages.content)[0] == 1) {
}) max = 100;
.catch(() => { difficulty = 'Easy';
return message.reply('Timed out'); } else if (messages.map(messages => messages.content)[0] == 2) {
}); max = 1000;
} difficulty = 'Normal';
} } else if (messages.map(messages => messages.content)[0] == 3) {
max = 10000;
difficulty = 'Hard';
} else {
return message.reply('This isin\'t a valid difficulty number! Please try again.');
}
let secretnumber = Math.floor((Math.random() * max) + 1);
let numberTry = 0;
console.log(secretnumber);
message.reply('What is the number?');
message.channel.awaitMessages(filter, {max: 1})
.then(input => {
checkNumber(input.map(input => input.content)[0]);
});
function tryAgain (input) {
if (input != secretnumber) {
if (input > secretnumber) {
message.reply('Its less!\nWhat is the number?');
} else if (input < secretnumber) {
message.reply('Its more!\nWhat is the number?');
}
}
message.channel.awaitMessages(filter, {max: 1})
.then(input => {
checkNumber(input.map(input => input.content)[0]);
});
}
async function checkNumber (input) {
numberTry++;
if (input.toLowerCase() == 'stop') {
return message.reply('Ok, let\'s stop playing :(');
} else if (input != secretnumber) {
tryAgain(input);
} else {
const leaderboard = await guessLeaderboard.findOne({where: {memberID: message.author.id, difficulty: difficulty}});
if (!leaderboard) {
const body = {memberID: message.author.id, try: numberTry, difficulty:difficulty};
await guessLeaderboard.create(body);
} else {
const body = {memberID: message.author.id, try: numberTry, difficulty:difficulty};
await guessLeaderboard.update(body, {where: {memberID: message.author.id, difficulty: difficulty}});
}
if (numberTry > 1) {
return message.reply(`Congratulations! You won! It took you ${numberTry} turns!`);
} else {
return message.reply('Congratulations! You won! It took you 1 Turn!');
}
}
}
})
.catch(() => {
return message.reply('Timed out');
});
}
}
module.exports = guessCommand; module.exports = guessCommand;

View file

@ -0,0 +1,33 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('guessLeaderboards', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
memberID: {
type: Sequelize.BIGINT
},
try: {
type: Sequelize.INTEGER
},
difficulty: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('guessLeaderboards');
}
};

View file

@ -0,0 +1,12 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const guessLeaderboard = sequelize.define('guessLeaderboard', {
memberID: DataTypes.INTEGER,
try: DataTypes.INTEGER,
difficulty: DataTypes.STRING
}, {});
guessLeaderboard.associate = function(models) {
// associations can be defined here
};
return guessLeaderboard;
};