Skip to content

Commit

Permalink
refactor: Change moveLimit syntax in setActivePlayers (#481)
Browse files Browse the repository at this point in the history
* feat: Set active player move limits with long-form syntax

Implements `player: { stage, moveLimit }` instead of `player: stage, 
moveLimit: { player: limit }`.

* refactor: Standardise application of active player arguments

* feat: Reimplement shorthand moveLimit syntax

Allows `setActivePlayers({ moveLimit })` to set limit for all active 
players.

* test: Update active player move limit syntax in tests

* test: Add edge-case tests, bringing coverage to 100%

* test: Remove unneeded test set-up

* test: Simplify undefined stage test

* refactor: Mutate state directly with `ApplyActivePlayerArgument`

* refactor: Test stage arg is `Stage.NULL` instead of `null`
  • Loading branch information
delucis authored and nicolodavis committed Sep 30, 2019
1 parent 64971ee commit 6762219
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 41 deletions.
96 changes: 63 additions & 33 deletions src/core/turn-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function SetActivePlayers(ctx, playerID, arg) {
}

let activePlayers = {};
let _activePlayersMoveLimit = {};

if (Array.isArray(arg)) {
let value = {};
Expand All @@ -60,59 +61,65 @@ export function SetActivePlayers(ctx, playerID, arg) {
}

if (arg.value) {
activePlayers = arg.value;
for (const id in arg.value) {
ApplyActivePlayerArgument(
activePlayers,
_activePlayersMoveLimit,
id,
arg.value[id]
);
}
}

if (arg.player !== undefined) {
activePlayers[playerID] = arg.player;
ApplyActivePlayerArgument(
activePlayers,
_activePlayersMoveLimit,
playerID,
arg.player
);
}

if (arg.others !== undefined) {
for (let i = 0; i < ctx.playOrder.length; i++) {
const p = ctx.playOrder[i];
if (p !== playerID) {
activePlayers[p] = arg.others;
const id = ctx.playOrder[i];
if (id !== playerID) {
ApplyActivePlayerArgument(
activePlayers,
_activePlayersMoveLimit,
id,
arg.others
);
}
}
}

if (arg.all !== undefined) {
for (let i = 0; i < ctx.playOrder.length; i++) {
activePlayers[ctx.playOrder[i]] = arg.all;
const id = ctx.playOrder[i];
ApplyActivePlayerArgument(
activePlayers,
_activePlayersMoveLimit,
id,
arg.all
);
}
}

if (Object.keys(activePlayers).length == 0) {
activePlayers = null;
}

let _activePlayersMoveLimit = null;

if (activePlayers && arg.moveLimit) {
if (typeof arg.moveLimit === 'number') {
_activePlayersMoveLimit = {};
for (const id in activePlayers) {
if (arg.moveLimit) {
for (const id in activePlayers) {
if (_activePlayersMoveLimit[id] === undefined) {
_activePlayersMoveLimit[id] = arg.moveLimit;
}
} else {
_activePlayersMoveLimit = {};

if (arg.moveLimit.value) {
_activePlayersMoveLimit = arg.moveLimit.value;
}
}
}

if (arg.moveLimit.player !== undefined && activePlayers[playerID]) {
_activePlayersMoveLimit[playerID] = arg.moveLimit.player;
}
if (Object.keys(activePlayers).length == 0) {
activePlayers = null;
}

if (arg.moveLimit.others !== undefined) {
for (const id in activePlayers) {
if (id !== playerID) {
_activePlayersMoveLimit[id] = arg.moveLimit.others;
}
}
}
}
if (Object.keys(_activePlayersMoveLimit).length == 0) {
_activePlayersMoveLimit = null;
}

let _activePlayersNumMoves = {};
Expand Down Expand Up @@ -175,6 +182,29 @@ export function UpdateActivePlayersOnceEmpty(ctx) {
};
}

/**
* Apply an active player argument to the given player ID
* @param {Object} activePlayers
* @param {Object} _activePlayersMoveLimit
* @param {String} playerID The player to apply the parameter to
* @param {(String|Object)} arg An active player argument
*/
function ApplyActivePlayerArgument(
activePlayers,
_activePlayersMoveLimit,
playerID,
arg
) {
if (typeof arg !== 'object' || arg === Stage.NULL) {
arg = { stage: arg };
}

if (arg.stage !== undefined) {
activePlayers[playerID] = arg.stage;
if (arg.moveLimit) _activePlayersMoveLimit[playerID] = arg.moveLimit;
}
}

/**
* Converts a playOrderPos index into its value in playOrder.
* @param {Array} playOrder - An array of player ID's.
Expand Down
40 changes: 32 additions & 8 deletions src/core/turn-order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,14 @@ describe('setActivePlayers', () => {
});
});

test('undefined stage leaves player inactive', () => {
const newState = flow.processEvent(
state,
gameEvent('setActivePlayers', [{ value: { '1': { moveLimit: 2 } } }])
);
expect(newState.ctx.activePlayers).toBeNull();
});

test('all', () => {
const newState = flow.processEvent(
state,
Expand Down Expand Up @@ -741,11 +749,8 @@ describe('setActivePlayers', () => {
const game = {
turn: {
activePlayers: {
all: 'play',
moveLimit: {
player: 2,
others: 1,
},
player: { stage: 'play', moveLimit: 2 },
others: { stage: 'play', moveLimit: 1 },
},
stages: {
play: { moves: { A: () => {} } },
Expand Down Expand Up @@ -785,13 +790,32 @@ describe('setActivePlayers', () => {
expect(state.ctx.activePlayers).toBeNull();
});

test('player-specific limit overrides moveLimit arg', () => {
const game = {
turn: {
activePlayers: {
all: { stage: 'play', moveLimit: 2 },
moveLimit: 1,
},
},
};

let state = InitializeGame({ game, numPlayers: 2 });

expect(state.ctx._activePlayersMoveLimit).toEqual({
'0': 2,
'1': 2,
});
});

test('value syntax', () => {
const game = {
turn: {
activePlayers: {
all: 'play',
moveLimit: {
value: { '0': 1, '1': 2, '2': 3 },
value: {
'0': { stage: 'play', moveLimit: 1 },
'1': { stage: 'play', moveLimit: 2 },
'2': { stage: 'play', moveLimit: 3 },
},
},
stages: {
Expand Down

0 comments on commit 6762219

Please sign in to comment.