Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions app/games/fast-piggie/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,26 @@ function _handleClick(event) {
_resolveRound(wedge);
}

/**
* Resolves the displayed outlier image index to its actual wedge slot.
* @param {object} round
* @returns {number}
*/
function _getCorrectWedgeIndex(round) {
const {
outlierWedgeIndex,
slotAssignment,
imageCount,
wedgeCount,
} = round;

if (slotAssignment && imageCount < wedgeCount) {
return slotAssignment[outlierWedgeIndex] ?? -1;
}

return outlierWedgeIndex;
}

/**
* Resolves the round after a wedge is selected, updates state and feedback.
* @param {number} wedge
Expand All @@ -438,6 +458,7 @@ function _resolveRound(wedge) {
imageCount,
} = _currentRound;
const { width, height } = _canvas;
const correctWedgeIndex = _getCorrectWedgeIndex(_currentRound);
// Map clicked wedge to image index if slotAssignment is used
let answerIdx = wedge;
if (slotAssignment && imageCount < wedgeCount) {
Expand Down Expand Up @@ -477,14 +498,16 @@ function _resolveRound(wedge) {
'rgba(220, 53, 69, 0.45)',
);
// Reveal the correct wedge in yellow
highlightWedge(
_ctx,
width,
height,
outlierWedgeIndex,
wedgeCount,
'rgba(255, 193, 7, 0.65)',
);
if (correctWedgeIndex !== -1) {
highlightWedge(
_ctx,
width,
height,
correctWedgeIndex,
wedgeCount,
'rgba(255, 193, 7, 0.65)',
);
}
game.addMiss(imageCount);
playFailureSound(audioCtx);
_triggerFlash('wrong');
Expand Down
25 changes: 25 additions & 0 deletions app/games/fast-piggie/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,31 @@ describe('_handleClick — wrong answer (checkAnswer returns false)', () => {
const flash = container.querySelector('#fp-flash');
expect(flash.classList.contains('fp-flash--wrong')).toBe(true);
});

it('highlights the actual outlier wedge slot when images were shuffled', () => {
plugin.reset();
const mathRandomSpy = jest.spyOn(Math, 'random')
.mockReturnValueOnce(0)
.mockReturnValueOnce(0)
.mockReturnValueOnce(0)
.mockReturnValueOnce(0)
.mockReturnValueOnce(0);

plugin.start();
jest.runAllTimers();
ctx2d.arc.mockClear();

fireClick();

const yellowHighlightCall = ctx2d.arc.mock.calls[1];
const expectedStartAngle = -Math.PI / 2 + ((2 * Math.PI) / 6) * 3;
const expectedEndAngle = expectedStartAngle + ((2 * Math.PI) / 6);

expect(yellowHighlightCall[3]).toBeCloseTo(expectedStartAngle);
expect(yellowHighlightCall[4]).toBeCloseTo(expectedEndAngle);

mathRandomSpy.mockRestore();
});
});

// ===========================================================================
Expand Down
Loading
Loading