Full typescript and mvvm implementation #3
@ -68,7 +68,7 @@
|
|||||||
<a href="https://www.flaticon.com/free-icons/processor" title="processor icons">Processor icons created by kerismaker - Flaticon</a>
|
<a href="https://www.flaticon.com/free-icons/processor" title="processor icons">Processor icons created by kerismaker - Flaticon</a>
|
||||||
</div> -->
|
</div> -->
|
||||||
|
|
||||||
|
<script src="./js/statistics.js" type="module"></script>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import {main, btnHigherClick, btnLowerClick} from "./js/game.js"
|
import {main, btnHigherClick, btnLowerClick} from "./js/game.js"
|
||||||
window.btnHigherClick = btnHigherClick
|
window.btnHigherClick = btnHigherClick
|
||||||
|
36
js/game.js
36
js/game.js
@ -1,30 +1,22 @@
|
|||||||
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";
|
||||||
|
import { Stats } from "./statistics.js";
|
||||||
|
|
||||||
var cpuList;
|
var cpuList;
|
||||||
var currentCpu;
|
var currentCpu;
|
||||||
var nextCpu;
|
var nextCpu;
|
||||||
|
|
||||||
// current user score
|
var localStats;
|
||||||
var score;
|
|
||||||
var highScore;
|
|
||||||
|
|
||||||
function setHighScore(score) {
|
|
||||||
const currHighScore = localStorage.getItem("highScore_cpu")
|
|
||||||
if (currHighScore < score) {
|
|
||||||
localStorage.setItem("highScore_cpu", score)
|
|
||||||
highScore = score
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function main() {
|
export async function main() {
|
||||||
await (fetch('./data.json')
|
localStats = new Stats();
|
||||||
|
|
||||||
|
await (fetch('./js/data.json')
|
||||||
.then((response) => response.json())
|
.then((response) => response.json())
|
||||||
.then((json) => cpuList = json));
|
.then((json) => cpuList = json));
|
||||||
|
|
||||||
currentCpu = getRandomCpu();
|
currentCpu = getRandomCpu();
|
||||||
nextCpu = getRandomCpu();
|
nextCpu = getRandomCpu();
|
||||||
score = 0;
|
|
||||||
highScore = localStorage.getItem("highScore_cpu") ?? 0
|
|
||||||
updateLayout();
|
updateLayout();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,22 +41,18 @@ function showResult(isCorrect) {
|
|||||||
document.getElementById("btnLower").setAttribute("disabled", "");
|
document.getElementById("btnLower").setAttribute("disabled", "");
|
||||||
|
|
||||||
document.getElementById("col2").style.backgroundColor = isCorrect ? "lightgreen" : "#FF4444";
|
document.getElementById("col2").style.backgroundColor = isCorrect ? "lightgreen" : "#FF4444";
|
||||||
if (!isCorrect) {
|
|
||||||
setHighScore(score)
|
void(isCorrect && (localStats.incrementScore()))
|
||||||
}
|
|
||||||
score = isCorrect ? score + 1 : 0;
|
document.getElementById("highScore").innerText = localStats.highScore;
|
||||||
if (score > highScore) {
|
document.getElementById("score").innerText = localStats.score;
|
||||||
highScore = score;
|
|
||||||
document.getElementById("highScore").innerText = highScore;
|
|
||||||
}
|
|
||||||
document.getElementById("score").innerText = score;
|
|
||||||
|
|
||||||
countUp();
|
countUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
// updates view based on the cpu objects
|
// updates view based on the cpu objects
|
||||||
function updateLayout() {
|
function updateLayout() {
|
||||||
document.getElementById("highScore").innerText = highScore;
|
document.getElementById("highScore").innerText = localStats.highScore;;
|
||||||
|
|
||||||
document.getElementById("currentCpuTitle").innerText = currentCpu.name;
|
document.getElementById("currentCpuTitle").innerText = currentCpu.name;
|
||||||
// add "." to large numbers
|
// add "." to large numbers
|
||||||
|
Loading…
Reference in New Issue
Block a user