Compare commits
3 Commits
main
...
refactor-s
Author | SHA1 | Date | |
---|---|---|---|
d63982bff0 | |||
e174b655af | |||
52b0e8c1da |
14
game.html
14
game.html
@ -45,21 +45,21 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="row mt-3">
|
<div class="row mt-3">
|
||||||
<div class="col-sm border text-center p-2" id="col1">
|
<div class="col-sm border text-center p-2" id="currCpuCol">
|
||||||
<span class="h2" id="currentCpuTitle"></span>
|
<span class="h2" id="currentCpuTitle"></span>
|
||||||
<br>
|
<br>
|
||||||
<span class="h3">cpu score:</span>
|
<span class="h3">cpu score:</span>
|
||||||
<span class="h3" id="currentCpuScore" style="color: blue;"></span>
|
<span class="h3" id="currentCpuScore" style="color: blue;"></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-sm border text-center p-2" id="col2">
|
<div class="col-sm border text-center p-2" id="nextCpuCol">
|
||||||
<span class="h2" id="nextCpuTitle"></span>
|
<span class="h2" id="nextCpuTitle"></span>
|
||||||
<br>
|
<br>
|
||||||
<span class="h3">cpu score:</span>
|
<span class="h3">cpu score:</span>
|
||||||
<span class="h3 counter-count" id="nextCpuScore" style="color: blue;">?</span>
|
<span class="h3 counter-count" id="nextCpuScore" style="color: blue;">?</span>
|
||||||
<br>
|
<br>
|
||||||
<button class="btn btn-lg bg-info m-3" onclick="btnHigherClick()" id="btnHigher">Higher</button>
|
<button class="btn btn-lg bg-info m-3" id="btnHigher">Higher</button>
|
||||||
<br>
|
<br>
|
||||||
<button class="btn btn-lg bg-info mb-3" onclick="btnLowerClick()" id="btnLower">Lower</button>
|
<button class="btn btn-lg bg-info mb-3" id="btnLower">Lower</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -69,10 +69,8 @@
|
|||||||
</div> -->
|
</div> -->
|
||||||
|
|
||||||
|
|
||||||
<script type="module">
|
<script defer type="module">
|
||||||
import {main, btnHigherClick, btnLowerClick} from "./game.js"
|
import { main } from "./game.js"
|
||||||
window.btnHigherClick = btnHigherClick
|
|
||||||
window.btnLowerClick = btnLowerClick
|
|
||||||
main()
|
main()
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
235
game.js
235
game.js
@ -1,110 +1,181 @@
|
|||||||
import { CountUp } from "https://cdnjs.cloudflare.com/ajax/libs/countup.js/2.6.0/countUp.min.js";
|
import { CountUp } from "https://cdnjs.cloudflare.com/ajax/libs/countup.js/2.6.0/countUp.min.js";
|
||||||
|
|
||||||
var cpuList;
|
class Ui {
|
||||||
var currentCpu;
|
#model;
|
||||||
var nextCpu;
|
|
||||||
|
|
||||||
// current user score
|
#scoreEl;
|
||||||
var score;
|
#highscoreEl;
|
||||||
var highscore;
|
|
||||||
|
|
||||||
function setHighscore(score) {
|
#currCpuTitleEl;
|
||||||
const currHighscore = localStorage.getItem("highscore_cpu")
|
#currCpuScoreEl;
|
||||||
if (currHighscore < score) {
|
|
||||||
localStorage.setItem("highscore_cpu", score)
|
#nextCpuTitleEl;
|
||||||
highscore = score
|
#nextCpuScoreEl;
|
||||||
|
|
||||||
|
#nextCpuStyle;
|
||||||
|
|
||||||
|
#btnHigher;
|
||||||
|
#btnLower;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.#model = new Model();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export async function main() {
|
async init() {
|
||||||
await fetch('./data.json')
|
await this.#model.init();
|
||||||
.then((response) => response.json())
|
|
||||||
.then((json) => cpuList = json);
|
|
||||||
|
|
||||||
currentCpu = getRandomCpu();
|
this.#scoreEl = document.getElementById("score");
|
||||||
nextCpu = getRandomCpu();
|
this.#highscoreEl = document.getElementById("highscore");
|
||||||
score = 0;
|
this.#currCpuTitleEl = document.getElementById("currentCpuTitle");
|
||||||
highscore = localStorage.getItem("highscore_cpu") ?? 0
|
this.#currCpuScoreEl = document.getElementById("currentCpuScore");
|
||||||
updateLayout();
|
this.#nextCpuTitleEl = document.getElementById("nextCpuTitle");
|
||||||
}
|
this.#nextCpuScoreEl = document.getElementById("nextCpuScore");
|
||||||
|
this.#nextCpuStyle = document.getElementById("nextCpuCol").style;
|
||||||
|
|
||||||
function getRandomCpu() {
|
this.#btnHigher = document.getElementById("btnHigher");
|
||||||
let randomIndex = getRandomInt(0, cpuList.length)
|
this.#btnLower = document.getElementById("btnLower");
|
||||||
return {
|
this.#btnHigher.onclick = () => this.handleHigherPress();
|
||||||
name: cpuList[randomIndex]["name"].split('@')[0],
|
this.#btnLower.onclick = () => this.handleLowerPress();
|
||||||
score: cpuList[randomIndex]["cpuScore"]
|
|
||||||
|
this.updateUiFromModel();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export function btnLowerClick() {
|
async handleHigherPress() {
|
||||||
nextCpu.score < currentCpu.score ? showResult(true) : showResult(false);
|
let result = this.#model.postAnswer('higher');
|
||||||
}
|
await this.animateTransition(result);
|
||||||
|
|
||||||
export function btnHigherClick() {
|
|
||||||
nextCpu.score > currentCpu.score ? showResult(true) : showResult(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
function showResult(isCorrect) {
|
|
||||||
document.getElementById("btnHigher").setAttribute("disabled", "");
|
|
||||||
document.getElementById("btnLower").setAttribute("disabled", "");
|
|
||||||
|
|
||||||
document.getElementById("col2").style.backgroundColor = isCorrect ? "lightgreen" : "#FF4444";
|
|
||||||
if (!isCorrect) {
|
|
||||||
setHighscore(score)
|
|
||||||
}
|
}
|
||||||
score = isCorrect ? score + 1 : 0;
|
|
||||||
document.getElementById("score").innerText = score;
|
|
||||||
|
|
||||||
countUp();
|
async handleLowerPress() {
|
||||||
}
|
let result = this.#model.postAnswer('lower');
|
||||||
|
await this.animateTransition(result);
|
||||||
|
}
|
||||||
|
|
||||||
// updates view based on the cpu objects
|
updateUiFromModel() {
|
||||||
function updateLayout() {
|
this.#scoreEl.innerText = this.#model.score;
|
||||||
document.getElementById("highscore").innerText = highscore;
|
this.#highscoreEl.innerText = this.#model.highscore;
|
||||||
document.getElementById("currentCpuTitle").innerText = currentCpu.name;
|
this.#currCpuTitleEl.innerText = this.#model.currentCpu.name;
|
||||||
// add "." to large numbers
|
this.#currCpuScoreEl.innerText = new Intl.NumberFormat().format(this.#model.currentCpu.score);
|
||||||
document.getElementById("currentCpuScore").innerText = new Intl.NumberFormat().format(currentCpu.score)
|
this.#nextCpuTitleEl.innerText = this.#model.nextCpu.name;
|
||||||
document.getElementById("nextCpuTitle").innerText = nextCpu.name;
|
}
|
||||||
|
|
||||||
document.getElementById("nextCpuScore").innerText = "?";
|
async animateTransition(wasCorrect) {
|
||||||
|
this.updateUiFromModel();
|
||||||
|
this.#btnHigher.setAttribute("disabled", "");
|
||||||
|
this.#btnLower.setAttribute("disabled", "");
|
||||||
|
this.#nextCpuStyle.backgroundColor = wasCorrect ? "lightgreen" : "#FF4444";
|
||||||
|
|
||||||
document.getElementById("col2").style.backgroundColor = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
function delay(time) {
|
|
||||||
return new Promise(resolve => setTimeout(resolve, time));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function countUp() {
|
|
||||||
const options = {
|
const options = {
|
||||||
startVal: nextCpu.score / 2,
|
startVal: this.#model.nextCpu.score / 2,
|
||||||
separator: '.',
|
separator: '.',
|
||||||
decimal: ',',
|
decimal: ',',
|
||||||
duration: 2
|
duration: 2
|
||||||
};
|
};
|
||||||
let counter = new CountUp('nextCpuScore', nextCpu.score, options);
|
const counter = new CountUp(this.#nextCpuScoreEl, this.#model.nextCpu.score, options);
|
||||||
if (!counter.error) {
|
|
||||||
counter.start();
|
await new Promise(resolve => counter.start(resolve));
|
||||||
} else {
|
await this.#delay(500);
|
||||||
counter.error(demo.error);
|
|
||||||
|
this.#model.nextRound();
|
||||||
|
this.updateUiFromModel();
|
||||||
|
|
||||||
|
this.#nextCpuStyle.backgroundColor = '';
|
||||||
|
this.#nextCpuScoreEl.innerText = '?';
|
||||||
|
this.#btnHigher.removeAttribute("disabled");
|
||||||
|
this.#btnLower.removeAttribute("disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
await delay(2500)
|
#delay(time) {
|
||||||
nextRound();
|
return new Promise(resolve => setTimeout(resolve, time));
|
||||||
|
}
|
||||||
document.getElementById("btnHigher").removeAttribute("disabled");
|
|
||||||
document.getElementById("btnLower").removeAttribute("disabled");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function nextRound() {
|
class Model {
|
||||||
currentCpu = nextCpu;
|
#cpuList;
|
||||||
nextCpu = getRandomCpu();
|
|
||||||
|
|
||||||
updateLayout();
|
#currentCpu;
|
||||||
|
#nextCpu;
|
||||||
|
#tookGuessOnCurrent;
|
||||||
|
|
||||||
|
#score;
|
||||||
|
#highscore;
|
||||||
|
|
||||||
|
async init() {
|
||||||
|
const fetchResult = await fetch("./data.json");
|
||||||
|
this.#cpuList = await fetchResult.json();
|
||||||
|
|
||||||
|
this.#currentCpu = this.#getRandomCpu();
|
||||||
|
this.#nextCpu = this.#getRandomCpu();
|
||||||
|
this.#score = 0;
|
||||||
|
this.#highscore = localStorage.getItem("highscore_cpu") ?? 0;
|
||||||
|
|
||||||
|
this.#tookGuessOnCurrent = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
get highscore() {
|
||||||
|
return this.#highscore;
|
||||||
|
}
|
||||||
|
|
||||||
|
get score() {
|
||||||
|
return this.#score;
|
||||||
|
}
|
||||||
|
|
||||||
|
get currentCpu() {
|
||||||
|
return this.#currentCpu;
|
||||||
|
}
|
||||||
|
|
||||||
|
get nextCpu() {
|
||||||
|
return this.#nextCpu;
|
||||||
|
}
|
||||||
|
|
||||||
|
get hasGuessed() {
|
||||||
|
return this.#tookGuessOnCurrent;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Can be 'higher' or 'lower'
|
||||||
|
postAnswer(answer) {
|
||||||
|
if (this.#tookGuessOnCurrent) return false;
|
||||||
|
this.#tookGuessOnCurrent = true;
|
||||||
|
|
||||||
|
let answerWasCorrect = false;
|
||||||
|
if (answer === 'higher') {
|
||||||
|
answerWasCorrect = this.nextCpu.score > this.#currentCpu.score;
|
||||||
|
} else if (answer === 'lower') {
|
||||||
|
answerWasCorrect = this.nextCpu.score < this.#currentCpu.score;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (answerWasCorrect) {
|
||||||
|
this.#score += 1;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
this.#updateHighscore(this.score)
|
||||||
|
this.#score = 0;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nextRound() {
|
||||||
|
this.#currentCpu = this.#nextCpu;
|
||||||
|
this.#nextCpu = this.#getRandomCpu();
|
||||||
|
this.#tookGuessOnCurrent = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#getRandomCpu() {
|
||||||
|
const item = this.#cpuList[Math.floor(Math.random() * this.#cpuList.length)];
|
||||||
|
return {
|
||||||
|
name: item["name"].split('@')[0],
|
||||||
|
score: item["cpuScore"]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#updateHighscore(newScore) {
|
||||||
|
if (newScore > this.#highscore) {
|
||||||
|
localStorage.setItem("highscore_cpu", newScore);
|
||||||
|
this.#highscore = newScore;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRandomInt(min, max) {
|
export async function main() {
|
||||||
min = Math.ceil(min);
|
const ui = new Ui();
|
||||||
max = Math.floor(max);
|
await ui.init()
|
||||||
return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user