Skip to content

Commit

Permalink
Issue #9 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
Katreque committed Dec 4, 2020
1 parent 914954c commit 337171a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
);
}
}
Expand All @@ -61,7 +61,7 @@ eventListener(GLOBAL_EVENTS.turnEnded, () => {
});

//Run one turn of the game
Game.runGameIteration();
await Game.runGameIteration();

/*
Output:
Expand Down
20 changes: 16 additions & 4 deletions src/classes/GameLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<fgPlayer>;
declare GameActions: Array<fgAction>;
declare stopIteration: boolean;

constructor(Players: Array<fgPlayer>, GameActions: Array<fgAction>) {
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<fgPlayer>, GameActions: Array<fgAction>): Array<fgAction> {
const orderedActions = [];

Expand Down
46 changes: 36 additions & 10 deletions tests/classes/GameLoop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {});
});
Expand All @@ -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();
});

0 comments on commit 337171a

Please sign in to comment.