Skip to content

Commit 97f5d43

Browse files
authored
Keep drop count (#18)
* Add keep/drop count to rendered expression output. * Fix codacy linting issue.
1 parent ffcfd5d commit 97f5d43

7 files changed

+76
-16
lines changed

karma.conf.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ module.exports = function (config) {
2323
],
2424
customLaunchers: {
2525
chromeDebugging: {
26-
base: 'Chrome',
27-
flags: [ '--remote-debugging-port=9333' ]
26+
base: "Chrome",
27+
flags: [ "--remote-debugging-port=9333" ]
2828
},
2929
chromeTravisCi: {
3030
base: "Chrome",

package-lock.json

+12-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dice-typescript",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"description": "A TypeScript library for parsing dice rolling expressions, most commonly used in tabletop RPGs.",
55
"main": "./dist/index.js",
66
"scripts": {

spec/generator/dice-generator.generate.drop.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,19 @@ describe('DiceGenerator', () => {
2929
const generator = new Generator.DiceGenerator();
3030
expect(generator.generate(exp)).toBe('2d6dh');
3131
});
32+
it('generates a drop with modifier (3d6dh2).', () => {
33+
const exp = Ast.Factory.create(Ast.NodeType.Drop)
34+
.setAttribute('type', 'highest');
35+
36+
const dice = Ast.Factory.create(Ast.NodeType.Dice);
37+
dice.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 3));
38+
dice.addChild(Ast.Factory.create(Ast.NodeType.DiceSides).setAttribute('value', 6));
39+
40+
exp.addChild(dice);
41+
exp.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 2));
42+
43+
const generator = new Generator.DiceGenerator();
44+
expect(generator.generate(exp)).toBe('3d6dh2');
45+
});
3246
});
3347
});

spec/generator/dice-generator.generate.keep.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,19 @@ describe('DiceGenerator', () => {
2929
const generator = new Generator.DiceGenerator();
3030
expect(generator.generate(exp)).toBe('2d6kl');
3131
});
32+
it('generates a keep with modifier (3d6kl2).', () => {
33+
const exp = Ast.Factory.create(Ast.NodeType.Keep)
34+
.setAttribute('type', 'lowest');
35+
36+
const dice = Ast.Factory.create(Ast.NodeType.Dice);
37+
dice.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 3));
38+
dice.addChild(Ast.Factory.create(Ast.NodeType.DiceSides).setAttribute('value', 6));
39+
40+
exp.addChild(dice);
41+
exp.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 2));
42+
43+
const generator = new Generator.DiceGenerator();
44+
expect(generator.generate(exp)).toBe('3d6kl2');
45+
});
3246
});
3347
});

spec/interpreter/dice-interpreter.interpret.simple.ts

+21
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,26 @@ describe('DiceInterpreter', () => {
2424
expect(res.renderedExpression).toBe('[1, 7, 4, 5] + 5', 'Expression rendered incorrectly');
2525
expect(res.total).toBe(22, 'Total counted incorrectly');
2626
});
27+
it('interprets a simple dice expression (4d6kh3).', () => {
28+
const exp = Ast.Factory.create(Ast.NodeType.Keep)
29+
.setAttribute('type', 'highest');
30+
31+
const dice = Ast.Factory.create(Ast.NodeType.Dice);
32+
dice.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 4));
33+
dice.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 6));
34+
35+
exp.addChild(dice);
36+
exp.addChild(Ast.Factory.create(Ast.NodeType.Number).setAttribute('value', 3));
37+
38+
const mockList = new MockListRandomProvider();
39+
mockList.numbers.push(6, 1, 4, 3);
40+
41+
const interpreter = new Interpreter.DiceInterpreter(null, mockList);
42+
const res = interpreter.interpret(exp);
43+
44+
expect(res.errors.length).toBe(0, 'Unexpected errors found.');
45+
expect(res.renderedExpression).toBe('[6, 1, 4, 3]kh3', 'Expression rendered incorrectly');
46+
expect(res.total).toBe(13, 'Total counted incorrectly');
47+
});
2748
});
2849
});

src/generator/dice-generator.class.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,22 @@ export class DiceGenerator implements Generator<string> {
134134

135135
generateKeep(expression: Ast.ExpressionNode): string {
136136
this.expectChildCount(expression, 1);
137-
let keep = 'k';
138-
if (expression.getAttribute('type') === 'highest') { keep += 'h'; }
139-
if (expression.getAttribute('type') === 'lowest') { keep += 'l'; }
140-
return this.generate(expression.getChild(0)) + keep;
137+
let exp = 'k';
138+
if (expression.getAttribute('type') === 'highest') { exp += 'h'; }
139+
if (expression.getAttribute('type') === 'lowest') { exp += 'l'; }
140+
141+
if (expression.getChildCount() > 1) { exp += this.generate(expression.getChild(1)); }
142+
return this.generate(expression.getChild(0)) + exp;
141143
}
142144

143145
generateDrop(expression: Ast.ExpressionNode): string {
144146
this.expectChildCount(expression, 1);
145-
let drop = 'd';
146-
if (expression.getAttribute('type') === 'highest') { drop += 'h'; }
147-
if (expression.getAttribute('type') === 'lowest') { drop += 'l'; }
148-
return this.generate(expression.getChild(0)) + drop;
147+
let exp = 'd';
148+
if (expression.getAttribute('type') === 'highest') { exp += 'h'; }
149+
if (expression.getAttribute('type') === 'lowest') { exp += 'l'; }
150+
151+
if (expression.getChildCount() > 1) { exp += this.generate(expression.getChild(1)); }
152+
return this.generate(expression.getChild(0)) + exp;
149153
}
150154

151155
generateCritical(expression: Ast.ExpressionNode): string {

0 commit comments

Comments
 (0)