Skip to content

Commit 5fb663a

Browse files
committed
allow calling setActionPlayers via TurnOrder objects
1 parent 53473ef commit 5fb663a

5 files changed

Lines changed: 65 additions & 77 deletions

File tree

docs/CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,32 @@ endTurnIf: () => ({ next: playerID })
6161
6. The semantics of enabling / disabling events has changed
6262
a bit: see https://boardgame.io/#/events for more details.
6363

64+
7. TurnOrder objects now support `setActionPlayers` args.
65+
Instead of returning `actionPlayers` in `first` / `next`,
66+
add an `actionPlayers` section instead.
67+
68+
```
69+
// old
70+
{
71+
first: (G, ctx) => {
72+
playOrderPos: 0,
73+
actionPlayers: [...ctx.playOrder],
74+
},
75+
76+
next: (G, ctx) => {
77+
playOrderPos: ctx.playOrderPos + 1,
78+
actionPlayers: [...ctx.playOrder],
79+
},
80+
}
81+
82+
// new
83+
{
84+
first: (G, ctx) => 0,
85+
next: (G, ctx) => ctx.playOrderPos + 1,
86+
actionPlayers: { all: true },
87+
}
88+
```
89+
6490
## v0.26.3
6591

6692
#### Features

docs/turn-order.md

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,20 @@ A `TurnOrder` object has the following structure:
120120
121121
```js
122122
{
123-
// Get the initial value of playOrderPos,
123+
// Get the initial value of playOrderPos.
124+
// This is called at the beginning of the phase.
124125
first: (G, ctx) => 0,
125126

126-
// Get the next value of playOrderPos when endTurn is called.
127+
// Get the next value of playOrderPos.
128+
// This is called at the end of each turn.
127129
next: (G, ctx) => (ctx.playOrderPos + 1) % ctx.numPlayers,
130+
131+
// If this section is present, setActionPlayers is called
132+
// at the beginning of the phase with the object below as
133+
// argument.
134+
actionPlayers: {
135+
...
136+
},
128137
}
129138
```
130139
@@ -150,28 +159,6 @@ Game({
150159
}
151160
```
152161
153-
You may also set `actionPlayers` from a `TurnOrder` object by
154-
returning an object of type:
155-
156-
```js
157-
{
158-
playOrderPos, actionPlayers;
159-
}
160-
```
161-
162-
```js
163-
{
164-
// Get the initial value of playOrderPos,
165-
first: (G, ctx) => { playOrderPos: 0, actionPlayers: ['0'] }
166-
167-
// Get the next value of playOrderPos when endTurn is called.
168-
next: (G, ctx) => {
169-
const playOrderPos = (ctx.playOrderPos + 1) % ctx.numPlayers;
170-
const actionPlayers = ['0', '1'];
171-
return { playOrderPos, actionPlayers };
172-
}
173-
```
174-
175162
### endTurn / endTurnIf
176163
177164
You can also specify the next player during the `endTurn` event.

src/core/flow.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {
10-
SetActionPlayers,
10+
SetActionPlayersEvent,
1111
InitTurnOrderState,
1212
UpdateTurnOrderState,
1313
TurnOrder,
@@ -673,7 +673,7 @@ export function FlowWithPhases({
673673
endTurn: endTurnEvent,
674674
endPhase: endPhaseEvent,
675675
endGame: endGameEvent,
676-
setActionPlayers: SetActionPlayers,
676+
setActionPlayers: SetActionPlayersEvent,
677677
};
678678

679679
let enabledEvents = {};

src/core/turn-order.js

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,33 @@ export const Pass = (G, ctx) => {
4747
* // (after which they're pruned from actionPlayers).
4848
* }
4949
*/
50-
export function SetActionPlayers(state, arg) {
50+
export function SetActionPlayersEvent(state, arg) {
51+
return { ...state, ctx: setActionPlayers(state.ctx, arg) };
52+
}
53+
54+
function setActionPlayers(ctx, arg) {
5155
let actionPlayers = [];
5256

5357
if (arg.value) {
5458
actionPlayers = arg.value;
5559
}
5660
if (arg.all) {
57-
actionPlayers = [...state.ctx.playOrder];
61+
actionPlayers = [...ctx.playOrder];
5862
}
5963

6064
if (arg.allOthers) {
61-
actionPlayers = [...state.ctx.playOrder].filter(
62-
nr => nr !== state.ctx.currentPlayer
63-
);
65+
actionPlayers = [...ctx.playOrder].filter(nr => nr !== ctx.currentPlayer);
6466
}
6567

6668
if (Array.isArray(arg)) {
6769
actionPlayers = arg;
6870
}
6971

7072
return {
71-
...state,
72-
ctx: {
73-
...state.ctx,
74-
actionPlayers,
75-
_actionPlayersOnce: arg.once,
76-
_actionPlayersAllOthers: arg.allOthers,
77-
},
73+
...ctx,
74+
actionPlayers,
75+
_actionPlayersOnce: arg.once,
76+
_actionPlayersAllOthers: arg.allOthers,
7877
};
7978
}
8079

@@ -94,26 +93,16 @@ function getCurrentPlayer(playOrder, playOrderPos) {
9493
* @param {object} turnOrder - A turn order object for this phase.
9594
*/
9695
export function InitTurnOrderState(G, ctx, turnOrder) {
97-
let playOrderPos;
98-
let actionPlayers;
99-
100-
const t = turnOrder.first(G, ctx);
101-
102-
if (t.playOrderPos !== undefined) {
103-
playOrderPos = t.playOrderPos;
104-
} else {
105-
playOrderPos = t;
106-
}
107-
96+
const playOrderPos = turnOrder.first(G, ctx);
10897
const currentPlayer = getCurrentPlayer(ctx.playOrder, playOrderPos);
10998

110-
if (t.actionPlayers !== undefined) {
111-
actionPlayers = t.actionPlayers;
99+
if (turnOrder.actionPlayers !== undefined) {
100+
ctx = setActionPlayers(ctx, turnOrder.actionPlayers);
112101
} else {
113-
actionPlayers = [currentPlayer];
102+
ctx = { ...ctx, actionPlayers: [currentPlayer] };
114103
}
115104

116-
return { ...ctx, currentPlayer, playOrderPos, actionPlayers };
105+
return { ...ctx, currentPlayer, playOrderPos };
117106
}
118107

119108
/**
@@ -141,20 +130,13 @@ export function UpdateTurnOrderState(G, ctx, turnOrder, endTurnArg) {
141130
} else {
142131
const t = turnOrder.next(G, ctx);
143132

144-
if (t == undefined) {
133+
if (t === undefined) {
145134
endPhase = true;
146135
} else {
147-
if (t.playOrderPos !== undefined) {
148-
playOrderPos = t.playOrderPos;
149-
} else {
150-
playOrderPos = t;
151-
}
152-
136+
playOrderPos = t;
153137
currentPlayer = getCurrentPlayer(ctx.playOrder, playOrderPos);
154138

155-
if (t.actionPlayers !== undefined) {
156-
actionPlayers = t.actionPlayers;
157-
} else {
139+
if (turnOrder.actionPlayers === undefined) {
158140
actionPlayers = [currentPlayer];
159141
}
160142
}
@@ -179,9 +161,9 @@ export function UpdateTurnOrderState(G, ctx, turnOrder, endTurnArg) {
179161
* begins, and also a function `next` to determine who the
180162
* next player is when the turn ends.
181163
*
182-
* first / next can also return an object of type
183-
* { playOrderPos, actionPlayers }
184-
* in which case they can also set actionPlayers simultaneously.
164+
* Objects can also contain an actionPlayers section which
165+
* is passed to SetActionPlayers above at the beginning of
166+
* the phase.
185167
*
186168
* The phase ends if next() returns undefined.
187169
*/
@@ -217,16 +199,9 @@ export const TurnOrder = {
217199
* currentPlayer switches around in round-robin fashion, but any player can play on each turn.
218200
*/
219201
ANY: {
220-
first: (G, ctx) => {
221-
return {
222-
actionPlayers: [...ctx.playOrder],
223-
playOrderPos: ctx.playOrderPos,
224-
};
225-
},
226-
next: (G, ctx) => {
227-
const playOrderPos = (ctx.playOrderPos + 1) % ctx.playOrder.length;
228-
return { actionPlayers: [...ctx.playOrder], playOrderPos };
229-
},
202+
first: (G, ctx) => ctx.playOrderPos,
203+
next: (G, ctx) => (ctx.playOrderPos + 1) % ctx.playOrder.length,
204+
actionPlayers: { all: true },
230205
},
231206

232207
/**

src/core/turn-order.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ describe('UpdateTurnOrderState', () => {
324324
currentPlayer: '0',
325325
playOrder: ['0', '1', '2'],
326326
playOrderPos: 0,
327-
actionPlayers: ['0'],
327+
actionPlayers: ['0', '1', '2'],
328328
};
329329

330330
test('without next player', () => {

0 commit comments

Comments
 (0)