From b762e795077be9ec6396729b531182bc2d689a80 Mon Sep 17 00:00:00 2001 From: Low Jeng Lam Date: Sun, 28 Apr 2019 13:00:41 +0800 Subject: [PATCH] Initial undo test commit --- src/CommandManager.ts | 27 ++++++++++++----------- src/Wiser.ts | 7 +++--- src/__tests__/CommandManager.test.ts | 33 ++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 17 deletions(-) create mode 100644 src/__tests__/CommandManager.test.ts diff --git a/src/CommandManager.ts b/src/CommandManager.ts index b6208b1..6a43adb 100644 --- a/src/CommandManager.ts +++ b/src/CommandManager.ts @@ -2,38 +2,39 @@ import { IGameState } from './Wiser'; export interface ICommand { command: string; - undo: (state: IGameState) => any; - redo: (state: IGameState) => any; + diff: any; + undo: () => void; + redo: () => void; } export interface ICommandManager { add: (command: ICommand) => void; - redo: (state: IGameState) => void; - undo: (stae: IGameState) => void; + redo: () => void; + undo: () => void; stackList: () => ICommand[]; stack: (i: number) => ICommand | null; } -export class CommandManager implements ICommandManager{ +export class CommandManager implements ICommandManager { private commands: ICommand[] = []; private index: number = -1; - + public add(command: ICommand) { - this.commands = this.commands.slice(0, this.index+1); + this.commands = this.commands.slice(0, this.index + 1); this.commands.push(command); this.index = this.index + 1; } - public redo(state: any) { + public redo() { if (this.index < this.commands.length - 1) { this.index += 1; - this.commands[this.index].redo(state); + this.commands[this.index].redo(); } } - public undo(state: any) { - if (this.index >=0) { - this.commands[this.index].undo(state); + public undo() { + if (this.index >= 0) { + this.commands[this.index].undo(); this.index -= 1; } } @@ -49,4 +50,4 @@ export class CommandManager implements ICommandManager{ return null; } -} \ No newline at end of file +} diff --git a/src/Wiser.ts b/src/Wiser.ts index c0deb7d..c0fdde6 100644 --- a/src/Wiser.ts +++ b/src/Wiser.ts @@ -380,10 +380,9 @@ export class Wiser { // wiser.state.info.rules.config.missHitType = 'WWSC'; -// wiser.process('r1r2'); -// wiser.process('w4r2'); -// wiser.process('r3r2'); -// wiser.process('w5w6'); +// wiser.process('r1w2'); +// wiser.process('r1w3'); +// wiser.process('r3w4'); // console.log(wiser.state.match.r.balls[0]); diff --git a/src/__tests__/CommandManager.test.ts b/src/__tests__/CommandManager.test.ts new file mode 100644 index 0000000..2b6293f --- /dev/null +++ b/src/__tests__/CommandManager.test.ts @@ -0,0 +1,33 @@ +import deepDiff from 'deep-diff'; + +import { CommandManager } from '../CommandManager'; + +test('Add', () => { + const cm = new CommandManager(); + + const s0 = { + state: 0, + list: [], + }; + + const s1 = { + state: 1, + list: ['r1'], + }; + + const command = { + command: 'r1r2', + diff: deepDiff.diff(s0, s1), + undo: () => { + // console.log(diff); + }, + redo: () => { + // console.log('redeo') + }, + }; + + cm.add(command); + + // console.log(cm.stackList()); + cm.undo(); +});