Skip to content

Commit

Permalink
Making step accept a Promise from bot.play() (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
vdfdev authored and nicolodavis committed Mar 4, 2019
1 parent 8bafa64 commit 4c3056c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ class _ClientImpl {
if (ai !== undefined && multiplayer === undefined) {
const bot = new ai.bot({ game, enumerate: ai.enumerate });

this.step = () => {
this.step = async () => {
const state = this.store.getState();
const playerID = state.ctx.actionPlayers[0];
const { action, metadata } = bot.play(state, playerID);
const { action, metadata } = await bot.play(state, playerID);

if (action) {
action.payload.metadata = metadata;
Expand Down
4 changes: 2 additions & 2 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ describe('step', () => {
},
});

test('advances game state', () => {
test('advances game state', async () => {
expect(client.getState().G).toEqual({ moved: false });
client.step();
await client.step();
expect(client.getState().G).toEqual({ moved: true });
});

Expand Down
12 changes: 6 additions & 6 deletions src/client/debug/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ export class Debug extends React.Component {
};

simulate = (iterations = 10000, sleepTimeout = 100) => {
const step = () => {
const action = this.props.step();
if (action && iterations > 1) {
iterations--;
setTimeout(step, sleepTimeout);
const step = async () => {
for (let i = 0; i < iterations; i++) {
const action = await this.props.step();
if (!action) break;
await new Promise(resolve => setTimeout(resolve, sleepTimeout));
}
};

step();
return step();
};

render() {
Expand Down
8 changes: 4 additions & 4 deletions src/client/debug/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test('basic', () => {
<Debug
gamestate={gamestate}
store={store}
step={() => {}}
step={async () => {}}
endTurn={() => {}}
gameID="default"
/>
Expand Down Expand Up @@ -208,7 +208,7 @@ describe('simulate', () => {
jest.useFakeTimers();

test('basic', () => {
const step = jest.fn(() => true);
const step = jest.fn(async () => true);
Enzyme.mount(
<Debug
step={step}
Expand All @@ -220,11 +220,11 @@ describe('simulate', () => {
expect(step).not.toHaveBeenCalled();
Mousetrap.simulate('5');
jest.runAllTimers();
expect(step).toHaveBeenCalledTimes(10000);
expect(step).toHaveBeenCalled();
});

test('break out if no action is returned', () => {
const step = jest.fn(() => undefined);
const step = jest.fn(async () => undefined);
Enzyme.mount(
<Debug
step={step}
Expand Down

0 comments on commit 4c3056c

Please sign in to comment.