Skip to content

Commit

Permalink
Convert add inventory command to event sourcing
Browse files Browse the repository at this point in the history
  • Loading branch information
dshaneg committed Mar 1, 2018
1 parent e1992ab commit c2f92c4
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 37 deletions.
6 changes: 2 additions & 4 deletions src/commands/add-inventory-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Command } from './command';
import { EventPublisher } from '../domain/event-publisher';
import { Voice } from '../domain/voice';
import { ItemFormatter } from './item-formatter';
import { GameState } from '../state/game-state';
import { ReadOnlyGameState } from '../state/game-state';

/** Class representing a command instructing the game to add a list of item deltas to inventory.
* Item deltas consist of an item and a count.
Expand Down Expand Up @@ -44,10 +44,8 @@ export class AddInventoryCommand implements Command {
}
}

execute(gameState: GameState, publisher: EventPublisher): void {
execute(gameState: ReadOnlyGameState, publisher: EventPublisher): void {
for (const delta of this.deltas) {
gameState.addInventory(delta.item, delta.count);

publisher.publish({
topic: 'player.inventory.added',
message: `You add ${ItemFormatter.formatProseItem(delta.item, delta.count)} to your pack.`,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/command.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

import { GameState } from '../state/game-state';
import { ReadOnlyGameState } from '../state/game-state';
import { EventPublisher } from '../domain/event-publisher';

export interface Command {
execute(gameState: GameState, publisher: EventPublisher): void;
execute(gameState: ReadOnlyGameState, publisher: EventPublisher): void;
}
3 changes: 3 additions & 0 deletions src/domain/game-state-event-subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export class GameStateEventSubscriber implements EventSubscriber {
case 'player.location.moved':
this.gameState.setCurrentLocation(this.map.get(event.currentNode.id));
break;
case 'player.inventory.added':
this.gameState.addInventory(event.item, event.count);
break;
}
}
}
31 changes: 0 additions & 31 deletions src/test/commands/add-inventory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ describe('AddInventoryCommand', () => {
command = new AddInventoryCommand(deltas);
});

it('Should call gs.addinventory once.', () => {
command.execute(gameState, publisher);

mockito.verify(GameStateMock.addInventory(item, 1)).once();
});

it('Should call publisher.publish once.', () => {
command.execute(gameState, publisher);

Expand All @@ -88,12 +82,6 @@ describe('AddInventoryCommand', () => {
command = new AddInventoryCommand(deltas, true);
});

it('Should call gs.addinventory once.', () => {
command.execute(gameState, publisher);

mockito.verify(GameStateMock.addInventory(item, 1)).once();
});

it('Should call publisher.publish once with mute voice.', () => {
command.execute(gameState, publisher);

Expand All @@ -116,13 +104,6 @@ describe('AddInventoryCommand', () => {
command = new AddInventoryCommand(deltas);
});

it('Should call gs.addinventory twice.', () => {
command.execute(gameState, publisher);

mockito.verify(GameStateMock.addInventory(item1, 5)).once();
mockito.verify(GameStateMock.addInventory(item2, 5)).once();
});

it('Should call publisher.publish twice.', () => {
command.execute(gameState, publisher);

Expand Down Expand Up @@ -154,12 +135,6 @@ describe('AddInventoryCommand', () => {
command = new AddInventoryCommand(deltas);
});

it('Should call gs.addinventory once with 0 as count.', () => {
command.execute(gameState, publisher);

mockito.verify(GameStateMock.addInventory(item, 0)).once();
});

it('Should call publisher.publish once.', () => {
command.execute(gameState, publisher);

Expand All @@ -183,12 +158,6 @@ describe('AddInventoryCommand', () => {
command.addDelta(item, 1);
});

it('Should call gs.addinventory once.', () => {
command.execute(gameState, publisher);

mockito.verify(GameStateMock.addInventory(item, 1)).once();
});

it('Should call publisher.publish once.', () => {
command.execute(gameState, publisher);

Expand Down

0 comments on commit c2f92c4

Please sign in to comment.