Skip to content

Commit 5bbbe77

Browse files
committed
#53 checkpoint
1 parent fdfa9ad commit 5bbbe77

File tree

10 files changed

+229
-59
lines changed

10 files changed

+229
-59
lines changed

package-lock.json

Lines changed: 39 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xpquiz.github.io",
3-
"version": "1.3.3",
3+
"version": "1.4.0",
44
"scripts": {
55
"ng": "ng",
66
"start": "ng serve",
@@ -34,6 +34,7 @@
3434
"@angular/compiler-cli": "^16.1.0",
3535
"@types/jasmine": "~4.3.0",
3636
"@types/mustache": "^4.2.2",
37+
"@types/node": "^20.12.7",
3738
"jasmine-core": "~4.6.0",
3839
"karma": "~6.4.0",
3940
"karma-chrome-launcher": "~3.2.0",

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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.3.3</label>
4+
<label><b>XPQuiz</b>&nbsp;- Version 1.4.0</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>
8-
<label>Questions provided by&nbsp;<b><a href="https://the-trivia-api.com/">The Trivia API</a></b></label>
8+
<label>Questions provided by&nbsp;<b><a href="https://the-trivia-api.com/">The Trivia API</a></b>,&nbsp;<b><a
9+
href="https://opentdb.com/">Open Trivia Database</a></b>&nbsp;and&nbsp;<b><a href="https://quizapi.io/">QuizAPI</a></b></label>
910
<label>Windows XP theming provided by&nbsp;<b><a href="https://botoxparty.github.io/XP.css/">XP.css</a></b></label>
1011
<label>Windows XP sounds provided by&nbsp;<b><a href="https://archive.org/details/windowsxpstartup_201910/">The
1112
Internet Archive</a></b></label>

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {Component, OnDestroy, OnInit} from '@angular/core';
22
import {TriviaService} from "../../service/trivia.service";
3-
import {TriviaResponse} from "../../model/TriviaResponse";
43
import {Router} from "@angular/router";
54
import {PathsEnum} from "../../model/enums/PathsEnum";
65
import {AppStorageService} from "../../service/app-storage.service";
76
import {QuestionResultTemplateParams} from "../../model/Template";
87
import {EncryptionService} from "../../service/encryption.service";
98
import {Subscription} from "rxjs";
9+
import {Question} from "../../model/questions/Question";
1010

1111
@Component({
1212
selector: 'app-question-window',
@@ -46,7 +46,10 @@ export class QuestionWindowComponent implements OnInit, OnDestroy {
4646
}
4747

4848
this.startLoadingProgressBar();
49-
this.loadQuestion();
49+
50+
while(!this.questionLoaded) {
51+
await this.loadQuestion();
52+
}
5053
}
5154

5255
public ngOnDestroy() {
@@ -64,20 +67,23 @@ export class QuestionWindowComponent implements OnInit, OnDestroy {
6467
return selectedAnswer ? `> ${answer} <` : answer;
6568
}
6669

67-
private loadQuestion(): void {
68-
this.getQuizzesSubscription = this.triviaService.getQuizzes().subscribe((response: TriviaResponse[]): void => {
69-
const singleQuiz: TriviaResponse = response[0];
70+
private async loadQuestion(): Promise<void> {
71+
const questions: Question[] = await this.triviaService.fetchQuestion()
72+
73+
if (questions.length === 0)
74+
return;
75+
76+
const singleQuiz: Question = questions[0];
7077

71-
this.question = singleQuiz.question.text;
72-
this.correctAnswer = singleQuiz.correctAnswer;
73-
this.answers = [singleQuiz.correctAnswer, ...singleQuiz.incorrectAnswers]
74-
.map((value) => ({value, sort: Math.random()}))
75-
.sort((a, b) => a.sort - b.sort)
76-
.map(({value}) => value);
78+
this.question = singleQuiz.question;
79+
this.correctAnswer = singleQuiz.correctAnswer;
80+
this.answers = [singleQuiz.correctAnswer, ...singleQuiz.incorrectAnswers]
81+
.map((value) => ({value, sort: Math.random()}))
82+
.sort((a, b) => a.sort - b.sort)
83+
.map(({value}) => value);
7784

78-
this.questionPoints = this.sumQuestionPoints(singleQuiz.difficulty, singleQuiz.isNiche);
79-
this.questionLoaded = true;
80-
});
85+
this.questionPoints = this.sumQuestionPoints(singleQuiz.difficulty, singleQuiz.isNiche);
86+
this.questionLoaded = true;
8187
}
8288

8389
public async confirmAnswer(): Promise<void> {
@@ -130,15 +136,15 @@ export class QuestionWindowComponent implements OnInit, OnDestroy {
130136
let revertProgressBar: boolean = false;
131137

132138
while (true) {
133-
if(this.loadingProgressBar === 100) {
139+
if (this.loadingProgressBar === 100) {
134140
revertProgressBar = true;
135141

136-
if(this.questionLoaded){
142+
if (this.questionLoaded) {
137143
this.showQuestion = true;
138144
await this.questionReadySound.play();
139145
break;
140146
}
141-
} else if(this.loadingProgressBar === 0) {
147+
} else if (this.loadingProgressBar === 0) {
142148
revertProgressBar = false;
143149
}
144150

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export interface OpenTriviaDBResponse {
2+
response_code: 0 | 1 | 2 | 3 | 4 | 5,
3+
results: OpenTriviaDBQuestion[]
4+
}
5+
6+
interface OpenTriviaDBQuestion {
7+
type: string;
8+
difficulty: string,
9+
category: 'easy' | 'medium' | 'hard',
10+
question: string,
11+
correct_answer: string,
12+
incorrect_answers: string[]
13+
}

src/model/questions/Question.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface Question {
2+
question: string;
3+
correctAnswer: string;
4+
incorrectAnswers: string[];
5+
difficulty: 'easy' | 'medium' | 'hard';
6+
isNiche: boolean;
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export interface QuizAPIResponse {
2+
id: number,
3+
question: string,
4+
description: string,
5+
answers: QuizAPIResponseAnswers
6+
multiple_correct_answers: boolean,
7+
correct_answers: QuizAPIResponseCorrectAnswers
8+
correct_answer: string,
9+
explanation: string,
10+
tip: string,
11+
tags: QuizAPIResponseTag[],
12+
category: string,
13+
difficulty: 'Easy' | 'Medium' | 'Hard'
14+
}
15+
16+
export interface QuizAPIResponseAnswers {
17+
answer_a: string | null,
18+
answer_b: string | null,
19+
answer_c: string | null,
20+
answer_d: string | null,
21+
answer_e: string | null,
22+
answer_f: string | null,
23+
}
24+
25+
export interface QuizAPIResponseCorrectAnswers {
26+
answer_a_correct: boolean,
27+
answer_b_correct: boolean,
28+
answer_c_correct: boolean,
29+
answer_d_correct: boolean,
30+
answer_e_correct: boolean,
31+
answer_f_correct: boolean,
32+
}
33+
34+
interface QuizAPIResponseTag {
35+
name: string
36+
}

src/model/TriviaResponse.ts renamed to src/model/questions/TheTriviaApiResponse.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export interface TriviaResponse {
1+
export interface TheTriviaApiResponse {
22
category:
33
| 'music'
44
| 'sport_and_leisure'
@@ -15,12 +15,12 @@ export interface TriviaResponse {
1515
difficulty: 'easy' | 'medium' | 'hard';
1616
regions: string[];
1717
isNiche: boolean;
18-
question: Question;
18+
question: TheTriviaApiQuestion;
1919
correctAnswer: string;
2020
incorrectAnswers: string[];
2121
type: 'text_choice';
2222
}
2323

24-
interface Question {
24+
interface TheTriviaApiQuestion {
2525
text: string;
2626
}

0 commit comments

Comments
 (0)