Skip to content

Commit

Permalink
Add roller to generate roll results
Browse files Browse the repository at this point in the history
Added classic and WoD roll function.
Added `Result` object to hold the roll results.
Added stringifier and normalizer functions for the roll result.
Renamed tests folder `/normalizers` to `normalizer`.
  • Loading branch information
edloidas committed May 28, 2017
1 parent 7299787 commit e6e5be8
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Each grammar is an array of RegExps
module.exports = {
simple: [
/^(\d+)(?:\s+(\d+)(?:\s+([+-]?\d+))?)?$/, // 1 20 4 || 1 20 +4 5 || 1 20 -4 5 20
/^(\d+)(?:\s+(\d+)(?:\s+([+-]?\d+))?)?$/, // 1 20 4 || 1 20 +4 5 || 1 20 -4 5 20
],
classic: [
/^(\d*)(?:[dD])(\d+)([+-]\d+)?$/, // d20 || 1d20 || d20+4 || 1d20-4
Expand Down
3 changes: 3 additions & 0 deletions src/normalizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const fixInvalid = backup => value => ( isPositiveInteger( value ) ? value : bac
// normalizeInteger :: Any -> Number
const normalizeInteger = value => ( Number.isInteger( value ) ? value : 0 );

// normalizeRollResult :: Any -> Number
const normalizeRollResult = fixInvalid( 1 );

// normalizeTop :: Number -> Number -> Number
const normalizeTop = max => top => Math.min( fixInvalid( Number.MAX_SAFE_INTEGER )( top ), max );
Expand Down Expand Up @@ -36,6 +38,7 @@ module.exports = {
isAbsent,
fixInvalid,
normalizeInteger,
normalizeRollResult,
normalizeTop,
normalizeBottom,
normalizeWodBorders,
Expand Down
23 changes: 23 additions & 0 deletions src/object/Result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { resultNotation } = require( '../stringifier' );
/**
* A class that represents a dice roll result
* @class
* @classdesc A class that represents a dice roll result
* @since v2.0.0
* @param {String} notation - A roll notation
* @param {Number} value - A numeric representation of roll result, like total summ or success count
* @param {Array} rolls - An array of rolls dome
* @see Roll
* @see WodRoll
*/
function Result( notation, value, rolls ) {
this.notation = notation;
this.value = value;
this.rolls = rolls;
}

Result.prototype.toString = function toString() {
return resultNotation( this );
};

module.exports = Result;
44 changes: 44 additions & 0 deletions src/roller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { randomRoll } = require( './random' );
const { normalizeRollResult } = require( './normalizer' );
const Result = require( './object/Result' );

function rollClassic( roll ) {
const { dice, count, modifier } = roll;

const rolls = new Array( count ).fill( randomRoll( dice ));
const summ = rolls.reduce(( prev, curr ) => prev + curr, 0 );
const result = normalizeRollResult( summ + modifier );

return new Result( roll.toString(), result, rolls );
}

function rollWod( roll ) {
const { dice, count, again, success, fail } = roll;

const rolls = [];

let i = count;
while ( i > 0 ) {
const value = randomRoll( dice );
rolls.push( value );
if ( value !== dice || !again ) {
i -= 1;
}
}

const result = rolls.reduce(( suc, val ) => {
if ( val >= success ) {
return suc + 1;
} else if ( val <= fail ) {
return suc - 1;
}
return val;
}, 0 );

return new Result( roll.toString(), result, rolls );
}

module.exports = {
rollClassic,
rollWod,
};
6 changes: 6 additions & 0 deletions src/stringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ function wodNotation( roll ) {
return `${ count }d${ roll.dice }${ again }>${ roll.success }f${ roll.fail }`;
}

function resultNotation( result ) {
const { notation, value, rolls } = result;
return `(${ notation }) ${ value } [${ rolls }]`;
}

module.exports = {
simpleNotation,
classicNotation,
wodNotation,
resultNotation,
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions test/object/result/result.generation.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Result = require( '../../../src/object/Result' );

describe( 'Result generation:', () => {
test( 'Should generate valid result.', () => {
const result = { notation: '4d10+1', value: 21, rolls: [ 10, 2, 7, 1 ] };
expect( new Result( '4d10+1', 21, [ 10, 2, 7, 1 ])).toEqual( result );
});
});
7 changes: 7 additions & 0 deletions test/object/result/result.toString.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Result = require( '../../../src/object/Result' );

describe( 'Result.toString:', () => {
test( 'Should generate notation `(4d10+1) 21 [10,2,7,1]`.', () => {
expect( new Result( '4d10+1', 21, [ 10, 2, 7, 1 ]).toString()).toEqual( '(4d10+1) 21 [10,2,7,1]' );
});
});
5 changes: 5 additions & 0 deletions test/stringifier/stringifier.result.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { testResultNotation } = require( '../util' );

describe( 'Roll result notation:', () => {
testResultNotation( '(4d10+1) 21 [10,2,7,1]', '4d10+1', 21, [ 10, 2, 7, 1 ]);
});
10 changes: 9 additions & 1 deletion test/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Roll = require( '../src/object/Roll' );
const WodRoll = require( '../src/object/WodRoll' );
const { simpleNotation, classicNotation, wodNotation } = require( '../src/stringifier' );
const Result = require( '../src/object/Result' );
const { simpleNotation, classicNotation, wodNotation, resultNotation } = require( '../src/stringifier' );

const validRoll = regexp => ( roll ) => {
test( `Should parse '${ roll }' roll.`, () => {
Expand Down Expand Up @@ -44,6 +45,12 @@ function testWodNotation( notation, ...args ) {
});
}

function testResultNotation( notation, ...args ) {
test( `Should generate '${ notation }' from parameters '${ stringifyArgs( args ) }'.`, () => {
expect( resultNotation( new Result( ...args ))).toBe( notation );
});
}

const validRollParse = parser => ( roll ) => {
test( `Should parse '${ roll }' roll.`, () => {
expect( parser( roll )).toBeTruthy();
Expand Down Expand Up @@ -73,6 +80,7 @@ module.exports = {
testSimpleNotation,
testClassicNotation,
testWodNotation,
testResultNotation,
validRollParse,
invalidRollParse,
testParse,
Expand Down

0 comments on commit e6e5be8

Please sign in to comment.