Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/app/correct-answer-window/correct-answer-window.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Component, OnInit} from '@angular/core';
import {Router} from "@angular/router";
import {PathsEnum} from "../../model/PathsEnum";
import {StorageService} from "../../service/storage.service";
import {StorageKeyEnum} from "../../model/StorageKeyEnum";
import {AppStorage} from "../../model/AppStorage";

@Component({
selector: 'app-correct-answer-window',
Expand Down Expand Up @@ -31,12 +31,14 @@ export class CorrectAnswerWindowComponent implements OnInit {
}

private saveCurrentScore(): void {
const currentScore: string | null = this.storageService.get(StorageKeyEnum.CURRENT_SCORE);
let currentScoreNumber: number = (currentScore === null || currentScore === '') ? 0 : parseInt(currentScore);

currentScoreNumber += this.questionScore;

this.storageService.save(StorageKeyEnum.CURRENT_SCORE, currentScoreNumber.toString());
this.storageService.save(StorageKeyEnum.LAST_QUIZ_RESPONSE_DATE, new Date().getTime().toString());
const appStorage: AppStorage = this.storageService.get();

this.storageService.save(
{
...appStorage,
currentScore: appStorage.currentScore + this.questionScore,
lastQuizResponseDate: new Date()
}
)
}
}
34 changes: 23 additions & 11 deletions src/app/main-window/main-window.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Component, OnInit} from '@angular/core';
import {StorageService} from "../../service/storage.service";
import {StorageKeyEnum} from "../../model/StorageKeyEnum";
import {Router} from "@angular/router";
import {PathsEnum} from "../../model/PathsEnum";
import {AppStorage} from "../../model/AppStorage";

@Component({
selector: 'app-main-window',
Expand All @@ -23,33 +23,38 @@ export class MainWindowComponent implements OnInit {
}

public async ngOnInit(): Promise<void> {
const lastQuizResponseDate: string | null = this.storageService.get(StorageKeyEnum.LAST_QUIZ_RESPONSE_DATE);
const appStorage: AppStorage = this.storageService.get();
const lastQuizResponseDate: Date | null = appStorage.lastQuizResponseDate;

this.quizCanBeAnswered = this.checkIfQuizCanBeAnswered(lastQuizResponseDate);

if (!this.quizCanBeAnswered)
this.startCountdown(lastQuizResponseDate);
}

private checkIfQuizCanBeAnswered(lastQuizResponseDate: string | null): boolean {
if (lastQuizResponseDate === null || lastQuizResponseDate === '') return true;
public async redirectTo(route: PathsEnum): Promise<void> {
await this.router.navigateByUrl(route);
}

private checkIfQuizCanBeAnswered(lastQuizResponseDate: Date | null): boolean {
if (lastQuizResponseDate === null) return true;

const now: Date = new Date();
const lastAnsweredDate: Date = new Date();
const threeHoursInMs: number = 1000 * 60 * 60 * 3;

lastAnsweredDate.setTime(parseInt(lastQuizResponseDate) + threeHoursInMs);
lastAnsweredDate.setTime(new Date(lastQuizResponseDate).getTime() + threeHoursInMs);

return now.getTime() >= lastAnsweredDate.getTime();
}

private startCountdown(lastQuizResponseDate: string | null): void {
if (lastQuizResponseDate === null || lastQuizResponseDate === '') return;
private startCountdown(lastQuizResponseDate: Date | null): void {
if (lastQuizResponseDate === null) return;

const nextAnswerDate: Date = new Date();
const threeHoursInMs: number = 1000 * 60 * 60 * 3;

nextAnswerDate.setTime(parseInt(lastQuizResponseDate) + threeHoursInMs);
nextAnswerDate.setTime(new Date(lastQuizResponseDate).getTime() + threeHoursInMs);

new Promise<void>(async (resolve, reject): Promise<void> => {
while (true) {
Expand All @@ -61,7 +66,7 @@ export class MainWindowComponent implements OnInit {

if (sameHour && sameMinute && sameSecond) {
this.quizCanBeAnswered = true;
this.storageService.clear(StorageKeyEnum.LAST_QUIZ_RESPONSE_DATE);
this.clearLastAnsweredDate();
resolve();
break;
}
Expand All @@ -81,7 +86,14 @@ export class MainWindowComponent implements OnInit {
});
}

public async redirectTo(route: PathsEnum): Promise<void> {
await this.router.navigateByUrl(route);
private clearLastAnsweredDate(): void {
const appStorage: AppStorage = this.storageService.get();

this.storageService.save(
{
...appStorage,
lastQuizResponseDate: null
}
);
}
}
1 change: 0 additions & 1 deletion src/app/question-window/question-window.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {TriviaService} from "../../service/trivia.service";
import {TriviaResponse} from "../../model/TriviaResponse";
import {Router} from "@angular/router";
import {PathsEnum} from "../../model/PathsEnum";
import {StorageKeyEnum} from "../../model/StorageKeyEnum";

@Component({
selector: 'app-question-window',
Expand Down
7 changes: 3 additions & 4 deletions src/app/score-window/score-window.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Component, OnInit} from '@angular/core';
import {StorageService} from "../../service/storage.service";
import {Router} from "@angular/router";
import {StorageKeyEnum} from "../../model/StorageKeyEnum";
import {PathsEnum} from "../../model/PathsEnum";
import {AppStorage} from "../../model/AppStorage";

@Component({
selector: 'app-score-window',
Expand All @@ -19,9 +19,8 @@ export class ScoreWindowComponent implements OnInit {
}

public ngOnInit(): void {
const currentScore: string | null = this.storageService.get(StorageKeyEnum.CURRENT_SCORE);

this.score = (currentScore === null || currentScore === '') ? 0 : parseInt(currentScore);
const appStorage: AppStorage = this.storageService.get();
this.score = appStorage.currentScore;
}

public async returnHome(): Promise<void> {
Expand Down
13 changes: 11 additions & 2 deletions src/app/wrong-answer-window/wrong-answer-window.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {StorageKeyEnum} from "../../model/StorageKeyEnum";
import {AppStorage} from "../../model/AppStorage";

@Component({
selector: 'app-wrong-answer-window',
Expand All @@ -24,11 +24,20 @@ export class WrongAnswerWindowComponent implements OnInit {

public async ngOnInit(): Promise<void> {
await this.wrongAnswerSound.play();
this.updateStorage();
}

public async returnHome(): Promise<void> {
this.storageService.save(StorageKeyEnum.LAST_QUIZ_RESPONSE_DATE, new Date().getTime().toString());
await this.router.navigateByUrl(PathsEnum.HOME);
}

private updateStorage() {
const appStorage: AppStorage = this.storageService.get();

this.storageService.save({
...appStorage,
currentScore: appStorage.currentScore,
lastQuizResponseDate: new Date()
});
}
}
4 changes: 4 additions & 0 deletions src/model/AppStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface AppStorage {
currentScore: number,
lastQuizResponseDate: Date | null
}
4 changes: 0 additions & 4 deletions src/model/StorageKeyEnum.ts

This file was deleted.

32 changes: 18 additions & 14 deletions src/service/storage.service.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
import {Injectable} from '@angular/core';
import {StorageKeyEnum} from "../model/StorageKeyEnum";
import { AES, enc } from 'crypto-js';
import {AES, enc} from 'crypto-js';
import {AppStorage} from "../model/AppStorage";

@Injectable({
providedIn: 'root'
})
export class StorageService {

private readonly localStorage: Storage;
private readonly KEY: string = 'ENCRYPTION_KEY';
private readonly storageKey: string = 'app_storage'
private readonly encryptionkey: string = 'ENCRYPTION_KEY_XPQUIZ';

constructor() {
this.localStorage = window.localStorage;
}

public save(key: StorageKeyEnum, value: string): void {
this.localStorage.setItem(key, this.encrypt(value));
}

public get(key: StorageKeyEnum): string | null {
const encryptedItem: string | null = this.localStorage.getItem(key);
public save(value: AppStorage): void {
const stringfiedObject: string = JSON.stringify(value);
const encryptedObject: string = this.encrypt(stringfiedObject);

return encryptedItem === null ? '' : this.decrypt(encryptedItem);
this.localStorage.setItem(this.storageKey, encryptedObject);
}

public clear(key: StorageKeyEnum) {
this.localStorage.removeItem(key);
public get(): AppStorage {
const encryptedItem: string | null = this.localStorage.getItem(this.storageKey);
return (encryptedItem === null || encryptedItem === '') ?
{
currentScore: 0,
lastQuizResponseDate: null
} :
JSON.parse(this.decrypt(encryptedItem));
}

private encrypt(value: string): string {
const encrypted = AES.encrypt(value, this.KEY);
const encrypted = AES.encrypt(value, this.encryptionkey);
return encrypted.toString();
}

private decrypt(value: string): string {
const decrypted = AES.decrypt(value, this.KEY);
const decrypted = AES.decrypt(value, this.encryptionkey);
return decrypted.toString(enc.Utf8);
}
}