fix attribute checks (#34)

This commit is contained in:
2021-05-01 23:21:33 +02:00
committed by GitHub
parent d45e4faad6
commit dc746276ab
2 changed files with 203 additions and 116 deletions

View File

@ -0,0 +1,115 @@
require('module-alias/register');
const rewire = require('rewire');
const rewireUtils = rewire('@Commands/Attribute');
const Attribute = require('@Commands/Attribute');
const HandleNamedAttributes = rewireUtils.__get__('HandleNamedAttributes');
const getAttributeLevel = rewireUtils.__get__('getAttributeLevel');
const getAttribute = rewireUtils.__get__('getAttribute');
const handleAttributeCheck = rewireUtils.__get__('handleAttributeCheck');
test('getAttribute should return Object', () => {
const obj = { id: 'mut', kuerzel: 'MU', name: 'Mut' };
expect(getAttribute('KK')).toEqual(
expect.objectContaining({
id: expect.any(String),
kuerzel: expect.any(String),
name: expect.any(String),
})
);
expect(getAttribute('MU')).toEqual(obj);
expect(getAttribute('mut')).toEqual(obj);
});
it('should return undefined', () => {
expect(getAttribute()).toBeUndefined();
});
it('returns a number ', () => {
expect(getAttributeLevel({ attributes: [{ id: 'mut', level: 8 }] }, { id: 'mut' })).toBe(8);
});
it('should return an object', () => {
const Character = {
attributes: [{ id: 'mut', level: 8 }],
};
expect(HandleNamedAttributes({ Character: Character, args: ['mut'] })).toEqual({
Name: 'Mut',
Level: 8,
});
});
it('should return with no errors', () => {
const reply = jest.fn(str => str);
const message = {
reply: reply,
author: {
tag: 'test',
},
};
const docs = [{ character: { attributes: [{ id: 'mut', level: 8 }] } }];
const args = ['mut'];
expect(handleAttributeCheck(docs, { message, args })).toEqual(expect.any(String));
});
it('should return with no errors', () => {
const reply = jest.fn(str => str);
const message = {
reply: reply,
author: {
tag: 'test',
},
};
const docs = [{ character: { attributes: [{ id: 'mut', level: 8 }] } }];
const args = ['MU'];
expect(handleAttributeCheck(docs, { message, args })).toEqual(expect.any(String));
});
it('should return with no errors', () => {
const reply = jest.fn(str => str);
const message = {
reply: reply,
author: {
tag: 'test',
},
};
const docs = [{ character: { attributes: [{ id: 'mut', level: 8 }] } }];
const args = [8];
for (let i = 0; i < 30; i++) {
expect(handleAttributeCheck(docs, { message, args })).toEqual(expect.any(String));
}
});
it('should return with no errors', () => {
const reply = jest.fn(str => str);
const message = {
reply: reply,
author: {
tag: 'test',
},
};
const docs = [{ character: { attributes: [{ id: 'mut', level: 8 }] } }];
const args = [8, '+2'];
for (let i = 0; i < 30; i++) {
expect(handleAttributeCheck(docs, { message, args })).toEqual(expect.any(String));
}
});
it('should return empty', () => {
const message = { author: { tag: 'test' } };
const args = ['MU'];
expect(Attribute.exec(message, args)).toEqual(expect.objectContaining({}));
});
/*
const reply = jest.fn(str => str);
const message = {
reply: reply,
author: {
tag: 'test',
},
};
const docs = [{ character: { attributes: [{ id: 'mut', level: 8 }] } }];
const args = ['MU'];
const run = [];
for (let i = 1; i < 100; i++) {
run.push(i);
}
test.each(run)('run multiple times (%s)', () => {
expect(handleAttributeCheck(docs, { message, args })).toEqual(expect.any(String));
});
*/

View File

@ -1,116 +1,88 @@
const globals = require('../globals'); require('module-alias/register');
const { CountOccurences } = require('@dsabot/CountOccurences'); const globals = require('../globals');
const Random = require('random'); const db = globals.db;
const db = globals.db; const { roll } = require('@dsabot/Roll');
const { findMessage } = require('@dsabot/findMessage');
module.exports = { const { CompareResults } = require('@dsabot/CompareResults');
name: 'attribute',
description: '', function handleAttributeCheck(docs, { message, args }) {
aliases: ['ap', 'ep'], let Attribute = isNaN(args[0])
usage: '<Eigenschaft> / <Eigenschaftswert>', ? HandleNamedAttributes({ Character: docs[0].character, args: args })
needs_args: true, : null;
async exec(message, args) { let Level = Attribute ? Attribute.Level : args[0] || 8;
let Attribute; let Bonus = parseInt(args[1]) || 0;
let AttributeName; let dice = roll(2, 20, message.author.tag).dice;
let Level = 8; const Result = CompareResults(dice, [Level, Level], Bonus);
await db.find(
{ // handle crits
user: message.author.tag, if (Result.CriticalHit === 2) {
}, return message.reply(
async (err, docs) => { `${findMessage('TITLE_CRIT_SUCCESS')}\n${findMessage('MSG_CRIT_SUCCESS')}`
// user calls with text, let's look him up in the database. );
if (isNaN(args[0])) { }
Attribute = HandleNamedAttributes({ if (Result.Fumbles === 2) {
Character: docs[0].character, return message.reply(
args: args, `${findMessage('TITLE_CRIT_FAILURE')}\n${findMessage('MSG_CRIT_FAILURE')}`
}); );
AttributeName = Attribute.Name; }
Level = Attribute.Level;
} else { // every
Level = args[0]; if (dice[0] + Bonus > Level) {
} return message.reply(
Random.use(message.author.tag); `Du hast die Probe (Stufe ${Level}) leider nicht bestanden 😢.\nDeine 🎲: ${dice[0]} ${
Bonus ? `+${Bonus}` : ''
const dice = []; }`
dice.push(Random.int(1, 20)); );
if (dice[0] === 1 || dice[0] === 20) { }
dice.push(Random.int(1, 20)); if (Attribute) {
} return message.reply(
// handle crits `Du hast die Probe auf ${Attribute.Name} (Stufe ${
if (CountOccurences(dice, 1) === 2) { Attribute.Level
message.reply( }) bestanden. Deine 🎲: ${dice[0]} ${Bonus ? `+${Bonus}` : ''}`
`Du hast einen kritischen Erfolg erzielt (${dice.join(', ')})! 🎉🥳🎆` );
); }
return;
} else if (CountOccurences(dice, 20) === 2) { return message.reply(
message.reply( `Du hast die Probe (Stufe ${Level}) bestanden. Deine 🎲: ${dice[0]} ${
'Du hast einen Patzer (' + Bonus ? `+${Bonus}` : ''
dice.join(', ') + }`
')! 😭 Viel Erfolg beim nächsten mal!' );
); }
return;
} function HandleNamedAttributes({ Character: Character = [], args: args = [] } = {}) {
if ( let Attribute = getAttribute(args[0]);
(dice.length == 2 && dice[0] != 20 && dice[1] <= Level) || let Level = getAttributeLevel(Character, Attribute) || 8;
(dice.length == 1 && dice[0] <= Level)
) { return {
if (AttributeName) { Name: Attribute.name,
message.reply( Level: Level,
'Du hast die Probe auf ' + };
AttributeName + }
' (Stufe ' +
Level + function getAttributeLevel(Character = {}, Attribute = {}) {
') bestanden.\n' + return Character.attributes.find(attribute => attribute.id === Attribute.id).level;
'Deine 🎲: ' + }
dice.join(', ')
); function getAttribute(attribute = '') {
} else { return attribute.length === 2
message.reply( ? globals.Werte.find(a => a.kuerzel === attribute.toUpperCase())
'Du hast die Probe (Stufe ' + : globals.Werte.find(a => a.name.toLowerCase() === attribute.toLowerCase());
Level + }
') bestanden.\n' +
'Deine 🎲: ' + module.exports = {
dice.join(', ') name: 'attribute',
); description: '',
} aliases: ['ap', 'ep'],
} else if (AttributeName) { usage: '<Eigenschaft> / <Eigenschaftswert>',
message.reply( needs_args: true,
'Du hast die Probe auf ' + async exec(message, args) {
AttributeName + db.find({ user: message.author.tag }, (err, docs) => {
' (Stufe ' + if (err) {
Level + message.reply(findMessage('ERROR'));
') leider nicht bestanden 😢.\n' + throw new Error(err);
'Deine 🎲: ' + }
dice.join(', ') handleAttributeCheck(docs, { message, args });
); });
} else { },
message.reply( };
`Du hast die Probe (Stufe ${Level}) leider nicht bestanden 😢.\nDeine 🎲: ${dice.join(
', '
)}`
);
}
}
);
},
};
function HandleNamedAttributes({ Character: Character = [], args: args = [] } = {}) {
let Attribute = getAttribute(args[0]);
let Level = getAttributeLevel(Character, Attribute) || 8;
return {
Name: Attribute.name,
Level: Level,
};
}
function getAttributeLevel(Character = {}, Attribute = {}) {
return Character.attributes.find(attribute => attribute.id === Attribute.id).level;
}
function getAttribute(attribute = '') {
return attribute.length === 2
? globals.Werte.find(a => a.kuerzel === attribute.toUpperCase())
: globals.Werte.find(a => a.name.toLowerCase() === attribute.toLowerCase());
}