Skip to content

Commit

Permalink
Initial undo test commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jenglamlow committed Apr 28, 2019
1 parent ba1cdca commit b762e79
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
27 changes: 14 additions & 13 deletions src/CommandManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -49,4 +50,4 @@ export class CommandManager implements ICommandManager{

return null;
}
}
}
7 changes: 3 additions & 4 deletions src/Wiser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/CommandManager.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit b762e79

Please sign in to comment.