Skip to content

Commit

Permalink
Make Random package not depend on global state (#146)
Browse files Browse the repository at this point in the history
* remove global state

* update docs

* remove from package

* update docs

* update docs

* review comments

* update docs/react/boardgameio.min.js
  • Loading branch information
nicolodavis authored Mar 15, 2018
1 parent 43fbc42 commit 8969433
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 211 deletions.
6 changes: 3 additions & 3 deletions docs/api/Game.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ game state and the moves. The moves are converted to a
import { Game } from `boardgame.io/core';
const game = Game({
setup: (numPlayers) => {
setup: (ctx) => {
const G = {...};
return G;
},
Expand All @@ -75,7 +75,7 @@ const game = Game({
import { Game } from 'boardgame.io/core';
const game = Game({
setup: (numPlayers) => {
setup: (ctx) => {
...
},
Expand All @@ -99,7 +99,7 @@ const game = Game({
import { Game } from 'boardgame.io/core';
const game = Game({
setup: (numPlayers) => {
setup: (ctx) => {
...
},
Expand Down
24 changes: 8 additions & 16 deletions docs/api/Random.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

API for code that requires randomness. See the guide [here](random.md).

## 1. Random.Die
## 1. Die

### Arguments

Expand All @@ -16,32 +16,28 @@ The die roll value (or an array of values if `diceCount` is greater than `1`).
#### Usage

```js
import { Random } from `boardgame.io/core';
const game = Game({
moves: {
move(G, ctx) {
const die = Random.Die(6); // die = 1-6
const dice = Random.Die(6, 3); // dice = [1-6, 1-6, 1-6]
const die = ctx.random.Die(6); // die = 1-6
const dice = ctx.random.Die(6, 3); // dice = [1-6, 1-6, 1-6]
...
},
}
});
```

## 2. Random.Number
## 2. Number

Returns a random number between `0` and `1`.

#### Usage

```js
import { Random } from `boardgame.io/core';
const game = Game({
moves: {
move(G, ctx) {
const n = Random.Number();
const n = ctx.random.Number();
...
},
}
Expand All @@ -61,15 +57,13 @@ The shuffled array.
#### Usage

```js
import { Random } from `boardgame.io/core';

const game = Game({
moves: {
move(G, ctx) {
const deck = Random.Shuffle(G.deck);
const deck = ctx.random.Shuffle(G.deck);
return { ...G, deck };
},
}
},
});
```

Expand All @@ -85,12 +79,10 @@ const game = Game({
### Usage

```js
import { Random } from `boardgame.io/core';
const game = Game({
moves: {
move(G, ctx) {
const die = Random.D6();
const die = ctx.random.D6();
...
},
}
Expand Down
4 changes: 1 addition & 3 deletions docs/random.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ This poses interesting challenges regarding the implementation.
### Using Randomness in Games

```js
import { Random } from 'boardgame.io/core';

Game({
moves: {
rollDie(G, ctx) {
return { ...G, dice: Random.D6() };
return { ...G, dice: ctx.random.D6() };
},
},
});
Expand Down
2 changes: 1 addition & 1 deletion docs/react/boardgameio.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ $ npm install --save boardgame.io
We create the game by providing the initial value of the
game state `G` (through the `setup` function), and the moves
of the game. The `setup` function also accepts a
`numPlayers` parameter if you need to customize the initial
state based on the number of players, but we don't need that
for Tic-Tac-Toe.
`ctx` parameter if you need to customize the initial
state based on some field in `ctx` (the number of players, for example),
but we don't need that for Tic-Tac-Toe.

In Tic-Tac-Toe, we have just one type of move that we shall
name `clickCell`. The move function accepts
Expand Down
8 changes: 4 additions & 4 deletions examples/modules/random/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* https://opensource.org/licenses/MIT.
*/

import { Game, Random } from 'boardgame.io/core';
import { Game } from 'boardgame.io/core';

const RandomExample = Game({
name: 'shuffle',
Expand All @@ -16,9 +16,9 @@ const RandomExample = Game({
}),

moves: {
shuffle: G => ({ ...G, deck: Random.Shuffle(G.deck) }),
rollDie: (G, ctx, value) => ({ ...G, dice: Random.Die(value) }),
rollD6: G => ({ ...G, dice: Random.D6() }),
shuffle: (G, ctx) => ({ ...G, deck: ctx.random.Shuffle(G.deck) }),
rollDie: (G, ctx, value) => ({ ...G, dice: ctx.random.Die(value) }),
rollD6: (G, ctx) => ({ ...G, dice: ctx.random.D6() }),
},
});

Expand Down
3 changes: 1 addition & 2 deletions packages/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ import Game from '../src/core/game.js';
import { Flow, FlowWithPhases } from '../src/core/flow.js';
import { TurnOrder, Pass } from '../src/core/turn-order.js';
import { PlayerView } from '../src/core/player-view.js';
import { Random } from '../src/core/random';

export { Game, Flow, FlowWithPhases, TurnOrder, Pass, PlayerView, Random };
export { Game, Flow, FlowWithPhases, TurnOrder, Pass, PlayerView };
2 changes: 0 additions & 2 deletions packages/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Token from '../src/ui/token.js';
import { Card } from '../src/ui/card.js';
import { Grid } from '../src/ui/grid.js';
import { HexGrid } from '../src/ui/hex.js';
import { Random } from '../src/core/random';

export default {
Client,
Expand All @@ -29,5 +28,4 @@ export default {
Token,
Grid,
HexGrid,
Random,
};
4 changes: 3 additions & 1 deletion src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { TurnOrder } from './turn-order';
import { Random } from './random';

/**
* Helper to create a reducer that manages ctx (with the
Expand Down Expand Up @@ -276,8 +277,9 @@ export function FlowWithPhases({
};

const startTurn = function(state, config) {
const ctx = { ...state.ctx };
let ctx = { ...state.ctx };
const G = config.onTurnBegin(state.G, ctx);
ctx = Random.detach(ctx);
const _undo = [{ G, ctx }];
return { ...state, G, ctx, _undo, _redo: [] };
};
Expand Down
4 changes: 2 additions & 2 deletions src/core/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { FlowWithPhases } from './flow';
import { GenSeed } from './random';
import { Random } from './random';

/**
* Game
Expand Down Expand Up @@ -76,7 +76,7 @@ function Game({ name, setup, moves, playerView, flow, seed }) {
if (setup === undefined) setup = () => ({});
if (moves === undefined) moves = {};
if (playerView === undefined) playerView = G => G;
if (seed === undefined) seed = GenSeed();
if (seed === undefined) seed = Random.seed();

if (!flow || flow.processGameEvent === undefined) {
flow = FlowWithPhases(flow || {});
Expand Down
Loading

0 comments on commit 8969433

Please sign in to comment.