Skip to content

Commit

Permalink
Actions Event Emitter Extended
Browse files Browse the repository at this point in the history
  • Loading branch information
adasq committed Jan 5, 2018
1 parent 765da18 commit a8665da
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
"author": "adasq",
"license": "MIT",
"dependencies": {
"@types/mocha": "2.2.44",
"@types/node": "8.0.20",
"lodash": "4.17.4",
"mocha": "2.3.4",
"redux": "3.7.2",
"should": "8.0.2",
"typescript": "2.4.2",
"wolfy87-eventemitter": "5.2.4"
},
"devDependencies": {
"mocha": "2.3.4",
"@types/mocha": "2.2.44",
"@types/node": "8.0.20",
"should": "8.0.2",
"typescript": "2.4.2",
"coveralls": "3.0.0",
"karma": "1.4.1",
"karma-chrome-launcher": "2.2.0",
Expand Down
25 changes: 21 additions & 4 deletions src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,29 @@ export function initializeGame(defaultState: Game = undefined): Thousand {
const store = createStore(gameReducer, defaultState);
const events: any = new EventEmitter();
let emitActionEvent = null;

function someActionListenersRegistered() {
return events.getListeners('action') > 0;
}

function manageAction(action: any) {
let result = false;
if (can(store.getState(), action)) {
emitActionEvent = () => events.emit('action', action);
emitActionEvent = (next) => {
if(someActionListenersRegistered()) {
events.emit('action', action, next);
} else {
next();
}
}
store.dispatch(action);
result = true;
} else {
console.log('can\'t do action:', action);
result = false;
}
return result;
}

const thousand: Thousand = {
events,
//actions:
Expand All @@ -50,9 +61,15 @@ export function initializeGame(defaultState: Game = undefined): Thousand {
let previousPhase: Phase = null;
store.subscribe(() => {
if(emitActionEvent) {
emitActionEvent();
const temporaryEmitActionEvent = emitActionEvent;
emitActionEvent = null;
temporaryEmitActionEvent(onStoreChanged);
} else {
onStoreChanged();
}
});

function onStoreChanged() {
const state: Game = store.getState();
const isNew = (previousPhase !== state.phase);

Expand Down Expand Up @@ -234,7 +251,7 @@ export function initializeGame(defaultState: Game = undefined): Thousand {
const isFirst = previousPhase !== state.phase;
updatePreviousState();
phaseHandler[state.phase] && phaseHandler[state.phase](isFirst);
});
}

return thousand;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Game, Player } from './game.interfaces';
export * from './game.interfaces';
export { initializeGame } from './game';
export { createCards, createCard } from './helpers/cards.helpers';
export { getNextTurn } from './helpers/players.helpers';

export function getWinner(state: Game): string|null {
const winner: Player = getWinnerInternal(state.players);
Expand Down
5 changes: 5 additions & 0 deletions test/api/battle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ describe('battle API', () => {
next();
});

thousand.events.addListener('action', (action, next) => {
console.log(action);
next();
});

thousand.init();

const actionsResult = [
Expand Down
4 changes: 0 additions & 4 deletions test/api/bidding.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ describe('bidding', () => {
next();
});

thousand.events.addListener('action', action => {
console.log(action);
});

thousand.init();

const actionsResult = [
Expand Down
2 changes: 0 additions & 2 deletions test/api/sharing-stock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ describe('share stock cards', () => {
history.push(state.phase);

if (state.phase === Phase.BATTLE_START) {
console.log(history);
should(history).be.deepEqual([
Phase.SHARE_STOCK,
Phase.SHARE_STOCK,
Expand Down Expand Up @@ -113,7 +112,6 @@ describe('share stock cards', () => {
history.push(state.phase);

if (state.phase === Phase.BOMB_DECLARED) {
console.log(history);
should(history).be.deepEqual([
Phase.SHARE_STOCK,
Phase.BOMB_DECLARED
Expand Down

0 comments on commit a8665da

Please sign in to comment.