From eb5b4ed45096f08fac5125a2128b04d427ff2d05 Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Sun, 23 Apr 2023 21:59:48 +0200 Subject: [PATCH 01/15] remove onclick duplicate --- gst.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gst.html b/gst.html index 742a0ba..de478cf 100644 --- a/gst.html +++ b/gst.html @@ -55,13 +55,13 @@

- +
- +
- +
- +
-- 2.45.2 From b2934f5c9c80cfda8588bdbb824b359f48025c06 Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Wed, 26 Apr 2023 22:23:38 +0200 Subject: [PATCH 02/15] add new mode --- newMode.html | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ src/newMode.ts | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 newMode.html create mode 100644 src/newMode.ts diff --git a/newMode.html b/newMode.html new file mode 100644 index 0000000..ad53aec --- /dev/null +++ b/newMode.html @@ -0,0 +1,75 @@ + + + + + + + The CPU Higher Lower Game + + + + + + + + +
+ +
+
+
+
+ Guess the release date of the CPU +
+ Score: 0 + Highscore: 0 +
+
+ +
+
+
+
+ +
+ +
+ +
+ +
+ +
+
+
+
+ + + + \ No newline at end of file diff --git a/src/newMode.ts b/src/newMode.ts new file mode 100644 index 0000000..3b005d2 --- /dev/null +++ b/src/newMode.ts @@ -0,0 +1,78 @@ +import { CpuRepository } from "./cpuRepository.js"; +import { Stats } from "./statistics.js"; + +class UI { + private model: ViewModel; + + private buttons: HTMLElement[]; + + constructor() { + this.model = new ViewModel(); + + let button1 = document.getElementById("btnDate1"); + let button2 = document.getElementById("btnDate2"); + let button3 = document.getElementById("btnDate3"); + let button4 = document.getElementById("btnDate4"); + this.buttons = [button1, button2, button3, button4]; + } + + async init() { + await this.model.init(); + + this.model.Dates.forEach((value, index) => + (this.buttons[index].innerText = value) + ); + } +} + +class ViewModel { + private repo: CpuRepository; + private stats: Stats; + + private hardmode: boolean; + + constructor() { + this.stats = new Stats("highScore_date"); + this.repo = new CpuRepository(); + + this.hardmode = false; + } + + async init() { + await this.repo.init(); + } + + nextRound() { + this.repo.reset(); + } + + incrementScore() { + this.stats.incrementScore(); + } + + resetScore() { + this.stats.resetScore(); + } + + get currentCpu() { + return this.repo.currentCpu; + } + + get score(): number { + return this.stats.score; + } + + get highScore(): number { + return this.stats.highScore; + } + + get Dates(): string[] { + let array; + return array = ["eins", "zwei", "drei", "vier"]; + } +} + +export async function main() { + const ui = new UI(); + await ui.init(); +} \ No newline at end of file -- 2.45.2 From 70bf9a6fc7028c99e14ef145801a9c877cc5d282 Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Wed, 26 Apr 2023 23:02:09 +0200 Subject: [PATCH 03/15] get dates --- src/newMode.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/newMode.ts b/src/newMode.ts index 3b005d2..b63287f 100644 --- a/src/newMode.ts +++ b/src/newMode.ts @@ -54,6 +54,52 @@ class ViewModel { this.stats.resetScore(); } + switchMode() { + this.hardmode = !this.hardmode; + } + + getDates() { + let cpuDate = this.repo.currentCpu.date; + // if date is undefined the go for recursion + if(cpuDate === undefined) { + this.repo.reset(); + return this.getDates(); + } + + const dateArray = cpuDate.split("-"); + const year = Number(dateArray[0]); + + let newDates: string[] = new Array(4); + + if (this.hardmode) { + for (let index = 0; index < 4; index++) { + newDates[index] = this.getRandomDate(new Date(year - 1), new Date(year + 1)); + } + newDates[this.getRandomInt(0, 4)] = cpuDate; + return newDates; + } + + // normal Mode + for (let index = 0; index < 4; index++) { + newDates[index] = this.getRandomDate(new Date("2000-01-01"), new Date(Date.now())); + } + newDates[this.getRandomInt(0, 4)] = cpuDate; + return newDates; + } + + private getRandomInt(min, max): number { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive + } + + private getRandomDate(startDate: Date, endDate: Date) { + const minValue = startDate.getTime(); + const maxValue = endDate.getTime(); + const timestamp = Math.floor(Math.random() * (maxValue - minValue + 1) + minValue); + return new Date(timestamp).toISOString().split("T")[0]; + } + get currentCpu() { return this.repo.currentCpu; } @@ -67,8 +113,7 @@ class ViewModel { } get Dates(): string[] { - let array; - return array = ["eins", "zwei", "drei", "vier"]; + return this.getDates(); } } -- 2.45.2 From b4830536a246c99e263aa48488c658bfb043d95e Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Wed, 26 Apr 2023 23:20:38 +0200 Subject: [PATCH 04/15] add button click --- src/newMode.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/newMode.ts b/src/newMode.ts index b63287f..e82c469 100644 --- a/src/newMode.ts +++ b/src/newMode.ts @@ -5,6 +5,7 @@ class UI { private model: ViewModel; private buttons: HTMLElement[]; + private cpuName: HTMLElement; constructor() { this.model = new ViewModel(); @@ -19,9 +20,23 @@ class UI { async init() { await this.model.init(); + this.cpuName = document.getElementById("cpuName"); + this.cpuName.innerText = this.model.currentCpu.name; + this.model.Dates.forEach((value, index) => (this.buttons[index].innerText = value) ); + + this.buttons.forEach( + (btn) => { + btn.addEventListener("click", (e:Event) => this.buttonClick(btn)); + } + ) + } + + buttonClick(btn: HTMLElement) { + let result = this.model.processClick(btn.innerText); + btn.style.backgroundColor = result ? "lightgreen" : "#FF4444"; } } @@ -100,6 +115,10 @@ class ViewModel { return new Date(timestamp).toISOString().split("T")[0]; } + processClick(text: string): boolean { + return text == this.repo.currentCpu.date; + } + get currentCpu() { return this.repo.currentCpu; } -- 2.45.2 From ae1d9a077adbf4539d7356c457732cbb6168ef59 Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Wed, 26 Apr 2023 23:34:40 +0200 Subject: [PATCH 05/15] add score and next round --- src/newMode.ts | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/src/newMode.ts b/src/newMode.ts index e82c469..91e2b45 100644 --- a/src/newMode.ts +++ b/src/newMode.ts @@ -7,6 +7,9 @@ class UI { private buttons: HTMLElement[]; private cpuName: HTMLElement; + private score: HTMLElement; + private highScore: HTMLElement; + constructor() { this.model = new ViewModel(); @@ -15,17 +18,16 @@ class UI { let button3 = document.getElementById("btnDate3"); let button4 = document.getElementById("btnDate4"); this.buttons = [button1, button2, button3, button4]; + + this.score = document.getElementById("score"); + this.highScore = document.getElementById("highScore"); } async init() { await this.model.init(); this.cpuName = document.getElementById("cpuName"); - this.cpuName.innerText = this.model.currentCpu.name; - - this.model.Dates.forEach((value, index) => - (this.buttons[index].innerText = value) - ); + this.nextRound(); this.buttons.forEach( (btn) => { @@ -35,8 +37,29 @@ class UI { } buttonClick(btn: HTMLElement) { + btn.setAttribute("disabled", ""); let result = this.model.processClick(btn.innerText); btn.style.backgroundColor = result ? "lightgreen" : "#FF4444"; + + this.updateScores(); + } + + nextRound() { + this.buttons.forEach( + (btn) => {btn.removeAttribute("disabled"); + } + ) + + this.cpuName.innerText = this.model.currentCpu.name; + + this.model.Dates.forEach((value, index) => + (this.buttons[index].innerText = value) + ); + } + + private updateScores() { + this.score.innerText = this.model.score.toString(); + this.highScore.innerText = this.model.highScore.toString(); } } @@ -61,8 +84,12 @@ class ViewModel { this.repo.reset(); } - incrementScore() { - this.stats.incrementScore(); + incrementScore(num: number = 1) { + this.stats.incrementScore(num); + } + + reduceScore() { + this.stats.incrementScore(-1); } resetScore() { @@ -116,7 +143,13 @@ class ViewModel { } processClick(text: string): boolean { - return text == this.repo.currentCpu.date; + let result = text == this.repo.currentCpu.date; + if (result) { + this.incrementScore(2); + } else { + this.reduceScore(); + } + return result; } get currentCpu() { -- 2.45.2 From 52dcce30133964a3199a4caefe47446cb9a29116 Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Wed, 26 Apr 2023 23:44:35 +0200 Subject: [PATCH 06/15] add more stuff --- src/newMode.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/newMode.ts b/src/newMode.ts index 91e2b45..23c0025 100644 --- a/src/newMode.ts +++ b/src/newMode.ts @@ -36,19 +36,32 @@ class UI { ) } - buttonClick(btn: HTMLElement) { + async buttonClick(btn: HTMLElement) { btn.setAttribute("disabled", ""); let result = this.model.processClick(btn.innerText); btn.style.backgroundColor = result ? "lightgreen" : "#FF4444"; this.updateScores(); + + await this.delay(1500); + + if (result) { + this.nextRound(); + } + } + private async delay(time) { + return new Promise(resolve => setTimeout(resolve, time)); } nextRound() { this.buttons.forEach( - (btn) => {btn.removeAttribute("disabled"); + (btn) => { + btn.removeAttribute("disabled"); + btn.style.backgroundColor = "#5bc0de"; } ) + this.model.nextRound(); + this.cpuName.innerText = this.model.currentCpu.name; -- 2.45.2 From c40030a13e70e6189cb37ed4001f0c30a14ae133 Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Wed, 26 Apr 2023 23:45:29 +0200 Subject: [PATCH 07/15] fix highscore bug --- src/newMode.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/newMode.ts b/src/newMode.ts index 23c0025..4aaffde 100644 --- a/src/newMode.ts +++ b/src/newMode.ts @@ -28,6 +28,7 @@ class UI { this.cpuName = document.getElementById("cpuName"); this.nextRound(); + this.updateScores(); this.buttons.forEach( (btn) => { @@ -62,7 +63,6 @@ class UI { ) this.model.nextRound(); - this.cpuName.innerText = this.model.currentCpu.name; this.model.Dates.forEach((value, index) => -- 2.45.2 From 41c3ac5652e66343bfba94ccf7b5a12ec5449e5a Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Wed, 26 Apr 2023 23:56:23 +0200 Subject: [PATCH 08/15] add switch --- newMode.html | 2 +- src/newMode.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/newMode.html b/newMode.html index ad53aec..6b8c8f3 100644 --- a/newMode.html +++ b/newMode.html @@ -48,7 +48,7 @@ Highscore: 0

- +
diff --git a/src/newMode.ts b/src/newMode.ts index 4aaffde..3a82da2 100644 --- a/src/newMode.ts +++ b/src/newMode.ts @@ -21,6 +21,8 @@ class UI { this.score = document.getElementById("score"); this.highScore = document.getElementById("highScore"); + + document.getElementById("modeSwitch").addEventListener("change", (e:Event) => {this.switchMode(); this.nextRound()}); } async init() { @@ -37,6 +39,10 @@ class UI { ) } + switchMode() { + this.model.switchMode(); + } + async buttonClick(btn: HTMLElement) { btn.setAttribute("disabled", ""); let result = this.model.processClick(btn.innerText); -- 2.45.2 From 223d212fd1a6aa849911737172afee38c3c6d40c Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Wed, 26 Apr 2023 23:58:00 +0200 Subject: [PATCH 09/15] fix hardmode --- src/newMode.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/newMode.ts b/src/newMode.ts index 3a82da2..065c4f4 100644 --- a/src/newMode.ts +++ b/src/newMode.ts @@ -134,7 +134,7 @@ class ViewModel { if (this.hardmode) { for (let index = 0; index < 4; index++) { - newDates[index] = this.getRandomDate(new Date(year - 1), new Date(year + 1)); + newDates[index] = this.getRandomDate(new Date((year - 1) + "-" + "01-01"), new Date((year + 1) + "-" + "01-01")); } newDates[this.getRandomInt(0, 4)] = cpuDate; return newDates; -- 2.45.2 From 8e5143fba84c6cc4abc0311ac28b26b6ef6e1da4 Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Fri, 28 Apr 2023 21:08:10 +0200 Subject: [PATCH 10/15] use quartal instead of whole date --- src/newMode.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/newMode.ts b/src/newMode.ts index 065c4f4..54b508a 100644 --- a/src/newMode.ts +++ b/src/newMode.ts @@ -129,6 +129,7 @@ class ViewModel { const dateArray = cpuDate.split("-"); const year = Number(dateArray[0]); + let currentCpuQuartal = this.getCurrentQuartal(dateArray); let newDates: string[] = new Array(4); @@ -136,7 +137,12 @@ class ViewModel { for (let index = 0; index < 4; index++) { newDates[index] = this.getRandomDate(new Date((year - 1) + "-" + "01-01"), new Date((year + 1) + "-" + "01-01")); } - newDates[this.getRandomInt(0, 4)] = cpuDate; + // check if the cpus quartal was randomly choosen already + if (newDates.includes(currentCpuQuartal)) { + return newDates; + } + + newDates[this.getRandomInt(0, 4)] = currentCpuQuartal; return newDates; } @@ -144,7 +150,13 @@ class ViewModel { for (let index = 0; index < 4; index++) { newDates[index] = this.getRandomDate(new Date("2000-01-01"), new Date(Date.now())); } - newDates[this.getRandomInt(0, 4)] = cpuDate; + + // check if the cpus quartal was randomly choosen already + if (newDates.includes(currentCpuQuartal)) { + return newDates; + } + + newDates[this.getRandomInt(0, 4)] = currentCpuQuartal; return newDates; } @@ -153,12 +165,16 @@ class ViewModel { max = Math.floor(max); return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive } + + private getCurrentQuartal(dateArray) { + let quartal = Math.ceil(dateArray[1] % 4); + return "Q" + quartal + " " + dateArray[0]; + } private getRandomDate(startDate: Date, endDate: Date) { - const minValue = startDate.getTime(); - const maxValue = endDate.getTime(); - const timestamp = Math.floor(Math.random() * (maxValue - minValue + 1) + minValue); - return new Date(timestamp).toISOString().split("T")[0]; + let newYear = this.getRandomInt(startDate.getFullYear(), endDate.getFullYear() + 1); + let quartal = this.getRandomInt(1, 5); + return "Q" + quartal + " " + newYear; } processClick(text: string): boolean { -- 2.45.2 From 1445942d04b32e6be61cad9cc2f35da1f891857e Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Fri, 28 Apr 2023 21:10:07 +0200 Subject: [PATCH 11/15] add updateScore method --- src/newMode.ts | 2 +- src/statistics.ts | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/newMode.ts b/src/newMode.ts index 54b508a..33ecc30 100644 --- a/src/newMode.ts +++ b/src/newMode.ts @@ -108,7 +108,7 @@ class ViewModel { } reduceScore() { - this.stats.incrementScore(-1); + this.stats.updateScore(-1); } resetScore() { diff --git a/src/statistics.ts b/src/statistics.ts index f320c64..54a7545 100644 --- a/src/statistics.ts +++ b/src/statistics.ts @@ -14,16 +14,26 @@ export class Stats { incrementScore(value = 1) { this.#score += value; - if (this.#highScore < this.#score) { - this.#highScore = this.#score; - localStorage.setItem(this.#highScoreStorageKey, this.#highScore.toString()); - } + this.checkHighScore; } resetScore(): void { this.#score = 0; } + updateScore(value) { + this.#score += value; + + this.checkHighScore; + } + + private checkHighScore() { + if (this.#highScore < this.#score) { + this.#highScore = this.#score; + localStorage.setItem(this.#highScoreStorageKey, this.#highScore.toString()); + } + } + get highScore(): number { return this.#highScore; } -- 2.45.2 From f552bc99b9a75e52a17ccb40b8ced53d6a35a288 Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Fri, 28 Apr 2023 21:18:13 +0200 Subject: [PATCH 12/15] rename mode --- newMode.html => WWTRA.html | 2 +- src/{newMode.ts => WWTRA.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename newMode.html => WWTRA.html (98%) rename src/{newMode.ts => WWTRA.ts} (100%) diff --git a/newMode.html b/WWTRA.html similarity index 98% rename from newMode.html rename to WWTRA.html index 6b8c8f3..0b17994 100644 --- a/newMode.html +++ b/WWTRA.html @@ -68,7 +68,7 @@
diff --git a/src/newMode.ts b/src/WWTRA.ts similarity index 100% rename from src/newMode.ts rename to src/WWTRA.ts -- 2.45.2 From fa6845037394b084ae3b8b1ea3e82f2fe8a5fc42 Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Fri, 28 Apr 2023 21:36:15 +0200 Subject: [PATCH 13/15] bug fix current quartal --- src/WWTRA.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/WWTRA.ts b/src/WWTRA.ts index 33ecc30..e1db2e9 100644 --- a/src/WWTRA.ts +++ b/src/WWTRA.ts @@ -167,7 +167,8 @@ class ViewModel { } private getCurrentQuartal(dateArray) { - let quartal = Math.ceil(dateArray[1] % 4); + let quartal = Math.ceil(Number(dateArray[1] / 4)); + return "Q" + quartal + " " + dateArray[0]; } @@ -178,7 +179,7 @@ class ViewModel { } processClick(text: string): boolean { - let result = text == this.repo.currentCpu.date; + let result = text == this.getCurrentQuartal(this.currentCpu.date.split("-")); if (result) { this.incrementScore(2); } else { -- 2.45.2 From 78dfb5ba9aedd8a9dfb683af78b3e8df16950b5f Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Fri, 28 Apr 2023 21:41:56 +0200 Subject: [PATCH 14/15] add WWTRA to nav bar --- WWTRA.html | 5 ++++- about.html | 3 +++ game.html | 3 +++ gst.html | 3 +++ index.html | 3 +++ 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/WWTRA.html b/WWTRA.html index 0b17994..4fcf66b 100644 --- a/WWTRA.html +++ b/WWTRA.html @@ -23,7 +23,7 @@