1 |
|
2 | (function() {
|
3 | var DiceRoll, exports, extend;
|
4 |
|
5 | DiceRoll = (function() {
|
6 | DiceRoll.prototype.defaults = {
|
7 | dice: null,
|
8 | sides: null,
|
9 | sum: true,
|
10 | onesSubtract: false,
|
11 | target: null
|
12 | };
|
13 |
|
14 | DiceRoll.prototype.opts = {};
|
15 |
|
16 | DiceRoll.prototype.rolls = [];
|
17 |
|
18 | DiceRoll.prototype.conclusion = 0;
|
19 |
|
20 | function DiceRoll(opts) {
|
21 | var i, result, _i, _ref;
|
22 | this.rolls = [];
|
23 | ({
|
24 | conclusion: 0
|
25 | });
|
26 | this.opts = extend(extend({}, this.defaults), opts);
|
27 | if (this.opts.dice === null || this.opts.sides === null) {
|
28 | throw new Error("You have to provide dice and sides parameters");
|
29 | }
|
30 | if (!this.opts.sum && this.opts.target === null) {
|
31 | throw new Error("If you set sum: false you must set target: <number> as well.");
|
32 | }
|
33 | for (i = _i = 1, _ref = this.opts.dice; _i <= _ref; i = _i += 1) {
|
34 | result = Math.ceil(Math.random() * this.opts.sides);
|
35 | this.rolls.push(result);
|
36 | if (!this.opts.sum && result >= this.opts.target) {
|
37 | this.conclusion++;
|
38 | } else if (this.opts.sum) {
|
39 | this.conclusion += result;
|
40 | }
|
41 | }
|
42 | }
|
43 |
|
44 | DiceRoll.prototype.result = function() {
|
45 | return {
|
46 | 'rolls': this.rolls,
|
47 | 'conclusion': this.conclusion
|
48 | };
|
49 | };
|
50 |
|
51 | return DiceRoll;
|
52 |
|
53 | })();
|
54 |
|
55 | extend = function(object, properties) {
|
56 | var key, val;
|
57 | for (key in properties) {
|
58 | val = properties[key];
|
59 | object[key] = val;
|
60 | }
|
61 | return object;
|
62 | };
|
63 |
|
64 | exports = module.exports = DiceRoll;
|
65 |
|
66 | }).call(this);
|