From 70bf9a6fc7028c99e14ef145801a9c877cc5d282 Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Wed, 26 Apr 2023 23:02:09 +0200 Subject: [PATCH] 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(); } }