-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from dxw/update-answers-and-bonus-points-at-t…
…urn-end Update scores and bonus points at turn end
- Loading branch information
Showing
6 changed files
with
255 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { describe, expect, it } from "bun:test"; | ||
import type { Colour, Player, PlayerScore } from "../@types/entities"; | ||
import { | ||
getCorrectSocketIdsFromAnswers, | ||
getUpdatedPlayerScoresAndBonusPoints, | ||
} from "./scoringUtils"; | ||
|
||
describe("scoringUtils", () => { | ||
describe("getCorrectSocketIdsFromAnswers", () => { | ||
const correctAnswer: Colour[] = ["red", "blue"]; | ||
const incorrectAnswer: Colour[] = ["pink", "blue"]; | ||
|
||
it("returns the IDs of the players with the correct answers", () => { | ||
expect( | ||
getCorrectSocketIdsFromAnswers( | ||
[ | ||
{ colours: correctAnswer, socketId: "1" }, | ||
{ colours: incorrectAnswer, socketId: "2" }, | ||
{ colours: correctAnswer, socketId: "3" }, | ||
], | ||
correctAnswer, | ||
), | ||
).toEqual(["1", "3"]); | ||
}); | ||
|
||
it("returns an empty array if there are no correct answers", () => { | ||
expect( | ||
getCorrectSocketIdsFromAnswers( | ||
[ | ||
{ colours: incorrectAnswer, socketId: "1" }, | ||
{ colours: incorrectAnswer, socketId: "2" }, | ||
], | ||
correctAnswer, | ||
), | ||
).toBeArrayOfSize(0); | ||
}); | ||
}); | ||
|
||
describe("getUpdatedPlayerScoresAndBonusPoints", () => { | ||
describe("if all players are correct", () => { | ||
it("increments the bonus points and returns the player scores unchanged", () => { | ||
const currentBonusPoints = 0; | ||
const correctPlayerSocketIds = ["1", "2"]; | ||
const currentPlayerScores: PlayerScore[] = [ | ||
{ | ||
player: { name: "olaf", socketId: correctPlayerSocketIds[0] }, | ||
score: 1, | ||
}, | ||
{ | ||
player: { name: "alex", socketId: correctPlayerSocketIds[1] }, | ||
score: 0, | ||
}, | ||
]; | ||
|
||
expect( | ||
getUpdatedPlayerScoresAndBonusPoints( | ||
currentBonusPoints, | ||
currentPlayerScores, | ||
correctPlayerSocketIds, | ||
), | ||
).toEqual({ | ||
bonusPoints: 1, | ||
playerScores: currentPlayerScores, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("if all players are incorrect", () => { | ||
it("resets the bonus points and returns the player scores unchanged", () => { | ||
const currentBonusPoints = 3; | ||
const correctPlayerSocketIds: Player["socketId"][] = []; | ||
const currentPlayerScores: PlayerScore[] = [ | ||
{ | ||
player: { name: "olaf", socketId: "1" }, | ||
score: 1, | ||
}, | ||
{ | ||
player: { name: "alex", socketId: "2" }, | ||
score: 0, | ||
}, | ||
]; | ||
|
||
expect( | ||
getUpdatedPlayerScoresAndBonusPoints( | ||
currentBonusPoints, | ||
currentPlayerScores, | ||
correctPlayerSocketIds, | ||
), | ||
).toEqual({ | ||
bonusPoints: 0, | ||
playerScores: currentPlayerScores, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("if some players are correct and others incorrect", () => { | ||
describe("and there are no bonus points", () => { | ||
it("awards points to the correct players and returns the player scores", () => { | ||
const currentBonusPoints = 2; | ||
const correctPlayerSocketIds = ["1", "3"]; | ||
const currentPlayerScores: PlayerScore[] = [ | ||
{ | ||
player: { name: "olaf", socketId: correctPlayerSocketIds[0] }, | ||
score: 0, | ||
}, | ||
{ | ||
player: { name: "alex", socketId: "2" }, | ||
score: 0, | ||
}, | ||
{ | ||
player: { name: "james", socketId: correctPlayerSocketIds[1] }, | ||
score: 0, | ||
}, | ||
]; | ||
|
||
expect( | ||
getUpdatedPlayerScoresAndBonusPoints( | ||
currentBonusPoints, | ||
currentPlayerScores, | ||
correctPlayerSocketIds, | ||
), | ||
).toEqual({ | ||
bonusPoints: 0, | ||
playerScores: [ | ||
{ player: { name: "olaf", socketId: "1" }, score: 3 }, | ||
{ player: { name: "alex", socketId: "2" }, score: 0 }, | ||
{ player: { name: "james", socketId: "3" }, score: 3 }, | ||
], | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import type { Answer, Player, PlayerScore, Question } from "../@types/entities"; | ||
|
||
const allCorrect = ( | ||
totalPlayerCount: number, | ||
correctPlayerSocketIds: Player["socketId"][], | ||
): boolean => { | ||
return correctPlayerSocketIds.length === totalPlayerCount; | ||
}; | ||
|
||
const allIncorrect = ( | ||
correctPlayerSocketIds: Player["socketId"][], | ||
): boolean => { | ||
return correctPlayerSocketIds.length === 0; | ||
}; | ||
|
||
const getUpdatedPlayerScores = ( | ||
currentPlayerScores: PlayerScore[], | ||
bonusPoints: number, | ||
correctPlayerSocketIds: Player["socketId"][], | ||
): PlayerScore[] => { | ||
const numberOfIncorrectAnswers = | ||
currentPlayerScores.length - correctPlayerSocketIds.length; | ||
const pointsToAward = numberOfIncorrectAnswers + bonusPoints; | ||
|
||
return currentPlayerScores.map(({ player, score }) => { | ||
if (correctPlayerSocketIds.includes(player.socketId)) { | ||
return { player, score: score + pointsToAward }; | ||
} | ||
|
||
return { player, score }; | ||
}); | ||
}; | ||
|
||
const getUpdatedPlayerScoresAndBonusPoints = ( | ||
currentBonusPoints: number, | ||
currentPlayerScores: PlayerScore[], | ||
correctPlayerSocketIds: Player["socketId"][], | ||
): { bonusPoints: number; playerScores: PlayerScore[] } => { | ||
if (allCorrect(currentPlayerScores.length, correctPlayerSocketIds)) { | ||
return { | ||
bonusPoints: currentBonusPoints + 1, | ||
playerScores: currentPlayerScores, | ||
}; | ||
} | ||
|
||
if (allIncorrect(correctPlayerSocketIds)) { | ||
return { bonusPoints: 0, playerScores: currentPlayerScores }; | ||
} | ||
|
||
return { | ||
bonusPoints: 0, | ||
playerScores: getUpdatedPlayerScores( | ||
currentPlayerScores, | ||
currentBonusPoints, | ||
correctPlayerSocketIds, | ||
), | ||
}; | ||
}; | ||
|
||
const getCorrectSocketIdsFromAnswers = ( | ||
answers: Answer[], | ||
correctAnswer: Question["colours"], | ||
) => { | ||
return answers | ||
.filter((answer) => { | ||
if (correctAnswer.length !== answer.colours.length) { | ||
return false; | ||
} | ||
|
||
return correctAnswer.every((colour) => answer.colours.includes(colour)); | ||
}) | ||
.map((answer) => answer.socketId); | ||
}; | ||
|
||
export { getCorrectSocketIdsFromAnswers, getUpdatedPlayerScoresAndBonusPoints }; |