Skip to content

Commit

Permalink
Keep drop count (#18)
Browse files Browse the repository at this point in the history
* Add keep/drop count to rendered expression output.

* Fix codacy linting issue.
  • Loading branch information
tom-wolfe authored Oct 10, 2018
1 parent ffcfd5d commit 97f5d43
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 16 deletions.
4 changes: 2 additions & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ module.exports = function (config) {
],
customLaunchers: {
chromeDebugging: {
base: 'Chrome',
flags: [ '--remote-debugging-port=9333' ]
base: "Chrome",
flags: [ "--remote-debugging-port=9333" ]
},
chromeTravisCi: {
base: "Chrome",
Expand Down
17 changes: 12 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dice-typescript",
"version": "1.5.0",
"version": "1.6.0",
"description": "A TypeScript library for parsing dice rolling expressions, most commonly used in tabletop RPGs.",
"main": "./dist/index.js",
"scripts": {
Expand Down
14 changes: 14 additions & 0 deletions spec/generator/dice-generator.generate.drop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,19 @@ describe('DiceGenerator', () => {
const generator = new Generator.DiceGenerator();
expect(generator.generate(exp)).toBe('2d6dh');
});
it('generates a drop with modifier (3d6dh2).', () => {
const exp = Ast.Factory.create(Ast.NodeType.Drop)
.setAttribute('type', 'highest');

const dice = Ast.Factory.create(Ast.NodeType.Dice);
dice.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 3));
dice.addChild(Ast.Factory.create(Ast.NodeType.DiceSides).setAttribute('value', 6));

exp.addChild(dice);
exp.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 2));

const generator = new Generator.DiceGenerator();
expect(generator.generate(exp)).toBe('3d6dh2');
});
});
});
14 changes: 14 additions & 0 deletions spec/generator/dice-generator.generate.keep.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,19 @@ describe('DiceGenerator', () => {
const generator = new Generator.DiceGenerator();
expect(generator.generate(exp)).toBe('2d6kl');
});
it('generates a keep with modifier (3d6kl2).', () => {
const exp = Ast.Factory.create(Ast.NodeType.Keep)
.setAttribute('type', 'lowest');

const dice = Ast.Factory.create(Ast.NodeType.Dice);
dice.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 3));
dice.addChild(Ast.Factory.create(Ast.NodeType.DiceSides).setAttribute('value', 6));

exp.addChild(dice);
exp.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 2));

const generator = new Generator.DiceGenerator();
expect(generator.generate(exp)).toBe('3d6kl2');
});
});
});
21 changes: 21 additions & 0 deletions spec/interpreter/dice-interpreter.interpret.simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,26 @@ describe('DiceInterpreter', () => {
expect(res.renderedExpression).toBe('[1, 7, 4, 5] + 5', 'Expression rendered incorrectly');
expect(res.total).toBe(22, 'Total counted incorrectly');
});
it('interprets a simple dice expression (4d6kh3).', () => {
const exp = Ast.Factory.create(Ast.NodeType.Keep)
.setAttribute('type', 'highest');

const dice = Ast.Factory.create(Ast.NodeType.Dice);
dice.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 4));
dice.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 6));

exp.addChild(dice);
exp.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 3));

const mockList = new MockListRandomProvider();
mockList.numbers.push(6, 1, 4, 3);

const interpreter = new Interpreter.DiceInterpreter(null, mockList);
const res = interpreter.interpret(exp);

expect(res.errors.length).toBe(0, 'Unexpected errors found.');
expect(res.renderedExpression).toBe('[6, 1, 4, 3]kh3', 'Expression rendered incorrectly');
expect(res.total).toBe(13, 'Total counted incorrectly');
});
});
});
20 changes: 12 additions & 8 deletions src/generator/dice-generator.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,22 @@ export class DiceGenerator implements Generator<string> {

generateKeep(expression: Ast.ExpressionNode): string {
this.expectChildCount(expression, 1);
let keep = 'k';
if (expression.getAttribute('type') === 'highest') { keep += 'h'; }
if (expression.getAttribute('type') === 'lowest') { keep += 'l'; }
return this.generate(expression.getChild(0)) + keep;
let exp = 'k';
if (expression.getAttribute('type') === 'highest') { exp += 'h'; }
if (expression.getAttribute('type') === 'lowest') { exp += 'l'; }

if (expression.getChildCount() > 1) { exp += this.generate(expression.getChild(1)); }
return this.generate(expression.getChild(0)) + exp;
}

generateDrop(expression: Ast.ExpressionNode): string {
this.expectChildCount(expression, 1);
let drop = 'd';
if (expression.getAttribute('type') === 'highest') { drop += 'h'; }
if (expression.getAttribute('type') === 'lowest') { drop += 'l'; }
return this.generate(expression.getChild(0)) + drop;
let exp = 'd';
if (expression.getAttribute('type') === 'highest') { exp += 'h'; }
if (expression.getAttribute('type') === 'lowest') { exp += 'l'; }

if (expression.getChildCount() > 1) { exp += this.generate(expression.getChild(1)); }
return this.generate(expression.getChild(0)) + exp;
}

generateCritical(expression: Ast.ExpressionNode): string {
Expand Down

0 comments on commit 97f5d43

Please sign in to comment.