From feec462f326a30225bee1bab23bd44709c249f6a Mon Sep 17 00:00:00 2001 From: Isahann Hanacleto Date: Mon, 7 Aug 2023 19:23:21 -0300 Subject: [PATCH] #22 creating map for score on cache --- .../correct-answer-window.component.ts | 16 ++++++++-- .../score-window/score-window.component.html | 15 ++++++++-- .../score-window/score-window.component.sass | 22 ++++++++++++-- .../score-window/score-window.component.ts | 21 ++++++++++++-- .../wrong-answer-window.component.ts | 29 ++++++++++++++----- src/model/AppStorage.ts | 10 +++++-- src/service/storage.service.ts | 25 ++++++++++++++-- 7 files changed, 117 insertions(+), 21 deletions(-) diff --git a/src/app/correct-answer-window/correct-answer-window.component.ts b/src/app/correct-answer-window/correct-answer-window.component.ts index e372f56..bb3e6ed 100644 --- a/src/app/correct-answer-window/correct-answer-window.component.ts +++ b/src/app/correct-answer-window/correct-answer-window.component.ts @@ -2,7 +2,7 @@ import {Component, OnInit} from '@angular/core'; import {ActivatedRoute, Router} from "@angular/router"; import {PathsEnum} from "../../model/PathsEnum"; import {StorageService} from "../../service/storage.service"; -import {AppStorage} from "../../model/AppStorage"; +import {AppStorage, WeekScore} from "../../model/AppStorage"; import * as moment from "moment"; @Component({ @@ -35,11 +35,23 @@ export class CorrectAnswerWindowComponent implements OnInit { private saveCurrentScore(): void { const appStorage: AppStorage = this.storageService.get(); + const currentWeek: number = moment().isoWeek(); + const newCurrentScoreMap: Map = new Map([[currentWeek, { + score: 0, + rightAnswers: 0, + wrongAnswers: 0, + }]]); + const currentWeekScoreMap: Map = appStorage.weekScoreMap ?? newCurrentScoreMap; + const currentWeekScore: WeekScore = currentWeekScoreMap.get(currentWeek)!; + + currentWeekScore.rightAnswers += 1; + currentWeekScore.score += this.questionScore; + currentWeekScoreMap.set(currentWeek, currentWeekScore!); this.storageService.save( { ...appStorage, - currentScore: appStorage.currentScore + this.questionScore, + weekScoreMap: currentWeekScoreMap, lastQuizResponseDate: moment().toISOString() } ) diff --git a/src/app/score-window/score-window.component.html b/src/app/score-window/score-window.component.html index 5de6b3c..b36a930 100644 --- a/src/app/score-window/score-window.component.html +++ b/src/app/score-window/score-window.component.html @@ -2,9 +2,20 @@
- + +
+
+ + + +
+
+ + + +
+
-
diff --git a/src/app/score-window/score-window.component.sass b/src/app/score-window/score-window.component.sass index 53965c2..181250c 100644 --- a/src/app/score-window/score-window.component.sass +++ b/src/app/score-window/score-window.component.sass @@ -10,6 +10,24 @@ align-items: center justify-content: center - label - margin: 5px + &_main + margin: 10px text-align: center + + &_scores + display: flex + flex-direction: row + align-items: center + justify-content: space-between + width: 200px + margin: 10px + + &_titles + display: flex + flex-direction: column + margin: 3px + + &_results + display: flex + flex-direction: column + margin: 3px diff --git a/src/app/score-window/score-window.component.ts b/src/app/score-window/score-window.component.ts index f4bf883..dc7f61c 100644 --- a/src/app/score-window/score-window.component.ts +++ b/src/app/score-window/score-window.component.ts @@ -2,7 +2,8 @@ import {Component, OnInit} from '@angular/core'; import {StorageService} from "../../service/storage.service"; import {Router} from "@angular/router"; import {PathsEnum} from "../../model/PathsEnum"; -import {AppStorage} from "../../model/AppStorage"; +import {AppStorage, WeekScore} from "../../model/AppStorage"; +import * as moment from "moment"; @Component({ selector: 'app-score-window', @@ -10,7 +11,10 @@ import {AppStorage} from "../../model/AppStorage"; styleUrls: ['./score-window.component.sass'] }) export class ScoreWindowComponent implements OnInit { - public score: number = 0; + public currentScore: number = 0; + public currentWeek: number = 0; + public rightAnswers: number = 0; + public wrongAnswers: number = 0; constructor( private readonly router: Router, @@ -19,8 +23,19 @@ export class ScoreWindowComponent implements OnInit { } public ngOnInit(): void { + const currentWeek: number = moment().isoWeek(); const appStorage: AppStorage = this.storageService.get(); - this.score = appStorage.currentScore; + const currentWeekScoreMap: Map | undefined = appStorage.weekScoreMap; + + this.currentWeek = currentWeek; + + if(currentWeekScoreMap === undefined) return; + + const currentWeekScore: WeekScore = currentWeekScoreMap.get(currentWeek)!; + + this.currentScore = currentWeekScore.score; + this.rightAnswers = currentWeekScore.rightAnswers; + this.wrongAnswers = currentWeekScore.wrongAnswers; } public async returnHome(): Promise { diff --git a/src/app/wrong-answer-window/wrong-answer-window.component.ts b/src/app/wrong-answer-window/wrong-answer-window.component.ts index 137b236..47dd7a8 100644 --- a/src/app/wrong-answer-window/wrong-answer-window.component.ts +++ b/src/app/wrong-answer-window/wrong-answer-window.component.ts @@ -2,7 +2,7 @@ import {Component, OnInit} from '@angular/core'; import {ActivatedRoute, Router} from "@angular/router"; import {PathsEnum} from "../../model/PathsEnum"; import {StorageService} from "../../service/storage.service"; -import {AppStorage} from "../../model/AppStorage"; +import {AppStorage, WeekScore} from "../../model/AppStorage"; import * as moment from "moment"; @Component({ @@ -25,20 +25,33 @@ export class WrongAnswerWindowComponent implements OnInit { public async ngOnInit(): Promise { await this.wrongAnswerSound.play(); - this.updateStorage(); + this.saveCurrentScore(); } public async returnHome(): Promise { await this.router.navigateByUrl(PathsEnum.HOME); } - private updateStorage() { + private saveCurrentScore() { const appStorage: AppStorage = this.storageService.get(); + const currentWeek: number = moment().isoWeek(); + const newCurrentScoreMap: Map = new Map([[currentWeek, { + score: 0, + rightAnswers: 0, + wrongAnswers: 0, + }]]); + const currentWeekScoreMap: Map = appStorage.weekScoreMap ?? newCurrentScoreMap; + const currentWeekScore: WeekScore = currentWeekScoreMap.get(currentWeek)!; - this.storageService.save({ - ...appStorage, - currentScore: appStorage.currentScore, - lastQuizResponseDate: moment().toISOString() - }); + currentWeekScore!.wrongAnswers += 1; + currentWeekScoreMap.set(currentWeek, currentWeekScore!); + + this.storageService.save( + { + ...appStorage, + weekScoreMap: currentWeekScoreMap, + lastQuizResponseDate: moment().toISOString() + } + ) } } diff --git a/src/model/AppStorage.ts b/src/model/AppStorage.ts index 7ce4614..39362b6 100644 --- a/src/model/AppStorage.ts +++ b/src/model/AppStorage.ts @@ -1,4 +1,10 @@ export interface AppStorage { - currentScore: number, - lastQuizResponseDate: string | null + lastQuizResponseDate: string | null, + weekScoreMap: Map | undefined +} + +export interface WeekScore { + score: number, + rightAnswers: number, + wrongAnswers: number } diff --git a/src/service/storage.service.ts b/src/service/storage.service.ts index b042cc7..e50db91 100644 --- a/src/service/storage.service.ts +++ b/src/service/storage.service.ts @@ -16,7 +16,7 @@ export class StorageService { } public save(value: AppStorage): void { - const stringfiedObject: string = JSON.stringify(value); + const stringfiedObject: string = JSON.stringify(value, this.replacer); const encryptedObject: string = this.encrypt(stringfiedObject); this.localStorage.setItem(this.storageKey, encryptedObject); @@ -29,7 +29,7 @@ export class StorageService { currentScore: 0, lastQuizResponseDate: null } : - JSON.parse(this.decrypt(encryptedItem)); + JSON.parse(this.decrypt(encryptedItem), this.reviver); } private encrypt(value: string): string { @@ -41,4 +41,25 @@ export class StorageService { const decrypted = AES.decrypt(value, this.encryptionkey); return decrypted.toString(enc.Utf8); } + + private replacer(key: any, value: any) { + if(value instanceof Map) { + return { + dataType: 'Map', + value: Array.from(value.entries()), // or with spread: value: [...value] + }; + } else { + return value; + } + } + + private reviver(key: any, value: any) { + if(typeof value === 'object' && value !== null) { + if (value.dataType === 'Map') { + return new Map(value.value); + } + } + return value; + } + }