-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
14 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]' ); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters