Merge pull request 'add WWTRA mode' (#4) from Developer into main

Reviewed-on: Schmidii99/CpuHigherLower#4
This commit is contained in:
Florian Schmid 2023-04-28 22:11:34 +02:00
commit ce443e26b5
7 changed files with 319 additions and 8 deletions

78
WWTRA.html Normal file
View File

@ -0,0 +1,78 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The CPU Higher Lower Game</title>
<link rel="icon" type="image/x" href="imgs/chip.png">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap-switch-button@1.1.0/css/bootstrap-switch-button.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap-switch-button@1.1.0/dist/bootstrap-switch-button.min.js"></script>
</head>
<body>
<div>
<nav class="navbar navbar-expand-sm bg-light navbar-light">
<div class="container-fluid h4">
<img class="navbar-brand" src="imgs/chip.png" height="50">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="game.html">Game</a>
</li>
<li class="nav-item">
<a class="nav-link" href="gst.html">GST</a>
</li>
<li class="nav-item">
<a class="nav-link active" style="text-decoration: underline;" href="WWTRA.html">WWTRA</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About</a>
</li>
</ul>
</div>
</div>
</nav>
</div>
<div class="container">
<div class="row">
<div class="col text-center mb-3">
<span class="h2"><span style="color: blue;">W</span>hen <span style="color: blue;">w</span>as <span style="color: blue;">t</span>hat <span style="color: blue;">r</span>eleased <span style="color: blue;">a</span>gain?</span>
<br>
<span class="h2">Score: </span><span class="h3" id="score" style="color: violet">0</span>
<span class="h2" style="margin-left: 10px;">Highscore: </span> <span class="h3" id="highScore" style="color: crimson;">0</span>
<br>
<br>
<input id="modeSwitch" type="checkbox" data-toggle="switchbutton" checked data-onlabel="Normal" data-offlabel="Hard Mode" data-onstyle="primary" data-offstyle="danger">
</div>
</div>
<div class="row align-items-center">
<div class="col-md-6 offset-md-3 border text-center p-2" id="mainCol">
<span class="h2" id="cpuName"></span>
<br>
<button class="btn btn-lg m-1 mt-2" id="btnDate1" style="background-color: #3CC3FA;"></button>
<br>
<button class="btn btn-lg m-1" id="btnDate2" style="background-color: #3CC3FA;"></button>
<br>
<button class="btn btn-lg m-1" id="btnDate3" style="background-color: #3CC3FA;"></button>
<br>
<button class="btn btn-lg m-1" id="btnDate4" style="background-color: #3CC3FA;"></button>
<br>
</div>
</div>
</div>
<script type="module">
import { main } from "./js/WWTRA.js"
main()
</script>
</body>
</html>

View File

@ -29,6 +29,9 @@
<li class="nav-item">
<a class="nav-link" href="gst.html">GST</a>
</li>
<li class="nav-item">
<a class="nav-link" href="WWTRA.html">WWTRA</a>
</li>
<li class="nav-item">
<a class="nav-link active" style="text-decoration: underline;" href="about.html">About</a>
</li>

View File

@ -29,6 +29,9 @@
<li class="nav-item">
<a class="nav-link" href="gst.html">GST</a>
</li>
<li class="nav-item">
<a class="nav-link" href="WWTRA.html">WWTRA</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About</a>
</li>

View File

@ -30,6 +30,9 @@
<li class="nav-item">
<a class="nav-link active" style="text-decoration: underline;" href="gst.html">GST</a>
</li>
<li class="nav-item">
<a class="nav-link" href="WWTRA.html">WWTRA</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About</a>
</li>
@ -55,13 +58,13 @@
<div class="col-md-6 offset-md-3 border text-center p-2" id="mainCol">
<span class="h2" id="cpuName"></span>
<br>
<button class="btn btn-lg m-1 mt-2" id="btnDesktop" style="background-color: #3CC3FA;" onclick="btnClick(0)">Desktop</button>
<button class="btn btn-lg m-1 mt-2" id="btnDesktop" style="background-color: #3CC3FA;" >Desktop</button>
<br>
<button class="btn btn-lg m-1" id="btnServer" style="background-color: #3CC3FA;" onclick="btnClick(3)">Server</button>
<button class="btn btn-lg m-1" id="btnServer" style="background-color: #3CC3FA;">Server</button>
<br>
<button class="btn btn-lg m-1" id="btnLaptop" style="background-color: #3CC3FA;" onclick="btnClick(1)">Laptop</button>
<button class="btn btn-lg m-1" id="btnLaptop" style="background-color: #3CC3FA;">Laptop</button>
<br>
<button class="btn btn-lg m-1" id="btnMobile" style="background-color: #3CC3FA;" onclick="btnClick(2)">Mobile/Embedded</button>
<button class="btn btn-lg m-1" id="btnMobile" style="background-color: #3CC3FA;">Mobile/Embedded</button>
<br>
</div>
</div>

View File

@ -29,6 +29,9 @@
<li class="nav-item">
<a class="nav-link" href="gst.html">GST</a>
</li>
<li class="nav-item">
<a class="nav-link" href="WWTRA.html">WWTRA</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About</a>
</li>

211
src/WWTRA.ts Normal file
View File

@ -0,0 +1,211 @@
import { CpuRepository } from "./cpuRepository.js";
import { Stats } from "./statistics.js";
class UI {
private model: ViewModel;
private buttons: HTMLElement[];
private cpuName: HTMLElement;
private score: HTMLElement;
private highScore: 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];
this.score = document.getElementById("score");
this.highScore = document.getElementById("highScore");
document.getElementById("modeSwitch").addEventListener("change", (e:Event) => {this.switchMode(); this.nextRound()});
}
async init() {
await this.model.init();
this.cpuName = document.getElementById("cpuName");
this.nextRound();
this.updateScores();
this.buttons.forEach(
(btn) => {
btn.addEventListener("click", (e:Event) => this.buttonClick(btn));
}
)
}
switchMode() {
this.model.switchMode();
}
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.style.backgroundColor = "#5bc0de";
}
)
this.model.nextRound();
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();
}
}
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(num: number = 1) {
this.stats.incrementScore(num);
}
reduceScore() {
this.stats.updateScore(-1);
}
resetScore() {
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 currentCpuQuartal = this.getCurrentQuartal(dateArray);
let newDates: string[] = new Array(4);
if (this.hardmode) {
for (let index = 0; index < 4; index++) {
newDates[index] = this.getRandomDate(new Date((year - 1) + "-" + "01-01"), new Date((year + 1) + "-" + "01-01"));
}
// check if the cpus quartal was randomly choosen already
if (newDates.includes(currentCpuQuartal)) {
return newDates;
}
newDates[this.getRandomInt(0, 4)] = currentCpuQuartal;
return newDates;
}
// normal Mode
for (let index = 0; index < 4; index++) {
newDates[index] = this.getRandomDate(new Date("2000-01-01"), new Date(Date.now()));
}
// check if the cpus quartal was randomly choosen already
if (newDates.includes(currentCpuQuartal)) {
return newDates;
}
newDates[this.getRandomInt(0, 4)] = currentCpuQuartal;
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 getCurrentQuartal(dateArray) {
let quartal = Math.ceil(Number(dateArray[1] / 4));
return "Q" + quartal + " " + dateArray[0];
}
private getRandomDate(startDate: Date, endDate: Date) {
let newYear = this.getRandomInt(startDate.getFullYear(), endDate.getFullYear() + 1);
let quartal = this.getRandomInt(1, 5);
return "Q" + quartal + " " + newYear;
}
processClick(text: string): boolean {
let result = text == this.getCurrentQuartal(this.currentCpu.date.split("-"));
if (result) {
this.incrementScore(2);
} else {
this.reduceScore();
}
return result;
}
get currentCpu() {
return this.repo.currentCpu;
}
get score(): number {
return this.stats.score;
}
get highScore(): number {
return this.stats.highScore;
}
get Dates(): string[] {
return this.getDates();
}
}
export async function main() {
const ui = new UI();
await ui.init();
}

View File

@ -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;
}