Skip to content

Commit

Permalink
Implement replayEvents on GameEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
dshaneg committed Mar 1, 2018
1 parent 9d6bdda commit 8057fff
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/game-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ export class GameEngine {
private mapNodeRepository: MapNodeRepository) {
}

replayEvents(gameState: GameState, events: any[]) {
const stateSubscriber = this.gameStateEventSubscriberFactory.create(gameState);
const publisher = new Publisher([stateSubscriber]);

events.forEach(event => {
publisher.publish(event);
});
}

startGame(gameState: GameState): any {
return this.handleInput(gameState, 'start game');
}
Expand Down
2 changes: 1 addition & 1 deletion src/game-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class GameManager {
constructor(private gameSessionRepository: any) {
}

createGame() {
createGame(): GameState {
const gameState = this.gameSessionRepository.create();

return gameState;
Expand Down
32 changes: 30 additions & 2 deletions src/test/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ class HelloParser extends Parser {
}
}

describe('Smoke Tests', () => {
const gameEngine = TextAdventureCore.createGameEngine(gameDefinitionRepository, mapNodeRepository, itemRepository, new HelloParser(), true);

describe('Sequential Smoke Tests', () => {

const gameState = TextAdventureCore.createGameManager(gameSessionRepository).createGame();
const gameEngine = TextAdventureCore.createGameEngine(gameDefinitionRepository, mapNodeRepository, itemRepository, new HelloParser(), true);

it('start the game', () => {
const response = gameEngine.startGame(gameState);
Expand Down Expand Up @@ -79,6 +80,33 @@ describe('Smoke Tests', () => {
});
});

describe('Replay Tests', () => {
let gameState: GameState;

beforeEach(() => {
gameState = TextAdventureCore.createGameManager(gameSessionRepository).createGame();
});

it('replay should reproduce same state as original play', () => {
const events: any[] = gameEngine.startGame(gameState).events
.concat(gameEngine.handleInput(gameState, 'go south').events)
.concat(gameEngine.handleInput(gameState, 'go south').events)
.concat(gameEngine.handleInput(gameState, 'go south').events)
.concat(gameEngine.handleInput(gameState, 'hello').events)
.concat(gameEngine.handleInput(gameState, 'conjureitem 1002').events)
.concat(gameEngine.handleInput(gameState, 'help').events)
.concat(gameEngine.handleInput(gameState, 'inventory').events)
.concat(gameEngine.handleInput(gameState, 'exit').events)
.concat(gameEngine.stopGame(gameState).events);

const newState = TextAdventureCore.createGameManager(gameSessionRepository).createGame();
gameEngine.replayEvents(newState, events);

expect(newState.player).to.deep.equal(gameState.player);
expect(newState.isStarted).to.equal(gameState.isStarted);
});
});

function debug(response: any) {
console.log('==============================================');
console.log(`input: ${response.command}`);
Expand Down

0 comments on commit 8057fff

Please sign in to comment.