From 126dce828d24310b2145626c7a7989f4cae668e4 Mon Sep 17 00:00:00 2001 From: TobenderZephyr Date: Thu, 22 Apr 2021 17:36:10 +0200 Subject: [PATCH] Cleanup (#19) * reformatting of skill checks table * Including own Random "generator", more refactoring --- __tests__/functions/CalculateQuality.js | 12 +++++ __tests__/functions/Roll.js | 14 ++--- commands/MeleeCombat.js | 61 --------------------- commands/RangedCombat.js | 64 ---------------------- commands/Roll.js | 8 +-- commands/Show.js | 13 ++--- commands/Talent.js | 72 +++++++------------------ functions/CalculateQuality.js | 15 ++++++ functions/CompareResults.js | 35 ++++++++++++ functions/CountOccurences.js | 4 +- functions/Random.js | 13 +++++ functions/Roll.js | 5 +- jest.config.js | 7 ++- jsconfig.json | 12 +++-- 14 files changed, 127 insertions(+), 208 deletions(-) create mode 100644 __tests__/functions/CalculateQuality.js delete mode 100644 commands/MeleeCombat.js delete mode 100644 commands/RangedCombat.js create mode 100644 functions/CalculateQuality.js create mode 100644 functions/CompareResults.js create mode 100644 functions/Random.js diff --git a/__tests__/functions/CalculateQuality.js b/__tests__/functions/CalculateQuality.js new file mode 100644 index 0000000..faaeeaa --- /dev/null +++ b/__tests__/functions/CalculateQuality.js @@ -0,0 +1,12 @@ +const {CalculateQuality} = require('@dsabot/CalculateQuality'); + +const TestValues = [ + [1,1], [2,1], [3,1], + [4,2], [5,2], [6,2], + [7,3], [8,3], [9,3], + [10,4],[11,4],[12,4], + [13,5],[14,5],[15,5] +]; +test.each(TestValues)('Retrieving Quality for %s', (input, output) => { + expect(CalculateQuality(input)).toBe(output); + }); \ No newline at end of file diff --git a/__tests__/functions/Roll.js b/__tests__/functions/Roll.js index 9dc7eb8..12d183f 100644 --- a/__tests__/functions/Roll.js +++ b/__tests__/functions/Roll.js @@ -1,14 +1,8 @@ -const { - roll -} = require('../../functions/Roll'); +const { roll } = require('@dsabot/Roll'); describe('rolling dice', () => { const expected = [1, 2, 3, 4, 5, 6]; - it('contain only numbers from 1 to 6', () => { - expect(roll(100, 6).dice).toEqual( - expect.arrayContaining(expected), - ); + test.each(expected)('contains only numbers from 1 to 6', (value) => { + expect(roll(200, 6).dice).toContain(value); }); -}); - -//todo: return sum \ No newline at end of file +}); \ No newline at end of file diff --git a/commands/MeleeCombat.js b/commands/MeleeCombat.js deleted file mode 100644 index 9883e41..0000000 --- a/commands/MeleeCombat.js +++ /dev/null @@ -1,61 +0,0 @@ -const globals = require('../globals'); -const db = globals.db; -const Random = require('random'); -/* - "meleeweapons": [{ - "amount": 1, - "equipped": true, - "handling": [], - "ruleelement": { - "id": "dolch", - "type": "meleeweapon" - } - }, { - "amount": 1, - "equipped": true, - "handling": [], - "ruleelement": { - "id": "waqqif", - "type": "meleeweapon" - } - }], -*/ -module.exports = { - name: 'melee', - description: 'Würfelt den Attackewert auf eine Nahkampfwaffe.', - aliases: ['nahkampf'], - usage: '', - needs_args: true, - - async exec(message, args) { - try { - db.find({ - user: message.author.tag, - }, function(err, docs) { - if (docs.length === 0) { - return message.reply(globals.Replies.find(r => r.id === 'NOENTRY').string); - } - else { - - Random.use(message.author.tag); - let dice = []; - const Weapon = globals.MeleeWeapons.find(weapon => weapon.id === args[0]); - - if(!Weapon) { return message.reply(globals.Replies.find(r => r.id === 'NO_SUCH_WEAPON').string);} - const DieModificator = Weapon.diemodificator; - let sum = DieModificator; - for (let i = 0; i < Weapon.dice; i++) { - dice.push(Random.int(1,6)); - } - dice.forEach(result => { - sum += result; - }); - message.reply('Deine 🎲: ' + dice.join(',') + '.\n' + Weapon.name + ' richtet ` ' + sum + ' ` Schaden an. (' + Weapon.dice + 'W6+' + Weapon.diemodificator +')'); - } - }); - } - catch (e) { - throw e; - } - }, -}; \ No newline at end of file diff --git a/commands/RangedCombat.js b/commands/RangedCombat.js deleted file mode 100644 index fba6606..0000000 --- a/commands/RangedCombat.js +++ /dev/null @@ -1,64 +0,0 @@ -const globals = require('../globals'); -const db = globals.db; -const Random = require('random'); -/* - "meleeweapons": [{ - "amount": 1, - "equipped": true, - "handling": [], - "ruleelement": { - "id": "dolch", - "type": "meleeweapon" - } - }, { - "amount": 1, - "equipped": true, - "handling": [], - "ruleelement": { - "id": "waqqif", - "type": "meleeweapon" - } - }], -*/ -module.exports = { - name: 'ranged', - description: 'Würfelt den Attackewert auf eine Nahkampfwaffe.', - aliases: ['fernkampf'], - usage: '', - needs_args: true, - - async exec(message, args) { - try { - db.find({ - user: message.author.tag, - }, function(err, docs) { - if (docs.length === 0) { - return message.reply(globals.Replies.find(r => r.id === 'NOENTRY').string); - } - else { - - Random.use(message.author.tag); - let dice = []; - /*for (i in docs[0].character.skills) { - if (docs[0].character.skills[i].id == args[0]) level = docs[0].character.skills[i].level; - } -*/ - const Weapon = globals.MeleeWeapons.find(weapon => weapon.id === args[0]); - if(!Weapon) { return message.reply(globals.Replies.find(r => r.id === 'NO_SUCH_WEAPON').string);} - const DieModificator = Weapon.diemodificator; - let sum = DieModificator; - for (let i = 0; i < Weapon.dice; i++) { - dice.push(Random.int(1,6)); - } - dice.forEach(result => { - sum += result; - }); - message.reply('Du schlägst mit ' + Weapon.name + ' zu. (' + Weapon.dice + 'W6+' + Weapon.diemodificator +')\nDeine 🎲: ' + dice.join(',') + '.\n' + 'Dein Angriff macht **' + sum + '** Schaden.'); - } - }); - } - catch (e) { - throw e; - } - }, -}; \ No newline at end of file diff --git a/commands/Roll.js b/commands/Roll.js index c2dfec0..0fb4f91 100644 --- a/commands/Roll.js +++ b/commands/Roll.js @@ -12,15 +12,11 @@ module.exports = { async exec(message, args) { let params = args.join('').split(globals.DiceRegex); if ( params.length >= 2 ) { - let bonus = 0; + const Bonus = params[2] || 0; const numberOfDice = parseInt( params[0] ); const diceValues = parseInt( params[1] ); - if ( params.length == 3 ) { - bonus = parseInt( params[2] ); - } - const result = roll( numberOfDice, diceValues, message.author.tag ); - message.reply(`${findMessage('ROLL')} ${result.dice.join(', ')} (Gesamt: ${result.sum} + ${bonus} = ${result.sum + bonus})` ); + message.reply(`${findMessage('ROLL')} ${result.dice.join(', ')} (Gesamt: ${result.sum} + ${Bonus} = ${result.sum + Bonus})` ); } }, }; \ No newline at end of file diff --git a/commands/Show.js b/commands/Show.js index aa2c2a0..b754184 100644 --- a/commands/Show.js +++ b/commands/Show.js @@ -20,14 +20,15 @@ module.exports = { return message.reply(findMessage('NOENTRY')); } else { - let gender; - if (docs[0].character.sex == 'female') { gender = '♀️'; } - else { gender = '♂️'; } + const Character = docs[0].character; + let Gender; + if (Character.sex == 'female') { Gender = '♀️'; } + else { Gender = '♂️'; } const Reply = new Discord.MessageEmbed(); Reply.setColor('#0099ff'); - Reply.setTitle(gender + ' ' + docs[0].character.name); - Reply.setDescription(docs[0].character.age + ' Jahre, ' + docs[0].character.race + '/' + docs[0].character.culture); - Reply.addField(docs[0].character.professionname, docs[0].character.xp.startinglevel); + Reply.setTitle(`${Gender} ${Character.name}`); + Reply.setDescription(`${Character.age} Jahre, ${Character.race}/${Character.culture}`); + Reply.addField(Character.professionname, Character.xp.startinglevel); message.reply( Reply ); } diff --git a/commands/Talent.js b/commands/Talent.js index d4c4404..220c76a 100644 --- a/commands/Talent.js +++ b/commands/Talent.js @@ -3,7 +3,9 @@ const Discord = require('discord.js'); const db = globals.db; const { roll } = require('@dsabot/Roll'); const { findMessage } = require('@dsabot/findMessage'); -const { getSkill } = require('@dsabot/getSkill') +const { getSkill } = require('@dsabot/getSkill'); +const { CalculateQuality } = require('@dsabot/CalculateQuality'); +const { CompareResults } = require('@dsabot/CompareResults'); module.exports = { name: 'talent', description: ' Du machst eine Fertigkeitsprobe.\n' + @@ -42,10 +44,7 @@ module.exports = { const Reply = new Discord.MessageEmbed(); Reply.addFields({ name: `Du würfelst auf das Talent **${Skill.Name}** (Stufe ${Skill.Level} + ${Bonus})`, - value:`\`\`\`\u200B\u2003\u2003\u2003\u2003${Attributes.map(a => a.Name).join('\u2003\u2003')}\n` + - `\u200B✊🏻\u2003\u2003${Attributes.map(a => a.Level).join('\u2003\u2003')}\n` + - `\u200B🎲\u2003\u2003${DiceThrow.join('\u2003\u2003')}\n` + - `\u200B-\u2003\u2003\u2003${PointsUsed.join('\u2003\u2003')}\`\`\``, + value: CreateTable({Attributes: Attributes, Throws: DiceThrow, PointsUsed: PointsUsed}), inline: false }); if (Fumbles >= 2) { @@ -65,13 +64,13 @@ module.exports = { } else if (Passed < 3) { Reply.addFields({ name: findMessage('TITLE_FAILURE'), - value: `nur ${Passed}/3 Proben erfolgreich. 😪`, + value: `${(Passed === 0) ? 'Keine Probe' : `nur ${Passed}/3 Proben`} erfolgreich. 😪`, inline: false }); } else { Reply.addFields({ name: findMessage('TITLE_SUCCESS'), - value: `Dein Bonus: ${PointsRemaining}/${Skill.Level} (QS${CalculateQuality(PointsRemaining)})`, + value: `Dein verbleibender Bonus: ${PointsRemaining}/${Skill.Level} (QS${CalculateQuality(PointsRemaining)})`, inline: false }); } @@ -85,49 +84,18 @@ module.exports = { }, }; -const CalculateQuality = (PointsAvailable = 0) => { - if (PointsAvailable<=3) return 1; - else if (PointsAvailable>3&&PointsAvailable<=6) return 2; - else if (PointsAvailable>6&&PointsAvailable<=9) return 3; - else if (PointsAvailable>9&&PointsAvailable<=12) return 4; - else if (PointsAvailable>12&&PointsAvailable<=15) return 5; - else if (PointsAvailable>15) return 6; -}; - -const CompareResults = (Throws = [], AttributeLevels = [8,8,8], Bonus = 0, PointsRemaining= 0) => { - - let Passed = 0; - let Fumbles = 0; - let CriticalHit = 0; - let AllPointsUsed = []; - - for (let i = 0; i < Throws.length; i++) { - let PointsUsed = 0; - if (Math.floor(AttributeLevels[i] + Bonus) >= Throws[i]) { - Passed++; - } else if (Math.floor(AttributeLevels[i] + PointsRemaining + Bonus) >= Throws[i]) { - Passed++; - PointsUsed = (Throws[i] - Bonus - AttributeLevels[i]); - PointsRemaining -= PointsUsed; - } - else { - // We need to use all our points, so that next die/dice - // would not return a 'Passed'. - PointsUsed = PointsRemaining; - PointsRemaining -= PointsUsed; - } - if(Throws[i] == 1) {CriticalHit++;} - if(Throws[i] == 20) {Fumbles++;} - AllPointsUsed.push(PointsUsed); - } - return { - Passed: Passed, - CriticalHit: CriticalHit, - Fumbles: Fumbles, - PointsUsed: AllPointsUsed, - PointsRemaining: PointsRemaining }; -}; - function Pad(Number = 0) { - return Number.toString().padStart(2, '0'); -} \ No newline at end of file + return Number.toString().padStart(1, ' '); +} + +const CreateTable = ({Attributes: Attributes, Throws: Throws, PointsUsed: PointsUsed}) => { + return ` + \`\`\` + ${' '.padEnd(15)} ${Attributes.map(attr => `${attr.Name}`.padStart(5)).join('\t|\t')}\t| + ${'Dein Wert'.padEnd(15)} ${Attributes.map(attr => `${attr.Level}`.padStart(5)).join('\t|\t')}\t| + ${'Dein Wurf'.padEnd(15)} ${Throws.map(Throw => `${Throw}`.padStart(5)).join('\t|\t')}\t| + ${'Abzüge'.padEnd(15)} ${PointsUsed.map(Points => `${Points}`.replace(0,'--').padStart(5)).join('\t|\t')}\t| + ${'Gesamt'.padEnd(15)} ${PointsUsed.reduce((acc,cur) => acc+cur).toString().padStart(5)} + \`\`\` + `; +}; \ No newline at end of file diff --git a/functions/CalculateQuality.js b/functions/CalculateQuality.js new file mode 100644 index 0000000..32699b2 --- /dev/null +++ b/functions/CalculateQuality.js @@ -0,0 +1,15 @@ +const CalculateQuality = (PointsAvailable = 0) => { + if (PointsAvailable <= 3) + return 1; + else if (PointsAvailable > 3 && PointsAvailable <= 6) + return 2; + else if (PointsAvailable > 6 && PointsAvailable <= 9) + return 3; + else if (PointsAvailable > 9 && PointsAvailable <= 12) + return 4; + else if (PointsAvailable > 12 && PointsAvailable <= 15) + return 5; + else if (PointsAvailable > 15) + return 6; +}; +module.exports = { CalculateQuality }; diff --git a/functions/CompareResults.js b/functions/CompareResults.js new file mode 100644 index 0000000..578d546 --- /dev/null +++ b/functions/CompareResults.js @@ -0,0 +1,35 @@ +const CompareResults = (Throws = [], AttributeLevels = [8, 8, 8], Bonus = 0, PointsRemaining = 0) => { + + let Passed = 0; + let Fumbles = 0; + let CriticalHit = 0; + let AllPointsUsed = []; + + for (let i = 0; i < Throws.length; i++) { + let PointsUsed = 0; + if (Math.floor(AttributeLevels[i] + Bonus) >= Throws[i]) { + Passed++; + } else if (Math.floor(AttributeLevels[i] + PointsRemaining + Bonus) >= Throws[i]) { + Passed++; + PointsUsed = (Throws[i] - Bonus - AttributeLevels[i]); + PointsRemaining -= PointsUsed; + } + else { + // We need to use all our points, so that next die/dice + // would not return a 'Passed'. + PointsUsed = PointsRemaining; + PointsRemaining -= PointsUsed; + } + if (Throws[i] == 1) { CriticalHit++; } + if (Throws[i] == 20) { Fumbles++; } + AllPointsUsed.push(PointsUsed); + } + return { + Passed: Passed, + CriticalHit: CriticalHit, + Fumbles: Fumbles, + PointsUsed: AllPointsUsed, + PointsRemaining: PointsRemaining + }; +}; +module.exports = { CompareResults }; diff --git a/functions/CountOccurences.js b/functions/CountOccurences.js index 1f7833f..2f98f8f 100644 --- a/functions/CountOccurences.js +++ b/functions/CountOccurences.js @@ -2,6 +2,4 @@ const CountOccurences = (arr, value) => { return arr.filter((v) => (v === value)).length; }; -module.exports = { CountOccurences }; - -//console.log(countOccurrences([1,2,3,4,3,2,3,3,2],2)); +module.exports = { CountOccurences }; \ No newline at end of file diff --git a/functions/Random.js b/functions/Random.js new file mode 100644 index 0000000..932fdd5 --- /dev/null +++ b/functions/Random.js @@ -0,0 +1,13 @@ +const Random = {int, use}; + +function int (min, max) { + if (!min || !max) { return; } + //return Math.floor(Math.random() * (Math.floor(max) - Math.ceil(min))) + min; + return Math.floor(Math.random() * (Math.floor(max) - Math.ceil(min) + 1)) + min; + } + +function use (str) { + return true; +} + +module.exports = { Random }; \ No newline at end of file diff --git a/functions/Roll.js b/functions/Roll.js index d840375..8bf1252 100644 --- a/functions/Roll.js +++ b/functions/Roll.js @@ -1,4 +1,6 @@ -const Random = require('random'); +const { Random } = require("@dsabot/Random"); +//const Random = require('random'); + const roll = (numberOfDice, numberOfEyes, tag) => { let dice = []; let sum = 0; @@ -13,3 +15,4 @@ const roll = (numberOfDice, numberOfEyes, tag) => { return { dice, sum }; }; module.exports = { roll }; + diff --git a/jest.config.js b/jest.config.js index 0691e30..7ede3f6 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,3 +1,8 @@ module.exports = { - testEnvironment: 'node' + testEnvironment: 'node', + moduleNameMapper: { + "@dsabot/(.*)": "/functions/$1", + "@Commands/(.*)": "/commands/$1", + "@Root/(.*)": "/$1", + } }; \ No newline at end of file diff --git a/jsconfig.json b/jsconfig.json index c967d43..a4fad4e 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -1,9 +1,13 @@ { "compilerOptions": { - "baseUrl": "./", + "baseUrl": ".", "paths": { - "@dsabot/*": ["functions/*"], + "@dsabot/*": ["./functions/*"], "@globals": ["./globals"] - } - } + }, + }, + "exclude": ["node_modules"], + "typeAcquisition": { + "exclude": [ "dotenv" ] + }, } \ No newline at end of file