Skip to content

Commit bfe5292

Browse files
authored
Merge pull request #39 from xpquiz/develop
v1.2.1
2 parents f61ddca + 8fd22fb commit bfe5292

File tree

12 files changed

+214
-166
lines changed

12 files changed

+214
-166
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xpquiz.github.io",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"scripts": {
55
"ng": "ng",
66
"start": "ng serve",

src/app/about-window/about-window.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="window">
22
<app-window-title-bar iconPath="about.png" title="About XPQuiz"></app-window-title-bar>
33
<div class="window-body">
4-
<label><b>XPQuiz</b>&nbsp;- Version 1.2.0</label>
4+
<label><b>XPQuiz</b>&nbsp;- Version 1.2.1</label>
55
<label class="main">Created by&nbsp;<b><a href="https://isahann.github.io">Isahann Hanacleto</a></b></label>
66

77
<label>Source code at&nbsp;<b><a href="https://github.com/xpquiz/xpquiz.github.io">GitHub</a></b></label>
Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import {Component, OnInit} from '@angular/core';
22
import {ActivatedRoute, Router} from "@angular/router";
33
import {PathsEnum} from "../../model/enums/PathsEnum";
4-
import {StorageService} from "../../service/storage.service";
5-
import {AppStorage, WeekScore} from "../../model/AppStorage";
6-
import * as moment from "moment";
7-
import {Moment} from "moment/moment";
4+
import {AppStorageService} from "../../service/app-storage.service";
85

96
@Component({
107
selector: 'app-correct-answer-window',
@@ -20,15 +17,13 @@ export class CorrectAnswerWindowComponent implements OnInit {
2017
constructor(
2118
private readonly router: Router,
2219
private readonly route: ActivatedRoute,
23-
private readonly storageService: StorageService
20+
private readonly appStorageService: AppStorageService
2421
) {
2522
this.questionScore = parseInt(this.route.snapshot.paramMap.get('points') ?? '0');
2623
}
2724

2825
public async ngOnInit(): Promise<void> {
29-
const quizCanBeAnswered: boolean = this.checkIfQuizCanBeAnswered();
30-
31-
if (!quizCanBeAnswered) {
26+
if (!this.appStorageService.canQuizBeAnswered()) {
3227
await this.returnHome();
3328
return;
3429
}
@@ -37,43 +32,11 @@ export class CorrectAnswerWindowComponent implements OnInit {
3732
this.saveCurrentScore();
3833
}
3934

40-
private checkIfQuizCanBeAnswered(): boolean {
41-
const appStorage: AppStorage = this.storageService.get();
42-
const lastQuizResponseDate: string | null = appStorage.lastQuizResponseDate;
43-
44-
if (lastQuizResponseDate === null) return true;
45-
46-
const now: Moment = moment();
47-
const nextResponseMinimumDate: Moment = moment(lastQuizResponseDate).add(3, "hours");
48-
49-
return now.isSame(nextResponseMinimumDate) || now.isAfter(nextResponseMinimumDate);
50-
}
51-
5235
public async returnHome(): Promise<void> {
5336
await this.router.navigateByUrl(PathsEnum.HOME);
5437
}
5538

5639
private saveCurrentScore(): void {
57-
const appStorage: AppStorage = this.storageService.get();
58-
const currentWeek: number = moment().isoWeek();
59-
const newCurrentScoreMap: Map<number, WeekScore> = new Map<number, WeekScore>([[currentWeek, {
60-
score: 0,
61-
rightAnswers: 0,
62-
wrongAnswers: 0,
63-
}]]);
64-
const currentWeekScoreMap: Map<number, WeekScore> = appStorage.weekScoreMap ?? newCurrentScoreMap;
65-
const currentWeekScore: WeekScore = currentWeekScoreMap.get(currentWeek)!;
66-
67-
currentWeekScore.rightAnswers += 1;
68-
currentWeekScore.score += this.questionScore;
69-
currentWeekScoreMap.set(currentWeek, currentWeekScore!);
70-
71-
this.storageService.save(
72-
{
73-
...appStorage,
74-
weekScoreMap: currentWeekScoreMap,
75-
lastQuizResponseDate: moment().toISOString()
76-
}
77-
)
40+
this.appStorageService.saveAnswer(true, this.questionScore);
7841
}
7942
}
Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {Component, OnInit} from '@angular/core';
2-
import {StorageService} from "../../service/storage.service";
32
import {Router} from "@angular/router";
43
import {PathsEnum} from "../../model/enums/PathsEnum";
54
import {AppStorage} from "../../model/AppStorage";
65
import * as moment from "moment";
76
import {Duration, Moment} from "moment";
7+
import {AppStorageService} from "../../service/app-storage.service";
88

99
@Component({
1010
selector: 'app-main-window',
@@ -19,46 +19,36 @@ export class MainWindowComponent implements OnInit {
1919
protected readonly PathsEnum = PathsEnum;
2020

2121
constructor(
22-
private readonly storageService: StorageService,
23-
private readonly router: Router
22+
private readonly router: Router,
23+
private readonly appStorageService: AppStorageService
2424
) {
2525
}
2626

2727
public async ngOnInit(): Promise<void> {
28-
const appStorage: AppStorage = this.storageService.get();
29-
const lastQuizResponseDate: string | null = appStorage.lastQuizResponseDate;
30-
31-
this.quizCanBeAnswered = this.checkIfQuizCanBeAnswered(lastQuizResponseDate);
28+
this.quizCanBeAnswered = this.appStorageService.canQuizBeAnswered();
3229

3330
if (!this.quizCanBeAnswered)
34-
this.startCountdown(lastQuizResponseDate);
31+
this.startCountdown();
3532
}
3633

3734
public async redirectTo(route: PathsEnum): Promise<void> {
3835
await this.router.navigateByUrl(route);
3936
}
4037

41-
private checkIfQuizCanBeAnswered(lastQuizResponseDate: string | null): boolean {
42-
if (lastQuizResponseDate === null) return true;
43-
44-
const now: Moment = moment();
45-
const nextResponseMinimumDate: Moment = moment(lastQuizResponseDate).add(3, "hours");
46-
47-
return now.isSame(nextResponseMinimumDate) || now.isAfter(nextResponseMinimumDate);
48-
}
38+
private startCountdown(): void {
39+
const appStorage: AppStorage = this.appStorageService.retrieveAppStorage();
4940

50-
private startCountdown(lastQuizResponseDate: string | null): void {
51-
if (lastQuizResponseDate === null) return;
41+
if (appStorage.lastQuizResponseDate === null) return;
5242

53-
const nextResponseMinimumDate: Moment = moment(lastQuizResponseDate).add(3, "hours");
43+
const nextResponseMinimumDate: Moment = moment(appStorage.lastQuizResponseDate).add(3, "hours");
5444

5545
new Promise<void>(async (resolve): Promise<void> => {
5646
while (true) {
5747
const now: Moment = moment();
5848

5949
if (now.isSame(nextResponseMinimumDate) || now.isAfter(nextResponseMinimumDate)) {
6050
this.quizCanBeAnswered = true;
61-
this.clearLastAnsweredDate();
51+
this.appStorageService.clearLastAnsweredDate();
6252
resolve();
6353
break;
6454
}
@@ -71,15 +61,4 @@ export class MainWindowComponent implements OnInit {
7161
}
7262
});
7363
}
74-
75-
private clearLastAnsweredDate(): void {
76-
const appStorage: AppStorage = this.storageService.get();
77-
78-
this.storageService.save(
79-
{
80-
...appStorage,
81-
lastQuizResponseDate: null
82-
}
83-
);
84-
}
8564
}

src/app/question-window/question-window.component.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import {TriviaService} from "../../service/trivia.service";
33
import {TriviaResponse} from "../../model/TriviaResponse";
44
import {Router} from "@angular/router";
55
import {PathsEnum} from "../../model/enums/PathsEnum";
6-
import {AppStorage} from "../../model/AppStorage";
7-
import {Moment} from "moment";
8-
import * as moment from "moment/moment";
9-
import {StorageService} from "../../service/storage.service";
6+
import {AppStorageService} from "../../service/app-storage.service";
107

118
@Component({
129
selector: 'app-question-window',
@@ -32,14 +29,12 @@ export class QuestionWindowComponent implements OnInit {
3229
constructor(
3330
private readonly triviaService: TriviaService,
3431
private readonly router: Router,
35-
private readonly storageService: StorageService
32+
private readonly appStorageService: AppStorageService
3633
) {
3734
}
3835

3936
public async ngOnInit(): Promise<void> {
40-
const quizCanBeAnswered: boolean = this.checkIfQuizCanBeAnswered();
41-
42-
if (!quizCanBeAnswered) {
37+
if (!this.appStorageService.canQuizBeAnswered()) {
4338
await this.returnHome();
4439
return;
4540
}
@@ -48,18 +43,6 @@ export class QuestionWindowComponent implements OnInit {
4843
await this.loadQuestion();
4944
}
5045

51-
private checkIfQuizCanBeAnswered(): boolean {
52-
const appStorage: AppStorage = this.storageService.get();
53-
const lastQuizResponseDate: string | null = appStorage.lastQuizResponseDate;
54-
55-
if (lastQuizResponseDate === null) return true;
56-
57-
const now: Moment = moment();
58-
const nextResponseMinimumDate: Moment = moment(lastQuizResponseDate).add(3, "hours");
59-
60-
return now.isSame(nextResponseMinimumDate) || now.isAfter(nextResponseMinimumDate);
61-
}
62-
6346
public async onClickAnswer(selectedAnswer: string) {
6447
this.selectedAnswer = selectedAnswer;
6548
await this.confirmAnswerSound.play();

src/app/score-window/score-window.component.html

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<label class="window-body_title_main">Here are your results for week&nbsp;<b>[{{this.currentWeek}}]</b>!</label>
66
<div class="window-body_title_scores">
77
<div class="window-body_title_scores_titles">
8-
<label>Right answers:</label>
9-
<label>Wrong answers:</label>
10-
<label>Total score:</label>
8+
<label>🟩 Right answers:</label>
9+
<label>🟥 Wrong answers:</label>
10+
<label>🟦 Total score:</label>
1111
</div>
1212
<div class="window-body_title_scores_results">
1313
<b><label>[{{this.rightAnswers}}] answers</label></b>
@@ -17,8 +17,27 @@
1717
</div>
1818
</div>
1919

20+
<div class="window-body_previous-scores">
21+
<label class="window-body_previous-scores_title">Here are your previous results:</label>
22+
<ul class="tree-view">
23+
<li>
24+
<ng-container *ngFor="let keyValue of previousScores! | keyvalue">
25+
<details>
26+
<summary>🗓 Week <b>{{keyValue.key}}</b></summary>
27+
<ul>
28+
<li>🟩 <b>{{keyValue.value.rightAnswers}}</b> right answers</li>
29+
<li>🟥 <b>{{keyValue.value.wrongAnswers}}</b> wrong answers</li>
30+
<li>🟦 <b>{{keyValue.value.score}}</b> total points</li>
31+
</ul>
32+
</details>
33+
</ng-container>
34+
</li>
35+
</ul>
36+
</div>
37+
2038
<div class="window-body_buttons">
21-
<app-icon-button iconPath="copy.png" title="Share score" [copy-clipboard]="this.clipboardText" (click)="this.showClipboardMessage()"></app-icon-button>
39+
<app-icon-button iconPath="copy.png" title="Share score" [copy-clipboard]="this.clipboardText"
40+
(click)="this.showClipboardMessage()"></app-icon-button>
2241
<app-icon-button iconPath="home.png" title="Return to home"
2342
(onButtonClick)="this.returnHome()"></app-icon-button>
2443
</div>

src/app/score-window/score-window.component.sass

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,17 @@
4545
color: green
4646
font-weight: bold
4747
margin: 5px
48+
49+
&_previous-scores
50+
width: 100%
51+
display: flex
52+
flex-direction: column
53+
align-items: center
54+
justify-content: center
55+
56+
.tree-view
57+
margin: 5px
58+
width: 200px
59+
max-height: 200px
60+
overflow: auto
61+

src/app/score-window/score-window.component.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {Component, OnInit} from '@angular/core';
2-
import {StorageService} from "../../service/storage.service";
32
import {Router} from "@angular/router";
43
import {PathsEnum} from "../../model/enums/PathsEnum";
5-
import {AppStorage, WeekScore} from "../../model/AppStorage";
6-
import * as moment from "moment";
4+
import {WeekScore} from "../../model/AppStorage";
5+
import moment from "moment";
76
import {TemplateService} from "../../service/template.service";
87
import {TemplateEnum, WeekScoreTemplateParams} from "../../model/enums/Template";
8+
import {AppStorageService} from "../../service/app-storage.service";
99

1010
@Component({
1111
selector: 'app-score-window',
@@ -17,31 +17,19 @@ export class ScoreWindowComponent implements OnInit {
1717
public currentWeek: number = 0;
1818
public rightAnswers: number = 0;
1919
public wrongAnswers: number = 0;
20+
public previousScores: Map<number, WeekScore> | null = null;
2021
public clipboardText: string = '';
2122
public displayClipboardMessage: boolean = false;
2223

2324
constructor(
2425
private readonly router: Router,
25-
private readonly storageService: StorageService,
26-
private readonly templateService: TemplateService
26+
private readonly templateService: TemplateService,
27+
private readonly appStorageService: AppStorageService
2728
) {
2829
}
2930

3031
public async ngOnInit(): Promise<void> {
31-
const currentWeek: number = moment().isoWeek();
32-
const appStorage: AppStorage = this.storageService.get();
33-
const currentWeekScoreMap: Map<number, WeekScore> | undefined = appStorage.weekScoreMap;
34-
35-
this.currentWeek = currentWeek;
36-
37-
if (currentWeekScoreMap === undefined) return;
38-
39-
const currentWeekScore: WeekScore = currentWeekScoreMap.get(currentWeek)!;
40-
41-
this.currentScore = currentWeekScore.score;
42-
this.rightAnswers = currentWeekScore.rightAnswers;
43-
this.wrongAnswers = currentWeekScore.wrongAnswers;
44-
32+
this.retrieveScore();
4533
await this.assembleClipboardText();
4634
}
4735

@@ -67,4 +55,18 @@ export class ScoreWindowComponent implements OnInit {
6755

6856
this.clipboardText = await this.templateService.render(TemplateEnum.WEEK_SCORE, templateParams);
6957
}
58+
59+
private retrieveScore(): void {
60+
const currentWeek: number = moment().isoWeek();
61+
const currentWeekScore: WeekScore = this.appStorageService.retrieveScoreByWeek(currentWeek);
62+
const previousScoresMap: Map<number, WeekScore> = this.appStorageService.retrieveAppStorage().weekScoreMap!;
63+
64+
previousScoresMap.delete(currentWeek);
65+
66+
this.previousScores = previousScoresMap;
67+
this.currentScore = currentWeekScore.score;
68+
this.rightAnswers = currentWeekScore.rightAnswers;
69+
this.wrongAnswers = currentWeekScore.wrongAnswers;
70+
this.currentWeek = currentWeek;
71+
}
7072
}

0 commit comments

Comments
 (0)