Add textdiff to typing-test

This commit is contained in:
Dragon Fire
2021-02-08 22:03:53 -05:00
parent 59c1f3539b
commit 886a7b61ba
3 changed files with 21 additions and 3 deletions
+7 -2
View File
@@ -2,7 +2,7 @@ const Command = require('../../structures/Command');
const { createCanvas, registerFont } = require('canvas');
const { stripIndents } = require('common-tags');
const path = require('path');
const { fetchHSUserDisplay } = require('../../util/Util');
const { textDiff, fetchHSUserDisplay } = require('../../util/Util');
const words = require('../../assets/json/word-list');
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-Regular.ttf'), { family: 'Noto' });
@@ -38,7 +38,12 @@ module.exports = class TypingTestCommand extends Command {
await this.client.redis.set('typing-test-user', msg.author.id);
}
if (!msgs.size) return msg.reply('Sorry! You lose!');
if (msgs.first().content.toLowerCase() !== sentence) return msg.reply('Sorry! You made a typo, so you lose!');
if (msgs.first().content.toLowerCase() !== sentence) {
return msg.reply(stripIndents`
Sorry! You made a typo, so you lose!
${textDiff(msgs.first().content.toLowerCase(), sentence)}
`);
}
const wpm = (sentence.length / 5) / ((newScore / 1000) / 60);
return msg.reply(stripIndents`
Nice job! 10/10! You deserve some cake! (Took ${newScore / 1000} seconds, ${Math.round(wpm)} WPM)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "129.0.1",
"version": "129.0.2",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {
+13
View File
@@ -181,6 +181,19 @@ module.exports = class Util {
return ((r << 16) | (g << 8) | b).toString(16);
}
static textDiff(oldText, newText) {
const changed = [];
return oldText.split('').map((char, i) => {
if (char === newText.charAt(i)) {
const chars = changed.length ? `**${changed.join('')}**${char}` : char;
if (changed.length) changed = [];
return chars;
}
changed.push(newText.charAt(i));
return '';
}).join('');
}
static magikToBuffer(magik) {
return new Promise((res, rej) => {
magik.toBuffer((err, buffer) => {