Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Events should be undoable #942

Merged
merged 3 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/core/reducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,17 @@ describe('undo / redo', () => {
G.roll = ctx.random.D6();
},
},
turn: {
stages: {
special: {},
},
},
};

beforeEach(() => {
jest.clearAllMocks();
});

const reducer = CreateGameReducer({ game });

const initialState = InitializeGame({ game });
Expand All @@ -341,7 +350,7 @@ describe('undo / redo', () => {
expect(state._undo[1].ctx.random).toBeUndefined();
});

test('undo restores previous state', () => {
test('undo restores previous state after move', () => {
let state = reducer(initialState, makeMove('move', 'A', '0'));
const { G, ctx, plugins } = state;
state = reducer(state, makeMove('roll', null, '0'));
Expand All @@ -351,6 +360,20 @@ describe('undo / redo', () => {
expect(state.plugins).toEqual(plugins);
});

test('undo restores previous state after event', () => {
let state = reducer(initialState, gameEvent('setStage', 'special', '0'));
const { G, ctx, plugins } = state;
state = reducer(state, gameEvent('endStage', undefined, '0'));
expect(error).not.toBeCalled();
// Make sure we actually modified the stage.
expect(state.ctx.activePlayers).not.toEqual(ctx.activePlayers);
state = reducer(state, undo());
expect(error).not.toBeCalled();
expect(state.G).toEqual(G);
expect(state.ctx).toEqual(ctx);
expect(state.plugins).toEqual(plugins);
});

test('redo restores undone state', () => {
let state = initialState;
// Make two moves.
Expand Down
18 changes: 10 additions & 8 deletions src/core/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,16 @@ export function CreateGameReducer({
return state;
}

// Only allow undoable moves to be undone.
const lastMove: Move = game.flow.getMove(
restore.ctx,
last.moveType,
last.playerID
);
if (!CanUndoMove(state.G, state.ctx, lastMove)) {
return state;
// If undoing a move, check it is undoable.
if (last.moveType) {
const lastMove: Move = game.flow.getMove(
restore.ctx,
last.moveType,
last.playerID
);
if (!CanUndoMove(state.G, state.ctx, lastMove)) {
return state;
}
}

state = initializeDeltalog(state, action);
Expand Down