diff --git a/.eslintrc.json b/.eslintrc.json index 0b31192..027b04c 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -11,6 +11,8 @@ }, "rules": { "class-methods-use-this": "off", - "no-unused-vars": ["error", { "varsIgnorePattern": "fg" }] + "no-unused-vars": ["error", { "varsIgnorePattern": "fg" }], + "no-plusplus": [0], + "no-await-in-loop": [0] } } diff --git a/README.md b/README.md index dc6231e..ff13f57 100644 --- a/README.md +++ b/README.md @@ -34,14 +34,14 @@ class MyPlayer extends Player { } class MyAction extends Action { - constructor(name, priority, subPriority) { - super(name, priority, subPriority); + constructor(id, priority, subPriority) { + super(id, priority, subPriority); } //The logic behind this Action. run() { console.log( - `Name: ${this.name}, Priority: ${this.priority}, SubPriority: ${this.subPriority}` + `ID: ${this.id}, Priority: ${this.priority}, SubPriority: ${this.subPriority}` ); } } @@ -61,7 +61,7 @@ eventListener(GLOBAL_EVENTS.turnEnded, () => { }); //Run one turn of the game -Game.runGameIteration(); +await Game.runGameIteration(); /* Output: diff --git a/src/classes/GameLoop.js b/src/classes/GameLoop.js index cf3fa7f..f562c5f 100644 --- a/src/classes/GameLoop.js +++ b/src/classes/GameLoop.js @@ -2,31 +2,43 @@ const { eventEmitter, GLOBAL_EVENTS } = require('../events.js'); +// Use fg[VarName] pattern for requires used only for flow types. const fgAction = require('./Action'); const fgPlayer = require('./Player'); class GameLoop { declare Players: Array; declare GameActions: Array; + declare stopIteration: boolean; constructor(Players: Array, GameActions: Array) { this.Players = Players; this.GameActions = GameActions; + this.stopIteration = false; } - runGameIteration() { + async runGameIteration() { + this.stopIteration = false; const orderedActions = this.getActions(this.Players, this.GameActions); // Sort by Priority orderedActions.sort(this.sortAction); - orderedActions.forEach((A) => { - A.run(); - }); + for (let i = 0; i < orderedActions.length; i++) { + if (this.stopIteration) { + break; + } + + await orderedActions[i].run(); + } eventEmitter(GLOBAL_EVENTS.turnEnded); } + stopGameIteration() { + this.stopIteration = true; + } + getActions(Players: Array, GameActions: Array): Array { const orderedActions = []; diff --git a/tests/classes/GameLoop.test.js b/tests/classes/GameLoop.test.js index a0a0789..d2323a5 100644 --- a/tests/classes/GameLoop.test.js +++ b/tests/classes/GameLoop.test.js @@ -39,6 +39,23 @@ const Player2 = new MyPlayer(2, [ma2, ma3]); const Game = new GameLoop([Player1, Player2], [ma0, ma1, ma2, ma3, ma4, ma5, ma6, ma7, ma8]); +test('Checking new instance of Action.', (t) => { + t.truthy(Game instanceof GameLoop); + + t.deepEqual(Game.Players, [Player1, Player2]); + t.deepEqual(Game.GameActions, [ma0, ma1, ma2, ma3, ma4, ma5, ma6, ma7, ma8]); + t.is(Game.stopIteration, false); +}); + +test('stopGameIteration should be a function.', (t) => { + t.is(typeof Game.stopGameIteration, typeof function () {}); +}); + +test('stopGameIteration should work.', (t) => { + Game.stopGameIteration(); + t.is(Game.stopIteration, true); +}); + test('sortAction should be a function.', (t) => { t.is(typeof Game.sortAction, typeof function () {}); }); @@ -64,18 +81,27 @@ test('runGameIteration should be a function.', (t) => { t.is(typeof Game.runGameIteration, typeof function () {}); }); -test('runGameIteration should work.', (t) => { - const spy = sinon.spy(Game); - const spyEE = sinon.spy(eventEmitter); - Game.runGameIteration(); +//Still need adjustments on Spys. +test('runGameIteration should work.', async (t) => { + const Game2 = new GameLoop([Player1, Player2], [ma0, ma1]); - t.true(Game.getActions.calledOnce); + //Spys + const gA = sinon.spy(Game2, 'getActions'); + const sA = sinon.spy(Game2, 'sortAction'); + const GA1 = sinon.spy(Game2.GameActions[0], 'run'); + const GA2 = sinon.spy(Game2.GameActions[1], 'run'); + const eE = sinon.spy(eventEmitter); - //Still need adjustments. + await Game2.runGameIteration(); - //Should be 1 - //console.log(spyEE.callCount); + t.true(gA.calledOnce); + //t.true(sA.calledOnce); + //t.true(GA1.calledOnce); + //t.true(GA2.calledOnce); + //t.true(eE.calledOnce); +}); - //t.true(Game.sortAction.calledOnce); - //t.true(spyEE.called); +//Still need adjustments. +test('runGameIteration should stop once stopGameIteration is called.', (t) => { + t.pass(); });