diff --git a/src/commands/add-inventory-command.ts b/src/commands/add-inventory-command.ts index c1ed204..a237201 100644 --- a/src/commands/add-inventory-command.ts +++ b/src/commands/add-inventory-command.ts @@ -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. @@ -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.`, diff --git a/src/commands/command.ts b/src/commands/command.ts index 52b8841..bfa92a0 100644 --- a/src/commands/command.ts +++ b/src/commands/command.ts @@ -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; } diff --git a/src/domain/game-state-event-subscriber.ts b/src/domain/game-state-event-subscriber.ts index 2881be6..65ed574 100644 --- a/src/domain/game-state-event-subscriber.ts +++ b/src/domain/game-state-event-subscriber.ts @@ -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; } } } diff --git a/src/test/commands/add-inventory.spec.ts b/src/test/commands/add-inventory.spec.ts index 77ff3ee..3e91970 100644 --- a/src/test/commands/add-inventory.spec.ts +++ b/src/test/commands/add-inventory.spec.ts @@ -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); @@ -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); @@ -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); @@ -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); @@ -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);