-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* MNG-44 Add GameHandler to store user game results * Tests * Corrections Co-authored-by: Daria Dziubaltowska <daria305@gmail.com>
- Loading branch information
1 parent
d345f47
commit a9fe237
Showing
5 changed files
with
78 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
export class GameHandler { | ||
constructor(name, totalTime) { | ||
this.name = name; | ||
this.totalTime = totalTime; //seconds | ||
this.score = 0; | ||
this.answers = []; | ||
} | ||
|
||
addAnswer(correctAnswer, answer, isCorrect) { | ||
this.answers.push({ | ||
answer: answer, | ||
correctAnswer: correctAnswer, | ||
isCorrect: isCorrect, | ||
}) | ||
if (isCorrect) { | ||
this.score++; | ||
} | ||
} | ||
|
||
// Subtruct time left from total time | ||
// returns time of the game | ||
calculateTime(timeLeft) { | ||
return this.totalTime - timeLeft | ||
} | ||
|
||
getResults(timeLeft) { | ||
return { | ||
name: this.name, | ||
time: this.calculateTime(timeLeft), | ||
score: this.score, | ||
answers: this.answers, | ||
} | ||
} | ||
} |
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,23 @@ | ||
import{GameHandler} from "./GameHandler" | ||
|
||
describe('Store user choosen and statistic during game', () => { | ||
it('Should update game statistic', () => { | ||
//given | ||
const gameHandler = new GameHandler("Ala", 120); | ||
const correctAnswer = "pikachu"; | ||
const answer = "charmander"; | ||
const isCorrect = false; | ||
//when | ||
gameHandler.addAnswer(correctAnswer, answer, isCorrect) | ||
//then | ||
expect(gameHandler.getResults(15)).toEqual({ | ||
name: "Ala", | ||
time: 105, | ||
score: 0, | ||
answers: [{ | ||
correctAnswer:"pikachu", | ||
answer: "charmander", | ||
isCorrect: false}] | ||
}) | ||
}) | ||
}) |