Skip to content

Commit

Permalink
Have Gunther answer calculation tests when they expire
Browse files Browse the repository at this point in the history
Fixes #770
  • Loading branch information
RussellLVP committed Jul 22, 2020
1 parent 9a2c23c commit 5a85db2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
14 changes: 14 additions & 0 deletions javascript/features/reaction_tests/reaction_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,20 @@ export default class ReactionTests extends Feature {
if (this.activeTestWinnerTime_ !== null)
return; // someone answered the previous reaction test, another was scheduled

// Some tests might prefer Gunther to share the answer rather than timing them out silently,
// when the answer might be beneficial for players on the server.
if (this.activeTest_.answerThroughGunter) {
const gunther = server.playerManager.getByName('Gunther');
if (gunther) {
dispatchEvent('playertext', {
playerid: gunther.id,
text: this.activeTest_.answer,
});

return;
}
}

this.activeTest_.stop();
this.activeTest_ = null;

Expand Down
28 changes: 26 additions & 2 deletions javascript/features/reaction_tests/reaction_tests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by the MIT license, a copy of which can
// be found in the LICENSE file.

import { CalculationStrategy } from 'features/reaction_tests/strategies/calculation_strategy.js';
import ReactionTests from 'features/reaction_tests/reaction_tests.js';
import { RememberStrategy } from 'features/reaction_tests/strategies/remember_strategy.js';
import Settings from 'features/settings/settings.js';
Expand Down Expand Up @@ -177,12 +178,12 @@ describe('ReactionTests', (it, beforeEach) => {
await server.clock.advance(timeout * 1000);

await gunther.issueMessage(answer); // goes to main chat
assert.equal(gunther.messages.length, 2);
assert.isAboveOrEqual(gunther.messages.length, 2);

// Wait until we're certain that the next reaction test has started.
await server.clock.advance((delay + jitter) * 1000);

assert.equal(gunther.messages.length, 3);
assert.isAboveOrEqual(gunther.messages.length, 3);
});

it('should award achievements at certain milestones', async (assert) => {
Expand Down Expand Up @@ -236,4 +237,27 @@ describe('ReactionTests', (it, beforeEach) => {
assert.isTrue(
collectables.hasAchievement(gunther, achievements.kAchievementReactionTestSequence));
});

it('should automatically answer through Gunther when certain tests timeout', async (assert) => {
// Only enable calculation strategies, which we know to enable this capability.
driver.strategies_ = [ CalculationStrategy ];

const delay = settings.getValue('playground/reaction_test_delay_sec');
const jitter = settings.getValue('playground/reaction_test_jitter_sec');
const timeout = settings.getValue('playground/reaction_test_expire_sec');

assert.equal(lucy.messages.length, 0);

// Wait until we're certain that the first reaction test has started.
await server.clock.advance((delay + jitter) * 1000);

assert.equal(lucy.messages.length, 1);

// Wait for the timeout. Gunther will automatically share the right answer.
await server.clock.advance(timeout * 1000);

assert.equal(lucy.messages.length, 3);
assert.includes(lucy.messages[1], 'has won the reaction test');
assert.includes(lucy.messages[2], gunther.name);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export class CalculationStrategy extends Strategy {
// Gets the answer to the current reaction test. May be NULL.
get answer() { return this.answer_; }

// Answers should be given by Gunther when the reaction test is about to time out.
get answerThroughGunter() { return true; }

// Gets the calculation that the players were asked to resolve. May be NULL.
get calculation() { return this.calculation_; }

Expand Down
3 changes: 3 additions & 0 deletions javascript/features/reaction_tests/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export class Strategy {
// Time, in milliseconds, to offset the player's actual answer time with.
get answerOffsetTimeMs() { return 0; }

// Whether an answer should be given through Gunther when the test is about to time out.
get answerThroughGunter() { return false; }

// Verifies whether the |message| is, or contains, the answer to this reaction test.
verify(message) {
throw new Error('This method must be overridden by the strategy.');
Expand Down

0 comments on commit 5a85db2

Please sign in to comment.