Pool Demo

What is Pool Demo?

A pool is useful if you wanna roll a fist full of dice (or other rollables) and want all the individual results back as an array.

Example

Result:

How the Template Works

The code for this demo creates an object showcasing a feature of the RollPlayer.js library and returns it to the template as demo.rollable. Both when the page loads and when the button above is pressed the template code calls demo.rollable.roll() and the returned result is displayed above. This process is identicle for all the demos allowing the explanations below to focus on only the object being demoed.

Every result is also logged to the console if you want to see a history after a bunch of button clicks.

Version

1.0

Authors

  • Derek Pennycuff

Full source

  1. 1: var demo = function() {
  2. 2: var d4 = RollPlayer.die(4);
  3. 3: var d6 = RollPlayer.die();
  4. 4: var inner = RollPlayer.pool(d6, d6, d6);
  5. 5: var myPool = RollPlayer.pool(d4, d4, d4, d6, d6, d6, inner);
  6. 6: return { rollable : myPool };
  7. 7: }();

Explanations

We're creating a wrapper object called demo so that the template code can access the inner workings of this particular example.

  1. 1: var demo = function() {

Let's make some rollables to populate our luck.

  1. 2: var d4 = RollPlayer.die(4);

Did I mention before the die() defualts to a d6 if you don't specify any arguments?

  1. 3: var d6 = RollPlayer.die();

Any rollable object should work, including another pool.

  1. 4: var inner = RollPlayer.pool(d6, d6, d6);

The result will look flattened due to the way toString works on arrays, but if you check the console you'll get a clearer picture of what the result looks like programatically.

By the way, if you want to roll a bunch of dice but have the result be a single number, such as 3d6 added together, that's what sumPools are for.

  1. 5: var myPool = RollPlayer.pool(d4, d4, d4, d6, d6, d6, inner);

Return an object so that our template code can access the star of this example as demo.rollable

  1. 6: return { rollable : myPool };
  2. 7: }();

Tutorial Builder is © Christian Heilmann, 2008. Licensed under the BSD license.