Boardgame.io: How to end turn when all players exit a stage

Created on 2 Aug 2020  路  2Comments  路  Source: boardgameio/boardgame.io

I'm trying to make turns end when all players end a certain stage, but so far I haven't been successful.
My logic was to check active players in the endIf hook, but apparently the ctx.activePlayers is not yet updated when that hook executes.

// "Submit" move that ends a player stage
// (I've also tried directly calling client.events.endStage() instead of using a move)
const submit = (G, ctx) => ctx.events.endStage()

// Turn
{
      activePlayers: {
          all: 'prepare',
      },
      stages: {
          prepare: {
              moves: { moveToTile, submit },
          },
      },
      endIf: (G, ctx) => {
          return ctx.activePlayers === null;
      },
}

Any tips? Thanks!

bug

Most helpful comment

Thanks, I can confirm that behaviour: https://codesandbox.io/s/bgio-end-turn-on-active-players-empty-bug-xc066

We should figure out a way to make that easier.

For now, you would probably have to do the check in the submit move. Not very elegant because you鈥檇 have to include the check in any move that ends a stage, but it might look something like this:

function endStageOrIfPlayerIsLastActivePlayerEndTurn(G, ctx) {
  if (!ctx.activePlayers || Object.keys(ctx.activePlayers).length === 1) {
    ctx.events.endTurn();
  } else {
    ctx.events.endStage();
  }
}

const submit = (G, ctx) => endStageOrIfPlayerIsLastActivePlayerEndTurn(G, ctx);

All 2 comments

Thanks, I can confirm that behaviour: https://codesandbox.io/s/bgio-end-turn-on-active-players-empty-bug-xc066

We should figure out a way to make that easier.

For now, you would probably have to do the check in the submit move. Not very elegant because you鈥檇 have to include the check in any move that ends a stage, but it might look something like this:

function endStageOrIfPlayerIsLastActivePlayerEndTurn(G, ctx) {
  if (!ctx.activePlayers || Object.keys(ctx.activePlayers).length === 1) {
    ctx.events.endTurn();
  } else {
    ctx.events.endStage();
  }
}

const submit = (G, ctx) => endStageOrIfPlayerIsLastActivePlayerEndTurn(G, ctx);

Thanks @delucis. It seemed strange to me but didn't know if it was a bug or me using it wrong.
I'll go with your solution for now, with a slight modification

function endStageAndIfPlayerIsLastActivePlayerEndTurnToo(G, ctx) {
  ctx.events.endStage();
  if (!ctx.activePlayers || (Object.keys(ctx.activePlayers).length === 1 && ctx.playerID in ctx.activePlayers)) {
    ctx.events.endTurn();
  }
}

const submit = (G, ctx) => endStageAndIfPlayerIsLastActivePlayerEndTurnToo(G, ctx);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

nicolodavis picture nicolodavis  路  3Comments

nicolodavis picture nicolodavis  路  7Comments

alexwoodsy picture alexwoodsy  路  7Comments

Pong420 picture Pong420  路  9Comments

nicolodavis picture nicolodavis  路  6Comments